/
ArborX_MinimumSpanningTree.hpp
293 lines (245 loc) · 11.1 KB
/
ArborX_MinimumSpanningTree.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
/****************************************************************************
* 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_MINIMUM_SPANNING_TREE_HPP
#define ARBORX_MINIMUM_SPANNING_TREE_HPP
#include <ArborX_AccessTraits.hpp>
#include <ArborX_DetailsKokkosExtViewHelpers.hpp>
#include <ArborX_DetailsMinimumSpanningTree.hpp>
#include <ArborX_DetailsMutualReachabilityDistance.hpp>
#include <ArborX_DetailsTreeNodeLabeling.hpp>
#include <ArborX_DetailsUtils.hpp>
#include <ArborX_DetailsWeightedEdge.hpp>
#include <ArborX_LinearBVH.hpp>
#include <Kokkos_Core.hpp>
#include <Kokkos_Profiling_ScopedRegion.hpp>
namespace ArborX::Details
{
template <class MemorySpace, BoruvkaMode Mode = BoruvkaMode::MST>
struct MinimumSpanningTree
{
using memory_space = MemorySpace;
static_assert(Kokkos::is_memory_space<MemorySpace>::value);
Kokkos::View<WeightedEdge *, MemorySpace> edges;
Kokkos::View<int *, MemorySpace> dendrogram_parents;
Kokkos::View<float *, MemorySpace> dendrogram_parent_heights;
template <class ExecutionSpace, class Primitives>
MinimumSpanningTree(ExecutionSpace const &space, Primitives const &primitives,
int k = 1)
: edges(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::edges"),
AccessTraits<Primitives, PrimitivesTag>::size(primitives) - 1)
, dendrogram_parents("ArborX::MST::dendrogram_parents", 0)
, dendrogram_parent_heights("ArborX::MST::dendrogram_parent_heights", 0)
{
Kokkos::Profiling::pushRegion("ArborX::MST::MST");
using Points = Details::AccessValues<Primitives, PrimitivesTag>;
using Point = typename Points::value_type;
static_assert(GeometryTraits::is_point<Point>{});
Points points{primitives};
auto const n = points.size();
Kokkos::Profiling::pushRegion("ArborX::MST::construction");
BoundingVolumeHierarchy<MemorySpace, PairIndexVolume<Point>> bvh(
space, Details::LegacyValues<Points, Point>{points});
Kokkos::Profiling::popRegion();
if (k > 1)
{
Kokkos::Profiling::pushRegion("ArborX::MST::compute_core_distances");
Kokkos::View<float *, MemorySpace> core_distances(
"ArborX::MST::core_distances", n);
bvh.query(space, NearestK<Points>{points, k},
MaxDistance<Points, decltype(core_distances)>{points,
core_distances});
Kokkos::Profiling::popRegion();
MutualReachability<decltype(core_distances)> mutual_reachability{
core_distances};
Kokkos::Profiling::pushRegion("ArborX::MST::boruvka");
doBoruvka(space, bvh, mutual_reachability);
Kokkos::Profiling::popRegion();
}
else
{
Kokkos::Profiling::pushRegion("ArborX::MST::boruvka");
doBoruvka(space, bvh, Euclidean{});
Kokkos::Profiling::popRegion();
}
finalizeEdges(space, bvh, edges);
Kokkos::Profiling::popRegion();
}
// enclosing function for an extended __host__ __device__ lambda cannot have
// private or protected access within its class
#ifndef KOKKOS_COMPILER_NVCC
private:
#endif
template <class ExecutionSpace, class BVH, class Metric>
void doBoruvka(ExecutionSpace const &space, BVH const &bvh,
Metric const &metric)
{
auto const n = bvh.size();
Kokkos::View<int *, MemorySpace> tree_parents(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::tree_parents"),
2 * n - 1);
findParents(space, bvh, tree_parents);
Kokkos::Profiling::pushRegion("ArborX::MST::initialize_node_labels");
Kokkos::View<int *, MemorySpace> labels(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::labels"),
2 * n - 1);
iota(space, Kokkos::subview(labels, std::make_pair((decltype(n))0, n)));
Kokkos::Profiling::popRegion();
Kokkos::View<DirectedEdge *, MemorySpace> component_out_edges(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::component_out_edges"),
n);
Kokkos::View<float *, MemorySpace> weights(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::weights"),
n);
Kokkos::View<float *, MemorySpace> radii(
Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::MST::radii"),
n);
Kokkos::View<float *, MemorySpace> lower_bounds("ArborX::MST::lower_bounds",
0);
constexpr bool use_lower_bounds =
#ifdef KOKKOS_ENABLE_SERIAL
std::is_same<ExecutionSpace, Kokkos::Serial>::value;
#else
false;
#endif
// Shared radii may or may not be faster for CUDA depending on the problem.
// In the ICPP'51 paper experiments, we ended up using it only in Serial.
// But we would like to keep an option open for the future, so the code is
// written to be able to run it if we want.
constexpr bool use_shared_radii =
#ifdef KOKKOS_ENABLE_SERIAL
std::is_same<ExecutionSpace, Kokkos::Serial>::value;
#else
false;
#endif
if constexpr (use_lower_bounds)
{
KokkosExt::reallocWithoutInitializing(space, lower_bounds, n);
Kokkos::deep_copy(space, lower_bounds, 0);
}
Kokkos::Profiling::pushRegion("ArborX::MST::Boruvka_loop");
Kokkos::View<int, MemorySpace> num_edges(
Kokkos::view_alloc(space, "ArborX::MST::num_edges")); // initialize to 0
Kokkos::View<int *, MemorySpace> edges_mapping("ArborX::MST::edges_mapping",
0);
Kokkos::View<int *, MemorySpace> sided_parents("ArborX::MST::sided_parents",
0);
if constexpr (Mode == BoruvkaMode::HDBSCAN)
{
KokkosExt::reallocWithoutInitializing(space, edges_mapping, n - 1);
KokkosExt::reallocWithoutInitializing(space, sided_parents, n - 1);
KokkosExt::reallocWithoutInitializing(space, dendrogram_parents,
2 * n - 1);
}
// Boruvka iterations
int iterations = 0;
int num_components = n;
[[maybe_unused]] int edges_start = 0;
[[maybe_unused]] int edges_end = 0;
do
{
Kokkos::Profiling::pushRegion("ArborX::Boruvka_" +
std::to_string(++iterations) + "_" +
std::to_string(num_components));
// Propagate leaf node labels to internal nodes
reduceLabels(space, tree_parents, labels);
constexpr auto inf = KokkosExt::ArithmeticTraits::infinity<float>::value;
constexpr DirectedEdge uninitialized_edge;
Kokkos::deep_copy(space, component_out_edges, uninitialized_edge);
Kokkos::deep_copy(space, weights, inf);
Kokkos::deep_copy(space, radii, inf);
resetSharedRadii(space, bvh, labels, metric, radii);
FindComponentNearestNeighbors(
space, bvh, labels, weights, component_out_edges, metric, radii,
lower_bounds, std::bool_constant<use_shared_radii>());
retrieveEdges(space, labels, weights, component_out_edges);
if constexpr (use_lower_bounds)
{
updateLowerBounds(space, labels, component_out_edges, lower_bounds);
}
UpdateComponentsAndEdges<decltype(labels), decltype(component_out_edges),
decltype(edges), decltype(edges_mapping),
decltype(num_edges), Mode>
f{labels, component_out_edges, edges, edges_mapping, num_edges};
// For every component C and a found shortest edge `(u, w)`, add the
// edge to the list of MST edges.
Kokkos::parallel_for(
"ArborX::MST::update_unidirectional_edges",
Kokkos::RangePolicy<ExecutionSpace, UnidirectionalEdgesTag>(space, 0,
n),
f);
int num_edges_host;
Kokkos::deep_copy(space, num_edges_host, num_edges);
space.fence();
if constexpr (Mode == BoruvkaMode::HDBSCAN)
{
Kokkos::parallel_for(
"ArborX::MST::update_bidirectional_edges",
Kokkos::RangePolicy<ExecutionSpace, BidirectionalEdgesTag>(space, 0,
n),
f);
if (iterations > 1)
updateSidedParents(space, labels, edges, edges_mapping, sided_parents,
edges_start, edges_end);
else
{
Kokkos::Profiling::ScopedRegion guard(
"ArborX::MST::compute_vertex_parents");
assignVertexParents(space, labels, component_out_edges, edges_mapping,
bvh, dendrogram_parents);
}
}
// For every component C and a found shortest edge `(u, w)`, merge C
// with the component that w belongs to by updating the labels
Kokkos::parallel_for(
"ArborX::MST::update_labels",
Kokkos::RangePolicy<ExecutionSpace, LabelsTag>(space, 0, n), f);
num_components = static_cast<int>(n) - num_edges_host;
edges_start = edges_end;
edges_end = num_edges_host;
Kokkos::Profiling::popRegion();
} while (num_components > 1);
// Deallocate some memory to reduce high water mark
Kokkos::resize(edges_mapping, 0);
Kokkos::resize(lower_bounds, 0);
Kokkos::resize(radii, 0);
Kokkos::resize(labels, 0);
Kokkos::resize(weights, 0);
Kokkos::resize(component_out_edges, 0);
Kokkos::resize(tree_parents, 0);
if constexpr (Mode == BoruvkaMode::HDBSCAN)
{
// Done with the recursion as there are no more alpha edges. Assign
// all current edges to the root chain.
Kokkos::deep_copy(space,
Kokkos::subview(sided_parents,
std::make_pair(edges_start, edges_end)),
ROOT_CHAIN_VALUE);
computeParents(space, edges, sided_parents, dendrogram_parents);
KokkosExt::reallocWithoutInitializing(space, dendrogram_parent_heights,
n - 1);
Kokkos::parallel_for(
"ArborX::MST::assign_dendrogram_parent_heights",
Kokkos::RangePolicy<ExecutionSpace>(space, 0, n - 1),
KOKKOS_CLASS_LAMBDA(int const e) {
dendrogram_parent_heights(e) = edges(e).weight;
});
}
Kokkos::Profiling::popRegion();
}
};
} // namespace ArborX::Details
#endif