Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Change MasterSlaveConstraint::Pointer from shared_ptr to intrusive_ptr #12896

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// KRATOS ______ __ __ _____ __ __ __
// / ____/___ ____ / /_____ ______/ /_/ ___// /________ _______/ /___ ___________ _/ /
// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ /
// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / /
// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ /
// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / /
// \____/\____/_/ /_/\__/\__,_/\___/\__//____/\__/_/ \__,_/\___/\__/\__,_/_/ \__,_/_/ MECHANICS
//
// License: BSD License
Expand Down Expand Up @@ -90,7 +90,7 @@ MasterSlaveConstraint::Pointer ContactMasterSlaveConstraint::Create(
const VectorType& rConstantVector
) const
{
return Kratos::make_shared<ContactMasterSlaveConstraint>(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector);
return Kratos::make_intrusive<ContactMasterSlaveConstraint>(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector);
}

/***********************************************************************************/
Expand All @@ -106,7 +106,7 @@ MasterSlaveConstraint::Pointer ContactMasterSlaveConstraint::Create(
const double Constant
) const
{
return Kratos::make_shared<ContactMasterSlaveConstraint>(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant);
return Kratos::make_intrusive<ContactMasterSlaveConstraint>(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant);
}

/***********************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// KRATOS ______ __ __ _____ __ __ __
// / ____/___ ____ / /_____ ______/ /_/ ___// /________ _______/ /___ ___________ _/ /
// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ /
// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / /
// / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ /
// / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / /
// \____/\____/_/ /_/\__/\__,_/\___/\__//____/\__/_/ \__,_/\___/\__/\__,_/_/ \__,_/_/ MECHANICS
//
// License: BSD License
Expand Down Expand Up @@ -128,7 +128,7 @@ Condition::Pointer MPCContactSearchProcess<TDim, TNumNodes, TNumNodesMaster>::Ad

// Creating constraint
if (p_cond.get() != nullptr) {
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared<ContactMasterSlaveConstraint>(GetMaximumConstraintsIds() + 1);
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive<ContactMasterSlaveConstraint>(GetMaximumConstraintsIds() + 1);
p_new_const->Set(ACTIVE);
const auto& r_process_info = rComputingModelPart.GetProcessInfo();
p_new_const->Initialize(r_process_info);
Expand Down
6 changes: 3 additions & 3 deletions kratos/constraints/linear_master_slave_constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint
) const override
{
KRATOS_TRY
return Kratos::make_shared<LinearMasterSlaveConstraint>(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector);
return Kratos::make_intrusive<LinearMasterSlaveConstraint>(Id, rMasterDofsVector, rSlaveDofsVector, rRelationMatrix, rConstantVector);
KRATOS_CATCH("");
}

Expand All @@ -228,7 +228,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint
) const override
{
KRATOS_TRY
return Kratos::make_shared<LinearMasterSlaveConstraint>(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant);
return Kratos::make_intrusive<LinearMasterSlaveConstraint>(Id, rMasterNode, rMasterVariable, rSlaveNode, rSlaveVariable, Weight, Constant);
KRATOS_CATCH("");
}

Expand All @@ -241,7 +241,7 @@ class KRATOS_API(KRATOS_CORE) LinearMasterSlaveConstraint
{
KRATOS_TRY

MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared<LinearMasterSlaveConstraint>(*this);
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive<LinearMasterSlaveConstraint>(*this);
p_new_const->SetId(NewId);
p_new_const->SetData(this->GetData());
p_new_const->Set(Flags(*this));
Expand Down
28 changes: 25 additions & 3 deletions kratos/includes/master_slave_constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint
typedef Kratos::Variable<double> VariableType;

/// Pointer definition of MasterSlaveConstraint
KRATOS_CLASS_POINTER_DEFINITION(MasterSlaveConstraint);
KRATOS_CLASS_INTRUSIVE_POINTER_DEFINITION(MasterSlaveConstraint);

///@}
///@name Enum's
Expand Down Expand Up @@ -215,7 +215,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint
KRATOS_TRY

KRATOS_WARNING("MasterSlaveConstraint") << " Call base class constraint Clone " << std::endl;
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared<MasterSlaveConstraint>(*this);
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive<MasterSlaveConstraint>(*this);
p_new_const->SetId(NewId);
p_new_const->SetData(this->GetData());
p_new_const->Set(Flags(*this));
Expand Down Expand Up @@ -520,7 +520,7 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint
* @brief Check if the Data exists with Has(..) methods:
* @param rThisVariable The variable to be check
*/
template<class TDataType>
template<class TDataType>
bool Has(const Variable<TDataType>& rThisVariable) const
{
return mData.Has(rThisVariable);
Expand Down Expand Up @@ -612,6 +612,28 @@ class KRATOS_API(KRATOS_CORE) MasterSlaveConstraint

DataValueContainer mData; /// Pointer to the data related to this constraint

///@}
///@name Private operations
///@{

//*********************************************
//this block is needed for refcounting
mutable std::atomic<int> mReferenceCounter{0};

friend void intrusive_ptr_add_ref(const MasterSlaveConstraint* x)
{
x->mReferenceCounter.fetch_add(1, std::memory_order_relaxed);
}

friend void intrusive_ptr_release(const MasterSlaveConstraint* x)
{
if (x->mReferenceCounter.fetch_sub(1, std::memory_order_release) == 1) {
std::atomic_thread_fence(std::memory_order_acquire);
delete x;
}
}
//*********************************************

///@}
///@name Serialization
///@{
Expand Down
2 changes: 1 addition & 1 deletion kratos/tests/cpp_tests/sources/test_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace Kratos {
const auto& r_master_dofs = p_const->GetMasterDofsVector();

// Create empty constraint
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_shared<LinearMasterSlaveConstraint>(2);
MasterSlaveConstraint::Pointer p_new_const = Kratos::make_intrusive<LinearMasterSlaveConstraint>(2);
p_new_const->SetLocalSystem(matrix, vector, r_process_info);
p_new_const->SetDofList(r_slave_dofs, r_master_dofs, r_process_info);

Expand Down
2 changes: 1 addition & 1 deletion kratos/utilities/rve_periodicity_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ MasterSlaveConstraint::Pointer RVEPeriodicityUtility::GenerateConstraint(
for (IndexType i = 0; i < rMasterIds.size(); ++i)
master_dofs.push_back(mrModelPart.pGetNode(rMasterIds[i])->pGetDof(rVar));

auto pconstraint = Kratos::make_shared<LinearMasterSlaveConstraint>(rConstraintId, master_dofs, slave_dofs, rRelationMatrix, rTranslationVector);
auto pconstraint = Kratos::make_intrusive<LinearMasterSlaveConstraint>(rConstraintId, master_dofs, slave_dofs, rRelationMatrix, rTranslationVector);
rConstraintId++;
return pconstraint;
}
Expand Down
Loading