Skip to content

Commit 3dba466

Browse files
fredroyalxbilger
andauthored
[SofaCUDA] ElementFEMForceField: Generic CUDA implementation (#6071)
* [FEM.Elastic] Remove complex unproductive stiffness matrix data structure * explicit namespace * wip * add example * new version * improvment of new version * add cuda version of ElementLinearSmallStrainFEMForceField * add cuda version of addforce for ElementLinearSmallStrainFEMForceField * add cuda version of addforce for ElementCorotationalFEMForceField * update addforce to compute everything on GPU * try to fix with direct solver * fix corot * update examples * organize examples and add cpu-gpu comparison * add double version (templates) * add benchmarks * template CUDA kernels on Dim and remove runtime dispatch Replace hardcoded 3D assumption and extern "C" + switch(nbNodesPerElem) runtime dispatch with fully compile-time C++ template parameters <T, NNodes, Dim>. All kernel dimensions, stiffness block sizes, and gather loops are now generic over Dim. The .inl callers use a single template call with constexpr nNodes and dim from the trait, eliminating both the if-constexpr type branching and the runtime NNodes switch. Explicit template instantiations in the .cu files provide the needed symbols. Applied to both ElementLinearSmallStrainFEMForceField and ElementCorotationalFEMForceField CUDA implementations. * dont compile double version if SOFA_GPU_CUDA_DOUBLE is not enabled * use w accessors * Revert "use w accessors" This reverts commit e41d0b6. * refactor cuda code * clarify code * fix to follow change from removestiffness branch rebase --------- Co-authored-by: Alex Bilger <alexbilger0@gmail.com>
1 parent aaa29fd commit 3dba466

29 files changed

Lines changed: 3030 additions & 1 deletion

applications/plugins/SofaCUDA/Component/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ set(HEADER_FILES
3939

4040

4141
### solidmechanics
42+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementFEMKernelUtils.cuh
43+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementCorotationalFEMForceField.h
44+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementCorotationalFEMForceField.inl
45+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementLinearSmallStrainFEMForceField.h
46+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementLinearSmallStrainFEMForceField.inl
4247
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaHexahedronFEMForceField.h
4348
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaHexahedronFEMForceField.inl
4449
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/hyperelastic/CudaStandardTetrahedralFEMForceField.h
@@ -111,6 +116,8 @@ set(SOURCE_FILES
111116
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/mass/CudaUniformMass.cpp
112117

113118
### Solidmechanics
119+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementCorotationalFEMForceField.cpp
120+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementLinearSmallStrainFEMForceField.cpp
114121
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaHexahedronFEMForceField.cpp
115122
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/hyperelastic/CudaStandardTetrahedralFEMForceField.cpp
116123
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/tensormass/CudaTetrahedralTensorMassForceField.cpp
@@ -181,6 +188,8 @@ set(CUDA_SOURCES
181188
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/mass/CudaUniformMass.cu
182189

183190
### solidmechanics
191+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementCorotationalFEMForceField.cu
192+
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaElementLinearSmallStrainFEMForceField.cu
184193
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/elastic/CudaHexahedronFEMForceField.cu
185194
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/fem/hyperelastic/CudaStandardTetrahedralFEMForceField.cu
186195
${SOFACUDA_COMPONENT_SOURCE_DIR}/SofaCUDA/component/solidmechanics/tensormass/CudaTetrahedralTensorMassForceField.cu

applications/plugins/SofaCUDA/Component/src/SofaCUDA/component/init.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extern void registerPlaneForceField(sofa::core::ObjectFactory* factory);
9090
extern void registerSphereForceField(sofa::core::ObjectFactory* factory);
9191

9292
// component::solidmechanics::fem::elastic
93+
extern void registerElementCorotationalFEMForceField(sofa::core::ObjectFactory* factory);
94+
extern void registerElementLinearSmallStrainFEMForceField(sofa::core::ObjectFactory* factory);
9395
extern void registerHexahedronFEMForceField(sofa::core::ObjectFactory* factory);
9496
extern void registerTetrahedronFEMForceField(sofa::core::ObjectFactory* factory);
9597
extern void registerTriangularFEMForceFieldOptim(sofa::core::ObjectFactory* factory);
@@ -224,6 +226,8 @@ void registerObjects(sofa::core::ObjectFactory* factory)
224226
registerLinearForceField(factory);
225227
registerPlaneForceField(factory);
226228
registerSphereForceField(factory);
229+
registerElementCorotationalFEMForceField(factory);
230+
registerElementLinearSmallStrainFEMForceField(factory);
227231
registerHexahedronFEMForceField(factory);
228232
registerTetrahedronFEMForceField(factory);
229233
registerTriangularFEMForceFieldOptim(factory);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Authors: The SOFA Team and external contributors (see Authors.txt) *
19+
* *
20+
* Contact information: contact@sofa-framework.org *
21+
******************************************************************************/
22+
#include <SofaCUDA/component/config.h>
23+
24+
#include <sofa/gpu/cuda/CudaTypes.h>
25+
#include <SofaCUDA/component/solidmechanics/fem/elastic/CudaElementCorotationalFEMForceField.inl>
26+
#include <sofa/core/ObjectFactory.h>
27+
28+
namespace sofa::component::solidmechanics::fem::elastic
29+
{
30+
31+
using namespace sofa::gpu::cuda;
32+
33+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Edge>;
34+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Triangle>;
35+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Quad>;
36+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Tetrahedron>;
37+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Hexahedron>;
38+
39+
#ifdef SOFA_GPU_CUDA_DOUBLE
40+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Edge>;
41+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Triangle>;
42+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Quad>;
43+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Tetrahedron>;
44+
template class SOFACUDA_COMPONENT_API CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Hexahedron>;
45+
#endif
46+
47+
} // namespace sofa::component::solidmechanics::fem::elastic
48+
49+
namespace sofa::gpu::cuda
50+
{
51+
52+
void registerElementCorotationalFEMForceField(sofa::core::ObjectFactory* factory)
53+
{
54+
using namespace sofa::component::solidmechanics::fem::elastic;
55+
56+
factory->registerObjects(sofa::core::ObjectRegistrationData(
57+
"Supports GPU-side computations using CUDA for EdgeCorotationalFEMForceField")
58+
.add< CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Edge> >()
59+
);
60+
factory->registerObjects(sofa::core::ObjectRegistrationData(
61+
"Supports GPU-side computations using CUDA for TriangleCorotationalFEMForceField")
62+
.add< CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Triangle> >()
63+
);
64+
factory->registerObjects(sofa::core::ObjectRegistrationData(
65+
"Supports GPU-side computations using CUDA for QuadCorotationalFEMForceField")
66+
.add< CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Quad> >()
67+
);
68+
factory->registerObjects(sofa::core::ObjectRegistrationData(
69+
"Supports GPU-side computations using CUDA for TetrahedronCorotationalFEMForceField")
70+
.add< CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Tetrahedron> >()
71+
);
72+
factory->registerObjects(sofa::core::ObjectRegistrationData(
73+
"Supports GPU-side computations using CUDA for HexahedronCorotationalFEMForceField")
74+
.add< CudaElementCorotationalFEMForceField<CudaVec3fTypes, sofa::geometry::Hexahedron> >()
75+
);
76+
77+
#ifdef SOFA_GPU_CUDA_DOUBLE
78+
factory->registerObjects(sofa::core::ObjectRegistrationData(
79+
"Supports GPU-side computations using CUDA (double) for EdgeCorotationalFEMForceField")
80+
.add< CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Edge> >()
81+
);
82+
factory->registerObjects(sofa::core::ObjectRegistrationData(
83+
"Supports GPU-side computations using CUDA (double) for TriangleCorotationalFEMForceField")
84+
.add< CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Triangle> >()
85+
);
86+
factory->registerObjects(sofa::core::ObjectRegistrationData(
87+
"Supports GPU-side computations using CUDA (double) for QuadCorotationalFEMForceField")
88+
.add< CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Quad> >()
89+
);
90+
factory->registerObjects(sofa::core::ObjectRegistrationData(
91+
"Supports GPU-side computations using CUDA (double) for TetrahedronCorotationalFEMForceField")
92+
.add< CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Tetrahedron> >()
93+
);
94+
factory->registerObjects(sofa::core::ObjectRegistrationData(
95+
"Supports GPU-side computations using CUDA (double) for HexahedronCorotationalFEMForceField")
96+
.add< CudaElementCorotationalFEMForceField<CudaVec3dTypes, sofa::geometry::Hexahedron> >()
97+
);
98+
#endif
99+
}
100+
101+
} // namespace sofa::gpu::cuda

0 commit comments

Comments
 (0)