Skip to content
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
3 changes: 3 additions & 0 deletions Sofa/Component/LinearSystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set(HEADER_FILES
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/CompositeLinearSystem.inl
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityPatternSystem.h
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityPatternSystem.inl
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityProjectionMethod.h
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityProjectionMethod.inl
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/CreateMatrixDispatcher.h
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/LinearSystemData.h
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/MappingGraph.h
Expand Down Expand Up @@ -40,6 +42,7 @@ set(SOURCE_FILES
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/BaseMatrixProjectionMethod.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/CompositeLinearSystem.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityPatternSystem.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/ConstantSparsityProjectionMethod.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/MappingGraph.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/MatrixLinearSystem.cpp
${SOFACOMPONENTLINEARSOLVERLINEARSYSTEM_SOURCE_DIR}/MatrixProjectionMethod.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/******************************************************************************
* 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] *
******************************************************************************/
#define SOFA_COMPONENT_LINEARSYSTEM_CONSTANTSPARSITYPROJECTIONMETHOD_CPP
#include <sofa/component/linearsystem/ConstantSparsityProjectionMethod.inl>
#include <sofa/core/ObjectFactory.h>

namespace sofa::component::linearsystem
{
template class SOFA_COMPONENT_LINEARSYSTEM_API ConstantSparsityProjectionMethod<sofa::linearalgebra::CompressedRowSparseMatrix<SReal> >;

int ConstantSparsityProjectionMethodClass = core::RegisterObject("Matrix mapping computing the matrix projection taking advantage of the constant sparsity pattern")
.add< ConstantSparsityProjectionMethod<sofa::linearalgebra::CompressedRowSparseMatrix<SReal> > >(true)
;
}
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/linearsystem/MatrixProjectionMethod.h>
#include <sofa/simulation/ParallelSparseMatrixProduct.h>

namespace sofa::component::linearsystem
{

/**
* Matrix prjection method computing the matrix projection taking advantage of the constant sparsity pattern
*/
template<class TMatrix>
class ConstantSparsityProjectionMethod : public MatrixProjectionMethod<TMatrix>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(ConstantSparsityProjectionMethod, TMatrix), SOFA_TEMPLATE(MatrixProjectionMethod, TMatrix));
using PairMechanicalStates = typename BaseMatrixProjectionMethod<TMatrix>::PairMechanicalStates;
using Block = typename Inherit1::Block;

ConstantSparsityProjectionMethod();
~ConstantSparsityProjectionMethod() override;

Data<bool> d_parallelProduct;

void init() override;

protected:
void computeProjection(
const Eigen::Map<Eigen::SparseMatrix<Block, Eigen::RowMajor> > KMap,
const sofa::type::fixed_array<std::shared_ptr<TMatrix>, 2> J,
Eigen::SparseMatrix<Block, Eigen::RowMajor>& JT_K_J) override;

using K_Type = Eigen::Map<Eigen::SparseMatrix<Block, Eigen::RowMajor> >;
using J_Type = Eigen::Map<Eigen::SparseMatrix<Block, Eigen::RowMajor> >;
using KJ_Type = Eigen::SparseMatrix<Block, Eigen::ColMajor>;
using JT_Type = const Eigen::Transpose<const Eigen::Map<Eigen::SparseMatrix<Block, Eigen::RowMajor> > >;
using JTKJ_Type = Eigen::SparseMatrix<Block, Eigen::RowMajor>;

std::unique_ptr<linearalgebra::SparseMatrixProduct< K_Type, J_Type, KJ_Type> > m_matrixProductKJ;
std::unique_ptr<linearalgebra::SparseMatrixProduct< JT_Type, KJ_Type, JTKJ_Type> > m_matrixProductJTKJ;
};

#if !defined(SOFA_COMPONENT_LINEARSYSTEM_CONSTANTSPARSITYPROJECTIONMETHOD_CPP)
extern template class SOFA_COMPONENT_LINEARSYSTEM_API ConstantSparsityProjectionMethod<sofa::linearalgebra::CompressedRowSparseMatrix<SReal> >;
#endif

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/******************************************************************************
* 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/linearsystem/ConstantSparsityProjectionMethod.h>

#include <Eigen/Sparse>
#include <sofa/helper/ScopedAdvancedTimer.h>
#include <sofa/simulation/MainTaskSchedulerFactory.h>
#include <sofa/simulation/ParallelSparseMatrixProduct.h>

namespace sofa::component::linearsystem
{

template <class TMatrix>
ConstantSparsityProjectionMethod<TMatrix>::ConstantSparsityProjectionMethod()
: d_parallelProduct(initData(&d_parallelProduct, true, "parallelProduct", "Compute the matrix product in parallel"))
{}

template <class TMatrix>
ConstantSparsityProjectionMethod<TMatrix>::
~ConstantSparsityProjectionMethod() = default;

template <class TMatrix>
void ConstantSparsityProjectionMethod<TMatrix>::init()
{
Inherit1::init();

if (d_parallelProduct.getValue())
{
auto* taskScheduler = simulation::MainTaskSchedulerFactory::createInRegistry();
taskScheduler->init();

auto* matrixPrductKJ = new sofa::simulation::ParallelSparseMatrixProduct<
K_Type, J_Type, KJ_Type>();

m_matrixProductKJ = std::unique_ptr<sofa::simulation::ParallelSparseMatrixProduct<
K_Type, J_Type, KJ_Type>>(matrixPrductKJ);

matrixPrductKJ->taskScheduler = taskScheduler;


auto* matrixPrductJTKJ = new sofa::simulation::ParallelSparseMatrixProduct<
JT_Type, KJ_Type, JTKJ_Type>();

m_matrixProductJTKJ = std::unique_ptr<sofa::simulation::ParallelSparseMatrixProduct<
JT_Type, KJ_Type, JTKJ_Type>>(matrixPrductJTKJ);

matrixPrductJTKJ->taskScheduler = taskScheduler;
}
else
{
m_matrixProductKJ = std::make_unique<sofa::linearalgebra::SparseMatrixProduct<
K_Type, J_Type, KJ_Type>>();

m_matrixProductJTKJ = std::make_unique<sofa::linearalgebra::SparseMatrixProduct<
JT_Type, KJ_Type, JTKJ_Type>>();
}
}

template <class TMatrix>
void ConstantSparsityProjectionMethod<TMatrix>::computeProjection(
const Eigen::Map<Eigen::SparseMatrix<Block, Eigen::RowMajor>> KMap,
const sofa::type::fixed_array<std::shared_ptr<TMatrix>, 2> J,
Eigen::SparseMatrix<Block, Eigen::RowMajor>& JT_K_J)
{
if (J[0] && J[1])
{
const auto JMap0 = this->makeEigenMap(*J[0]);
const auto JMap1 = this->makeEigenMap(*J[1]);

m_matrixProductKJ->m_lhs = &KMap;
m_matrixProductKJ->m_rhs = &JMap1;
m_matrixProductKJ->computeProduct();

const JT_Type JMap0T = JMap0.transpose();
m_matrixProductJTKJ->m_lhs = &JMap0T;
m_matrixProductJTKJ->m_rhs = &m_matrixProductKJ->getProductResult();
m_matrixProductJTKJ->computeProduct();

JT_K_J = m_matrixProductJTKJ->getProductResult();
}
else if (J[0] && !J[1])
{
const auto JMap0 = this->makeEigenMap(*J[0]);
JT_K_J = JMap0.transpose() * KMap;
}
else if (!J[0] && J[1])
{
const auto JMap1 = this->makeEigenMap(*J[1]);

m_matrixProductKJ->m_lhs = &KMap;
m_matrixProductKJ->m_rhs = &JMap1;

m_matrixProductKJ->computeProduct();

JT_K_J = m_matrixProductKJ->getProductResult();
}
else
{
JT_K_J = KMap;
}
}


}