Skip to content

Commit

Permalink
[Multithreading] Apply new factory registration mechanism (#5178)
Browse files Browse the repository at this point in the history
* apply new register mechanism to multithreading

* fix test

* Apply suggestions from code review (update descriptions)

Co-authored-by: Hugo <[email protected]>

---------

Co-authored-by: Hugo <[email protected]>
  • Loading branch information
fredroy and hugtalbot authored Dec 28, 2024
1 parent bcc5a16 commit 82c4ea2
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ namespace sofa::core

SOFA_EVENT_CPP(DataExchangeEvent)

// Register in the Factory
int DataExchangeClass = core::RegisterObject("DataExchange")
.add< DataExchange< sofa::type::vector<sofa::type::Vec3d> > >(true)
.add< DataExchange< sofa::type::vector<sofa::type::Vec2d> > >()
.add< DataExchange< sofa::type::vector<double> > >()
.add< DataExchange< sofa::type::Vec3d > >()
.add< DataExchange< double > >()

.add< DataExchange< sofa::type::vector<sofa::type::Vec3f> > >()
.add< DataExchange< sofa::type::vector<sofa::type::Vec2f> > >()
.add< DataExchange< sofa::type::vector<float> > >()
.add< DataExchange< sofa::type::Vec3f > >()
.add< DataExchange< float > >()
void registerDataExchange(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(core::ObjectRegistrationData("Component for data memory sharing in the context of multi-threading applications")
.add< DataExchange< sofa::type::vector<sofa::type::Vec3d> > >(true)
.add< DataExchange< sofa::type::vector<sofa::type::Vec2d> > >()
.add< DataExchange< sofa::type::vector<double> > >()
.add< DataExchange< sofa::type::Vec3d > >()
.add< DataExchange< double > >()

.add< DataExchange< sofa::type::vector<int> > >()
.add< DataExchange< sofa::type::vector<unsigned int> > >()
.add< DataExchange< bool > >()
;
.add< DataExchange< sofa::type::vector<sofa::type::Vec3f> > >()
.add< DataExchange< sofa::type::vector<sofa::type::Vec2f> > >()
.add< DataExchange< sofa::type::vector<float> > >()
.add< DataExchange< sofa::type::Vec3f > >()
.add< DataExchange< float > >()

.add< DataExchange< sofa::type::vector<int> > >()
.add< DataExchange< sofa::type::vector<unsigned int> > >()
.add< DataExchange< bool > >());
}

template class SOFA_MULTITHREADING_PLUGIN_API DataExchange< sofa::type::vector<sofa::type::Vec3d> >;
template class SOFA_MULTITHREADING_PLUGIN_API DataExchange< sofa::type::vector<sofa::type::Vec2d> >;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
namespace sofa::component::engine
{

int MeanComputationEngineClass = core::RegisterObject("Compute the mean of the input elements")
.add< MeanComputation<defaulttype::Vec3Types> >(true) // default template
.add< MeanComputation<defaulttype::Vec1Types> >()
.add< MeanComputation<defaulttype::Vec2Types> >()
.add< MeanComputation<defaulttype::Rigid2Types> >()
.add< MeanComputation<defaulttype::Rigid3Types> >()
;
void registerMeanComputationEngine(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(core::ObjectRegistrationData("Compute the mean of the input elements.")
.add< MeanComputation<defaulttype::Vec3Types> >(true) // default template
.add< MeanComputation<defaulttype::Vec1Types> >()
.add< MeanComputation<defaulttype::Vec2Types> >()
.add< MeanComputation<defaulttype::Rigid2Types> >()
.add< MeanComputation<defaulttype::Rigid3Types> >());
}

template class SOFA_MULTITHREADING_PLUGIN_API MeanComputation< defaulttype::Vec3Types >;
template class SOFA_MULTITHREADING_PLUGIN_API MeanComputation< defaulttype::Vec1Types >;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
namespace multithreading::component::animationloop
{

int AnimationLoopParallelSchedulerClass = sofa::core::RegisterObject("parallel animation loop, using intel tbb library")
.add< AnimationLoopParallelScheduler >()
;
void registerAnimationLoopParallelScheduler(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel animation loop, using the Intel tbb library.")
.add< AnimationLoopParallelScheduler >());
}

AnimationLoopParallelScheduler::AnimationLoopParallelScheduler(sofa::simulation::Node* _gnode)
: Inherit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ namespace multithreading::component::collision::detection::algorithm
const bool isParallelBVHNarrowPhaseImplementationRegistered =
multithreading::ParallelImplementationsRegistry::addEquivalentImplementations("BVHNarrowPhase", "ParallelBVHNarrowPhase");

void registerParallelBVHNarrowPhase(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel version of the narrow phase collision detection based on boundary volume hierarchy.")
.add< ParallelBVHNarrowPhase >());
}

using sofa::helper::ScopedAdvancedTimer;

int ParallelBVHNarrowPhaseClass = sofa::core::RegisterObject("Narrow phase collision detection based on boundary volume hierarchy")
.add< ParallelBVHNarrowPhase >()
;

ParallelBVHNarrowPhase::ParallelBVHNarrowPhase()
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ namespace multithreading::component::collision::detection::algorithm
const bool isParallelParallelBruteForceBroadPhaseImplementationRegistered =
multithreading::ParallelImplementationsRegistry::addEquivalentImplementations("BruteForceBroadPhase", "ParallelBruteForceBroadPhase");

using sofa::helper::ScopedAdvancedTimer;
void registerParallelBruteForceBroadPhase(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel version of the collision detection using extensive pair-wise tests performed concurrently.")
.add< ParallelBruteForceBroadPhase >());
}

int ParallelBruteForceBroadPhaseClass = sofa::core::RegisterObject("Collision detection using extensive pair-wise tests performed in parallel")
.add< ParallelBruteForceBroadPhase >()
;
using sofa::helper::ScopedAdvancedTimer;

ParallelBruteForceBroadPhase::ParallelBruteForceBroadPhase()
: BruteForceBroadPhase()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/******************************************************************************
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
Expand Down Expand Up @@ -59,9 +59,12 @@ ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<SReal>, sofa
template class SOFA_MULTITHREADING_PLUGIN_API
ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<sofa::type::Mat<3,3,SReal>>, sofa::linearalgebra::FullVector<SReal> >;

int ParallelCGLinearSolverClass = sofa::core::RegisterObject("Linear system solver using the conjugate gradient iterative algorithm in parallel")
.add< ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<SReal>, sofa::linearalgebra::FullVector<SReal> > >(true)
.add< ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<sofa::type::Mat<3,3,SReal>>, sofa::linearalgebra::FullVector<SReal> > >();
void registerParallelCGLinearSolver(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel version of the linear solver using the conjugate gradient iterative algorithm.")
.add< ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<SReal>, sofa::linearalgebra::FullVector<SReal> > >(true)
.add< ParallelCGLinearSolver< ParallelCompressedRowSparseMatrixMechanical<sofa::type::Mat<3,3,SReal>>, sofa::linearalgebra::FullVector<SReal> > >());
}

const bool isParallelCGLinearSolverImplementationRegistered =
ParallelImplementationsRegistry::addEquivalentImplementations("CGLinearSolver", "ParallelCGLinearSolver");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace multithreading::component::mapping::linear
const bool isBeamLinearMapping_mtImplementationRegistered =
multithreading::ParallelImplementationsRegistry::addEquivalentImplementations("BeamLinearMapping", "BeamLinearMapping_mt");

//using namespace defaulttype;
// Register in the Factory
int BeamLinearMapping_mtClass = sofa::core::RegisterObject("Set the positions and velocities of points attached to a beam using linear interpolation between DOFs")
.add< BeamLinearMapping_mt< Rigid3Types, Vec3Types > >()
;
void registerBeamLinearMapping_mt(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Set the positions and velocities of points attached to a beam using linear interpolation between DOFs.")
.add< BeamLinearMapping_mt< Rigid3Types, Vec3Types > >());
}

template class BeamLinearMapping_mt< Rigid3Types, Vec3Types >;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
namespace multithreading::component::forcefield::solidmechanics::fem::elastic
{

using namespace sofa::defaulttype;

const bool isParallelHexahedronFEMForceFieldImplementationRegistered =
multithreading::ParallelImplementationsRegistry::addEquivalentImplementations("HexahedronFEMForceField", "ParallelHexahedronFEMForceField");

using namespace sofa::defaulttype;

// Register in the Factory
int ParallelHexahedronFEMForceFieldClass = sofa::core::RegisterObject("Parallel hexahedral finite elements")
.add < ParallelHexahedronFEMForceField < Vec3Types > > ();
void registerParallelHexahedronFEMForceField(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel implementation of a linear elastic material using hexahedral finite elements.")
.add < ParallelHexahedronFEMForceField < Vec3Types > > ());
}

template class SOFA_MULTITHREADING_PLUGIN_API ParallelHexahedronFEMForceField<Vec3Types>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@

#include <MultiThreading/ParallelImplementationsRegistry.h>

namespace multithreading::component::solidmechanics::fem::elastic
namespace multithreading::component::forcefield::solidmechanics::fem::elastic
{

using namespace sofa::defaulttype;

const bool isParallelTetrahedronFEMForceFieldImplementationRegistered =
multithreading::ParallelImplementationsRegistry::addEquivalentImplementations("TetrahedronFEMForceField", "ParallelTetrahedronFEMForceField");

// Register in the Factory
int ParallelTetrahedronFEMForceFieldClass = sofa::core::RegisterObject("Parallel tetrahedral finite elements")
.add < ParallelTetrahedronFEMForceField < Vec3Types > > ();
void registerParallelTetrahedronFEMForceField(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel implementation of a linear elastic material using tetrahedral finite elements..")
.add < ParallelTetrahedronFEMForceField < Vec3Types > > ());
}

template class SOFA_MULTITHREADING_PLUGIN_API ParallelTetrahedronFEMForceField<Vec3Types>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include <thread>

namespace multithreading::component::solidmechanics::fem::elastic
namespace multithreading::component::forcefield::solidmechanics::fem::elastic
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <sofa/simulation/MainTaskSchedulerFactory.h>
#include <sofa/simulation/ParallelForEach.h>

namespace multithreading::component::solidmechanics::fem::elastic
namespace multithreading::component::forcefield::solidmechanics::fem::elastic
{

template<class DataTypes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
namespace multithreading::component::solidmechanics::spring
{

int ParallelMeshSpringForceFieldClass = sofa::core::RegisterObject("Parallel stiff springs acting along the edges of a mesh")
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec3Types> >()
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec2Types> >()
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec1Types> >();
void registerParallelMeshSpringForceField(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel stiff springs acting along the edges of a mesh.")
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec3Types> >()
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec2Types> >()
.add< ParallelMeshSpringForceField<sofa::defaulttype::Vec1Types> >());
}

template class SOFA_MULTITHREADING_PLUGIN_API ParallelMeshSpringForceField<sofa::defaulttype::Vec3Types>;
template class SOFA_MULTITHREADING_PLUGIN_API ParallelMeshSpringForceField<sofa::defaulttype::Vec2Types>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
namespace multithreading::component::solidmechanics::spring
{

int ParallelSpringForceFieldClass = sofa::core::RegisterObject("Parallel stiff springs")
.add< ParallelSpringForceField<sofa::defaulttype::Vec3Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec2Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec1Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec6Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Rigid3Types> >();
void registerParallelSpringForceField(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(sofa::core::ObjectRegistrationData("Parallel stiff springs.")
.add< ParallelSpringForceField<sofa::defaulttype::Vec3Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec2Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec1Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Vec6Types> >()
.add< ParallelSpringForceField<sofa::defaulttype::Rigid3Types> >());
}

template class SOFA_MULTITHREADING_PLUGIN_API ParallelSpringForceField<sofa::defaulttype::Vec3Types>;
template class SOFA_MULTITHREADING_PLUGIN_API ParallelSpringForceField<sofa::defaulttype::Vec2Types>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#ifndef MULTITHREADING_CONFIG_H
#define MULTITHREADING_CONFIG_H
#pragma once

#include <sofa/config.h>

Expand All @@ -31,7 +30,11 @@
# define SOFA_MULTITHREADING_PLUGIN_API SOFA_IMPORT_DYNAMIC_LIBRARY
#endif

#endif
namespace multithreading
{
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@";
} // namespace multithreading

#ifdef SOFA_BUILD_MULTITHREADING
#define SOFA_ATTRIBUTE_DISABLED__TASKSCHEDULERUSER_DATANAME(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,73 @@

#include <sofa/component/linearsolver/iterative/init.h>

#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/system/PluginManager.h>


namespace sofa::core
{
extern void registerDataExchange(sofa::core::ObjectFactory* factory);
}

namespace sofa::component::engine
{
extern void registerMeanComputationEngine(sofa::core::ObjectFactory* factory);
}

namespace multithreading
{

namespace component::animationloop
{
extern void registerAnimationLoopParallelScheduler(sofa::core::ObjectFactory* factory);
}

namespace component::collision::detection::algorithm
{
extern void registerParallelBVHNarrowPhase(sofa::core::ObjectFactory* factory);
extern void registerParallelBruteForceBroadPhase(sofa::core::ObjectFactory* factory);
}

namespace component::linearsolver::iterative
{
extern void registerParallelCGLinearSolver(sofa::core::ObjectFactory* factory);
}

namespace component::mapping::linear
{
extern void registerBeamLinearMapping_mt(sofa::core::ObjectFactory* factory);
}

namespace component::forcefield::solidmechanics::fem::elastic
{
extern void registerParallelHexahedronFEMForceField(sofa::core::ObjectFactory* factory);
extern void registerParallelTetrahedronFEMForceField(sofa::core::ObjectFactory* factory);
}

namespace component::solidmechanics::spring
{
extern void registerParallelMeshSpringForceField(sofa::core::ObjectFactory* factory);
extern void registerParallelSpringForceField(sofa::core::ObjectFactory* factory);
}

extern "C" {
SOFA_MULTITHREADING_PLUGIN_API void initExternalModule();
SOFA_MULTITHREADING_PLUGIN_API const char* getModuleName();
SOFA_MULTITHREADING_PLUGIN_API const char* getModuleVersion();
SOFA_MULTITHREADING_PLUGIN_API const char* getModuleLicense();
SOFA_MULTITHREADING_PLUGIN_API const char* getModuleDescription();
SOFA_MULTITHREADING_PLUGIN_API void registerObjects(sofa::core::ObjectFactory* factory);
}

void init()
{
static bool first = true;
if (first)
{
// make sure that this plugin is registered into the PluginManager
sofa::helper::system::PluginManager::getInstance().registerPlugin(MODULE_NAME);

sofa::component::linearsolver::iterative::init();
first = false;
}
Expand All @@ -53,12 +103,12 @@ void initExternalModule()

const char* getModuleName()
{
return "MultiThreading";
return MODULE_NAME;
}

const char* getModuleVersion()
{
return "1.0";
return MODULE_VERSION;
}

const char* getModuleLicense()
Expand All @@ -71,4 +121,20 @@ const char* getModuleDescription()
return "MultiThreading SOFA Framework";
}

void registerObjects(sofa::core::ObjectFactory* factory)
{
sofa::core::registerDataExchange(factory);
sofa::component::engine::registerMeanComputationEngine(factory);
multithreading::component::animationloop::registerAnimationLoopParallelScheduler(factory);
multithreading::component::collision::detection::algorithm::registerParallelBVHNarrowPhase(factory);
multithreading::component::collision::detection::algorithm::registerParallelBruteForceBroadPhase(factory);
multithreading::component::linearsolver::iterative::registerParallelCGLinearSolver(factory);
multithreading::component::mapping::linear::registerBeamLinearMapping_mt(factory);
multithreading::component::forcefield::solidmechanics::fem::elastic::registerParallelHexahedronFEMForceField(factory);
multithreading::component::forcefield::solidmechanics::fem::elastic::registerParallelTetrahedronFEMForceField(factory);
multithreading::component::solidmechanics::spring::registerParallelMeshSpringForceField(factory);
multithreading::component::solidmechanics::spring::registerParallelSpringForceField(factory);

}

}
Loading

0 comments on commit 82c4ea2

Please sign in to comment.