-
Notifications
You must be signed in to change notification settings - Fork 34
/
ArborX_DetailsDistributedTreeNearest.hpp
407 lines (360 loc) · 15.6 KB
/
ArborX_DetailsDistributedTreeNearest.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
/****************************************************************************
* 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_DISTRIBUTED_TREE_NEAREST_HPP
#define ARBORX_DETAILS_DISTRIBUTED_TREE_NEAREST_HPP
#include <ArborX_AccessTraits.hpp>
#include <ArborX_Box.hpp>
#include <ArborX_DetailsDistributedTreeImpl.hpp>
#include <ArborX_DetailsDistributedTreeUtils.hpp>
#include <ArborX_DetailsHappyTreeFriends.hpp>
#include <ArborX_DetailsKokkosExtMinMaxOperations.hpp>
#include <ArborX_DetailsKokkosExtStdAlgorithms.hpp>
#include <ArborX_DetailsKokkosExtViewHelpers.hpp>
#include <ArborX_LinearBVH.hpp>
#include <ArborX_Point.hpp>
#include <ArborX_Predicates.hpp>
#include <ArborX_Ray.hpp>
#include <ArborX_Sphere.hpp>
#include <Kokkos_Core.hpp>
#include <Kokkos_Profiling_ScopedRegion.hpp>
// Don't really need it, but our self containment tests rely on its presence
#include <mpi.h>
namespace ArborX
{
namespace Details
{
template <class Predicates, class Distances>
struct WithinDistanceFromPredicates
{
Predicates predicates;
Distances distances;
};
} // namespace Details
template <class Predicates, class Distances>
struct AccessTraits<
Details::WithinDistanceFromPredicates<Predicates, Distances>, PredicatesTag>
{
using Predicate = typename Predicates::value_type;
using Geometry =
std::decay_t<decltype(getGeometry(std::declval<Predicate const &>()))>;
using Self = Details::WithinDistanceFromPredicates<Predicates, Distances>;
using memory_space = typename Predicates::memory_space;
using size_type = decltype(std::declval<Predicates const &>().size());
static KOKKOS_FUNCTION size_type size(Self const &x)
{
return x.predicates.size();
}
template <class Dummy = Geometry,
std::enable_if_t<std::is_same_v<Dummy, Geometry> &&
std::is_same_v<Dummy, Point>> * = nullptr>
static KOKKOS_FUNCTION auto get(Self const &x, size_type i)
{
auto const point = getGeometry(x.predicates(i));
auto const distance = x.distances(i);
return intersects(Sphere{point, distance});
}
template <class Dummy = Geometry,
std::enable_if_t<std::is_same_v<Dummy, Geometry> &&
std::is_same_v<Dummy, Box>> * = nullptr>
static KOKKOS_FUNCTION auto get(Self const &x, size_type i)
{
auto box = getGeometry(x.predicates(i));
auto &min_corner = box.minCorner();
auto &max_corner = box.maxCorner();
auto const distance = x.distances(i);
for (int d = 0; d < 3; ++d)
{
min_corner[d] -= distance;
max_corner[d] += distance;
}
return intersects(box);
}
template <class Dummy = Geometry,
std::enable_if_t<std::is_same_v<Dummy, Geometry> &&
std::is_same_v<Dummy, Sphere>> * = nullptr>
static KOKKOS_FUNCTION auto get(Self const &x, size_type i)
{
auto const sphere = getGeometry(x.predicates(i));
auto const distance = x.distances(i);
return intersects(Sphere{sphere.centroid(), distance + sphere.radius()});
}
template <
class Dummy = Geometry,
std::enable_if_t<std::is_same_v<Dummy, Geometry> &&
std::is_same_v<Dummy, Experimental::Ray>> * = nullptr>
static KOKKOS_FUNCTION auto get(Self const &x, size_type i)
{
auto const ray = getGeometry(x.predicates(i));
return intersects(ray);
}
};
namespace Details
{
template <typename Value>
struct PairValueDistance
{
Value value;
float distance;
};
template <typename Tree>
struct CallbackWithDistance
{
Tree _tree;
template <typename ExecutionSpace>
CallbackWithDistance(ExecutionSpace const &, Tree const &tree)
: _tree(tree)
{}
template <typename Query, typename Value, typename Output>
KOKKOS_FUNCTION void operator()(Query const &query, Value const &value,
Output const &out) const
{
out({value, distance(getGeometry(query), _tree.indexable_get()(value))});
}
};
template <typename MemorySpace>
struct CallbackWithDistance<BoundingVolumeHierarchy<
MemorySpace, Details::LegacyDefaultTemplateValue,
Details::DefaultIndexableGetter, ExperimentalHyperGeometry::Box<3, float>>>
{
using Tree =
BoundingVolumeHierarchy<MemorySpace, Details::LegacyDefaultTemplateValue,
Details::DefaultIndexableGetter,
ExperimentalHyperGeometry::Box<3, float>>;
Tree _tree;
Kokkos::View<unsigned int *, typename Tree::memory_space> _rev_permute;
template <typename ExecutionSpace>
CallbackWithDistance(ExecutionSpace const &exec_space, Tree const &tree)
: _tree(tree)
{
// NOTE cannot have extended __host__ __device__ lambda in constructor with
// NVCC
computeReversePermutation(exec_space);
}
template <typename ExecutionSpace>
void computeReversePermutation(ExecutionSpace const &exec_space)
{
auto const n = _tree.size();
_rev_permute = Kokkos::View<unsigned int *, typename Tree::memory_space>(
Kokkos::view_alloc(
Kokkos::WithoutInitializing,
"ArborX::DistributedTree::query::nearest::reverse_permutation"),
n);
if (!_tree.empty())
{
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::"
"compute_reverse_permutation",
Kokkos::RangePolicy<ExecutionSpace>(exec_space, 0, n),
KOKKOS_CLASS_LAMBDA(int const i) {
_rev_permute(HappyTreeFriends::getValue(_tree, i).index) = i;
});
}
}
template <typename Query, typename OutputFunctor>
KOKKOS_FUNCTION void operator()(Query const &query, int index,
OutputFunctor const &out) const
{
// TODO: This breaks the abstraction of the distributed Tree not knowing
// the details of the local tree. Right now, this is the only way. Will
// need to be fixed with a proper callback abstraction.
int const leaf_node_index = _rev_permute(index);
auto const &leaf_node_bounding_volume =
HappyTreeFriends::getIndexable(_tree, leaf_node_index);
out({index, distance(getGeometry(query), leaf_node_bounding_volume)});
}
};
template <typename ExecutionSpace, typename Tree, typename Predicates,
typename Indices, typename Offset>
void DistributedTreeImpl::deviseStrategy(ExecutionSpace const &space,
Tree const &tree,
Predicates const &predicates,
Indices &nearest_ranks, Offset &offset)
{
Kokkos::Profiling::ScopedRegion guard(
"ArborX::DistributedTree::query::nearest::deviseStrategy");
using namespace DistributedTree;
using MemorySpace = typename Tree::memory_space;
auto const &top_tree = tree._top_tree;
auto const &bottom_tree_sizes = tree._bottom_tree_sizes;
// Find the k nearest local trees.
top_tree.query(space, predicates, LegacyDefaultCallback{}, nearest_ranks,
offset);
// Accumulate total leave count in the local trees until it reaches k which
// is the number of neighbors queried for. Stop if local trees get
// empty because it means that they are no more leaves and there is no point
// on forwarding predicates to leafless trees.
auto const n_predicates = predicates.size();
Kokkos::View<int *, MemorySpace> new_offset(
Kokkos::view_alloc(space, offset.label()), n_predicates + 1);
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::"
"bottom_trees_with_required_cumulated_leaves_count",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n_predicates),
KOKKOS_LAMBDA(int i) {
int leaves_count = 0;
int const n_nearest_neighbors = getK(predicates(i));
for (int j = offset(i); j < offset(i + 1); ++j)
{
int const bottom_tree_size = bottom_tree_sizes(nearest_ranks(j));
if ((bottom_tree_size == 0) || (leaves_count >= n_nearest_neighbors))
break;
leaves_count += bottom_tree_size;
++new_offset(i);
}
});
KokkosExt::exclusive_scan(space, new_offset, new_offset, 0);
// Truncate results so that predicates will only be forwarded to as many local
// trees as necessary to find k neighbors.
Kokkos::View<int *, MemorySpace> new_nearest_ranks(
Kokkos::view_alloc(space, nearest_ranks.label()),
KokkosExt::lastElement(space, new_offset));
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::truncate_before_forwarding",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n_predicates),
KOKKOS_LAMBDA(int i) {
for (int j = 0; j < new_offset(i + 1) - new_offset(i); ++j)
new_nearest_ranks(new_offset(i) + j) = nearest_ranks(offset(i) + j);
});
offset = new_offset;
nearest_ranks = new_nearest_ranks;
}
template <typename ExecutionSpace, typename Tree, typename Predicates,
typename Distances, typename Indices, typename Offset>
void DistributedTreeImpl::reassessStrategy(
ExecutionSpace const &space, Tree const &tree, Predicates const &queries,
Distances const &distances, Indices &nearest_ranks, Offset &offset)
{
Kokkos::Profiling::ScopedRegion guard(
"ArborX::DistributedTree::query::nearest::reassessStrategy");
using namespace DistributedTree;
using MemorySpace = typename Tree::memory_space;
auto const &top_tree = tree._top_tree;
auto const n_queries = queries.size();
// Determine distance to the farthest neighbor found so far.
Kokkos::View<float *, MemorySpace> farthest_distances(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::DistributedTree::query::nearest::"
"reassessStrategy::distances"),
n_queries);
// NOTE: in principle distances( j ) are arranged in ascending order for
// offset( i ) <= j < offset( i + 1 ) so max() is not necessary.
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::most_distant_neighbor_so_far",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n_queries),
KOKKOS_LAMBDA(int i) {
using KokkosExt::max;
farthest_distances(i) = 0.;
for (int j = offset(i); j < offset(i + 1); ++j)
farthest_distances(i) = max(farthest_distances(i), distances(j));
});
check_valid_access_traits(
PredicatesTag{},
WithinDistanceFromPredicates<Predicates, decltype(farthest_distances)>{
queries, farthest_distances});
top_tree.query(
space,
WithinDistanceFromPredicates<Predicates, decltype(farthest_distances)>{
queries, farthest_distances},
LegacyDefaultCallback{}, nearest_ranks, offset);
// NOTE: in principle, we could perform radius searches on the bottom_tree
// rather than nearest queries.
}
template <typename Tree, typename ExecutionSpace, typename Predicates,
typename Values, typename Offset, typename Ranks>
std::enable_if_t<Kokkos::is_view_v<Values> && Kokkos::is_view_v<Offset> &&
Kokkos::is_view_v<Ranks>>
DistributedTreeImpl::queryDispatchImpl(NearestPredicateTag, Tree const &tree,
ExecutionSpace const &space,
Predicates const &queries,
Values &values, Offset &offset,
Ranks &ranks)
{
Kokkos::Profiling::ScopedRegion guard(
"ArborX::DistributedTree::query::nearest");
if (tree.empty())
{
KokkosExt::reallocWithoutInitializing(space, values, 0);
KokkosExt::reallocWithoutInitializing(space, offset, queries.size() + 1);
Kokkos::deep_copy(space, offset, 0);
return;
}
using namespace DistributedTree;
using MemorySpace = typename Tree::memory_space;
using Value = typename Values::value_type;
auto const &bottom_tree = tree._bottom_tree;
auto comm = tree.getComm();
using Distances = Kokkos::View<float *, MemorySpace>;
Distances distances("ArborX::DistributedTree::query::nearest::distances", 0);
// "Strategy" is used to determine what ranks to forward queries to. In
// the 1st pass, the queries are sent to as many ranks as necessary to
// guarantee that all k neighbors queried for are found. In the 2nd pass,
// queries are sent again to all ranks that may have a neighbor closer to
// the farthest neighbor identified in the 1st pass.
//
// The current implementation discards the results after the 1st pass and
// recompute everything instead of just searching for potential better
// neighbors and updating the list.
CallbackWithDistance<std::decay_t<decltype(bottom_tree)>>
callback_with_distance(space, bottom_tree);
Kokkos::View<int *, MemorySpace> nearest_ranks(
"ArborX::DistributedTree::query::nearest::nearest_ranks", 0);
Kokkos::View<PairValueDistance<Value> *, MemorySpace> out(
"ArborX::DistributedTree::query::nearest::pairs_index_distance", 0);
// Phase I
deviseStrategy(space, tree, queries, nearest_ranks, offset);
forwardQueriesAndCommunicateResults(comm, space, bottom_tree, queries,
callback_with_distance, nearest_ranks,
offset, out, ranks);
// unzip
auto n = out.extent(0);
KokkosExt::reallocWithoutInitializing(space, values, n);
KokkosExt::reallocWithoutInitializing(space, distances, n);
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::"
"split_index_distance_pairs",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n), KOKKOS_LAMBDA(int i) {
values(i) = out(i).value;
distances(i) = out(i).distance;
});
filterResults(space, queries, distances, values, offset, ranks);
// Phase II
reassessStrategy(space, tree, queries, distances, nearest_ranks, offset);
forwardQueriesAndCommunicateResults(comm, space, bottom_tree, queries,
callback_with_distance, nearest_ranks,
offset, out, ranks);
// Unzip
n = out.extent(0);
KokkosExt::reallocWithoutInitializing(space, values, n);
KokkosExt::reallocWithoutInitializing(space, distances, n);
Kokkos::parallel_for(
"ArborX::DistributedTree::query::nearest::"
"split_index_distance_pairs",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n), KOKKOS_LAMBDA(int i) {
values(i) = out(i).value;
distances(i) = out(i).distance;
});
filterResults(space, queries, distances, values, offset, ranks);
}
template <typename Tree, typename ExecutionSpace, typename Predicates,
typename Values, typename Offset>
std::enable_if_t<Kokkos::is_view_v<Values> && Kokkos::is_view_v<Offset>>
DistributedTreeImpl::queryDispatch(NearestPredicateTag tag, Tree const &tree,
ExecutionSpace const &space,
Predicates const &queries, Values &values,
Offset &offset)
{
Kokkos::View<int *, ExecutionSpace> ranks(
"ArborX::DistributedTree::query::nearest::ranks", 0);
queryDispatchImpl(tag, tree, space, queries, values, offset, ranks);
}
} // namespace Details
} // namespace ArborX
#endif