-
Notifications
You must be signed in to change notification settings - Fork 35
/
benchmark_registration.hpp
436 lines (372 loc) · 15.8 KB
/
benchmark_registration.hpp
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
/****************************************************************************
* Copyright (c) 2017-2022 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#ifndef BENCHMARK_REGISTRATION_HPP
#define BENCHMARK_REGISTRATION_HPP
#include <ArborXBenchmark_PointClouds.hpp>
#include <ArborX_Point.hpp>
#include <ArborX_Predicates.hpp>
#include <Kokkos_Core.hpp>
#include <chrono>
#include <cmath> // cbrt
#include <benchmark/benchmark.h>
struct Spec
{
using PointCloudType = ArborXBenchmark::PointCloudType;
std::string backends;
int n_values;
int n_queries;
int n_neighbors;
bool sort_predicates;
int buffer_size;
PointCloudType source_point_cloud_type;
PointCloudType target_point_cloud_type;
Spec() = default;
Spec(std::string const &spec_string)
{
std::istringstream ss(spec_string);
std::string token;
// clang-format off
getline(ss, token, '/'); backends = token;
getline(ss, token, '/'); n_values = std::stoi(token);
getline(ss, token, '/'); n_queries = std::stoi(token);
getline(ss, token, '/'); n_neighbors = std::stoi(token);
getline(ss, token, '/'); sort_predicates = static_cast<bool>(std::stoi(token));
getline(ss, token, '/'); buffer_size = std::stoi(token);
getline(ss, token, '/'); source_point_cloud_type = static_cast<PointCloudType>(std::stoi(token));
getline(ss, token, '/'); target_point_cloud_type = static_cast<PointCloudType>(std::stoi(token));
// clang-format on
if (!(backends == "all" || backends == "serial" || backends == "openmp" ||
backends == "threads" || backends == "cuda" || backends == "rtree" ||
backends == "hip" || backends == "sycl" ||
backends == "openmptarget"))
throw std::runtime_error("Backend " + backends + " invalid!");
}
std::string create_label_construction(std::string const &tree_name) const
{
std::string s = std::string("BM_construction<") + tree_name + ">";
for (auto const &var :
{n_values, static_cast<int>(source_point_cloud_type)})
s += "/" + std::to_string(var);
return s;
}
std::string create_label_radius_search(std::string const &tree_name,
std::string const &flavor = "") const
{
std::string s = std::string("BM_radius_") +
(flavor.empty() ? "" : flavor + "_") + "search<" +
tree_name + ">";
for (auto const &var :
{n_values, n_queries, n_neighbors, static_cast<int>(sort_predicates),
buffer_size, static_cast<int>(source_point_cloud_type),
static_cast<int>(target_point_cloud_type)})
s += "/" + std::to_string(var);
return s;
};
std::string create_label_knn_search(std::string const &tree_name,
std::string const &flavor = "") const
{
std::string s = std::string("BM_knn_") +
(flavor.empty() ? "" : flavor + "_") + "search<" +
tree_name + ">";
for (auto const &var :
{n_values, n_queries, n_neighbors, static_cast<int>(sort_predicates),
static_cast<int>(source_point_cloud_type),
static_cast<int>(target_point_cloud_type)})
s += "/" + std::to_string(var);
return s;
};
};
template <typename DeviceType>
Kokkos::View<ArborX::Point *, DeviceType>
constructPoints(int n_values, ArborXBenchmark::PointCloudType point_cloud_type)
{
using ExecutionSpace = typename DeviceType::execution_space;
ExecutionSpace exec;
Kokkos::View<ArborX::Point *, DeviceType> random_points(
Kokkos::view_alloc(exec, Kokkos::WithoutInitializing,
"Benchmark::random_points"),
n_values);
// Generate random points uniformly distributed within a box. The edge
// length of the box chosen such that object density (here objects will be
// boxes 2x2x2 centered around a random point) will remain constant as
// problem size is changed.
auto const a = std::cbrt(n_values);
ArborXBenchmark::generatePointCloud(exec, point_cloud_type, a, random_points);
return random_points;
}
template <typename DeviceType>
Kokkos::View<decltype(ArborX::intersects(ArborX::Sphere{})) *, DeviceType>
makeSpatialQueries(int n_values, int n_queries, int n_neighbors,
ArborXBenchmark::PointCloudType target_point_cloud_type)
{
using ExecutionSpace = typename DeviceType::execution_space;
ExecutionSpace exec;
Kokkos::View<ArborX::Point *, DeviceType> random_points(
Kokkos::view_alloc(exec, Kokkos::WithoutInitializing,
"Benchmark::random_points"),
n_queries);
auto const a = std::cbrt(n_values);
ArborXBenchmark::generatePointCloud(exec, target_point_cloud_type, a,
random_points);
Kokkos::View<decltype(ArborX::intersects(ArborX::Sphere{})) *, DeviceType>
queries(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "Benchmark::queries"),
n_queries);
// Radius is computed so that the number of results per query for a uniformly
// distributed points in a [-a,a]^3 box is approximately n_neighbors.
// Calculation: n_values*(4/3*pi*r^3)/(2a)^3 = n_neighbors
double const r = std::cbrt(static_cast<double>(n_neighbors) * 6. /
Kokkos::numbers::pi_v<double>);
Kokkos::parallel_for(
"Benchmark::setup_radius_search_queries",
Kokkos::RangePolicy<ExecutionSpace>(exec, 0, n_queries),
KOKKOS_LAMBDA(int i) {
queries(i) = ArborX::intersects(ArborX::Sphere{random_points(i), r});
});
return queries;
}
template <typename DeviceType>
Kokkos::View<ArborX::Nearest<ArborX::Point> *, DeviceType>
makeNearestQueries(int n_values, int n_queries, int n_neighbors,
ArborXBenchmark::PointCloudType target_point_cloud_type)
{
using ExecutionSpace = typename DeviceType::execution_space;
ExecutionSpace exec;
Kokkos::View<ArborX::Point *, DeviceType> random_points(
Kokkos::view_alloc(Kokkos::WithoutInitializing,
"Benchmark::random_points"),
n_queries);
auto const a = std::cbrt(n_values);
ArborXBenchmark::generatePointCloud(exec, target_point_cloud_type, a,
random_points);
Kokkos::View<ArborX::Nearest<ArborX::Point> *, DeviceType> queries(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "Benchmark::queries"),
n_queries);
Kokkos::parallel_for(
"Benchmark::setup_knn_search_queries",
Kokkos::RangePolicy<ExecutionSpace>(exec, 0, n_queries),
KOKKOS_LAMBDA(int i) {
queries(i) =
ArborX::nearest<ArborX::Point>(random_points(i), n_neighbors);
});
return queries;
}
template <typename DeviceType>
struct CountCallback
{
Kokkos::View<int *, DeviceType> count_;
template <typename Query>
KOKKOS_FUNCTION void operator()(Query const &query, int) const
{
auto const i = ArborX::getData(query);
Kokkos::atomic_increment(&count_(i));
}
};
template <typename ExecutionSpace, class TreeType>
void BM_construction(benchmark::State &state, Spec const &spec)
{
using DeviceType =
Kokkos::Device<ExecutionSpace, typename TreeType::memory_space>;
ExecutionSpace exec_space;
auto const points =
constructPoints<DeviceType>(spec.n_values, spec.source_point_cloud_type);
for (auto _ : state)
{
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
TreeType index(exec_space, points);
exec_space.fence();
auto const end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
state.SetIterationTime(elapsed_seconds.count());
}
state.counters["rate"] = benchmark::Counter(
spec.n_values, benchmark::Counter::kIsIterationInvariantRate);
}
template <typename ExecutionSpace, class TreeType>
void BM_radius_search(benchmark::State &state, Spec const &spec)
{
using DeviceType =
Kokkos::Device<ExecutionSpace, typename TreeType::memory_space>;
ExecutionSpace exec_space;
TreeType index(exec_space, constructPoints<DeviceType>(
spec.n_values, spec.source_point_cloud_type));
auto const queries = makeSpatialQueries<DeviceType>(
spec.n_values, spec.n_queries, spec.n_neighbors,
spec.target_point_cloud_type);
for (auto _ : state)
{
Kokkos::View<int *, DeviceType> offset("offset", 0);
Kokkos::View<int *, DeviceType> indices("indices", 0);
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
ArborX::query(index, exec_space, queries, indices, offset,
ArborX::Experimental::TraversalPolicy()
.setPredicateSorting(spec.sort_predicates)
.setBufferSize(spec.buffer_size));
exec_space.fence();
auto const end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
state.SetIterationTime(elapsed_seconds.count());
}
state.counters["rate"] = benchmark::Counter(
spec.n_queries, benchmark::Counter::kIsIterationInvariantRate);
}
template <typename ExecutionSpace, class TreeType>
void BM_radius_callback_search(benchmark::State &state, Spec const &spec)
{
using DeviceType =
Kokkos::Device<ExecutionSpace, typename TreeType::memory_space>;
ExecutionSpace exec_space;
TreeType index(
ExecutionSpace{},
constructPoints<DeviceType>(spec.n_values, spec.source_point_cloud_type));
auto const queries = makeSpatialQueries<DeviceType>(
spec.n_values, spec.n_queries, spec.n_neighbors,
spec.target_point_cloud_type);
for (auto _ : state)
{
Kokkos::View<int *, DeviceType> num_neigh("Testing::num_neigh",
spec.n_queries);
CountCallback<DeviceType> callback{num_neigh};
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
index.query(exec_space, ArborX::Experimental::attach_indices<int>(queries),
callback,
ArborX::Experimental::TraversalPolicy().setPredicateSorting(
spec.sort_predicates));
exec_space.fence();
auto const end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
state.SetIterationTime(elapsed_seconds.count());
}
state.counters["rate"] = benchmark::Counter(
spec.n_queries, benchmark::Counter::kIsIterationInvariantRate);
}
template <typename ExecutionSpace, class TreeType>
void BM_knn_search(benchmark::State &state, Spec const &spec)
{
using DeviceType =
Kokkos::Device<ExecutionSpace, typename TreeType::memory_space>;
ExecutionSpace exec_space;
TreeType index(exec_space, constructPoints<DeviceType>(
spec.n_values, spec.source_point_cloud_type));
auto const queries = makeNearestQueries<DeviceType>(
spec.n_values, spec.n_queries, spec.n_neighbors,
spec.target_point_cloud_type);
for (auto _ : state)
{
Kokkos::View<int *, DeviceType> offset("Benchmark::offset", 0);
Kokkos::View<int *, DeviceType> indices("Benchmark::indices", 0);
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
ArborX::query(index, exec_space, queries, indices, offset,
ArborX::Experimental::TraversalPolicy().setPredicateSorting(
spec.sort_predicates));
exec_space.fence();
auto const end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
state.SetIterationTime(elapsed_seconds.count());
}
state.counters["rate"] = benchmark::Counter(
spec.n_queries, benchmark::Counter::kIsIterationInvariantRate);
}
template <typename ExecutionSpace, class TreeType>
void BM_knn_callback_search(benchmark::State &state, Spec const &spec)
{
using DeviceType =
Kokkos::Device<ExecutionSpace, typename TreeType::memory_space>;
ExecutionSpace exec_space;
TreeType index(exec_space, constructPoints<DeviceType>(
spec.n_values, spec.source_point_cloud_type));
auto const queries = makeNearestQueries<DeviceType>(
spec.n_values, spec.n_queries, spec.n_neighbors,
spec.target_point_cloud_type);
for (auto _ : state)
{
Kokkos::View<int *, DeviceType> num_neigh("Benchmark::num_neigh",
spec.n_queries);
CountCallback<DeviceType> callback{num_neigh};
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
index.query(exec_space, ArborX::Experimental::attach_indices<int>(queries),
callback,
ArborX::Experimental::TraversalPolicy().setPredicateSorting(
spec.sort_predicates));
exec_space.fence();
auto const end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
state.SetIterationTime(elapsed_seconds.count());
}
state.counters["rate"] = benchmark::Counter(
spec.n_queries, benchmark::Counter::kIsIterationInvariantRate);
}
template <typename ExecutionSpace, typename TreeType>
void register_benchmark_construction(Spec const &spec,
std::string const &description)
{
benchmark::RegisterBenchmark(
spec.create_label_construction(description).c_str(),
[=](benchmark::State &state) {
BM_construction<ExecutionSpace, TreeType>(state, spec);
})
->UseManualTime()
->Unit(benchmark::kMicrosecond);
}
template <typename ExecutionSpace, typename TreeType>
void register_benchmark_spatial_query_no_callback(
Spec const &spec, std::string const &description)
{
benchmark::RegisterBenchmark(
spec.create_label_radius_search(description).c_str(),
[=](benchmark::State &state) {
BM_radius_search<ExecutionSpace, TreeType>(state, spec);
})
->UseManualTime()
->Unit(benchmark::kMicrosecond);
}
template <typename ExecutionSpace, typename TreeType>
void register_benchmark_spatial_query_callback(Spec const &spec,
std::string const &description)
{
benchmark::RegisterBenchmark(
spec.create_label_radius_search(description, "callback").c_str(),
[=](benchmark::State &state) {
BM_radius_callback_search<ExecutionSpace, TreeType>(state, spec);
})
->UseManualTime()
->Unit(benchmark::kMicrosecond);
}
template <typename ExecutionSpace, typename TreeType>
void register_benchmark_nearest_query_no_callback(
Spec const &spec, std::string const &description)
{
benchmark::RegisterBenchmark(
spec.create_label_knn_search(description).c_str(),
[=](benchmark::State &state) {
BM_knn_search<ExecutionSpace, TreeType>(state, spec);
})
->UseManualTime()
->Unit(benchmark::kMicrosecond);
}
template <typename ExecutionSpace, typename TreeType>
void register_benchmark_nearest_query_callback(Spec const &spec,
std::string const &description)
{
benchmark::RegisterBenchmark(
spec.create_label_knn_search(description, "callback").c_str(),
[=](benchmark::State &state) {
BM_knn_callback_search<ExecutionSpace, TreeType>(state, spec);
})
->UseManualTime()
->Unit(benchmark::kMicrosecond);
}
#endif