forked from rapidsai/cugraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg_bfs_test.cpp
300 lines (247 loc) · 11.6 KB
/
mg_bfs_test.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
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
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <utilities/base_fixture.hpp>
#include <utilities/device_comm_wrapper.hpp>
#include <utilities/high_res_clock.h>
#include <utilities/mg_utilities.hpp>
#include <utilities/test_graphs.hpp>
#include <utilities/test_utilities.hpp>
#include <utilities/thrust_wrapper.hpp>
#include <cugraph/algorithms.hpp>
#include <cugraph/graph.hpp>
#include <cugraph/graph_functions.hpp>
#include <cugraph/graph_view.hpp>
#include <cugraph/partition_manager.hpp>
#include <raft/comms/comms.hpp>
#include <raft/comms/mpi_comms.hpp>
#include <raft/handle.hpp>
#include <rmm/device_scalar.hpp>
#include <rmm/device_uvector.hpp>
#include <gtest/gtest.h>
#include <random>
struct BFS_Usecase {
size_t source{0};
bool check_correctness{true};
};
template <typename input_usecase_t>
class Tests_MGBFS : public ::testing::TestWithParam<std::tuple<BFS_Usecase, input_usecase_t>> {
public:
Tests_MGBFS() {}
static void SetUpTestCase() { handle_ = cugraph::test::initialize_mg_handle(); }
static void TearDownTestCase() { handle_.reset(); }
virtual void SetUp() {}
virtual void TearDown() {}
// Compare the results of running BFS on multiple GPUs to that of a single-GPU run
template <typename vertex_t, typename edge_t>
void run_current_test(BFS_Usecase const& bfs_usecase, input_usecase_t const& input_usecase)
{
using weight_t = float;
HighResClock hr_clock{};
// 1. create MG graph
if (cugraph::test::g_perf) {
RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement
handle_->get_comms().barrier();
hr_clock.start();
}
auto [mg_graph, d_mg_renumber_map_labels] =
cugraph::test::construct_graph<vertex_t, edge_t, weight_t, false, true>(
*handle_, input_usecase, false, true);
if (cugraph::test::g_perf) {
RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement
handle_->get_comms().barrier();
double elapsed_time{0.0};
hr_clock.stop(&elapsed_time);
std::cout << "MG construct_graph took " << elapsed_time * 1e-6 << " s.\n";
}
auto mg_graph_view = mg_graph.view();
ASSERT_TRUE(static_cast<vertex_t>(bfs_usecase.source) >= 0 &&
static_cast<vertex_t>(bfs_usecase.source) < mg_graph_view.number_of_vertices())
<< "Invalid starting source.";
// 2. run MG BFS
rmm::device_uvector<vertex_t> d_mg_distances(mg_graph_view.local_vertex_partition_range_size(),
handle_->get_stream());
rmm::device_uvector<vertex_t> d_mg_predecessors(
mg_graph_view.local_vertex_partition_range_size(), handle_->get_stream());
if (cugraph::test::g_perf) {
RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement
handle_->get_comms().barrier();
hr_clock.start();
}
auto const d_mg_source =
mg_graph_view.in_local_vertex_partition_range_nocheck(bfs_usecase.source)
? std::make_optional<rmm::device_scalar<vertex_t>>(bfs_usecase.source,
handle_->get_stream())
: std::nullopt;
cugraph::bfs(*handle_,
mg_graph_view,
d_mg_distances.data(),
d_mg_predecessors.data(),
d_mg_source ? (*d_mg_source).data() : static_cast<vertex_t const*>(nullptr),
d_mg_source ? size_t{1} : size_t{0},
false,
std::numeric_limits<vertex_t>::max());
if (cugraph::test::g_perf) {
RAFT_CUDA_TRY(cudaDeviceSynchronize()); // for consistent performance measurement
handle_->get_comms().barrier();
double elapsed_time{0.0};
hr_clock.stop(&elapsed_time);
std::cout << "MG BFS took " << elapsed_time * 1e-6 << " s.\n";
}
// 3. compare SG & MG results
if (bfs_usecase.check_correctness) {
// 3-1. aggregate MG results
auto d_mg_aggregate_renumber_map_labels = cugraph::test::device_gatherv(
*handle_, (*d_mg_renumber_map_labels).data(), (*d_mg_renumber_map_labels).size());
auto d_mg_aggregate_distances =
cugraph::test::device_gatherv(*handle_, d_mg_distances.data(), d_mg_distances.size());
auto d_mg_aggregate_predecessors =
cugraph::test::device_gatherv(*handle_, d_mg_predecessors.data(), d_mg_predecessors.size());
if (handle_->get_comms().get_rank() == int{0}) {
// 3-2. unrenumbr MG results
cugraph::unrenumber_int_vertices<vertex_t, false>(
*handle_,
d_mg_aggregate_predecessors.data(),
d_mg_aggregate_predecessors.size(),
d_mg_aggregate_renumber_map_labels.data(),
std::vector<vertex_t>{mg_graph_view.number_of_vertices()});
std::tie(std::ignore, d_mg_aggregate_distances) = cugraph::test::sort_by_key(
*handle_, d_mg_aggregate_renumber_map_labels, d_mg_aggregate_distances);
std::tie(std::ignore, d_mg_aggregate_predecessors) = cugraph::test::sort_by_key(
*handle_, d_mg_aggregate_renumber_map_labels, d_mg_aggregate_predecessors);
// 3-3. create SG graph
cugraph::graph_t<vertex_t, edge_t, weight_t, false, false> sg_graph(*handle_);
std::tie(sg_graph, std::ignore) =
cugraph::test::construct_graph<vertex_t, edge_t, weight_t, false, false>(
*handle_, input_usecase, false, false);
auto sg_graph_view = sg_graph.view();
ASSERT_TRUE(mg_graph_view.number_of_vertices() == sg_graph_view.number_of_vertices());
// 3-4. run SG BFS
rmm::device_uvector<vertex_t> d_sg_distances(sg_graph_view.number_of_vertices(),
handle_->get_stream());
rmm::device_uvector<vertex_t> d_sg_predecessors(
sg_graph_view.local_vertex_partition_range_size(), handle_->get_stream());
vertex_t unrenumbered_source{};
raft::update_host(&unrenumbered_source,
d_mg_aggregate_renumber_map_labels.data() + bfs_usecase.source,
size_t{1},
handle_->get_stream());
handle_->sync_stream();
rmm::device_scalar<vertex_t> const d_sg_source(unrenumbered_source, handle_->get_stream());
cugraph::bfs(*handle_,
sg_graph_view,
d_sg_distances.data(),
d_sg_predecessors.data(),
d_sg_source.data(),
size_t{1},
false,
std::numeric_limits<vertex_t>::max());
// 3-5. compare
std::vector<edge_t> h_sg_offsets =
cugraph::test::to_host(*handle_, sg_graph_view.local_edge_partition_view().offsets());
std::vector<vertex_t> h_sg_indices =
cugraph::test::to_host(*handle_, sg_graph_view.local_edge_partition_view().indices());
std::vector<vertex_t> h_mg_aggregate_distances =
cugraph::test::to_host(*handle_, d_mg_aggregate_distances);
std::vector<vertex_t> h_mg_aggregate_predecessors =
cugraph::test::to_host(*handle_, d_mg_aggregate_predecessors);
std::vector<vertex_t> h_sg_distances = cugraph::test::to_host(*handle_, d_sg_distances);
std::vector<vertex_t> h_sg_predecessors =
cugraph::test::to_host(*handle_, d_sg_predecessors);
ASSERT_TRUE(std::equal(h_mg_aggregate_distances.begin(),
h_mg_aggregate_distances.end(),
h_sg_distances.begin()));
for (size_t i = 0; i < h_mg_aggregate_predecessors.size(); ++i) {
if (h_mg_aggregate_predecessors[i] == cugraph::invalid_vertex_id<vertex_t>::value) {
ASSERT_TRUE(h_sg_predecessors[i] == h_mg_aggregate_predecessors[i])
<< "vertex reachability does not match with the SG result.";
} else {
ASSERT_TRUE(h_sg_distances[h_mg_aggregate_predecessors[i]] + 1 == h_sg_distances[i])
<< "distances to this vertex != distances to the predecessor vertex + 1.";
bool found{false};
for (auto j = h_sg_offsets[h_mg_aggregate_predecessors[i]];
j < h_sg_offsets[h_mg_aggregate_predecessors[i] + 1];
++j) {
if (h_sg_indices[j] == i) {
found = true;
break;
}
}
ASSERT_TRUE(found) << "no edge from the predecessor vertex to this vertex.";
}
}
}
}
}
private:
static std::unique_ptr<raft::handle_t> handle_;
};
template <typename input_usecase_t>
std::unique_ptr<raft::handle_t> Tests_MGBFS<input_usecase_t>::handle_ = nullptr;
using Tests_MGBFS_File = Tests_MGBFS<cugraph::test::File_Usecase>;
using Tests_MGBFS_Rmat = Tests_MGBFS<cugraph::test::Rmat_Usecase>;
TEST_P(Tests_MGBFS_File, CheckInt32Int32)
{
auto param = GetParam();
run_current_test<int32_t, int32_t>(std::get<0>(param), std::get<1>(param));
}
TEST_P(Tests_MGBFS_Rmat, CheckInt32Int32)
{
auto param = GetParam();
run_current_test<int32_t, int32_t>(
std::get<0>(param), override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param)));
}
TEST_P(Tests_MGBFS_Rmat, CheckInt32Int64)
{
auto param = GetParam();
run_current_test<int32_t, int64_t>(
std::get<0>(param), override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param)));
}
TEST_P(Tests_MGBFS_Rmat, CheckInt64Int64)
{
auto param = GetParam();
run_current_test<int64_t, int64_t>(
std::get<0>(param), override_Rmat_Usecase_with_cmd_line_arguments(std::get<1>(param)));
}
INSTANTIATE_TEST_SUITE_P(
file_test,
Tests_MGBFS_File,
::testing::Combine(
// enable correctness checks
::testing::Values(BFS_Usecase{0}),
::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"),
cugraph::test::File_Usecase("test/datasets/web-Google.mtx"),
cugraph::test::File_Usecase("test/datasets/ljournal-2008.mtx"),
cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx"))));
INSTANTIATE_TEST_SUITE_P(rmat_small_test,
Tests_MGBFS_Rmat,
::testing::Values(
// enable correctness checks
std::make_tuple(BFS_Usecase{0},
cugraph::test::Rmat_Usecase(
10, 16, 0.57, 0.19, 0.19, 0, false, false, 0, true))));
INSTANTIATE_TEST_SUITE_P(
rmat_benchmark_test, /* note that scale & edge factor can be overridden in benchmarking (with
--gtest_filter to select only the rmat_benchmark_test with a specific
vertex & edge type combination) by command line arguments and do not
include more than one Rmat_Usecase that differ only in scale or edge
factor (to avoid running same benchmarks more than once) */
Tests_MGBFS_Rmat,
::testing::Values(
// disable correctness checks for large graphs
std::make_tuple(
BFS_Usecase{0, false},
cugraph::test::Rmat_Usecase(20, 32, 0.57, 0.19, 0.19, 0, false, false, 0, true))));
CUGRAPH_MG_TEST_PROGRAM_MAIN()