-
Notifications
You must be signed in to change notification settings - Fork 297
/
CudaSpatialGridContainer.inl
336 lines (296 loc) · 13.6 KB
/
CudaSpatialGridContainer.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/******************************************************************************
* 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 *
******************************************************************************/
//
// C++ Interface: SpatialGridContainer
//
// Description:
//
//
// Author: The SOFA team <http://www.sofa-framework.org>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef SOFA_GPU_CUDA_CUDASPATIALGRIDCONTAINER_INL
#define SOFA_GPU_CUDA_CUDASPATIALGRIDCONTAINER_INL
#include <sofa/gpu/cuda/CudaSpatialGridContainer.h>
#include <sofa/gpu/cuda/CudaSort.h>
#include <SofaSphFluid/SpatialGridContainer.inl>
#include <sofa/gl/template.h>
namespace sofa
{
namespace gpu::cuda
{
extern "C"
{
void SpatialGridContainer3f_computeHash(int cellBits, float cellWidth, int nbPoints, void* particleIndex8, void* particleHash8, const void* x);
void SpatialGridContainer3f1_computeHash(int cellBits, float cellWidth, int nbPoints, void* particleIndex8, void* particleHash8, const void* x);
void SpatialGridContainer_findCellRange(int cellBits, int index0, float cellWidth, int nbPoints, const void* particleHash8, void* cellRange, void* cellGhost);
//void SpatialGridContainer3f_reorderData(int nbPoints, const void* particleHash, void* sorted, const void* x);
//void SpatialGridContainer3f1_reorderData(int nbPoints, const void* particleHash, void* sorted, const void* x);
}
} // namespace gpu::cuda
namespace component::container
{
using namespace sofa::helper;
// template<class TCoord, class TDeriv, class TReal>
// typename SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::Grid SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::emptyGrid;
template<class TCoord, class TDeriv, class TReal>
SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::SpatialGrid(Real cellWidth)
: cellWidth(cellWidth), invCellWidth(1/cellWidth), lastX(NULL)
{
cellBits = 15;
nbCells = 1<<cellBits;
}
template<class TCoord, class TDeriv, class TReal>
SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::~SpatialGrid()
{
}
template<class TCoord, class TDeriv, class TReal> template<class NeighborListener>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::findNeighbors(NeighborListener* /*dest*/, Real /*dist*/)
{
msg_error("SpatialGrid") << "TODO: SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::findNeighbors(NeighborListener* dest, Real dist)";
}
template<class TCoord, class TDeriv, class TReal>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::computeField(ParticleField* /*field*/, Real /*dist*/)
{
msg_error("SpatialGrid") << "TODO: SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::computeField(ParticleField* field, Real dist)";
}
template<class TCoord, class TDeriv, class TReal>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::reorderIndices(type::vector<Index>* /*old2new*/, type::vector<Index>* /*new2old*/)
{
msg_error("SpatialGrid") << "TODO: SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::reorderIndices(type::vector<Index>* old2new, type::vector<Index>* new2old)";
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3fTypes > >::kernel_computeHash(
int cellBits, Real cellWidth, int nbPoints, void* particleIndex, void* particleHash,
const void* x)
{
/*
{
helper::ReadAccessor< sofa::gpu::cuda::CudaVector< unsigned int > > pparticleHash = this->particleHash;
for (int i=0;i<nbPoints; i+= nbPoints/9+1)
{
std::cout << "Chash["<<i<<"] =" ;
for (int x=0;x<8;++x) std::cout << "\t" << pparticleHash[i*8+x];
std::cout<<std::endl;
}
}
*/
gpu::cuda::SpatialGridContainer3f_computeHash(cellBits, cellWidth, nbPoints, particleIndex, particleHash, x);
/*
this->particleHash.deviceWrite();
{
helper::ReadAccessor< sofa::gpu::cuda::CudaVector< unsigned int > > pparticleHash = this->particleHash;
for (int i=0;i<nbPoints; i+= nbPoints/9+1)
{
std::cout << "Ghash["<<i<<"] =" ;
for (int x=0;x<8;++x) std::cout << "\t" << pparticleHash[i*8+x];
std::cout<<std::endl;
}
}
*/
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3fTypes > >::kernel_updateGrid(
int cellBits, int index0, Real cellWidth, int nbPoints, const void* particleHash,
void* cells, void* cellGhost)
{
//int nbbits = 8;
//while (nbbits < cellBits + 1) nbbits+=8;
//CudaSort(particleHash,particleIndex,nbbits,nbPoints*8);
gpu::cuda::SpatialGridContainer_findCellRange(cellBits, index0, cellWidth, nbPoints, particleHash, cells, cellGhost);
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3f1Types > >::kernel_computeHash(
int cellBits, Real cellWidth, int nbPoints, void* particleIndex, void* particleHash,
const void* x)
{
gpu::cuda::SpatialGridContainer3f1_computeHash(cellBits, cellWidth, nbPoints, particleIndex, particleHash, x);
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3f1Types > >::kernel_updateGrid(
int cellBits, int index0, Real cellWidth, int nbPoints, const void* particleHash,
void* cells, void* cellGhost)
{
gpu::cuda::SpatialGridContainer_findCellRange(cellBits, index0, cellWidth, nbPoints, particleHash, cells, cellGhost);
}
#ifdef SOFA_GPU_CUDA_DOUBLE
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3dTypes > >::kernel_computeHash(
int /*cellBits*/, Real /*cellWidth*/, int /*nbPoints*/, void* /*particleIndex*/, void* /*particleHash*/,
const void* /*x*/)
{
msg_error("SpatialGrid") << "TODO: SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3dTypes > >::kernel_computeHash()";
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3dTypes > >::kernel_updateGrid(
int /*cellBits*/, int /*index0*/, Real /*cellWidth*/, int /*nbPoints*/, const void* /*particleHash*/,
void* /*cells*/, void* /*cellGhost*/)
{
msg_error("SpatialGrid") << "TODO: SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3dTypes > >::kernel_updateGrid()";
}
#endif // SOFA_GPU_CUDA_DOUBLE
/*
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3fTypes > >::kernel_reorderData(int nbPoints, const void* particleHash, void* sorted, const void* x)
{
gpu::cuda::SpatialGridContainer3f_reorderData(nbPoints, particleHash, sorted, x);
}
template<>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVec3f1Types > >::kernel_reorderData(int nbPoints, const void* particleHash, void* sorted, const void* x)
{
gpu::cuda::SpatialGridContainer3f1_reorderData(nbPoints, particleHash, sorted, x);
}
*/
template<class TCoord, class TDeriv, class TReal>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::update(const VecCoord& x)
{
lastX = &x;
data.clear();
int nbPoints = x.size();
int index0 = nbCells+BSIZE;
/*particleIndex*/ cells.recreate(index0+nbPoints*8,8*BSIZE);
particleHash.recreate(nbPoints*8,8*BSIZE);
{
unsigned int numElements = (unsigned int)nbPoints*8;
sofa::gpu::cuda::CudaSortPrepare(numElements);
}
//cells.recreate(nbCells+1);
cellGhost.recreate(nbCells);
//sortedPos.recreate(nbPoints);
#if 0
{
helper::WriteAccessor< sofa::gpu::cuda::CudaVector< int > > pcells = cells;
helper::WriteAccessor< sofa::gpu::cuda::CudaVector< unsigned int > > pparticleHash = particleHash;
helper::ReadAccessor< VecCoord > px = x;
const Real pcellWidth = cellWidth*2.0f;
//const Real pinvCellWidth = 1.0f/pcellWidth;
const int pcellMask = (1<<cellBits)-1;
//const Real phalfCellWidth = pcellWidth*0.5f;
const Real pinvHalfCellWidth = 2.0f/pcellWidth;
for (int i=0; i<nbPoints; ++i)
{
Coord p = px[i];
int hgpos_x,hgpos_y,hgpos_z;
hgpos_x = helper::rfloor(p[0] * pinvHalfCellWidth);
hgpos_y = helper::rfloor(p[1] * pinvHalfCellWidth);
hgpos_z = helper::rfloor(p[2] * pinvHalfCellWidth);
int halfcell = ((hgpos_x&1) + ((hgpos_y&1)<<1) + ((hgpos_z&1)<<2))^7;
// compute the first cell to be influenced by the particle
hgpos_x = (hgpos_x-1) >> 1;
hgpos_y = (hgpos_y-1) >> 1;
hgpos_z = (hgpos_z-1) >> 1;
unsigned int hx = (HASH_PX*hgpos_x);
unsigned int hy = (HASH_PY*hgpos_y);
unsigned int hz = (HASH_PZ*hgpos_z);
for (int x=0; x<8; ++x)
{
unsigned int h_x = hx; if (x&1) h_x += HASH_PX;
unsigned int h_y = hy; if (x&2) h_y += HASH_PY;
unsigned int h_z = hz; if (x&4) h_z += HASH_PZ;
unsigned int hash = ((h_x ^ h_y ^ h_z) & pcellMask)<<1;
if (halfcell != x) ++hash;
pcells[index0 + i*8 + x] = i;
pparticleHash[i*8 + x] = hash;
}
}
}
#endif
#if 0
type::vector< std::pair<unsigned int,int> > cpusort;
cpusort.resize(8*nbPoints);
for (int i=0; i<8*nbPoints; ++i)
cpusort[i] = std::make_pair(pparticleHash[i],pcells[index0+i]);
std::sort(cpusort.begin(),cpusort.end(),compare_pair_first);
for (int i=0; i<8*nbPoints; ++i)
{
pparticleHash[i] = cpusort[i].first;
pcells[index0+i] = cpusort[i].second;
}
#endif
kernel_computeHash(
cellBits, cellWidth*2, nbPoints, cells.deviceWriteAt(index0), particleHash.deviceWrite(),
x.deviceRead());
{
int nbbits = 8;
while (nbbits < cellBits + 1) nbbits+=8;
sofa::gpu::cuda::CudaSort(&particleHash,0, &cells,index0, nbPoints*8, nbbits);
}
kernel_updateGrid(
cellBits, index0, cellWidth*2, nbPoints, particleHash.deviceRead(),
cells.deviceWrite(), cellGhost.deviceWrite());
#if 0
std::cout << nbPoints*8 << " entries in " << nbCells << " cells." << std::endl;
int nfill = 0;
for (int c=0; c<nbCells; ++c)
{
int cellBegin = cells[c];
int cellEnd = cells[c+1]&~(1U<<31);
if (cells[c] <= 0) continue;
if (cellEnd > cellBegin + nbPoints/2) // || nfill >= 100 && nfill < 110)
std::cout << "Cell " << c << ": range = " << cellBegin-index0 << " - " << cellEnd-index0 << " ghost = " << cellGhost[c]-index0 << std::endl;
++nfill;
}
std::cout << ((1000*nfill)/nbCells) * 0.1 << " % cells with particles." << std::endl;
#endif
//kernel_reorderData(nbPoints, particleHash.deviceRead(), sortedPos.deviceWrite(), x.deviceRead());
}
template<class TCoord, class TDeriv, class TReal>
void SpatialGrid< SpatialGridTypes < gpu::cuda::CudaVectorTypes<TCoord,TDeriv,TReal> > >::draw(const core::visual::VisualParams* )
{
#if SOFACUDA_HAVE_SOFA_GL == 1
if (!lastX) return;
int nbPoints = particleHash.size();
int index0 = nbCells+BSIZE;
glDisable(GL_LIGHTING);
glColor4f(1,1,1,1);
glPointSize(3);
glBegin(GL_POINTS);
unsigned int last = 0;
for (int i=0; i<nbPoints; i++)
{
unsigned int cell = particleHash[i];
unsigned int p = cells[index0+i]; //particleIndex[i];
if (cell < last)
{
msg_error("SpatialGrid") << "SORT ERROR: index " << i << " key " << cell << " value " << p << " last key " << last;
}
last = cell;
if (!(cell&1)) continue; // this is a ghost particle from a neighbor cell
cell>>=1;
//if (cell != 0 && cell != 65535)
// std::cout << i << ": "<<p<<" -> "<<cell<<", "<<(*lastX)[p]<<" -> "<<sortedPos[i]<<std::endl;
int r = cell&3;
int g = (cell>>2)&3;
int b = (cell>>4)&3;
glColor4ub(63+r*64,63+g*64,63+b*64,255);
//glVertex3fv(sortedPos[i].ptr());
sofa::gl::glVertexT((*lastX)[p]);
}
glEnd();
glPointSize(1);
#endif // SOFACUDA_HAVE_SOFA_GL == 1
}
} // namespace component::container
} // namespace sofa
#endif