Skip to content

Commit 1ed9aac

Browse files
committed
- rewrote implementation of neighborhood search to improve performance and maintainability
- removed spinlocks to improve performance (now the neighborhood search is approx. 2x faster in an SPH simulation)
1 parent b40afcf commit 1ed9aac

6 files changed

Lines changed: 519 additions & 326 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,26 @@ install(TARGETS CompactNSearch
5757

5858
OPTION(USE_DOUBLE_PRECISION "Use double precision" ON)
5959
if (USE_DOUBLE_PRECISION)
60-
add_definitions( -DUSE_DOUBLE)
61-
endif (USE_DOUBLE_PRECISION)
60+
add_definitions( -DUSE_DOUBLE)
61+
endif (USE_DOUBLE_PRECISION)
62+
63+
# AVX2 version of the distance test in the neighborhood query. It is only available
64+
# in single precision (USE_DOUBLE_PRECISION=OFF). The library then requires a CPU
65+
# with AVX2 and POPCNT (Intel Haswell / AMD Excavator or newer).
66+
OPTION(USE_AVX2 "Use AVX2 in the neighborhood query (single precision only)" OFF)
67+
if (USE_AVX2)
68+
if (USE_DOUBLE_PRECISION)
69+
message(WARNING "USE_AVX2 is only supported in single precision. "
70+
"Set USE_DOUBLE_PRECISION=OFF to use it. The scalar code is used.")
71+
else()
72+
target_compile_definitions(CompactNSearch PRIVATE USE_AVX2)
73+
if (MSVC)
74+
target_compile_options(CompactNSearch PRIVATE /arch:AVX2)
75+
else()
76+
target_compile_options(CompactNSearch PRIVATE -mavx2 -mpopcnt)
77+
endif()
78+
endif()
79+
endif (USE_AVX2)
6280

6381
if(WIN32)
6482
add_definitions( -D_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING)

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
The library was used to generate all results of the SPH-based fluid simulations presented by Bender and Koschier in [BK15, BK16].
88

9-
**Author**: Dan Koschier, **License**: MIT
9+
**Authors**: Dan Koschier, Jan Bender, **License**: MIT
1010

1111
## Libraries using CompactNSearch
1212
* [PBD] - A C++ library for physically-based simulation of rigid bodies, deformables, cloth and fluids using Position-Based Dynamics
@@ -17,7 +17,7 @@ The library was used to generate all results of the SPH-based fluid simulations
1717
## Build Instructions
1818

1919
This project is based on [CMake](https://cmake.org/). Simply generate project, Makefiles, etc. using [CMake](https://cmake.org/) and compile the project with the compiler of your choice. The code was tested with the following configurations:
20-
- Windows 10 64-bit, CMake 3.7, Visual Studio 2015
20+
- Windows 11 64-bit, CMake 4.0.3, Visual Studio 2022
2121
- Debian 8 64-bit, CMake 3.7, GCC 4.9.2.
2222

2323
## Usage

‎include/CompactNSearch.h‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,18 @@ class NeighborhoodSearch
205205

206206
private:
207207

208+
/** Builds the grid (hash map and cells) from scratch. */
208209
void init();
210+
/** Inserts a point into the cell with the given key; creates the cell if necessary. */
211+
void add_to_cell(HashKey const& key, PointID const& id, bool searching);
212+
/** Removes a point from the cell with the given key; returns the index of the cell. */
213+
unsigned int remove_from_cell(HashKey const& key, PointID const& id, bool searching);
214+
/** Moves all points that changed their cell; collects cells that became empty. */
209215
void update_hash_table(std::vector<unsigned int>& to_delete);
210-
void erase_empty_entries(std::vector<unsigned int> const& to_delete);
216+
/** Removes the listed empty cells (swap with the last cell, then pop). The list is
217+
* sorted and de-duplicated by the function itself. */
218+
void erase_empty_entries(std::vector<unsigned int>& to_delete);
219+
/** Neighborhood search for all points of all point sets (lock-free "gather"). */
211220
void query();
212221
void query(unsigned int point_set_id, unsigned int point_index, std::vector<std::vector<unsigned int>> &neighbors);
213222
void query(Real const* xa, std::vector<std::vector<unsigned int>> &neighbors);
@@ -222,9 +231,21 @@ class NeighborhoodSearch
222231

223232
Real m_inv_cell_size;
224233
Real m_r2;
234+
// Spatial grid: m_entries holds all cells, m_map maps the key of a cell to its index
235+
// in m_entries. The order of the cells in m_entries is arbitrary and changes when
236+
// empty cells are erased.
225237
std::unordered_map<HashKey, unsigned int, SpatialHasher> m_map;
226238
std::vector<HashEntry> m_entries;
227239

240+
// Copies of the point ids and positions, ordered by cell, rebuilt in each query():
241+
// points of cell e occupy the slots m_cell_start[e] ... m_cell_start[e+1]-1,
242+
// m_cell_ids[s] is the id and m_cell_x[3*s .. 3*s+2] the position of slot s.
243+
// They are members (not local variables) so that their memory is reused between
244+
// queries.
245+
std::vector<unsigned int> m_cell_start;
246+
std::vector<PointID> m_cell_ids;
247+
std::vector<Real> m_cell_x;
248+
228249
bool m_erase_empty_cells;
229250
bool m_initialized;
230251
};

‎include/DataStructures.h‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,54 @@ struct HashKey
5151
int k[3];
5252
};
5353

54+
/**
55+
* One cell of the spatial grid.
56+
* The cells are stored in NeighborhoodSearch::m_entries; the hash map
57+
* NeighborhoodSearch::m_map maps the key of a cell to its index in that array.
58+
*/
5459
struct HashEntry
5560
{
5661
HashEntry() : n_searching_points(0u)
5762
{
5863
indices.reserve(INITIAL_NUMBER_OF_INDICES);
5964
}
6065

61-
HashEntry(PointID const& id) : n_searching_points(0u)
66+
/** Creates an empty cell with the given grid coordinates. */
67+
explicit HashEntry(HashKey const& k) : key(k), n_searching_points(0u)
6268
{
63-
add(id);
6469
}
6570

6671
void add(PointID const& id)
6772
{
6873
indices.push_back(id);
6974
}
7075

76+
/** Removes a point from the cell. The order of the points in a cell is irrelevant, so
77+
* the point is overwritten by the last one and the vector is shortened by one. This
78+
* avoids shifting all following elements. */
7179
void erase(PointID const& id)
7280
{
7381
auto it = std::find(indices.begin(), indices.end(), id);
7482
if (it != indices.end())
75-
indices.erase(it);
83+
{
84+
*it = indices.back();
85+
indices.pop_back();
86+
}
7687
}
7788

7889
unsigned int n_indices() const
7990
{
8091
return static_cast<unsigned int>(indices.size());
8192
}
8293

94+
/** Grid coordinates of the cell. Stored here so that the cells can be processed
95+
* without iterating over the hash map, and so that the map entry of a cell can be
96+
* found when the cell is moved in m_entries (see erase_empty_entries()). */
97+
HashKey key;
98+
/** Points inside the cell (in no particular order). */
8399
std::vector<PointID> indices;
100+
/** Number of points in this cell whose point set searches for neighbors. Cells with
101+
* zero searching points are skipped by the search. */
84102
unsigned int n_searching_points;
85103
};
86104

‎include/PointSet.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ class PointSet
4141

4242
m_sort_table = other.m_sort_table;
4343

44-
m_locks.resize(other.m_locks.size());
45-
for (unsigned int i = 0; i < other.m_locks.size(); ++i)
46-
{
47-
m_locks[i].resize(other.m_locks[i].size());
48-
}
49-
5044
return *this;
5145
}
5246

@@ -115,7 +109,7 @@ class PointSet
115109

116110
friend NeighborhoodSearch;
117111
PointSet(Real const* x, std::size_t n, bool dynamic, void *user_data = nullptr)
118-
: m_x(x), m_n(n), m_dynamic(dynamic), m_user_data(user_data), m_neighbors(n)
112+
: m_x(x), m_n(n), m_dynamic(dynamic), m_user_data(user_data)
119113
, m_keys(n, {
120114
std::numeric_limits<int>::lowest(),
121115
std::numeric_limits<int>::lowest(),
@@ -136,7 +130,10 @@ class PointSet
136130
std::numeric_limits<int>::lowest(),
137131
std::numeric_limits<int>::lowest(),
138132
std::numeric_limits<int>::lowest() });
139-
m_neighbors.resize(n);
133+
// The outer index of m_neighbors is the point set, the per-point lists are
134+
// resized in NeighborhoodSearch::query().
135+
for (auto& n_ : m_neighbors)
136+
n_.resize(n);
140137
}
141138

142139
Real const* point(unsigned int i) const { return &m_x[3*i]; }
@@ -148,10 +145,13 @@ class PointSet
148145
bool m_dynamic;
149146
void *m_user_data;
150147

148+
// m_neighbors[j][i] = indices of the neighbors of point i (of this set) in point set j.
149+
// The outer size is the number of point sets, the inner size the number of points.
151150
std::vector<std::vector<std::vector<unsigned int>>> m_neighbors;
152151

152+
// Cell of each point in the current (m_keys) and the previous (m_old_keys) update.
153+
// Points whose two keys differ have to be moved to another cell.
153154
std::vector<HashKey> m_keys, m_old_keys;
154-
std::vector<std::vector<Spinlock>> m_locks;
155155
std::vector<unsigned int> m_sort_table;
156156
};
157157

0 commit comments

Comments
 (0)