-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneighborhood.cpp
441 lines (402 loc) · 21.9 KB
/
neighborhood.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "neighborhood.h"
/**
* @brief Returns a packed accessor for a given tensor.
*
* This function builds a C++ accessor for a given tensor, based on the specified scalar type and dimension.
*
* @tparam scalar_t The scalar type of the tensor.
* @tparam dim The dimension of the tensor.
* @param t The input tensor.
* @param name The name of the accessor.
* @param cuda Flag indicating whether the tensor should be on CUDA.
* @param verbose Flag indicating whether to print verbose information.
* @param optional Flag indicating whether the tensor is optional.
* @return The packed accessor for the tensor.
* @throws std::runtime_error If the tensor is not defined (and not optional), not contiguous, not on CUDA (if cuda=true), or has an incorrect dimension.
*/
template <typename scalar_t, std::size_t dim>
auto getAccessor(const torch::Tensor &t, const std::string &name, bool cuda = false, bool verbose = false, bool optional = false) {
if (verbose) {
std::cout << "Building C++ accessor: " << name << " for " << typeid(scalar_t).name() << " x " << dim << std::endl;
}
if (!optional && !t.defined()) {
throw std::runtime_error(name + " is not defined");
}
if (optional && !t.defined()) {
return t.template packed_accessor32<scalar_t, dim>();
}
if (!t.is_contiguous()) {
throw std::runtime_error(name + " is not contiguous");
}
if (cuda && (t.device().type() != c10::kCUDA)) {
throw std::runtime_error(name + " is not on CUDA");
}
if (t.dim() != dim) {
throw std::runtime_error(name + " is not of the correct dimension " + std::to_string(t.dim()) + " vs " + std::to_string(dim));
}
return t.template packed_accessor32<scalar_t, dim>();
}
/**
*
* @brief C++ based neighborhood counting for all particles in the query set relative to the sorted set.
*
* This function counts the number of neighbors for all particles in the query set relative to the sorted set.
*
* @param queryPositions_ The positions of the query particles.
* @param querySupport_ The support radii of the query particles.
* @param searchRange The search range.
* @param sortedPositions_ The sorted positions of the particles.
* @param sortedSupport_ The sorted support radii of the particles.
* @param hashTable_ The hash table.
* @param hashMapLength The length of the hash map.
* @param numCells_ The number of cells.
* @param cellTable_ The cell table.
* @param qMin_ The minimum domain bounds.
* @param hCell The cell size.
* @param maxDomain_ The maximum domain bounds.
* @param minDomain_ The minimum domain bounds.
* @param periodicity_ The periodicity flags.
* @param mode The support mode.
* @param verbose Flag indicating whether to print verbose information.
* @return The number of neighbors for each particle in the query set.
*/
torch::Tensor countNeighbors(
torch::Tensor queryPositions_, torch::Tensor querySupport_, int searchRange,
torch::Tensor sortedPositions_, torch::Tensor sortedSupport_,
torch::Tensor hashTable_, int hashMapLength,
torch::Tensor numCells_, torch::Tensor cellTable_,
torch::Tensor qMin_, float hCell, torch::Tensor maxDomain_, torch::Tensor minDomain_, torch::Tensor periodicity_,
std::string mode, bool verbose = false){
if(verbose)
std::cout << "C++: countNeighbors" << std::endl;
// Convert the mode to an enum for easier handling
supportMode searchMode = supportMode::symmetric;
if(mode == "symmetric"){
searchMode = supportMode::symmetric;
} else if(mode == "gather"){
searchMode = supportMode::gather;
} else if(mode == "scatter"){
searchMode = supportMode::scatter;
} else {
throw std::runtime_error("Invalid support mode: " + mode);
}
bool useCuda = queryPositions_.is_cuda();
// Check if the input tensors are defined and contiguous and have the correct dimensions
auto queryPositions = getAccessor<float, 2>(queryPositions_, "queryPositions", useCuda, verbose);
auto querySupport = getAccessor<float, 1>(querySupport_, "querySupport", useCuda, verbose, supportMode::scatter == searchMode);
auto sortedPositions = getAccessor<float, 2>(sortedPositions_, "sortedPositions", useCuda, verbose);
auto sortedSupport = getAccessor<float, 1>(sortedSupport_, "sortedSupport", useCuda, verbose, supportMode::gather == searchMode);
// Get the dimensions of the input tensors
int nQuery = queryPositions.size(0);
int dim = queryPositions.size(1);
int nSorted = sortedPositions.size(0);
// Check if the datastructure tensors are defined and contiguous and have the correct dimensions
auto hashTable = getAccessor<int, 2>(hashTable_, "hashTable", useCuda, verbose);
auto numCells = getAccessor<int, 1>(numCells_, "numCells", useCuda, verbose);
auto cellTable = getAccessor<int64_t, 2>(cellTable_, "cellTable", useCuda, verbose);
auto qMin = getAccessor<float, 1>(qMin_, "qMin", useCuda, verbose);
auto maxDomain = getAccessor<float, 1>(maxDomain_, "maxDomain", useCuda, verbose);
auto minDomain = getAccessor<float, 1>(minDomain_, "minDomain", useCuda, verbose);
auto periodicBoolHost = periodicity_.to(at::kCPU).to(at::kBool);
auto periodicityBool = getAccessor<bool, 1>(periodicBoolHost, "periodicity", false, verbose);
auto periodicTensor = torch::zeros({dim}, torch::TensorOptions().dtype(torch::kInt32));
for (int32_t d = 0; d < dim; d++)
periodicTensor[d] = periodicityBool[d] ? 1 : 0;
periodicTensor = periodicTensor.to(queryPositions_.device());
auto periodicity = periodicTensor.packed_accessor32<int32_t,1, traits>();
// Output input state to console for debugging, enable via verbose flag
if (verbose) {
std::cout << "Search Parameters:" << std::endl;
std::cout << "\tnQuery: " << nQuery << std::endl;
std::cout << "\tdim: " << dim << std::endl;
std::cout << "\tnSorted: " << nSorted << std::endl;
std::cout << "\tsearchRange: " << searchRange << std::endl;
std::cout << "\thashMapLength: " << hashMapLength << std::endl;
std::cout << "\thCell: " << hCell << std::endl;
std::cout << "\tMode: " << mode << std::endl;
std::cout << "\nInput Tensors:" << std::endl;
std::cout << "\tqueryPositions: " << queryPositions.size(0) << "x" << queryPositions.size(1) << std::endl;
std::cout << "\tquerySupport: " << querySupport.size(0) << std::endl;
std::cout << "\tsortedPositions: " << sortedPositions.size(0) << "x" << sortedPositions.size(1) << std::endl;
std::cout << "\tsortedSupport: " << sortedSupport.size(0) << std::endl;
std::cout << "\nDatastructure Tensors:" << std::endl;
std::cout << "\thashTable: " << hashTable.size(0) << "x" << hashTable.size(1) << std::endl;
std::cout << "\tnumCells: " << numCells.size(0) << std::endl;
std::cout << "\tcellTable: " << cellTable.size(0) << "x" << cellTable.size(1) << std::endl;
std::cout << "\nDomain Tensors:" << std::endl;
std::cout << "\tqMin: " << qMin.size(0) << std::endl;
std::cout << "\tmaxDomain: " << maxDomain.size(0) << std::endl;
std::cout << "\tminDomain: " << minDomain.size(0) << std::endl;
std::cout << "\tperiodicity: " << periodicity.size(0) << std::endl;
std::cout << "\n";
}
// Create the default options for created tensors
auto defaultOptions = at::TensorOptions().device(queryPositions_.device());
auto hostOptions = at::TensorOptions();
// Create the cell offsets on CPU and move them to the device afterwards to avoid overhead
auto offsets = torch::zeros({power(1 + 2 * searchRange, dim), dim}, hostOptions.dtype(torch::kInt32));
for (int32_t d = 0; d < dim; d++){
int32_t itr = -searchRange;
int32_t ctr = 0;
for(int32_t o = 0; o < offsets.size(0); ++o){
int32_t c = o % power(1 + 2 * searchRange, d);
if(c == 0 && ctr > 0)
itr++;
if(itr > searchRange)
itr = -searchRange;
offsets[o][dim - d - 1] = itr;
ctr++;
}
}
offsets = offsets.to(queryPositions_.device());
// Output the cell offsets to the console for debugging, enable via verbose flag
if(verbose){
std::cout << "Cell Offsets:" << std::endl;
for (int32_t i = 0; i < offsets.size(0); i++){
std::cout << "\t[" << i << "]: ";
for (int32_t d = 0; d < dim; d++){
std::cout << offsets[i][d].item<int32_t>() << " ";
}
std::cout << std::endl;
}
}
// Allocate output tensor for the neighbor counters
auto neighborCounters = torch::zeros({nQuery}, defaultOptions.dtype(torch::kInt32));
// Create the accessors for the input tensors as packed accessors
auto queryPositionAccessor = queryPositions_.packed_accessor32<float, 2, traits>();
auto querySupportAccessor = querySupport_.packed_accessor32<float, 1, traits>();
auto referencePositionAccessor = sortedPositions_.packed_accessor32<float, 2, traits>();
auto referenceSupportAccessor = sortedSupport_.packed_accessor32<float, 1, traits>();
auto hashTableAccessor = hashTable_.packed_accessor32<int32_t, 2, traits>();
auto celTableAccessor = cellTable_.packed_accessor32<int64_t, 2, traits>();
auto offsetAccessor = offsets.packed_accessor32<int32_t, 2, traits>();
auto numCellsAccessor = numCells_.packed_accessor32<int32_t, 1, traits>();
auto neighborCounterAccessor = neighborCounters.packed_accessor32<int32_t, 1, traits>();
// Loop over all query particles and count the number of neighbors per particle
if(queryPositions_.is_cuda()){
#ifndef CUDA_VERSION
throw std::runtime_error("CUDA support is not available in this build");
#else
countNeighborsForParticleCuda(neighborCounters,
queryPositions_, querySupport_, searchRange,
sortedPositions_, sortedSupport_,
hashTable_, hashMapLength,
cellTable_, numCells_,
offsets,
hCell, minDomain_, maxDomain_, periodicTensor, searchMode);
#endif
}else{
at::parallel_for(0, nQuery, 0, [&](int32_t start, int32_t end){
for(int32_t i = start; i < end; ++i){
#define args i, neighborCounterAccessor,\
queryPositionAccessor, querySupportAccessor, searchRange, \
referencePositionAccessor, referenceSupportAccessor,\
hashTableAccessor, hashMapLength, \
celTableAccessor, numCellsAccessor,\
offsetAccessor,\
hCell, minDomain, maxDomain, periodicity, searchMode
if(dim == 1)
countNeighborsForParticle<1>(args);
else if(dim == 2)
countNeighborsForParticle<2>(args);
else if(dim == 3)
countNeighborsForParticle<3>(args);
else
throw std::runtime_error("Unsupported dimension: " + std::to_string(dim));
#undef args
}
});
}
// Return the neighbor counters
return neighborCounters;
}
/**
*
* @brief C++ based neighborhood search for all particles in the query set relative to the sorted set.
*
* This function searches the neighbors for all particles in the query set relative to the sorted set.
*
* @param queryPositions_ The positions of the query particles.
* @param querySupport_ The support radii of the query particles.
* @param searchRange The search range.
* @param sortedPositions_ The sorted positions of the particles.
* @param sortedSupport_ The sorted support radii of the particles.
* @param hashTable_ The hash table.
* @param hashMapLength The length of the hash map.
* @param numCells_ The number of cells.
* @param cellTable_ The cell table.
* @param qMin_ The minimum domain bounds.
* @param hCell The cell size.
* @param maxDomain_ The maximum domain bounds.
* @param minDomain_ The minimum domain bounds.
* @param periodicity_ The periodicity flags.
* @param mode The support mode.
* @param verbose Flag indicating whether to print verbose information.
* @return The neighbor list as a pair of tensors
*/
std::pair<torch::Tensor, torch::Tensor> buildNeighborList(
torch::Tensor neighborCounter_, torch::Tensor neighborOffsets_, int neighborListLength,
torch::Tensor queryPositions_, torch::Tensor querySupport_, int searchRange,
torch::Tensor sortedPositions_, torch::Tensor sortedSupport_,
torch::Tensor hashTable_, int hashMapLength,
torch::Tensor numCells_, torch::Tensor cellTable_,
torch::Tensor qMin_, float hCell, torch::Tensor maxDomain_, torch::Tensor minDomain_, torch::Tensor periodicity_,
std::string mode, bool verbose = false){
if(verbose)
std::cout << "C++: countNeighbors" << std::endl;
// Convert the mode to an enum for easier handling
supportMode searchMode = supportMode::symmetric;
if(mode == "symmetric"){
searchMode = supportMode::symmetric;
} else if(mode == "gather"){
searchMode = supportMode::gather;
} else if(mode == "scatter"){
searchMode = supportMode::scatter;
} else {
throw std::runtime_error("Invalid support mode: " + mode);
}
bool useCuda = queryPositions_.is_cuda();
// Check if the input tensors are defined and contiguous and have the correct dimensions
auto queryPositions = getAccessor<float, 2>(queryPositions_, "queryPositions", useCuda, verbose);
auto querySupport = getAccessor<float, 1>(querySupport_, "querySupport", useCuda, verbose, supportMode::scatter == searchMode);
auto sortedPositions = getAccessor<float, 2>(sortedPositions_, "sortedPositions", useCuda, verbose);
auto sortedSupport = getAccessor<float, 1>(sortedSupport_, "sortedSupport", useCuda, verbose, supportMode::gather == searchMode);
// Check if the datastructure tensors are defined and contiguous and have the correct dimensions
auto hashTable = getAccessor<int, 2>(hashTable_, "hashTable", useCuda, verbose);
auto numCells = getAccessor<int, 1>(numCells_, "numCells", useCuda, verbose);
auto cellTable = getAccessor<int64_t, 2>(cellTable_, "cellTable", useCuda, verbose);
auto qMin = getAccessor<float, 1>(qMin_, "qMin", useCuda, verbose);
auto maxDomain = getAccessor<float, 1>(maxDomain_, "maxDomain", useCuda, verbose);
auto minDomain = getAccessor<float, 1>(minDomain_, "minDomain", useCuda, verbose);
// Check if the neighbor counter tensor is defined and contiguous
auto neighborCounter = getAccessor<int, 1>(neighborCounter_, "neighborCounter", useCuda, verbose);
auto neighborOffsets = getAccessor<int, 1>(neighborOffsets_, "neighborOffsets", useCuda, verbose);
// Get the dimensions of the input tensors
int nQuery = queryPositions.size(0);
int dim = queryPositions.size(1);
int nSorted = sortedPositions.size(0);
auto periodicBoolHost = periodicity_.to(at::kCPU).to(at::kBool);
auto periodicityBool = getAccessor<bool, 1>(periodicBoolHost, "periodicity", false, verbose);
auto periodicTensor = torch::zeros({dim}, torch::TensorOptions().dtype(torch::kInt32));
for (int32_t d = 0; d < dim; d++)
periodicTensor[d] = periodicityBool[d] ? 1 : 0;
periodicTensor = periodicTensor.to(queryPositions_.device());
auto periodicity = periodicTensor.packed_accessor32<int32_t,1, traits>();
// Output input state to console for debugging, enable via verbose flag
if (verbose) {
std::cout << "Search Parameters:" << std::endl;
std::cout << "\tnQuery: " << nQuery << std::endl;
std::cout << "\tdim: " << dim << std::endl;
std::cout << "\tnSorted: " << nSorted << std::endl;
std::cout << "\tsearchRange: " << searchRange << std::endl;
std::cout << "\thashMapLength: " << hashMapLength << std::endl;
std::cout << "\thCell: " << hCell << std::endl;
std::cout << "\tMode: " << mode << std::endl;
std::cout << "\tneighborListLength: " << neighborListLength << std::endl;
std::cout << "\nInput Tensors:" << std::endl;
std::cout << "\tqueryPositions: " << queryPositions.size(0) << "x" << queryPositions.size(1) << std::endl;
std::cout << "\tquerySupport: " << querySupport.size(0) << std::endl;
std::cout << "\tsortedPositions: " << sortedPositions.size(0) << "x" << sortedPositions.size(1) << std::endl;
std::cout << "\tsortedSupport: " << sortedSupport.size(0) << std::endl;
std::cout << "\nDatastructure Tensors:" << std::endl;
std::cout << "\thashTable: " << hashTable.size(0) << "x" << hashTable.size(1) << std::endl;
std::cout << "\tnumCells: " << numCells.size(0) << std::endl;
std::cout << "\tcellTable: " << cellTable.size(0) << "x" << cellTable.size(1) << std::endl;
std::cout << "\nDomain Tensors:" << std::endl;
std::cout << "\tqMin: " << qMin.size(0) << std::endl;
std::cout << "\tmaxDomain: " << maxDomain.size(0) << std::endl;
std::cout << "\tminDomain: " << minDomain.size(0) << std::endl;
std::cout << "\tperiodicity: " << periodicityBool.size(0) << std::endl;
std::cout << "\nOffsets Tensors:" << std::endl;
std::cout << "\tneighborCounter: " << neighborCounter.size(0) << std::endl;
std::cout << "\tneighborOffsets: " << neighborOffsets.size(0) << std::endl;
std::cout << "\n";
}
// Create the default options for created tensors
auto defaultOptions = at::TensorOptions().device(queryPositions_.device());
auto hostOptions = at::TensorOptions();
// Create the cell offsets on CPU and move them to the device afterwards to avoid overhead
auto offsets = torch::zeros({power(1 + 2 * searchRange, dim), dim}, hostOptions.dtype(torch::kInt32));
for (int32_t d = 0; d < dim; d++){
int32_t itr = -searchRange;
int32_t ctr = 0;
for(int32_t o = 0; o < offsets.size(0); ++o){
int32_t c = o % power(1 + 2 * searchRange, d);
if(c == 0 && ctr > 0)
itr++;
if(itr > searchRange)
itr = -searchRange;
offsets[o][dim - d - 1] = itr;
ctr++;
}
}
offsets = offsets.to(queryPositions_.device());
// Output the cell offsets to the console for debugging, enable via verbose flag
if(verbose){
std::cout << "Cell Offsets:" << std::endl;
for (int32_t i = 0; i < offsets.size(0); i++){
std::cout << "\t[" << i << "]: ";
for (int32_t d = 0; d < dim; d++){
std::cout << offsets[i][d].item<int32_t>() << " ";
}
std::cout << std::endl;
}
}
// Allocate output tensor for the neighbor counters
auto neighborList_i = torch::zeros({neighborListLength}, defaultOptions.dtype(torch::kInt32));
auto neighborList_j = torch::zeros({neighborListLength}, defaultOptions.dtype(torch::kInt32));
// Create the accessors for the input tensors as packed accessors
auto referencePositionAccessor = sortedPositions_.packed_accessor32<float, 2, traits>();
auto referenceSupportAccessor = sortedSupport_.packed_accessor32<float, 1, traits>();
auto hashTableAccessor = hashTable_.packed_accessor32<int32_t, 2, traits>();
auto cellTableAccessor = cellTable_.packed_accessor32<int64_t, 2, traits>();
auto offsetAccessor = offsets.packed_accessor32<int32_t, 2, traits>();
auto numCellsAccessor = numCells_.packed_accessor32<int32_t, 1, traits>();
auto neighborCounterAccessor = neighborCounter_.packed_accessor32<int32_t, 1, traits>();
auto neighborOffsetsAccessor = neighborOffsets_.packed_accessor32<int32_t, 1, traits>();
auto neighborList_iAccessor = neighborList_i.packed_accessor32<int32_t, 1, traits>();
auto neighborList_jAccessor = neighborList_j.packed_accessor32<int32_t, 1, traits>();
// Loop over all query particles and count the number of neighbors per particle
// auto dim = queryPositions.size(1);
// int32_t dim = queryPositions.size(1);
if(queryPositions_.is_cuda()){
#ifndef CUDA_VERSION
throw std::runtime_error("CUDA support is not available in this build");
#else
buildNeighborhoodCuda(neighborOffsets_, neighborList_i, neighborList_j,
queryPositions_, querySupport_, searchRange,
sortedPositions_, sortedSupport_,
hashTable_, hashMapLength,
cellTable_, numCells_,
offsets,
hCell, minDomain_, maxDomain_, periodicTensor, searchMode);
#endif
}else{
at::parallel_for(0, nQuery, 0, [&](int32_t start, int32_t end){
for(int32_t i = start; i < end; ++i){
#define args i, neighborOffsetsAccessor, neighborList_iAccessor, neighborList_jAccessor,\
queryPositions, querySupport, searchRange, \
sortedPositions, sortedSupport,\
hashTableAccessor, hashMapLength,\
cellTableAccessor, numCellsAccessor,\
offsetAccessor,\
hCell, minDomain, maxDomain, periodicity, searchMode
if(dim == 1)
buildNeighborhood<1>(args);
else if(dim == 2)
buildNeighborhood<2>(args);
else if(dim == 3)
buildNeighborhood<3>(args);
else
throw std::runtime_error("Unsupported dimension: " + std::to_string(dim));
#undef args
}
});
}
return std::make_pair(neighborList_i, neighborList_j);
}
// Create the python bindings for the C++ functions
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("countNeighbors", &countNeighbors, "Count the Number of Neighbors (C++) using a precomputed hash table and cell map");
m.def("buildNeighborList", &buildNeighborList, "Build the Neighborlist (C++) using a precomputed hash table and cell map as well as neighbor counts");
}