-
Notifications
You must be signed in to change notification settings - Fork 298
/
CudaBaseVector.h
303 lines (246 loc) · 8.41 KB
/
CudaBaseVector.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/******************************************************************************
* 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_GPU_CUDA_CUDABASEVECTOR_H
#define SOFA_GPU_CUDA_CUDABASEVECTOR_H
#include <sofa/gpu/cuda/CudaTypes.h>
#include <sofa/linearalgebra/BaseVector.h>
namespace sofa::gpu::cuda
{
///////////////
// KERNELS //
///////////////
extern "C"
{
void SOFA_GPU_CUDA_API copy_vectorf(int dim,const void * a, void * b);
void SOFA_GPU_CUDA_API vector_vector_peqf(int dim,float f,const void * a,void * b);
void SOFA_GPU_CUDA_API sub_vector_vectorf(int dim,const void * a, const void * b, void * r);
void SOFA_GPU_CUDA_API permute_vectorf(int dim,const void * a, const void * perm, void * b);
#ifdef SOFA_GPU_CUDA_DOUBLE
void SOFA_GPU_CUDA_API copy_vectord(int dim,const void * a, void * b);
void SOFA_GPU_CUDA_API vector_vector_peqd(int dim,double f,const void * a,void * b);
void SOFA_GPU_CUDA_API sub_vector_vectord(int dim,const void * a, const void * b, void * r);
void SOFA_GPU_CUDA_API permute_vectord(int dim,const void * a, const void * perm, void * b);
#endif
}
template<class real> class CudaVectorUtilsKernels;
template<> class CudaVectorUtilsKernels<float>
{
public:
// copy the dim first value of a(float) in b(float)
static void copy_vector(int dim,const void * a,void * b)
{ copy_vectorf(dim,a,b); }
// compute b = b + a*f
static void vector_vector_peq(int dim,float f,const void * a,void * b)
{ vector_vector_peqf(dim,f,a,b); }
// compute b = b + a*f
static void sub_vector_vector(int dim,const void * a,const void * b,void * r)
{ sub_vector_vectorf(dim,a,b,r); }
static void permute_vector(int dim,const void * a, const void * perm, void * b)
{ permute_vectorf(dim,a,perm,b); }
};
#ifdef SOFA_GPU_CUDA_DOUBLE
template<> class CudaVectorUtilsKernels<double>
{
public:
// copy the dim first value of a(float) in b(float)
static void copy_vector(int dim,const void * a,void * b)
{ copy_vectord(dim,a,b); }
// compute b = b + a*f
static void vector_vector_peq(int dim,double f,const void * a,void * b)
{ vector_vector_peqd(dim,f,a,b); }
// compute b = b + a*f
static void sub_vector_vector(int dim,const void * a,const void * b,void * r)
{ sub_vector_vectord(dim,a,b,r); }
static void permute_vector(int dim,const void * a, const void * perm, void * b)
{ permute_vectord(dim,a,perm,b); }
};
#endif
using namespace sofa::defaulttype;
template<class T>
class CudaBaseVectorType : public linearalgebra::BaseVector {
public :
typedef T Real;
typedef typename linearalgebra::BaseVector::Index Index;
virtual void resize(Index nbRow) = 0;
virtual Index size() const = 0;
virtual SReal element(Index i) const = 0;
virtual void clear() = 0;
virtual void set(Index i, SReal val) = 0;
virtual void add(Index i, SReal val) = 0;
virtual const void* deviceRead(Index off=0) const = 0;
virtual void * deviceWrite(Index off=0) = 0;
virtual const T* hostRead(Index off=0) const = 0;
virtual T * hostWrite(Index off=0) = 0;
virtual void invalidateDevice() = 0;
virtual void invalidateHost() = 0;
virtual T getSingle(Index off=0) = 0;
/// this += a*f
template<typename Real>
void peq(const CudaBaseVectorType<Real> & a, double f) {
CudaVectorUtilsKernels<Real>::vector_vector_peq(this->size(),
(Real)f,
a.deviceRead(),
this->deviceWrite());
}
/// this = a - b
template<typename Real>
void sub(const CudaBaseVectorType<Real>& a, const CudaBaseVectorType<Real>& b)
{
CudaVectorUtilsKernels<Real>::sub_vector_vector(this->size(),
a.deviceRead(),
b.deviceRead(),
this->deviceWrite());
}
};
template <class T>
class CudaBaseVector : public CudaBaseVectorType<T>
{
public :
typedef T Real;
typedef typename CudaBaseVectorType<T>::Index Index;
CudaVector<T>& getCudaVector()
{
return v;
}
const CudaVector<T>& getCudaVector() const
{
return v;
}
T& operator[](Index i)
{
return v[i];
}
const T& operator[](Index i) const
{
return v[i];
}
void fastResize(Index nbRow)
{
v.fastResize(nbRow);
}
void fastResize(Index nbRow,Index warp_size)
{
v.fastResize(nbRow,warp_size);
}
void resize(Index nbRow)
{
v.resize(nbRow);
}
void recreate(Index nbRow)
{
v.recreate(nbRow);
}
void resize(Index nbRow,Index warp_size)
{
v.resize(nbRow,warp_size);
}
Index size() const
{
return v.size();
}
SReal element(Index i) const
{
return v[i];
}
void clear()
{
//for (unsigned Index i=0; i<size(); i++) v[i]=(T)(0.0);
// v.memsetHost();
// Index size = v.size();
v.clear();
// v.resize(size);
}
void set(Index i, SReal val)
{
v[i] = (T) val;
}
void add(Index i, SReal val)
{
v[i] += (T)val;
}
void operator=(const CudaBaseVector<Real> & e)
{
v = e.v;
}
void eq(const CudaBaseVector<Real> & e)
{
v = e.v;
}
const void* deviceRead(Index off=0) const
{
return v.deviceReadAt(off);
}
void * deviceWrite(Index off=0)
{
return v.deviceWriteAt(off);
}
void invalidateDevice()
{
v.invalidateDevice();
}
void invalidateHost()
{
v.invalidateHost();
}
void memsetDevice()
{
v.memsetDevice();
}
const T* hostRead(Index off=0) const
{
return v.hostReadAt(off);
}
T * hostWrite(Index off=0)
{
return v.hostWriteAt(off);
}
T getSingle(Index off)
{
return v.getSingle(off);
}
static const char* Name(); /* {
return "CudaBaseVector";
}*/
friend std::ostream& operator<< ( std::ostream& os, const CudaBaseVector<T> & vec )
{
os << vec.v;
return os;
}
private :
CudaVector<T> v;
};
typedef CudaBaseVector<float> CudaBaseVectorf;
#ifdef SOFA_GPU_CUDA_DOUBLE
typedef CudaBaseVector<double> CudaBaseVectord;
#endif
template<> inline const char* CudaBaseVectorf::Name() { return "CudaBaseVectorf"; }
#ifdef SOFA_GPU_CUDA_DOUBLE
template<> inline const char* CudaBaseVectord::Name() { return "CudaBaseVectord"; }
#endif
#if !defined(SOFA_BUILD_GPU_CUDA)
extern template class SOFA_GPU_CUDA_API CudaBaseVector< float >;
#ifdef SOFA_GPU_CUDA_DOUBLE
extern template class SOFA_GPU_CUDA_API CudaBaseVector< double >;
#endif
#endif
} // namespace sofa::gpu::cuda
#endif