-
Notifications
You must be signed in to change notification settings - Fork 343
[Lagrangian] Refactoring of GenericConstraintSolver #5666
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
Merged
hugtalbot
merged 11 commits into
sofa-framework:master
from
bakpaul:25_08_modularize_solving_method_in_generic_constraint_solver
Oct 16, 2025
+1,448
−965
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad0a832
First working version of exploded constraint problrms. Still lots of …
bakpaul ad5fe25
Changed all problems into solvers. It makes more sense to use an inhe…
bakpaul cbeadf6
Apply changes to scenes, tests and tutorials
bakpaul ac6be24
Apply review comments
bakpaul 956c777
change ownership of d_multithreading
bakpaul 9a823b2
Add comment on do methods and changed signature to add constraint pro…
bakpaul dd1c031
Add scene check to fix and display info if GenericConstraintSolver is…
bakpaul c2d565e
Merge branch 'master' into 25_08_modularize_solving_method_in_generic…
bakpaul fbcb27c
Revert "Add scene check to fix and display info if GenericConstraintS…
bakpaul 2e94ac1
Add component change for GenericConstraintSolver
bakpaul f6260c4
Merge branch 'master' into 25_08_modularize_solving_method_in_generic…
hugtalbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...grangian/Solver/src/sofa/component/constraint/lagrangian/solver/BuiltConstraintSolver.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: [email protected] * | ||
| ******************************************************************************/ | ||
|
|
||
| #include <sofa/component/constraint/lagrangian/solver/BuiltConstraintSolver.h> | ||
|
|
||
| #include <sofa/helper/ScopedAdvancedTimer.h> | ||
| #include <sofa/simulation/MainTaskSchedulerFactory.h> | ||
| #include <sofa/simulation/ParallelForEach.h> | ||
|
|
||
| namespace sofa::component::constraint::lagrangian::solver | ||
| { | ||
| BuiltConstraintSolver::BuiltConstraintSolver() | ||
| : d_multithreading(initData(&d_multithreading, false, "multithreading", "Build compliances concurrently")) | ||
| {} | ||
|
|
||
| void BuiltConstraintSolver::init() | ||
| { | ||
| Inherit1::init(); | ||
| if(d_multithreading.getValue()) | ||
| { | ||
| simulation::MainTaskSchedulerFactory::createInRegistry()->init(); | ||
| } | ||
| } | ||
|
|
||
| void BuiltConstraintSolver::doBuildSystem( const core::ConstraintParams *cParams, GenericConstraintProblem * problem ,unsigned int numConstraints) | ||
| { | ||
| SOFA_UNUSED(numConstraints); | ||
| SCOPED_TIMER_VARNAME(getComplianceTimer, "Get Compliance"); | ||
| dmsg_info() <<" computeCompliance in " << l_constraintCorrections.size()<< " constraintCorrections" ; | ||
|
|
||
| const bool multithreading = d_multithreading.getValue(); | ||
|
|
||
| const simulation::ForEachExecutionPolicy execution = multithreading ? | ||
| simulation::ForEachExecutionPolicy::PARALLEL : | ||
| simulation::ForEachExecutionPolicy::SEQUENTIAL; | ||
|
|
||
| simulation::TaskScheduler* taskScheduler = simulation::MainTaskSchedulerFactory::createInRegistry(); | ||
| assert(taskScheduler); | ||
|
|
||
| //Used to prevent simultaneous accesses to the main compliance matrix | ||
| std::mutex mutex; | ||
|
|
||
| //Visits all constraint corrections to compute the compliance matrix projected | ||
| //in the constraint space. | ||
| simulation::forEachRange(execution, *taskScheduler, l_constraintCorrections.begin(), l_constraintCorrections.end(), | ||
| [&cParams, this, &multithreading, &mutex, problem](const auto& range) | ||
| { | ||
| ComplianceWrapper compliance(problem->W, multithreading); | ||
|
|
||
| for (auto it = range.start; it != range.end; ++it) | ||
| { | ||
| core::behavior::BaseConstraintCorrection* cc = *it; | ||
| if (cc->isActive()) | ||
| { | ||
| cc->addComplianceInConstraintSpace(cParams, &compliance.matrix()); | ||
| } | ||
| } | ||
|
|
||
| std::lock_guard guard(mutex); | ||
| compliance.assembleMatrix(); | ||
| }); | ||
|
|
||
| addRegularization(problem->W, d_regularizationTerm.getValue()); | ||
| dmsg_info() << " computeCompliance_done " ; | ||
| } | ||
|
|
||
|
|
||
| BuiltConstraintSolver::ComplianceWrapper::ComplianceMatrixType& BuiltConstraintSolver::ComplianceWrapper::matrix() | ||
| { | ||
| if (m_isMultiThreaded) | ||
| { | ||
| if (!m_threadMatrix) | ||
| { | ||
| m_threadMatrix = std::make_unique<ComplianceMatrixType>(); | ||
| m_threadMatrix->resize(m_complianceMatrix.rowSize(), m_complianceMatrix.colSize()); | ||
| } | ||
| return *m_threadMatrix; | ||
| } | ||
| return m_complianceMatrix; | ||
| } | ||
|
|
||
| void BuiltConstraintSolver::ComplianceWrapper::assembleMatrix() const | ||
| { | ||
| if (m_threadMatrix) | ||
| { | ||
| for (linearalgebra::BaseMatrix::Index j = 0; j < m_threadMatrix->rowSize(); ++j) | ||
| { | ||
| for (linearalgebra::BaseMatrix::Index l = 0; l < m_threadMatrix->colSize(); ++l) | ||
| { | ||
| m_complianceMatrix.add(j, l, m_threadMatrix->element(j,l)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
68 changes: 68 additions & 0 deletions
68
...Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/BuiltConstraintSolver.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: [email protected] * | ||
| ******************************************************************************/ | ||
| #pragma once | ||
|
|
||
| #include <sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h> | ||
|
|
||
| namespace sofa::component::constraint::lagrangian::solver | ||
| { | ||
| /** | ||
| * \brief This component implements a generic way of building system for solvers that use a built | ||
| * version of the constraint matrix. Any solver that uses a build matrix should inherit from this. | ||
| * This component is purely virtual because doSolve is not defined and needs to be defined in the | ||
| * inherited class | ||
| */ | ||
| class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API BuiltConstraintSolver : public GenericConstraintSolver | ||
| { | ||
|
|
||
|
|
||
| public: | ||
| SOFA_CLASS(BuiltConstraintSolver, GenericConstraintSolver); | ||
| Data<bool> d_multithreading; ///< Build compliances concurrently | ||
|
|
||
| BuiltConstraintSolver(); | ||
|
|
||
| virtual void init() override; | ||
|
|
||
| protected: | ||
| virtual void doBuildSystem( const core::ConstraintParams *cParams, GenericConstraintProblem * problem ,unsigned int numConstraints) override; | ||
|
|
||
| private: | ||
|
|
||
| struct ComplianceWrapper | ||
| { | ||
| using ComplianceMatrixType = sofa::linearalgebra::LPtrFullMatrix<SReal>; | ||
|
|
||
| ComplianceWrapper(ComplianceMatrixType& complianceMatrix, bool isMultiThreaded) | ||
| : m_isMultiThreaded(isMultiThreaded), m_complianceMatrix(complianceMatrix) {} | ||
|
|
||
| ComplianceMatrixType& matrix(); | ||
|
|
||
| void assembleMatrix() const; | ||
|
|
||
| private: | ||
| bool m_isMultiThreaded { false }; | ||
| ComplianceMatrixType& m_complianceMatrix; | ||
| std::unique_ptr<ComplianceMatrixType> m_threadMatrix; | ||
| }; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bakpaul marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.