-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfind_neighbors.cu
372 lines (320 loc) · 15 KB
/
find_neighbors.cu
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/////////////////////////////////////////////////////////////////////////////
/// \file find_neighbors.cu
///
/// \brief Cuda implementation of the operation to find the neighboring
/// points within a certain radius.
///
/// \copyright Copyright (c) 2018 Visual Computing group of Ulm University,
/// Germany. See the LICENSE file at the top-level directory of
/// this distribution.
///
/// \author pedro hermosilla ([email protected])
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <iostream>
#include <fstream>
#include "cuda_kernel_utils.h"
#define POINT_BLOCK_SIZE 128
#define POINT_BLOCK_PACK_SIZE 256
////////////////////////////////////////////////////////////////////////////////// GPU
__constant__ int cellOffsets[27][3];
/**
* Method to count the neighboring points for each point.
* @param pNumPoints Number of points.
* @param pNumCells Number of cells of the grid.
* @param pAABBMinPoint Minimum point of the grid (3 componenets).
* @param pAABBMaxPoint Maximum point of the grid (3 componenets).
* @param pPoints List of points.
* @param pBatchIds List of batch ids.
* @param pPoints2 List of points from where to find neighbors.
* @param pCellIndexs Indexs of the grid cells.
* @param pOutNeigbors Output parameter with the number of neighbors of each point.
* @param pOutNumNeigbors Output parameter with the total number of neighbors.
*/
__global__ void countNeighbors(
const bool pScaleInv,
const int pNumPoints,
const int pNumCells,
const float pRadius,
const float* __restrict__ pAABBMinPoint,
const float* __restrict__ pAABBMaxPoint,
const float* __restrict__ pPoints,
const int* __restrict__ pBatchIds,
const float* __restrict__ pPoints2,
const int* __restrict__ pCellIndexs,
int* __restrict__ pOutNeigbors,
int* __restrict__ pOutNumNeigbors)
{
__shared__ int blockTotalNeighbors;
if(threadIdx.x == 0){
blockTotalNeighbors = 0;
}
__syncthreads();
int currentIndex = threadIdx.x + blockIdx.x * blockDim.x;
if(currentIndex < pNumPoints){
int currBatchId = pBatchIds[currentIndex];
int pointIndex = currentIndex * 3;
float maxAabbSize = max(max(
pAABBMaxPoint[currBatchId*3] - pAABBMinPoint[currBatchId*3],
pAABBMaxPoint[currBatchId*3+1] - pAABBMinPoint[currBatchId*3+1]),
pAABBMaxPoint[currBatchId*3+2] - pAABBMinPoint[currBatchId*3+2]);
float cellSize = maxAabbSize/(float)pNumCells;
float scaledRadius = (pScaleInv)?pRadius*maxAabbSize:pRadius;
float centralCoords[3] = {pPoints[pointIndex], pPoints[pointIndex+1], pPoints[pointIndex+2]};
int xCell = max(min((int)floor((centralCoords[0] - pAABBMinPoint[currBatchId*3])/cellSize), pNumCells -1), 0);
int yCell = max(min((int)floor((centralCoords[1] - pAABBMinPoint[currBatchId*3+1])/cellSize), pNumCells -1), 0);
int zCell = max(min((int)floor((centralCoords[2] - pAABBMinPoint[currBatchId*3+2])/cellSize), pNumCells -1), 0);
int neighborIter = 0;
for(int i = 0; i < 27; ++i)
{
int currCellIndex[3] = {xCell+cellOffsets[i][0], yCell+cellOffsets[i][1], zCell+cellOffsets[i][2]};
if(currCellIndex[0] >= 0 && currCellIndex[0] < pNumCells &&
currCellIndex[1] >= 0 && currCellIndex[1] < pNumCells &&
currCellIndex[2] >= 0 && currCellIndex[2] < pNumCells)
{
int cellIndexFlat = currBatchId*pNumCells*pNumCells*pNumCells + currCellIndex[0]*pNumCells*pNumCells + currCellIndex[1]*pNumCells + currCellIndex[2];
int initIndex = pCellIndexs[cellIndexFlat*2];
int endIndex = pCellIndexs[cellIndexFlat*2 + 1];
for(int j = initIndex; j < endIndex; ++j)
{
int currPointIndex = j * 3;
float currentCoords[3] = {pPoints2[currPointIndex], pPoints2[currPointIndex+1], pPoints2[currPointIndex+2]};
float diffVector[3] = {currentCoords[0] - centralCoords[0], currentCoords[1] - centralCoords[1], currentCoords[2] - centralCoords[2]};
float pointDist = sqrt(diffVector[0]*diffVector[0] + diffVector[1]*diffVector[1] + diffVector[2]*diffVector[2]);
if(pointDist < scaledRadius){
neighborIter++;
}
}
}
}
pOutNeigbors[currentIndex] = neighborIter;
atomicAdd(&blockTotalNeighbors, neighborIter);
}
__syncthreads();
if(threadIdx.x == 0){
atomicAdd(&pOutNumNeigbors[0], blockTotalNeighbors);
}
}
/**
* Method to compute the offsets in the neighboring list.
* @param pNumOffsets Number of offsets.
* @param pNumOffsets Number of offsets 2.
* @param pOutNeighborsOffsets List with the offsets of each block.
* @param pOutNeighborsOffsets2 List with the offsets of each block of blocks.
*/
__global__ void computeOffsets(
const bool pStep1,
const int pNumOffsets,
const int pNumOffsets2,
int* __restrict__ pOutNeighborsOffsets,
int* __restrict__ pOutNeighborsOffsets2)
{
__shared__ int groupOffsets[POINT_BLOCK_PACK_SIZE];
//Get the local and global counter.
int currCounter = threadIdx.x;
int currGlobalCounter = threadIdx.x + blockIdx.x * blockDim.x;
//Update the shared memory.
if(currGlobalCounter < pNumOffsets)
groupOffsets[currCounter] = pOutNeighborsOffsets[currGlobalCounter];
else
groupOffsets[currCounter] = 0;
//SIMD scan.
for(int i = 1; i <= POINT_BLOCK_PACK_SIZE/2; i*=2)
{
__syncthreads();
//Get the values of the pass.
int currIndex = currCounter + i;
int value1 = 0;
int value2 = 0;
if(currIndex < POINT_BLOCK_PACK_SIZE){
value1 = groupOffsets[currCounter];
value2 = groupOffsets[currIndex];
}
__syncthreads();
//Update with the new value.
if(currIndex < POINT_BLOCK_PACK_SIZE)
groupOffsets[currIndex] = value1 + value2;
}
__syncthreads();
//Save the counter into global memory.
if(currGlobalCounter < pNumOffsets){
if(currCounter > 0)
pOutNeighborsOffsets[currGlobalCounter] = groupOffsets[currCounter-1];
else
pOutNeighborsOffsets[currGlobalCounter] = 0;
}
if(pStep1){
//Update the offsets buffer.
if(currCounter == (POINT_BLOCK_PACK_SIZE-1) && blockIdx.x < pNumOffsets2)
pOutNeighborsOffsets2[blockIdx.x] = groupOffsets[POINT_BLOCK_PACK_SIZE-1];
}else{
//Update the second level offset buffer.
if(currCounter > blockIdx.x && currCounter < pNumOffsets2){
atomicAdd(&pOutNeighborsOffsets2[currCounter], groupOffsets[POINT_BLOCK_PACK_SIZE-1]);
}
}
}
/**
* Method to find the neighboring points for each point.
* @param pNumPoints Number of points.
* @param pNumCells Number of cells of the grid.
* @param pAABBMinPoint Minimum point of the grid (3 componenets).
* @param pAABBMaxPoint Maximum point of the grid (3 componenets).
* @param pPoints List of points.
* @param pBatchIds List of batch ids.
* @param pPoints2 List of points from where to find neighbors.
* @param pCellIndexs Indexs of the grid cells.
* @param pStartIndexsOffset List with the first level offset to teh start indices.
* @param pStartIndexsOffset2 List with the second level offset to teh start indices.
* @param pStartIndexs Input/Output parameter with the list of the starting indices in the neighboring list.
* @param pOutNeigbors Output parameter with the list neighbors of each point.
*/
__global__ void findNeighbors(
const bool pScaleInv,
const int pNumPoints,
const int pNumCells,
const int pNumNeighbors,
const float pRadius,
const float* __restrict__ pAABBMinPoint,
const float* __restrict__ pAABBMaxPoint,
const float* __restrict__ pPoints,
const int* __restrict__ pBatchIds,
const float* __restrict__ pPoints2,
const int* __restrict__ pCellIndexs,
const int* __restrict__ pStartIndexsOffset,
const int* __restrict__ pStartIndexsOffset2,
int* __restrict__ pStartIndexs,
int* __restrict__ pOutNeigbors)
{
int currentIndex = threadIdx.x + blockIdx.x * blockDim.x;
if(currentIndex < pNumPoints){
int currBatchId = pBatchIds[currentIndex];
int pointIndex = currentIndex * 3;
int offsetIndex = currentIndex/POINT_BLOCK_PACK_SIZE;
int globalOffsetIndex = offsetIndex/POINT_BLOCK_PACK_SIZE;
int neighborIndex = pStartIndexs[currentIndex]+pStartIndexsOffset[offsetIndex]+pStartIndexsOffset2[globalOffsetIndex];
pStartIndexs[currentIndex] = neighborIndex;
float maxAabbSize = max(max(
pAABBMaxPoint[currBatchId*3] - pAABBMinPoint[currBatchId*3],
pAABBMaxPoint[currBatchId*3+1] - pAABBMinPoint[currBatchId*3+1]),
pAABBMaxPoint[currBatchId*3+2] - pAABBMinPoint[currBatchId*3+2]);
float cellSize = maxAabbSize/(float)pNumCells;
float scaledRadius = (pScaleInv)?pRadius*maxAabbSize:pRadius;
float centralCoords[3] = {pPoints[pointIndex], pPoints[pointIndex+1], pPoints[pointIndex+2]};
int xCell = max(min((int)floor((centralCoords[0] - pAABBMinPoint[currBatchId*3])/cellSize), pNumCells -1), 0);
int yCell = max(min((int)floor((centralCoords[1] - pAABBMinPoint[currBatchId*3+1])/cellSize), pNumCells -1), 0);
int zCell = max(min((int)floor((centralCoords[2] - pAABBMinPoint[currBatchId*3+2])/cellSize), pNumCells -1), 0);
int neighborIter = 0;
for(int i = 0; i < 27; ++i)
{
int currCellIndex[3] = {xCell+cellOffsets[i][0], yCell+cellOffsets[i][1], zCell+cellOffsets[i][2]};
if(currCellIndex[0] >= 0 && currCellIndex[0] < pNumCells &&
currCellIndex[1] >= 0 && currCellIndex[1] < pNumCells &&
currCellIndex[2] >= 0 && currCellIndex[2] < pNumCells)
{
int cellIndexFlat = currBatchId*pNumCells*pNumCells*pNumCells + currCellIndex[0]*pNumCells*pNumCells + currCellIndex[1]*pNumCells + currCellIndex[2];
int initIndex = pCellIndexs[cellIndexFlat*2];
int endIndex = pCellIndexs[cellIndexFlat*2 + 1];
for(int j = initIndex; j < endIndex; ++j)
{
int currPointIndex = j * 3;
float currentCoords[3] = {pPoints2[currPointIndex], pPoints2[currPointIndex+1], pPoints2[currPointIndex+2]};
float diffVector[3] = {currentCoords[0] - centralCoords[0], currentCoords[1] - centralCoords[1], currentCoords[2] - centralCoords[2]};
float pointDist = sqrt(diffVector[0]*diffVector[0] + diffVector[1]*diffVector[1] + diffVector[2]*diffVector[2]);
if(pointDist < scaledRadius){
pOutNeigbors[neighborIndex*2 + neighborIter] = j;
pOutNeigbors[neighborIndex*2 + neighborIter + 1] = currentIndex;
neighborIter+=2;
}
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////// CPU
unsigned int countNeighborsCPU(
const bool pScaleInv,
const int pNumPoints,
const int pNumCells,
const float pRadius,
const float* pInPts,
const int* pInBatchIds,
const float* pInPts2,
const int* pCellIndexs,
const float* pAABBMin,
const float* pAABBMax,
int* pStartIndex)
{
//Init device symbols.
int cellOffsetsCPU[27][3] = {
{1, 1, 1},{0, 1, 1},{-1, 1, 1},
{1, 0, 1},{0, 0, 1},{-1, 0, 1},
{1, -1, 1},{0, -1, 1},{-1, -1, 1},
{1, 1, 0},{0, 1, 0},{-1, 1, 0},
{1, 0, 0},{0, 0, 0},{-1, 0, 0},
{1, -1, 0},{0, -1, 0},{-1, -1, 0},
{1, 1, -1},{0, 1, -1},{-1, 1, -1},
{1, 0, -1},{0, 0, -1},{-1, 0, -1},
{1, -1, -1},{0, -1, -1},{-1, -1, -1}};
cudaMemcpyToSymbol(cellOffsets, cellOffsetsCPU, 27*3*sizeof(int));
int numBlocksPoints = pNumPoints/POINT_BLOCK_SIZE;
numBlocksPoints += (pNumPoints%POINT_BLOCK_SIZE != 0)?1:0;
//Find the neighbors for each point.
int* totalNeighbors;
gpuErrchk(cudaMalloc(&totalNeighbors, sizeof(int)));
cudaMemset(totalNeighbors, 0, sizeof(int));
countNeighbors<<<numBlocksPoints, POINT_BLOCK_SIZE>>>(pScaleInv, pNumPoints, pNumCells,
pRadius, pAABBMin, pAABBMax, pInPts, pInBatchIds, pInPts2, pCellIndexs, pStartIndex, totalNeighbors);
gpuErrchk(cudaPeekAtLastError());
int totalNeighborsCPU = 0;
cudaMemcpy(&totalNeighborsCPU, totalNeighbors, sizeof(int), cudaMemcpyDeviceToHost);
gpuErrchk(cudaFree(totalNeighbors));
#ifdef PRINT_CONV_INFO
printf("Forward Num points: %d | Neighbors: %d\n", pNumPoints, totalNeighborsCPU);
#endif
return totalNeighborsCPU;
}
void computeAuxiliarBuffersSize(
const int pNumPoints,
int* PBufferSize1,
int* PBufferSize2)
{
(*PBufferSize1) = pNumPoints/POINT_BLOCK_PACK_SIZE;
(*PBufferSize1) += (pNumPoints%POINT_BLOCK_PACK_SIZE != 0)?1:0;
(*PBufferSize2) = (*PBufferSize1)/POINT_BLOCK_PACK_SIZE;
(*PBufferSize2) += ((*PBufferSize1)%POINT_BLOCK_PACK_SIZE != 0)?1:0;
}
void packNeighborsCPU(
const bool pScaleInv,
const int pNumPoints,
const int pNumNeighbors,
const int pNumCells,
const float pRadius,
const float* pInPts,
const int* pInBatchIds,
const float* pInPts2,
const int* pCellIndexs,
const float* pAABBMin,
const float* pAABBMax,
int* pAuxBuffOffsets,
int* pAuxBuffOffsets2,
int* pStartIndexs,
int* pPackedIndexs)
{
//Pack the indexs of the neighbors.
int numBlocksPointsPack = pNumPoints/POINT_BLOCK_PACK_SIZE;
numBlocksPointsPack += (pNumPoints%POINT_BLOCK_PACK_SIZE != 0)?1:0;
int numBlocksPointsPack2 = numBlocksPointsPack/POINT_BLOCK_PACK_SIZE;
numBlocksPointsPack2 += (numBlocksPointsPack%POINT_BLOCK_PACK_SIZE != 0)?1:0;
gpuErrchk(cudaMemset(pAuxBuffOffsets, 0, sizeof(int)*numBlocksPointsPack));
gpuErrchk(cudaMemset(pAuxBuffOffsets2, 0, sizeof(int)*numBlocksPointsPack2));
computeOffsets<<<numBlocksPointsPack, POINT_BLOCK_PACK_SIZE>>>(true, pNumPoints, numBlocksPointsPack, pStartIndexs, pAuxBuffOffsets);
gpuErrchk(cudaPeekAtLastError());
computeOffsets<<<numBlocksPointsPack2, POINT_BLOCK_PACK_SIZE>>>(false, numBlocksPointsPack, numBlocksPointsPack2, pAuxBuffOffsets, pAuxBuffOffsets2);
gpuErrchk(cudaPeekAtLastError());
int numBlocksPoints = pNumPoints/POINT_BLOCK_SIZE;
numBlocksPoints += (pNumPoints%POINT_BLOCK_SIZE != 0)?1:0;
findNeighbors<<<numBlocksPoints,POINT_BLOCK_SIZE>>>(pScaleInv, pNumPoints, pNumCells, pNumNeighbors, pRadius, pAABBMin, pAABBMax,
pInPts, pInBatchIds, pInPts2, pCellIndexs, pAuxBuffOffsets, pAuxBuffOffsets2, pStartIndexs, pPackedIndexs);
gpuErrchk(cudaPeekAtLastError());
}