Repository navigation
Expand file tree
/
Copy pathcuNSearch.h
More file actions
286 lines (248 loc) · 12.4 KB
/
Copy pathcuNSearch.h
File metadata and controls
286 lines (248 loc) · 12.4 KB
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
#pragma once
// This is a public header. Avoid references to cuda or other external references.
#include "Common.h"
#include "ActivationTable.h"
#include "PointSet.h"
namespace cuNSearch
{
class cuNSearchDeviceData;
/**
* @class NeighborhoodSearch
* Stores point data multiple set of points in which neighborhood information for a fixed
* radius r should be generated.
*
* Host interface: identical to CompactNSearch::NeighborhoodSearch (add_point_set,
* find_neighbors, z_sort, PointSet::n_neighbors / neighbor / neighbor_list / sort_field, ...),
* so cuNSearch can be used as a drop-in replacement. Two points are neighbors if
* 0 < |x_i - x_j| < r. There is no limit on the number of neighbors.
*
* Device interface for CUDA solvers (all data stays on the GPU):
* - PointSet::device_points(): positions on the GPU; a solver can update them directly.
* - find_neighbors_device(): search with the GPU positions, no host <-> device transfers.
* - PointSet::device_neighbor_list(j): CSR neighbor lists (DeviceNeighborList), usable in kernels.
* - set_host_neighbor_lists(false): do not copy the neighbor lists to the host.
* - z_sort_device() + PointSet::sort_field_device(): z-order sorting of GPU arrays.
* All work is issued on the default CUDA stream (kernels of the solver on the default stream
* are ordered with it).
*
* Typical CUDA solver loop:
* nsearch.set_host_neighbor_lists(false);
* // upload once: positions are copied to the GPU by add_point_set()
* for each step:
* if (step % 500 == 0) { nsearch.z_sort_device(); fluid.sort_field_device(d_x); fluid.sort_field_device(d_v); ... }
* nsearch.find_neighbors_device();
* auto nl = nsearch.point_set(fluid).device_neighbor_list(fluid);
* myKernel<<<...>>>(nl, fluid.device_points(), ...);
*/
class NeighborhoodSearch
{
private:
std::vector<PointSet> pointSets;
public:
/**
* Constructor.
* Creates a new instance of the neighborhood search class.
* @param r Search radius. If two points are closer to each other than a distance r they are considered neighbors.
* @param erase_empty_cells Only for compatibility with CompactNSearch (ignored: cuNSearch
* rebuilds its sparse grid in every search, so there are no empty cells).
*/
NeighborhoodSearch(Real searchRadius, bool erase_empty_cells = false);
/**
* Destructor.
*/
~NeighborhoodSearch();
//Define descructor in cpp file to allow unique_ptr to incomplete type.
//https://stackoverflow.com/questions/9954518/stdunique-ptr-with-an-incomplete-type-wont-compile
/**
* Get method to access a point set.
* @param i Index of the point set to retrieve.
*/
PointSet const& point_set(unsigned int i) const { return pointSets[i]; }
/**
* Get method to access a point set.
* @param i Index of the point set to retrieve.
*/
PointSet & point_set(unsigned int i) { return pointSets[i]; }
/**
* Returns the number of point sets contained in the search.
*/
std::size_t n_point_sets() const { return pointSets.size(); }
/**
* Get method to access the list of point sets.
*/
std::vector<PointSet> const& point_sets() const { return pointSets; }
/**
* Get method to access the list of point sets.
*/
std::vector<PointSet> & point_sets() { return pointSets; }
/**
* Increases the size of a point set under the assumption that the existing points remain at
* the same position.
* @param i Index of point set that will be resized.
* @param x Pointer to the point position data. Must point to continguous data of 3 * n
* real values.
* @param n Number of points.
*/
void resize_point_set(unsigned int i, Real const* x, std::size_t n);
/**
* Creates and adds a new set of points.
* @param x Pointer to the point position data. Must point to continguous data of 3 * n
* real values.
* @param n Number of points.
* @param is_dynamic Specifies whether the point positions will change for future queries.
* @param search_neighbors If true, neighbors in all other point sets are searched.
* @param find_neighbors If true, the new point set is activated in the neighborhood search of all other point sets.
* @returns Returns unique identifier in form of an index assigned to the newly created point
* set.
*/
unsigned int add_point_set(Real const* x, std::size_t n, bool is_dynamic = true,
bool search_neighbors = true, bool find_neighbors = true, void *user_data = nullptr);
/**
* Performs the actual query. This method will assign a list of neighboring points to each point
* every added point set.
* The host positions of all dynamic point sets are copied to the GPU first. The neighbor
* lists are available on the GPU (PointSet::device_neighbor_list) and, if
* host_neighbor_lists() is true, also on the host.
* @param points_changed If false, the positions are assumed to be unchanged since the last search.
*/
void find_neighbors(bool points_changed = true);
/**
* Like find_neighbors(), but uses the positions in GPU memory (PointSet::device_points()),
* e.g. after a CUDA solver updated them. No positions are copied between host and device.
* @param points_changed If false, the positions are assumed to be unchanged since the last search.
*/
void find_neighbors_device(bool points_changed = true);
/**
* If false, the neighbor lists are only computed on the GPU and not copied to the host
* (saves the transfer, which dominates the run time for large point sets). The host
* accessors PointSet::n_neighbors / neighbor / neighbor_list must not be used then.
* Default: true.
*/
void set_host_neighbor_lists(bool enabled) { m_hostNeighborLists = enabled; }
bool host_neighbor_lists() const { return m_hostNeighborLists; }
/**
* Performs the query for a single point of a point set. After the call, neighbors[j] contains
* the indices (in ascending order) of all points of point set j that are neighbors of point
* point_index of point set point_set_id. neighbors[j] is empty if point set point_set_id does
* not search point set j (see set_active()).
* The query uses the current host position of the query point and the search grids of the
* last find_neighbors() / update_point_sets() call. Therefore, update_point_sets() must be
* called each time when the positions of a point set changed.
* After find_neighbors_device() / z_sort_device(), the GPU position of the query point is used.
* The result is identical to the lists computed by find_neighbors() for the same positions.
* Each call launches a small CUDA kernel per searched point set, so this is meant for few
* queries; use find_neighbors() to search the neighbors of all points.
* @throws std::out_of_range if point_set_id or point_index is invalid.
*/
void find_neighbors(unsigned int point_set_id, unsigned int point_index, std::vector<std::vector<unsigned int>> &neighbors);
/**
* Performs the query for an arbitrary position x (3 Real values). After the call, neighbors[j]
* contains the indices (in ascending order) of all points of point set j that are neighbors
* of x, for all point sets j. Points at exactly the position x are not reported.
* As for the single point query above, update_point_sets() must be called each time when the
* positions of a point set changed.
*/
void find_neighbors(Real const* x, std::vector<std::vector<unsigned int>> &neighbors);
/**
* Update neighborhood search data structures after a position change.
* If general find_neighbors() function is called there is no requirement to manually update the point sets.
* Otherwise, in case of using point-wise search (find_neighbors(i, j, neighbors)) the method must be called explicitly.
*/
void update_point_sets();
/**
* Update neighborhood search data structures after a position change.
* Has to be called when the positions of a non-dynamic pointset change.
* If general find_neighbors() function is called there is no requirement to manually update the point sets.
* Otherwise, in case of using point-wise search (find_neighbors(i, j, neighbors)) the method must be called explicitly.
*/
void update_point_set(int i);
/**
* Update neighborhood search data structures after changing the activation table.
* If general find_neighbors() function is called there is no requirement to manually update the point sets.
* Otherwise, in case of using point-wise search (find_neighbors(i, j, neighbors)) the method must be called explicitly.
*/
void update_activation_table();
/*
* Generates a sort table according to a space-filling Z curve. Any array-based per point
* information can then be reordered using the function sort_field of the PointSet class.
* Please note that the position data will not be modified by this class, such that the user has
* to invoke the sort_field function on the position array. Moreover, be aware the the grid has
* be reinitialized after each sort. Therefore, the points should not be reordered too
* frequently.
* The table is computed from the current host positions (copied to the GPU) and is also
* available for GPU arrays (PointSet::sort_field_device).
*/
void z_sort();
/**
* Like z_sort(), but computes the sort table from the positions in GPU memory
* (PointSet::device_points()). Reorder the GPU arrays with PointSet::sort_field_device()
* (including device_points()!) and/or host arrays with PointSet::sort_field() afterwards.
*/
void z_sort_device();
/**
* Forces a complete rebuild of all data structures in the next search (CompactNSearch
* compatible).
*/
void reset() { isInitialized = false; }
/*
* @returns Returns the radius in which point neighbors are searched.
*/
Real radius() const { return searchRadius; }
/**
* Sets the radius in which point point neighbors are searched.
* Updates the hash table for all non-dynamic point sets
* @param r Search radius.
*/
void set_radius(Real r);
/** Activate/deactivate that neighbors in point set j are found when searching for neighbors of point set i.
* @param i Index of searching point set.
* @param j Index of point set of which points should/shouldn't be found by point set i.
* @param active Flag in order to (de)activate that points in i find point in j.
*/
void set_active(unsigned int i, unsigned int j, bool active)
{
m_activation_table.set_active(i, j, active);
}
/** Activate/Deactivate all point set pairs containing the given index. If search_neighbors is true, neighbors in all other point sets are searched.
* If find_neighbors is true, the new point set is activated in the neighborhood search of all other point sets.
* @param i Index of searching point set.
* @param search_neighbors If true/false enables/disables that point set i searches points in all other point sets.
* @param find_neighbors If true/false enable/disables that point set i is found by all other point sets.
*/
void set_active(unsigned int i, bool search_neighbors = true, bool find_neighbors = true)
{
m_activation_table.set_active(i, search_neighbors, find_neighbors);
}
/** Activate/Deactivate all point set pairs.
*/
void set_active(bool active)
{
m_activation_table.set_active(active);
}
/** Returns true if point set i searchs points in point set j.
* @param i Searching point set.
* @param j Set of points to be found by i.
*/
bool is_active(unsigned int i, unsigned int j) const
{
return m_activation_table.is_active(i, j);
}
private:
bool isInitialized = false;
bool m_hostNeighborLists = true;
bool m_devicePositions = false; // last search used the GPU positions (find_neighbors_device)
Real searchRadius;
ActivationTable m_activation_table;
// Implementation and cuda data are hidden in the cuNSearchDeviceData class to avoid unnecessary dependencies in public headers.
std::unique_ptr<cuNSearchDeviceData> deviceData;
/** Rebuilds the search grid of a point set; uploads the host positions first if uploadPoints. */
void updatePointSet(PointSet &pointSet, bool uploadPoints = true);
/** Updates the grids (if points changed) and computes all active neighbor lists. */
void search(bool points_changed, bool uploadPoints);
/** Builds the grids of all point sets and stores their order as z-sort tables. */
void zSort(bool uploadPoints);
/** Builds the search grids of all point sets if this has not been done yet (e.g. a single
* point query before the first find_neighbors() call or after set_radius()). */
void ensureInitialized();
};
}