/
ArborX_DetailsTreeTraversal.hpp
549 lines (486 loc) · 17.3 KB
/
ArborX_DetailsTreeTraversal.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/****************************************************************************
* Copyright (c) 2017-2023 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 ARBORX_DETAILS_TREE_TRAVERSAL_HPP
#define ARBORX_DETAILS_TREE_TRAVERSAL_HPP
#include <ArborX_DetailsAlgorithms.hpp>
#include <ArborX_DetailsHappyTreeFriends.hpp>
#include <ArborX_DetailsKokkosExtArithmeticTraits.hpp>
#include <ArborX_DetailsKokkosExtViewHelpers.hpp>
#include <ArborX_DetailsNode.hpp> // ROPE_SENTINEL
#include <ArborX_DetailsPriorityQueue.hpp>
#include <ArborX_DetailsStack.hpp>
#include <ArborX_DetailsUtils.hpp>
#include <ArborX_Exception.hpp>
#include <ArborX_Predicates.hpp>
namespace ArborX
{
namespace Details
{
template <typename BVH, typename Predicates, typename Callback, typename Tag>
struct TreeTraversal
{};
template <typename BVH, typename Predicates, typename Callback>
struct TreeTraversal<BVH, Predicates, Callback, SpatialPredicateTag>
{
BVH _bvh;
Predicates _predicates;
Callback _callback;
template <typename ExecutionSpace>
TreeTraversal(ExecutionSpace const &space, BVH const &bvh,
Predicates const &predicates, Callback const &callback)
: _bvh{bvh}
, _predicates{predicates}
, _callback{callback}
{
if (_bvh.empty())
{
// do nothing
}
else if (_bvh.size() == 1)
{
Kokkos::parallel_for(
"ArborX::TreeTraversal::spatial::degenerated_one_leaf_tree",
Kokkos::RangePolicy<ExecutionSpace, OneLeafTree>(space, 0,
predicates.size()),
*this);
}
else
{
Kokkos::parallel_for("ArborX::TreeTraversal::spatial",
Kokkos::RangePolicy<ExecutionSpace, FullTree>(
space, 0, predicates.size()),
*this);
}
}
KOKKOS_FUNCTION TreeTraversal(BVH const &bvh, Callback const &callback)
: _bvh{bvh}
, _callback{callback}
{}
struct OneLeafTree
{};
struct FullTree
{};
KOKKOS_FUNCTION void operator()(OneLeafTree, int queryIndex) const
{
auto const &predicate = _predicates(queryIndex);
auto const root = 0;
auto const &root_bounding_volume =
HappyTreeFriends::getIndexable(_bvh, root);
if (predicate(root_bounding_volume))
{
_callback(predicate, HappyTreeFriends::getValue(_bvh, 0));
}
}
KOKKOS_FUNCTION void operator()(FullTree, int queryIndex) const
{
operator()(_predicates(queryIndex));
}
template <typename Predicate>
KOKKOS_FUNCTION void operator()(Predicate const &predicate) const
{
int node = HappyTreeFriends::getRoot(_bvh); // start with root
do
{
if (HappyTreeFriends::isLeaf(_bvh, node))
{
if (predicate(HappyTreeFriends::getIndexable(_bvh, node)) &&
invoke_callback_and_check_early_exit(
_callback, predicate, HappyTreeFriends::getValue(_bvh, node)))
return;
node = HappyTreeFriends::getRope(_bvh, node);
}
else
{
node =
(predicate(HappyTreeFriends::getInternalBoundingVolume(_bvh, node))
? HappyTreeFriends::getLeftChild(_bvh, node)
: HappyTreeFriends::getRope(_bvh, node));
}
} while (node != ROPE_SENTINEL);
}
};
template <typename BVH, typename Predicates, typename Callback>
struct TreeTraversal<BVH, Predicates, Callback, NearestPredicateTag>
{
using MemorySpace = typename BVH::memory_space;
BVH _bvh;
Predicates _predicates;
Callback _callback;
using Buffer = Kokkos::View<Kokkos::pair<int, float> *, MemorySpace>;
using Offset = Kokkos::View<int *, MemorySpace>;
struct BufferProvider
{
Buffer _buffer;
Offset _offset;
KOKKOS_FUNCTION auto operator()(int i) const
{
auto const *offset_ptr = &_offset(i);
return Kokkos::subview(_buffer,
Kokkos::make_pair(*offset_ptr, *(offset_ptr + 1)));
}
};
BufferProvider _buffer;
template <typename ExecutionSpace>
void allocateBuffer(ExecutionSpace const &space)
{
auto const n_queries = _predicates.size();
Offset offset(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::TreeTraversal::nearest::offset"),
n_queries + 1);
Kokkos::parallel_for(
"ArborX::TreeTraversal::nearest::"
"scan_queries_for_numbers_of_neighbors",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n_queries),
KOKKOS_CLASS_LAMBDA(int i) { offset(i) = getK(_predicates(i)); });
KokkosExt::exclusive_scan(space, offset);
int const buffer_size = KokkosExt::lastElement(space, offset);
// Allocate buffer over which to perform heap operations in
// TreeTraversal::nearestQuery() to store nearest leaf nodes found so far.
// It is not possible to anticipate how much memory to allocate since the
// number of nearest neighbors k is only known at runtime.
Buffer buffer(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::TreeTraversal::nearest::buffer"),
buffer_size);
_buffer = BufferProvider{buffer, offset};
}
template <typename ExecutionSpace>
TreeTraversal(ExecutionSpace const &space, BVH const &bvh,
Predicates const &predicates, Callback const &callback)
: _bvh{bvh}
, _predicates{predicates}
, _callback{callback}
{
if (_bvh.empty())
{
// do nothing
}
else if (_bvh.size() == 1)
{
Kokkos::parallel_for(
"ArborX::TreeTraversal::nearest::degenerated_one_leaf_tree",
Kokkos::RangePolicy<ExecutionSpace, OneLeafTree>(space, 0,
predicates.size()),
*this);
}
else
{
allocateBuffer(space);
Kokkos::parallel_for(
"ArborX::TreeTraversal::nearest",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, predicates.size()),
*this);
}
}
struct OneLeafTree
{};
KOKKOS_FUNCTION void operator()(OneLeafTree, int queryIndex) const
{
auto const &predicate = _predicates(queryIndex);
auto const k = getK(predicate);
// NOTE thinking about making this a precondition
if (k < 1)
return;
_callback(predicate, HappyTreeFriends::getValue(_bvh, 0));
}
KOKKOS_FUNCTION void operator()(int queryIndex) const
{
auto const &predicate = _predicates(queryIndex);
auto const k = getK(predicate);
auto const buffer = _buffer(queryIndex);
// NOTE thinking about making this a precondition
if (k < 1)
return;
// Nodes with a distance that exceed that radius can safely be
// discarded. Initialize the radius to infinity and tighten it once k
// neighbors have been found.
auto radius = KokkosExt::ArithmeticTraits::infinity<float>::value;
using PairIndexDistance = Kokkos::pair<int, float>;
static_assert(
std::is_same<typename decltype(buffer)::value_type,
PairIndexDistance>::value,
"Type of the elements stored in the buffer passed as argument to "
"TreeTraversal::nearestQuery is not right");
struct CompareDistance
{
KOKKOS_INLINE_FUNCTION bool operator()(PairIndexDistance const &lhs,
PairIndexDistance const &rhs) const
{
return lhs.second < rhs.second;
}
};
// Use a priority queue for convenience to store the results and
// preserve the heap structure internally at all time. There is no
// memory allocation, elements are stored in the buffer passed as an
// argument. The farthest leaf node is on top.
KOKKOS_ASSERT(k == (int)buffer.size());
PriorityQueue<PairIndexDistance, CompareDistance,
UnmanagedStaticVector<PairIndexDistance>>
heap(UnmanagedStaticVector<PairIndexDistance>(buffer.data(),
buffer.size()));
auto &bvh = _bvh;
auto const distance = [&predicate, &bvh](int j) {
return HappyTreeFriends::isLeaf(bvh, j)
? predicate.distance(HappyTreeFriends::getIndexable(bvh, j))
: predicate.distance(
HappyTreeFriends::getInternalBoundingVolume(bvh, j));
};
constexpr int SENTINEL = -1;
int stack[64];
auto *stack_ptr = stack;
*stack_ptr++ = SENTINEL;
#if !defined(__CUDA_ARCH__)
float stack_distance[64];
auto *stack_distance_ptr = stack_distance;
*stack_distance_ptr++ = 0.f;
#endif
int node = HappyTreeFriends::getRoot(_bvh);
int left_child;
int right_child;
float distance_left = 0.f;
float distance_right = 0.f;
float distance_node = 0.f;
do
{
bool traverse_left = false;
bool traverse_right = false;
if (distance_node < radius)
{
// Insert children into the stack and make sure that the
// closest one ends on top.
left_child = HappyTreeFriends::getLeftChild(_bvh, node);
right_child = HappyTreeFriends::getRightChild(_bvh, node);
distance_left = distance(left_child);
distance_right = distance(right_child);
if (distance_left < radius)
{
if (HappyTreeFriends::isLeaf(_bvh, left_child))
{
auto leaf_pair = Kokkos::make_pair(left_child, distance_left);
if ((int)heap.size() < k)
heap.push(leaf_pair);
else
heap.popPush(leaf_pair);
if ((int)heap.size() == k)
radius = heap.top().second;
}
else
{
traverse_left = true;
}
}
// Note: radius may have been already updated here from the left child
if (distance_right < radius)
{
if (HappyTreeFriends::isLeaf(_bvh, right_child))
{
auto leaf_pair = Kokkos::make_pair(right_child, distance_right);
if ((int)heap.size() < k)
heap.push(leaf_pair);
else
heap.popPush(leaf_pair);
if ((int)heap.size() == k)
radius = heap.top().second;
}
else
{
traverse_right = true;
}
}
}
if (!traverse_left && !traverse_right)
{
node = *--stack_ptr;
#if defined(__CUDA_ARCH__)
if (node != SENTINEL)
{
// This is a theoretically unnecessary duplication of distance
// calculation for stack nodes. However, for Cuda it's better than
// putting the distances in stack.
distance_node = distance(node);
}
#else
distance_node = *--stack_distance_ptr;
#endif
}
else
{
node = (traverse_left &&
(distance_left <= distance_right || !traverse_right))
? left_child
: right_child;
distance_node = (node == left_child ? distance_left : distance_right);
if (traverse_left && traverse_right)
{
*stack_ptr++ = (node == left_child ? right_child : left_child);
#if !defined(__CUDA_ARCH__)
*stack_distance_ptr++ =
(node == left_child ? distance_right : distance_left);
#endif
}
}
} while (node != SENTINEL);
// Sort the leaf nodes and output the results.
// NOTE: Do not try this at home. Messing with the underlying container
// invalidates the state of the PriorityQueue.
sortHeap(heap.data(), heap.data() + heap.size(), heap.valueComp());
for (decltype(heap.size()) i = 0; i < heap.size(); ++i)
{
_callback(predicate,
HappyTreeFriends::getValue(_bvh, (heap.data() + i)->first));
}
}
};
template <class BVH, class Predicates, class Callback>
struct TreeTraversal<BVH, Predicates, Callback,
Experimental::OrderedSpatialPredicateTag>
{
BVH _bvh;
Predicates _predicates;
Callback _callback;
template <class ExecutionSpace>
TreeTraversal(ExecutionSpace const &space, BVH const &bvh,
Predicates const &predicates, Callback const &callback)
: _bvh{bvh}
, _predicates{predicates}
, _callback{callback}
{
if (_bvh.empty())
{
// do nothing
}
else if (_bvh.size() == 1)
{
Kokkos::parallel_for(
"ArborX::Experimental::TreeTraversal::OrderedSpatialPredicate"
"degenerated_one_leaf_tree",
Kokkos::RangePolicy<ExecutionSpace, OneLeafTree>(space, 0,
predicates.size()),
*this);
}
else
{
Kokkos::parallel_for(
"ArborX::Experimental::TreeTraversal::OrderedSpatialPredicate",
Kokkos::RangePolicy<ExecutionSpace, FullTree>(space, 0,
predicates.size()),
*this);
}
}
KOKKOS_FUNCTION TreeTraversal(BVH const &bvh, Callback const &callback)
: _bvh{bvh}
, _callback{callback}
{}
struct OneLeafTree
{};
struct FullTree
{};
KOKKOS_FUNCTION void operator()(OneLeafTree, int queryIndex) const
{
auto const &predicate = _predicates(queryIndex);
auto const root = 0;
auto const &root_bounding_volume =
HappyTreeFriends::getIndexable(_bvh, root);
using distance_type =
decltype(distance(getGeometry(predicate), root_bounding_volume));
constexpr auto inf =
KokkosExt::ArithmeticTraits::infinity<distance_type>::value;
if (distance(getGeometry(predicate), root_bounding_volume) != inf)
{
_callback(predicate, HappyTreeFriends::getValue(_bvh, 0));
}
}
KOKKOS_FUNCTION void operator()(FullTree, int queryIndex) const
{
operator()(_predicates(queryIndex));
}
template <typename Predicate>
KOKKOS_FUNCTION void operator()(Predicate const &predicate) const
{
using ArborX::Details::HappyTreeFriends;
using distance_type = decltype(predicate.distance(
HappyTreeFriends::getInternalBoundingVolume(_bvh, 0)));
using PairIndexDistance = Kokkos::pair<int, distance_type>;
struct CompareDistance
{
KOKKOS_FUNCTION bool operator()(PairIndexDistance const &lhs,
PairIndexDistance const &rhs) const
{
return lhs.second > rhs.second;
}
};
constexpr int buffer_size = 64;
PairIndexDistance buffer[buffer_size];
PriorityQueue<PairIndexDistance, CompareDistance,
UnmanagedStaticVector<PairIndexDistance>>
heap(UnmanagedStaticVector<PairIndexDistance>(buffer, buffer_size));
constexpr auto inf =
KokkosExt::ArithmeticTraits::infinity<distance_type>::value;
auto &bvh = _bvh;
auto const distance = [&predicate, &bvh](int j) {
return HappyTreeFriends::isLeaf(bvh, j)
? predicate.distance(HappyTreeFriends::getIndexable(bvh, j))
: predicate.distance(
HappyTreeFriends::getInternalBoundingVolume(bvh, j));
};
int node = HappyTreeFriends::getRoot(_bvh);
int left_child;
int right_child;
while (true)
{
if (HappyTreeFriends::isLeaf(_bvh, node))
{
if (invoke_callback_and_check_early_exit(
_callback, predicate, HappyTreeFriends::getValue(_bvh, node)))
return;
if (heap.empty())
return;
node = heap.top().first;
heap.pop();
}
else
{
left_child = HappyTreeFriends::getLeftChild(_bvh, node);
right_child = HappyTreeFriends::getRightChild(_bvh, node);
auto const distance_left = distance(left_child);
auto const left_pair = Kokkos::make_pair(left_child, distance_left);
auto const distance_right = distance(right_child);
auto const right_pair = Kokkos::make_pair(right_child, distance_right);
auto const &closer_pair =
distance_left < distance_right ? left_pair : right_pair;
auto const &further_pair =
distance_left < distance_right ? right_pair : left_pair;
if (!heap.empty() && heap.top().second < closer_pair.second)
{
node = heap.top().first;
heap.pop();
if (closer_pair.second < inf)
heap.push(closer_pair);
}
else
node = closer_pair.first;
if (further_pair.second < inf)
heap.push(further_pair);
}
}
}
};
template <typename ExecutionSpace, typename BVH, typename Predicates,
typename Callback>
void traverse(ExecutionSpace const &space, BVH const &bvh,
Predicates const &predicates, Callback const &callback)
{
using Tag = typename Predicates::value_type::Tag;
TreeTraversal<BVH, Predicates, Callback, Tag>(space, bvh, predicates,
callback);
}
} // namespace Details
} // namespace ArborX
#endif