/
union_find.cpp
182 lines (154 loc) · 6.1 KB
/
union_find.cpp
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
/****************************************************************************
* 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 *
****************************************************************************/
#include <ArborX_DetailsKokkosExtStdAlgorithms.hpp>
#include <ArborX_DetailsKokkosExtVersion.hpp>
#include <ArborX_DetailsSortUtils.hpp>
#include <ArborX_DetailsUnionFind.hpp>
#include <ArborX_DetailsUtils.hpp>
#include <ArborX_Version.hpp>
#include <Kokkos_Core.hpp>
#include <Kokkos_Random.hpp>
#include <Kokkos_Timer.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <benchmark/benchmark.h>
struct UnweightedEdge
{
unsigned int source;
unsigned int target;
};
struct AllowLoops
{};
struct DisallowLoops
{};
template <typename ExecutionSpace>
Kokkos::View<UnweightedEdge *, typename ExecutionSpace::memory_space>
buildEdges(AllowLoops, ExecutionSpace const &exec_space, int num_edges)
{
using MemorySpace = typename ExecutionSpace::memory_space;
Kokkos::View<UnweightedEdge *, MemorySpace> edges(
Kokkos::view_alloc(exec_space, Kokkos::WithoutInitializing,
"Benchmark::edges"),
num_edges);
Kokkos::Random_XorShift1024_Pool<ExecutionSpace> rand_pool(1984);
Kokkos::parallel_for(
"Benchmark::init_edges",
Kokkos::RangePolicy<ExecutionSpace>(exec_space, 0, num_edges),
KOKKOS_LAMBDA(unsigned i) {
auto rand_gen = rand_pool.get_state();
do
{
edges(i) = {rand_gen.urand() % num_edges,
rand_gen.urand() % num_edges};
} while (edges(i).source == edges(i).target); // no self loops
rand_pool.free_state(rand_gen);
});
return edges;
}
template <typename ExecutionSpace>
Kokkos::View<UnweightedEdge *, typename ExecutionSpace::memory_space>
buildEdges(DisallowLoops, ExecutionSpace const &exec_space, int num_edges)
{
using MemorySpace = typename ExecutionSpace::memory_space;
Kokkos::Random_XorShift1024_Pool<ExecutionSpace> rand_pool(1984);
// Construct random permutation by sorting a vector of random values
Kokkos::View<int *, MemorySpace> random_values(
Kokkos::view_alloc(exec_space, Kokkos::WithoutInitializing,
"Benchmark::random_values"),
num_edges);
Kokkos::parallel_for(
"Benchmark::init_random_values",
Kokkos::RangePolicy<ExecutionSpace>(exec_space, 0, num_edges),
KOKKOS_LAMBDA(int i) {
auto rand_gen = rand_pool.get_state();
random_values(i) = rand_gen.rand();
rand_pool.free_state(rand_gen);
});
auto permute = ArborX::Details::sortObjects(exec_space, random_values);
// Init edges in a random order
Kokkos::View<UnweightedEdge *, MemorySpace> edges(
Kokkos::view_alloc(exec_space, Kokkos::WithoutInitializing,
"Benchmark::edges"),
num_edges);
Kokkos::parallel_for(
"Benchmark::init_edges",
Kokkos::RangePolicy<ExecutionSpace>(exec_space, 0, num_edges),
KOKKOS_LAMBDA(unsigned i) {
auto rand_gen = rand_pool.get_state();
edges(permute(i)) = {rand_gen.urand() % (i + 1), i + 1};
rand_pool.free_state(rand_gen);
});
return edges;
}
template <typename ExecutionSpace>
auto buildUnionFind(ExecutionSpace const &exec_space, int n)
{
using MemorySpace = typename ExecutionSpace::memory_space;
Kokkos::View<int *, MemorySpace> labels(
Kokkos::view_alloc(Kokkos::WithoutInitializing, "Benchmark::labels"), n);
ArborX::Details::KokkosExt::iota(exec_space, labels);
#ifdef KOKKOS_ENABLE_SERIAL
if constexpr (std::is_same_v<ExecutionSpace, Kokkos::Serial>)
return ArborX::Details::UnionFind<MemorySpace, /*DoSerial*/ true>(labels);
else
#endif
return ArborX::Details::UnionFind<MemorySpace, /*DoSerial*/ false>(labels);
}
template <typename Tag>
void BM_union_find(benchmark::State &state)
{
using ExecutionSpace = Kokkos::DefaultExecutionSpace;
ExecutionSpace exec_space;
auto const num_edges = state.range(0);
auto const n = num_edges + 1;
auto edges = buildEdges(Tag{}, exec_space, num_edges);
auto union_find = buildUnionFind(exec_space, n);
for (auto _ : state)
{
exec_space.fence();
auto const start = std::chrono::high_resolution_clock::now();
Kokkos::parallel_for(
"Benchmark::union-find",
Kokkos::RangePolicy<ExecutionSpace>(exec_space, 0, edges.size()),
KOKKOS_LAMBDA(int e) {
int i = edges(e).source;
int j = edges(e).target;
union_find.merge(i, j);
});
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(
num_edges, benchmark::Counter::kIsIterationInvariantRate);
}
int main(int argc, char *argv[])
{
Kokkos::ScopeGuard guard(argc, argv);
std::cout << "ArborX version : " << ArborX::version() << std::endl;
std::cout << "ArborX hash : " << ArborX::gitCommitHash() << std::endl;
std::cout << "Kokkos version : " << ArborX::Details::KokkosExt::version()
<< std::endl;
benchmark::Initialize(&argc, argv);
BENCHMARK_TEMPLATE1(BM_union_find, AllowLoops)
->RangeMultiplier(10)
->Range(10000, 100000)
->UseManualTime()
->Unit(benchmark::kMicrosecond);
BENCHMARK_TEMPLATE1(BM_union_find, DisallowLoops)
->RangeMultiplier(10)
->Range(10000, 100000)
->UseManualTime()
->Unit(benchmark::kMicrosecond);
benchmark::RunSpecifiedBenchmarks();
return EXIT_SUCCESS;
}