forked from pnnl/NWGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.hpp
254 lines (222 loc) · 7.92 KB
/
common.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
/**
* @file common.hpp
*
* @copyright SPDX-FileCopyrightText: 2022 Battelle Memorial Institute
* @copyright SPDX-FileCopyrightText: 2022 University of Washington
*
* SPDX-License-Identifier: BSD-3-Clause
*
* @authors
* Andrew Lumsdaine
* Kevin Deweese
* Tony Liu
* liux238
*
*/
#ifndef NW_GRAPH_BENCH_COMMON_HPP
#define NW_GRAPH_BENCH_COMMON_HPP
#include "nwgraph/adaptors/edge_range.hpp"
#include "nwgraph/adjacency.hpp"
#include "nwgraph/edge_list.hpp"
#include "nwgraph/graph_base.hpp"
#include "nwgraph/graph_traits.hpp"
#include "nwgraph/io/mmio.hpp"
#include "nwgraph/util/timer.hpp"
#include "nwgraph/util/traits.hpp"
#include <iomanip>
#include <map>
#include <random>
#include <string>
#include <tbb/global_control.h>
#include <tuple>
#include <vector>
namespace nw::graph {
namespace bench {
constexpr inline bool WITH_TBB = true;
auto set_n_threads(long n) {
if constexpr (WITH_TBB) {
return tbb::global_control(tbb::global_control::max_allowed_parallelism, n);
} else {
return 0;
}
}
long get_n_threads() {
if constexpr (WITH_TBB) {
return tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism);
} else {
return 1;
}
}
std::vector<long> parse_n_threads(const std::vector<std::string>& args) {
std::vector<long> threads;
if constexpr (WITH_TBB) {
if (args.size() == 0) {
threads.push_back(tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism));
} else {
for (auto&& n : args) {
threads.push_back(std::stol(n));
}
}
} else {
threads.push_back(1);
}
return threads;
}
std::vector<long> parse_ids(const std::vector<std::string>& args) {
std::vector<long> ids;
for (auto&& n : args) {
ids.push_back(std::stol(n));
}
return ids;
}
template <directedness Directedness, class... Attributes>
edge_list<Directedness, Attributes...> load_graph(std::string file) {
std::ifstream in(file);
std::string type;
in >> type;
if (type == "NW") {
nw::util::life_timer _("deserialize");
edge_list<Directedness, Attributes...> aos_a(0);
aos_a.deserialize(file);
return aos_a;
} else if (type == "%%MatrixMarket") {
std::cout << "Reading matrix market input " << file << " (slow)\n";
nw::util::life_timer _("read mm");
return read_mm<Directedness, Attributes...>(file);
} else {
std::cerr << "Did not recognize graph input file " << file << "\n";
exit(1);
}
}
template <int Adj, class ExecutionPolicy = std::execution::parallel_unsequenced_policy, directedness Directedness, class... Attributes>
adjacency<Adj, Attributes...> build_adjacency(edge_list<Directedness, Attributes...>& graph, bool sort_adjacency = false, ExecutionPolicy&& policy = {}) {
nw::util::life_timer _("build adjacency");
return {graph, sort_adjacency, policy};
}
template <class Graph>
auto build_degrees(const Graph& graph) {
using Id = typename nw::graph::vertex_id_t<std::decay_t<Graph>>;
nw::util::life_timer _("degrees");
std::vector<Id> degrees(graph.size());
tbb::parallel_for(edge_range(graph), [&](auto&& edges) {
for (auto&& [i, j] : edges) {
__atomic_fetch_add(°rees[j], 1, __ATOMIC_ACQ_REL);
}
});
return degrees;
}
template <class Graph>
auto build_random_sources(const Graph& graph, size_t n, long seed) {
using Id = typename nw::graph::vertex_id_t<std::decay_t<Graph>>;
auto sources = std::vector<Id>(n);
auto degrees = build_degrees(graph);
auto gen = std::mt19937(seed);
auto dis = std::uniform_int_distribution<Id>(0, num_vertices(graph));
for (auto& id : sources) {
for (id = dis(gen); degrees[id] == 0; id = dis(gen)) {
}
}
return sources;
}
/// Load a set of vertices from a file.
///
/// This will load a set of vertices from the passed `file` and verify that we
/// have the expected number `n`.
template <class Graph>
auto load_sources_from_file(const Graph&, std::string file, size_t n = 0) {
using Id = typename nw::graph::vertex_id_t<std::decay_t<Graph>>;
std::vector sources = read_mm_vector<Id>(file);
if (n && sources.size() != n) {
std::cerr << file << " contains " << sources.size() << " sources, however options require " << n << "\n";
exit(1);
}
return sources;
}
/// Helper to time an operation.
template <class Op>
auto time_op(Op&& op) {
if constexpr (std::is_void_v<decltype(op())>) {
auto start = std::chrono::high_resolution_clock::now();
op();
std::chrono::duration<double> end = std::chrono::high_resolution_clock::now() - start;
return std::tuple{end.count()};
} else {
auto start = std::chrono::high_resolution_clock::now();
auto e = op();
std::chrono::duration<double> end = std::chrono::high_resolution_clock::now() - start;
return std::tuple{end.count(), std::move(e)};
}
}
template <class Op, class Check>
auto time_op_verify(Op&& op, Check&& check) {
auto&& [t, e] = time_op(std::forward<Op>(op));
return std::tuple(t, check(std::move(e)));
}
template <class... Extra>
class Times {
using Sample = std::tuple<double, Extra...>;
std::map<std::tuple<std::string, long, long>, std::vector<Sample>> times_ = {};
public:
decltype(auto) begin() const { return times_.begin(); }
decltype(auto) end() const { return times_.end(); }
template <class Op>
auto record(const std::string& file, long id, long thread, Op&& op, Extra... extra) {
return std::apply(
[&](auto time, auto&&... rest) {
append(file, id, thread, time, extra...);
return std::tuple{std::forward<decltype(rest)>(rest)...};
},
time_op(std::forward<Op>(op)));
}
template <class Op, class Verify>
void record(const std::string& file, long id, long thread, Op&& op, Verify&& verify, Extra... extra) {
auto&& [time, result] = time_op(std::forward<Op>(op));
verify(std::forward<decltype(result)>(result));
append(file, id, thread, time, extra...);
}
void append(std::string file, long id, long thread, double trial, Extra... extra) {
times_[std::tuple(file, id, thread)].emplace_back(trial, extra...);
}
void print(std::ostream& out) const {
std::size_t n = 4;
for (auto&& [config, samples] : times_) {
n = std::max(n, std::get<0>(config).size());
}
out << std::setw(n + 2) << std::left << "File";
out << std::setw(10) << std::left << "Version";
out << std::setw(10) << std::left << "Threads";
out << std::setw(20) << std::left << "Min";
out << std::setw(20) << std::left << "Avg";
out << std::setw(20) << std::left << "Max";
out << "\n";
for (auto&& [config, samples] : times_) {
auto [file, id, threads] = config;
auto [min, max, avg] = minmaxavg(samples);
out << std::setw(n + 2) << std::left << file;
out << std::setw(10) << std::left << id;
out << std::setw(10) << std::left << threads;
out << std::setw(20) << std::left << std::setprecision(6) << std::fixed << min;
out << std::setw(20) << std::left << std::setprecision(6) << std::fixed << avg;
out << std::setw(20) << std::left << std::setprecision(6) << std::fixed << max;
out << "\n";
}
}
private:
static auto average(const std::vector<Sample>& times) {
double total = 0.0;
for (auto&& sample : times) {
total += std::get<0>(sample);
}
return total / times.size();
}
static auto minmax(const std::vector<Sample>& times) {
return std::apply([](auto... minmax) { return std::tuple(std::get<0>(*minmax)...); },
std::minmax_element(times.begin(), times.end(), [](auto&& a, auto&& b) { return std::get<0>(a) < std::get<0>(b); }));
}
static auto minmaxavg(const std::vector<Sample>& times) {
return std::apply([&](auto... minmax) { return std::tuple(minmax..., average(times)); }, minmax(times));
}
};
} // namespace bench
} // namespace nw::graph
#endif // NW_GRAPH_BENCH_COMMON_HPP