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

[StateContainer] Accelerate copy of MatrixDeriv for CRS matrices #4443

Merged
merged 7 commits into from
Jan 25, 2024
Merged
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
Expand Up @@ -1036,10 +1036,11 @@ auto MatrixLinearSystem<TMatrix, TVector>::computeJacobiansFrom(BaseMechanicalSt
return jacobians;
}

MechanicalResetConstraintVisitor(&cparams).execute(this->getSolveContext());

auto mappingJacobianId = sofa::core::MatrixDerivId::mappingJacobian();

// this clears the matrix identified by mappingJacobian() among others
MechanicalResetConstraintVisitor(&cparams).execute(this->getSolveContext());

// optimisation to build only the relevent entries of the jacobian matrices
// The relevent entries are the ones that have a influence on the result
// of the product J^T * K * J.
Expand Down Expand Up @@ -1073,6 +1074,13 @@ auto MatrixLinearSystem<TMatrix, TVector>::computeJacobiansFrom(BaseMechanicalSt
J->resize(mstate->getMatrixSize(), input->getMatrixSize());
unsigned int offset {};
input->copyToBaseMatrix(J.get(), mappingJacobianId, offset);

//set the sizes again because in some cases they are changed in copyToBaseMatrix
J->nCol = input->getMatrixSize();
J->nRow = mstate->getMatrixSize();
J->nBlockCol = J->nCol;
J->nBlockRow = J->nRow;

J->fullRows();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#include <algorithm>
#include <cassert>
#include <sofa/linearalgebra/CompressedRowSparseMatrixMechanical.h>


namespace
{
Expand Down Expand Up @@ -855,17 +857,28 @@ void MechanicalObject<DataTypes>::copyToBaseMatrix(linearalgebra::BaseMatrix* de
{
const MatrixDeriv& matrix = matrixData->getValue();

for (MatrixDerivRowConstIterator rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt)
if (auto* crs = dynamic_cast<linearalgebra::CompressedRowSparseMatrixMechanical<Real, sofa::linearalgebra::CRSMechanicalPolicy>*>(dest))
{
// This is more performant compared to the generic case
// The structure of the matrix is the same compared to the generic
// case, but dest sizes may be modified compared to the generic case
crs->copyNonZeros(matrix);
}
else //generic case
{
const int cid = rowIt.index();
for (MatrixDerivColConstIterator colIt = rowIt.begin(); colIt != rowIt.end(); ++colIt)
//no modification of the size
for (MatrixDerivRowConstIterator rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt)
{
const unsigned int dof = colIt.index();
const Deriv n = colIt.val();

for (unsigned int r = 0; r < Deriv::size(); ++r)
const int cid = rowIt.index();
for (MatrixDerivColConstIterator colIt = rowIt.begin(); colIt != rowIt.end(); ++colIt)
{
dest->add(cid, offset + dof * Deriv::size() + r, n[r]);
const unsigned int dof = colIt.index();
const Deriv n = colIt.val();

for (unsigned int r = 0; r < Deriv::size(); ++r)
{
dest->add(cid, offset + dof * Deriv::size() + r, n[r]);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class CompressedRowSparseMatrixMechanical final // final is used to allow the co
}
}

if (!keepEmptyRows && this->rowBegin.back() == vid) // row was empty
if (!keepEmptyRows && !this->rowBegin.empty() && this->rowBegin.back() == vid) // row was empty
{
this->rowIndex.pop_back();
this->rowBegin.pop_back();
Expand Down
Loading