A C++/CUDA library to efficiently compute neighborhood information on the GPU for 3D point clouds within a fixed radius. Suitable for many applications, e.g. neighborhood search for SPH fluid simulations.
The library interface is similar to the CPU neighborhood search CompactNSearch.
- SPlisHSPlasH - A C++ library for the physically-based simulation of fluids using Smoothed Particle Hydrodynamics (see screenshot)
This project is based on CMake. Simply generate project, Makefiles, etc. using CMake and compile the project with the compiler of your choice.
Requirements:
- CMake 3.18
- CUDA SDK 11.0 or newer
- C++ 17
The code was tested with the following configurations:
- Windows 11 64-bit, CMake 4.0.3, Visual Studio 2022, CUDA SDK 13.5
The tests (comparison with a brute-force search) can be run with ctest in the build directory.
A data structure to perform a neighborhood search can be created by calling the constructor given a fixed search radius r.
cuNSearch::NeighborhoodSearch nsearch(r);An arbitrary number of point clouds can then be added to the data structure using the method add_point_set. The library expects the point positions to be contiguously stored in an array-like structure. The method will return a unique id associated with the initialized point set.
std::vector<std::array<Real, 3>> positions;
// ... Fill array with 3 * n real numbers representing three-dimensional point positions.
unsigned int point_set_id = nsearch.add_point_set(positions.front().data(), positions.size());
nsearch.find_neighbors();In order to generate the neighborhood information simply execute the following command
nsearch.find_neighbors();Finally, the neighborhood information can be accessed as follows
PointSet const& ps = nsearch.point_set(point_set_id);
for (unsigned int i = 0; i < ps.n_points(); ++i)
{
// Neighbors of the ith point in the point set with the id point_set_id (here: the same point set).
for (unsigned int j = 0; j < ps.n_neighbors(point_set_id, i); ++j)
{
// Index of the jth neighbor of the ith point within the point set point_set_id.
const unsigned int pid = ps.neighbor(point_set_id, i, j);
// ...
}
}Besides the basic functionality the library offers to compute a rule for reordering the points according to a space-filling Z curve. The reordering will improve the performance of future neighborhood queries and accesses. The rule can be computed via
nsearch.z_sort();Please note that the actual reordering must be invoked by the user by
ps.sort_field(positions.data());Assuming that there is additional information stored per-point (e.g. velocity, color, mass etc.) the information must also be reorded using the same method to maintain consistency. Subsequently, the find_neighbors function has to be invoked again to update the neighborhood information.
Another self-explaining (benchmark) demo is contained in the project.
The neighbors of a single point of a point set or of an arbitrary position can be determined with nsearch.find_neighbors(point_set_id, point_index, neighbors) and nsearch.find_neighbors(x, neighbors).
CUDA solvers can keep all data on the GPU: find_neighbors_device() performs the search with the GPU positions PointSet::device_points(), PointSet::device_neighbor_list(j) returns the neighbor lists in GPU memory (can be passed to a kernel), set_host_neighbor_lists(false) avoids the copy of the lists to the host and z_sort_device() / PointSet::sort_field_device() sort GPU arrays. See cuNSearch.h for an example.
cuNSearch divides the space into a uniform grid whose cell size equals the search radius r. All neighbors of a point then lie in its own cell or in the directly adjacent cells (3x3x3 cells in 3D). The images show the steps in 2D.
1. Cells and keys. Each point is assigned to the cell that contains it. A cell is identified by a key, the Morton code of its integer cell coordinates (the bits of the coordinates interleaved). Sorting by this key orders the cells along a space-filling Z-curve, so cells that are close in space usually get close keys.
2. Sparse grid. The points are sorted by their cell key on the GPU (radix sort) and their positions are stored in this order, so the points of a cell are contiguous in memory. The occupied cells are determined from the sorted keys, each with its first point and number of points, and inserted into a hash table. Empty cells are not stored at all: the memory only depends on the number of points, not on the size of the domain, and points can move anywhere in space.
3. Query. For each point, the neighbor cells are visited. Cells that are farther than r from the point are skipped, the others are looked up in the hash table and the distance to each of their points is tested. The search runs in two passes: the first one counts the neighbors of each point, a prefix sum of the counts gives the position of each list in one array, and the second pass writes the neighbor indices. The resulting lists (compressed sparse row format) stay in GPU memory and are only copied to the host if required. The points are processed in the sorted order, so threads that run together access the same cells.
The grids of all point sets are rebuilt in each call of find_neighbors() (only for point sets that can move). The order of step 2 is also used by z_sort(): reordering the point data with it improves the memory access of the search and of the user's own computations.
When maintaining multiple it is sometimes desired that only certain point sets can find points from other point sets. Therefore an activation table is implemented where the user can specify whether a point set i searches points in another point set j. When nothing else is specified all point sets will search points in all other point sets. The activation table can be modified with e.g.
nsearch.set_active(i, j, false)Visual Studio may not detect changes in ".cu" files.
Use of thrust library in cpp files: Some thrust classes can only be used when the file is compiled by the nvidia compiler nvcc. This is usually solved by change the file ending to .cu to mark the file for the nvcc compiler.
- R. Hoetzlein, 2014. "Fast Fixed-Radius Nearest Neighbors: Interactive Million-Particle Fluids", GPU Technology Conference (GTC), Santa Clara, CA.
