-
Notifications
You must be signed in to change notification settings - Fork 299
/
CudaDiagonalMass.inl
169 lines (132 loc) · 6.45 KB
/
CudaDiagonalMass.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/******************************************************************************
* 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: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_COLLISION_CUDADIAGONALMASS_INL
#define SOFA_COMPONENT_COLLISION_CUDADIAGONALMASS_INL
#include <SofaCUDA/component/mass/CudaDiagonalMass.h>
#include <sofa/component/mass/DiagonalMass.inl>
namespace sofa
{
namespace gpu::cuda
{
using namespace sofa::gpu::cuda;
extern "C"
{
void DiagonalMassCuda_addMDxf(unsigned int size, float factor, const void * mass, const void* dx, void* res);
void DiagonalMassCuda_addMDxd(unsigned int size, double factor, const void * mass, const void* dx, void* res);
void DiagonalMassCuda_accFromFf(unsigned int size, const void * mass, const void* f, void* a);
void DiagonalMassCuda_accFromFd(unsigned int size, const void * mass, const void* f, void* a);
void DiagonalMassCuda_addForcef(unsigned int size, const void * mass,const void * g, const void* f);
void DiagonalMassCuda_addForced(unsigned int size, const void * mass,const void * g, const void* f);
}
}
namespace component::mass
{
template<>
void DiagonalMass<CudaVec3fTypes>::addMDx(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecDeriv& d_dx, SReal d_factor)
{
VecDeriv& f = *d_f.beginEdit();
const VecDeriv& dx = d_dx.getValue();
DiagonalMassCuda_addMDxf(dx.size(),(float) d_factor, d_vertexMass.getValue().deviceRead() , dx.deviceRead(), f.deviceWrite());
// const MassVector &masses= d_vertexMass.getValue();
// for (unsigned int i=0;i<dx.size();i++) {
// res[i] += dx[i] * masses[i] * (Real)factor;
// }
d_f.endEdit();
}
template<>
void DiagonalMass<CudaVec3fTypes>::accFromF(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_a, const DataVecDeriv& d_f)
{
VecDeriv& a = *d_a.beginEdit();
const VecDeriv& f = d_f.getValue();
DiagonalMassCuda_accFromFf(f.size(), d_vertexMass.getValue().deviceRead(), f.deviceRead(), a.deviceWrite());
// const MassVector &masses= d_vertexMass.getValue();
// for (unsigned int i=0;i<f.size();i++) {
// a[i] = f[i] / masses[i];
// }
d_a.endEdit();
}
template <>
void DiagonalMass<CudaVec3fTypes>::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/)
{
VecDeriv& f = *d_f.beginEdit();
//const VecCoord& x = d_x.getValue();
//const VecDeriv& v = d_v.getValue();
type::Vec3d g ( this->getContext()->getGravity() );
const MassVector &masses= d_vertexMass.getValue();
DiagonalMassCuda_addForcef(masses.size(),masses.deviceRead(),g.ptr(), f.deviceWrite());
// // gravity
// Vec3d g ( this->getContext()->getGravity() );
// Deriv theGravity;
// DataTypes::set ( theGravity, g[0], g[1], g[2]);
//
// for (unsigned int i=0;i<masses.size();i++) {
// f[i] += theGravity*masses[i];
// }
d_f.endEdit();
}
#ifdef SOFA_GPU_CUDA_DOUBLE
template<>
//void DiagonalMass<CudaVec3dTypes>::addMDx(VecDeriv& res, const VecDeriv& dx, double factor)
void DiagonalMass<CudaVec3dTypes>::addMDx(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecDeriv& d_dx, SReal d_factor)
{
VecDeriv& f = *d_f.beginEdit();
const VecDeriv& dx = d_dx.getValue();
DiagonalMassCuda_addMDxd(dx.size(),(double) d_factor, d_vertexMass.getValue().deviceRead() , dx.deviceRead(), f.deviceWrite());
d_f.endEdit();
}
template<>
void DiagonalMass<CudaVec3dTypes>::accFromF(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_a, const DataVecDeriv& d_f)
{
VecDeriv& a = *d_a.beginEdit();
const VecDeriv& f = d_f.getValue();
DiagonalMassCuda_accFromFd(f.size(), d_vertexMass.getValue().deviceRead(), f.deviceRead(), a.deviceWrite());
d_a.endEdit();
}
template<>
void DiagonalMass<CudaVec3dTypes>::addForce(const core::MechanicalParams* /*mparams*/, DataVecDeriv& d_f, const DataVecCoord& /*d_x*/, const DataVecDeriv& /*d_v*/)
{
VecDeriv& f = *d_f.beginEdit();
//const VecCoord& x = d_x.getValue();
//const VecDeriv& v = d_v.getValue();
Vec3d g ( this->getContext()->getGravity() );
const MassVector &masses= d_vertexMass.getValue();
DiagonalMassCuda_addForced(masses.size(),masses.deviceRead(),g.ptr(), f.deviceWrite());
d_f.endEdit();
}
// template <>
// bool DiagonalMass<CudaVec3dTypes>::addBBox(double* minBBox, double* maxBBox) {
// const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
// //if (!x.isHostValid()) return false; // Do not recompute bounding box if it requires to transfer data from device
// for (unsigned int i=0; i<x.size(); i++) {
// //const Coord& p = x[i];
// const Coord& p = x.getCached(i);
// for (int c=0;c<3;c++) {
// if (p[c] > maxBBox[c]) maxBBox[c] = p[c];
// if (p[c] < minBBox[c]) minBBox[c] = p[c];
// }
// }
// return true;
// }
#endif
} // namespace component::mass
} // namespace sofa
#endif