-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneighborhoodSmall_cuda.cu
265 lines (237 loc) · 12.8 KB
/
neighborhoodSmall_cuda.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
#include "neighborhoodSmall.h"
template<typename scalar_t, int32_t dim>
hostDeviceInline auto modDistancePtrCUDA(const scalar_t* __restrict__ x_i, const scalar_t* __restrict__ x_j, const scalar_t* __restrict__ minDomain, const scalar_t* __restrict__ maxDomain, const bool* __restrict__ periodicity){
scalar_t sum(0.0);
for(int32_t i = 0; i < dim; i++){
auto diff = periodicity[i] ? moduloOp(x_i[i], x_j[i], maxDomain[i] - minDomain[i]) : x_i[i] - x_j[i];
sum += diff * diff;
}
return std::sqrt(sum);
}
template<typename scalar_t, int32_t dim>
hostDeviceInline auto modDistancePtrCUDA2(const scalar_t* __restrict__ x_i, const scalar_t* __restrict__ x_j, const scalar_t* __restrict__ minDomain, const scalar_t* __restrict__ maxDomain, const bool* __restrict__ periodicity){
scalar_t sum(0.0);
for(int32_t i = 0; i < dim; i++){
auto diff = periodicity[i] ? moduloOp(x_i[i], x_j[i], maxDomain[i] - minDomain[i]) : x_i[i] - x_j[i];
sum += diff * diff;
}
return sum;
}
using scalar_t = float;
template<int32_t dim>
__global__ void countNeighborsSmallKernel( int32_t* __restrict__ neighborCounterPtr,
const float* __restrict__ queryPositionsPtr, const float* __restrict__ querySupportPtr,
const float* __restrict__ referencePositionsPtr, const float* __restrict__ referenceSupportPtr,
const float* __restrict__ minDomainPtr, const float* __restrict__ maxDomainPtr, const bool* __restrict__ periodicityPtr,
int32_t nQuery, int32_t nReference, supportMode searchMode){
extern __shared__ char array[];
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= nQuery) return;
const float* __restrict__ queryPosition = queryPositionsPtr + idx * dim;
int32_t neighborCounter = 0;
auto requires_hj = searchMode == supportMode::scatter || searchMode == supportMode::symmetric;
auto requires_hi = searchMode == supportMode::gather || searchMode == supportMode::symmetric;
float querySupport = requires_hi ? querySupportPtr[idx] : 0.f;
for (int32_t j = 0; j < nReference; j++){
const float* __restrict__ referencePosition = referencePositionsPtr + j * dim;
float referenceSupport = requires_hj ? referenceSupportPtr[j] : 0.f;
scalar_t dist = modDistancePtrCUDA<scalar_t, dim>(queryPosition, referencePosition, minDomainPtr, maxDomainPtr, periodicityPtr);
// if(dist < h2){
// neighborCounter++;
// }
if ((searchMode == supportMode::scatter && dist < referenceSupport) ||
(searchMode == supportMode::gather && dist < querySupport) ||
(searchMode == supportMode::symmetric && dist < (querySupport + referenceSupport) / 2.f)) {
neighborCounter++;
}
}
neighborCounterPtr[idx] = neighborCounter;
}
void countNeighborsSmallCUDA( int32_t* neighborCounterPtr,
float* queryPositionsPtr, float* querySupportPtr,
float* referencePositionsPtr, float* referenceSupportPtr,
float* minDomainPtr, float* maxDomainPtr, bool* periodicityPtr,
int32_t nQuery, int32_t nReference, int32_t dim, supportMode mode){
int threads = 512;
int blocks = (nQuery + threads - 1) / threads;
int32_t sharedMemorySize = dim * sizeof(bool) + 2 * dim * sizeof(float);
switch(dim){
case 1: countNeighborsSmallKernel<1><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
case 2: countNeighborsSmallKernel<2><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
case 3: countNeighborsSmallKernel<3><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
}
}
template<int32_t dim = 2>
__global__ void neighborSearchSmallCUDAKernel( int32_t* __restrict__ neighborCounterPtr, int64_t* __restrict__ neighborList_iPtr, int64_t* __restrict__ neighborList_jPtr,
float* __restrict__ queryPositionsPtr, float* __restrict__ querySupportPtr,
float* __restrict__ referencePositionsPtr, float* __restrict__ referenceSupportPtr,
float* __restrict__ maxDomainPtr, float* __restrict__ minDomainPtr, bool* __restrict__ periodicityPtr,
int32_t nQuery, int32_t nReference, supportMode searchMode){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= nQuery) return;
const float* __restrict__ queryPosition = queryPositionsPtr + idx * dim;
int32_t neighborCounter = 0;
auto requires_hj = searchMode == supportMode::scatter || searchMode == supportMode::symmetric;
auto requires_hi = searchMode == supportMode::gather || searchMode == supportMode::symmetric;
float querySupport = requires_hi ? querySupportPtr[idx] : 0.f;
int32_t neighborOffset = idx > 0 ? neighborCounterPtr[idx - 1] : 0;
for (int32_t j = 0; j < nReference; j++){
const float* __restrict__ referencePosition = referencePositionsPtr + j * dim;
float referenceSupport = requires_hj ? referenceSupportPtr[j] : 0.f;
scalar_t dist = modDistancePtrCUDA<scalar_t, dim>(queryPosition, referencePosition, minDomainPtr, maxDomainPtr, periodicityPtr);
if ((searchMode == supportMode::scatter && dist < referenceSupport) ||
(searchMode == supportMode::gather && dist < querySupport) ||
(searchMode == supportMode::symmetric && dist < (querySupport + referenceSupport) / 2.f)) {
neighborList_jPtr[neighborOffset + neighborCounter] = j;
neighborList_iPtr[neighborOffset + neighborCounter] = idx;
neighborCounter++;
}
}
}
void neighborSearchSmallCUDA(
int32_t* neighborCounterPtr, int64_t* neighborList_iPtr, int64_t* neighborList_jPtr,
float* queryPositionsPtr, float* querySupportPtr,
float* referencePositionsPtr, float* referenceSupportPtr,
float* minDomainPtr, float* maxDomainPtr, bool* periodicityPtr,
int32_t nQuery, int32_t nReference, int32_t dim, supportMode mode){
int threads = 512;
int blocks = (nQuery + threads - 1) / threads;
int32_t sharedMemorySize = dim * sizeof(bool) + 2 * dim * sizeof(float);
switch(dim){
case 1: neighborSearchSmallCUDAKernel<1><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
case 2: neighborSearchSmallCUDAKernel<2><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
case 3: neighborSearchSmallCUDAKernel<3><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr, querySupportPtr,
referencePositionsPtr, referenceSupportPtr,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference, mode);
break;
}
}
template<int32_t dim>
__global__ void countNeighborsSmallFixedKernel( int32_t* __restrict__ neighborCounterPtr,
const float* __restrict__ queryPositionsPtr,
const float* __restrict__ referencePositionsPtr, const float support,
const float* __restrict__ minDomainPtr, const float* __restrict__ maxDomainPtr, const bool* __restrict__ periodicityPtr,
int32_t nQuery, int32_t nReference){
extern __shared__ char array[];
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= nQuery) return;
const float* __restrict__ queryPosition = queryPositionsPtr + idx * dim;
int32_t neighborCounter = 0;
float h2 = support * support;
for (int32_t j = 0; j < nReference; j++){
const float* __restrict__ referencePosition = referencePositionsPtr + j * dim;
scalar_t dist = modDistancePtrCUDA2<scalar_t, dim>(queryPosition, referencePosition, minDomainPtr, maxDomainPtr, periodicityPtr);
if(dist < h2){
neighborCounter++;
}
}
neighborCounterPtr[idx] = neighborCounter;
}
void countNeighborsSmallFixedCUDA( int32_t* neighborCounterPtr,
float* queryPositionsPtr,
float* referencePositionsPtr, float support,
float* minDomainPtr, float* maxDomainPtr, bool* periodicityPtr,
int32_t nQuery, int32_t nReference, int32_t dim){
int threads = 512;
int blocks = (nQuery + threads - 1) / threads;
int32_t sharedMemorySize = dim * sizeof(bool) + 2 * dim * sizeof(float);
switch(dim){
case 1: countNeighborsSmallFixedKernel<1><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
case 2: countNeighborsSmallFixedKernel<2><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
case 3: countNeighborsSmallFixedKernel<3><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
}
}
template<int32_t dim = 2>
__global__ void neighborSearchSmallFixedCUDAKernel( int32_t* __restrict__ neighborCounterPtr, int64_t* __restrict__ neighborList_iPtr, int64_t* __restrict__ neighborList_jPtr,
float* __restrict__ queryPositionsPtr,
float* __restrict__ referencePositionsPtr, float support,
float* __restrict__ maxDomainPtr, float* __restrict__ minDomainPtr, bool* __restrict__ periodicityPtr,
int32_t nQuery, int32_t nReference){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= nQuery) return;
const float* __restrict__ queryPosition = queryPositionsPtr + idx * dim;
int32_t neighborCounter = 0;
float h2 = support * support;
int32_t neighborOffset = idx > 0 ? neighborCounterPtr[idx - 1] : 0;
for (int32_t j = 0; j < nReference; j++){
const float* __restrict__ referencePosition = referencePositionsPtr + j * dim;
scalar_t dist = modDistancePtrCUDA2<scalar_t, dim>(queryPosition, referencePosition, minDomainPtr, maxDomainPtr, periodicityPtr);
if(dist < h2){
neighborList_jPtr[neighborOffset + neighborCounter] = j;
neighborList_iPtr[neighborOffset + neighborCounter] = idx;
neighborCounter++;
}
}
}
void neighborSearchSmallFixedCUDA(
int32_t* neighborCounterPtr, int64_t* neighborList_iPtr, int64_t* neighborList_jPtr,
float* queryPositionsPtr,
float* referencePositionsPtr, float support,
float* minDomainPtr, float* maxDomainPtr, bool* periodicityPtr,
int32_t nQuery, int32_t nReference, int32_t dim){
int threads = 512;
int blocks = (nQuery + threads - 1) / threads;
int32_t sharedMemorySize = dim * sizeof(bool) + 2 * dim * sizeof(float);
switch(dim){
case 1: neighborSearchSmallFixedCUDAKernel<1><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
case 2: neighborSearchSmallFixedCUDAKernel<2><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
case 3: neighborSearchSmallFixedCUDAKernel<3><<<blocks, threads, sharedMemorySize>>>(neighborCounterPtr, neighborList_iPtr, neighborList_jPtr,
queryPositionsPtr,
referencePositionsPtr, support,
minDomainPtr, maxDomainPtr, periodicityPtr,
nQuery, nReference);
break;
}
}