-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgrid_benchmarks.cc
More file actions
179 lines (146 loc) · 7.93 KB
/
grid_benchmarks.cc
File metadata and controls
179 lines (146 loc) · 7.93 KB
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
#include "grid_test_utils.h"
#include "test_utils.h"
#include <tuple>
#include <vector>
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/generators/catch_generators_range.hpp>
using namespace celerity;
using namespace celerity::detail;
TEST_CASE("normalizing randomized box sets - 2d", "[benchmark][group:grid]") {
test_utils::benchmark_thread_pinner pinner;
const auto [label, grid_size, max_box_size, num_boxes] = GENERATE(values<std::tuple<const char*, size_t, size_t, size_t>>({
{"small", 10, 5, 4},
{"medium", 50, 1, 50},
{"large", 200, 20, 200},
}));
const auto input_2d = test_utils::create_random_boxes<2>(grid_size, max_box_size, num_boxes, 42);
BENCHMARK(fmt::format("{}, native", label)) { return grid_detail::normalize(test_utils::copy(input_2d)); };
const auto input_3d = boxes_cast<3>(input_2d);
BENCHMARK(fmt::format("{}, embedded in 3d", label)) { return grid_detail::normalize(test_utils::copy(input_3d)); };
const auto normalized_2d = grid_detail::normalize(test_utils::copy(input_2d));
const auto normalized_3d = grid_detail::normalize(test_utils::copy(input_3d));
CHECK(normalized_3d == boxes_cast<3>(normalized_2d));
test_utils::render_boxes(input_2d, fmt::format("{}-input", label));
test_utils::render_boxes(normalized_2d, fmt::format("{}-normalized", label));
}
TEST_CASE("normalizing randomized box sets - 3d", "[benchmark][group:grid]") {
test_utils::benchmark_thread_pinner pinner;
const auto [label, grid_size, max_box_size, num_boxes] = GENERATE(values<std::tuple<const char*, size_t, size_t, size_t>>({
{"small", 10, 5, 4},
{"medium", 50, 1, 50},
{"large", 200, 20, 200},
}));
const auto input_3d = test_utils::create_random_boxes<3>(grid_size, max_box_size, num_boxes, 42);
BENCHMARK(fmt::format("{} - native", label)) { return grid_detail::normalize(test_utils::copy(input_3d)); };
test_utils::black_hole(grid_detail::normalize(test_utils::copy(input_3d))); // to attach a profiler
}
template <int Dims>
box_vector<Dims> create_box_tiling(const size_t n_per_side) {
const size_t length = 5;
size_t n_linear = 1;
for(int d = 0; d < Dims; ++d) {
n_linear *= n_per_side;
}
box_vector<Dims> boxes(n_linear, box<Dims>());
for(size_t i = 0; i < n_linear; ++i) {
subrange<Dims> sr;
auto dist_i = i;
for(int d = 0; d < Dims; ++d) {
sr.offset[d] = length * (dist_i % n_per_side);
sr.range[d] = length;
dist_i /= n_per_side;
}
boxes[i] = sr;
}
return boxes;
}
TEMPLATE_TEST_CASE_SIG("normalizing a fully mergeable tiling of boxes", "[benchmark][group:grid]", ((int Dims), Dims), 1, 2, 3) {
test_utils::benchmark_thread_pinner pinner;
const auto [label, n] = GENERATE(values<std::tuple<const char*, size_t>>({
{"small", 4},
{"medium", 50},
{"large", 1000},
}));
const size_t n_per_side = llrint(pow(n, 1.0 / Dims));
const auto boxes_nd = create_box_tiling<Dims>(n_per_side);
const auto normalized_nd = grid_detail::normalize(test_utils::copy(boxes_nd));
CHECK(normalized_nd.size() == 1);
BENCHMARK(fmt::format("{}, native", label)) { return grid_detail::normalize(test_utils::copy(boxes_nd)); };
if constexpr(Dims < 3) {
const auto boxes_3d = boxes_cast<3>(boxes_nd);
BENCHMARK(fmt::format("{}, embedded in 3d", label)) { return grid_detail::normalize(test_utils::copy(boxes_3d)); };
}
if constexpr(Dims == 2) {
test_utils::render_boxes(boxes_nd, fmt::format("{}-input", label));
test_utils::render_boxes(normalized_nd, fmt::format("{}-normalized", label));
}
}
TEST_CASE("performing set operations between randomized regions - 2d", "[benchmark][group:grid]") {
test_utils::benchmark_thread_pinner pinner;
const auto [label, grid_size, max_box_size, num_boxes] = GENERATE(values<std::tuple<const char*, size_t, size_t, size_t>>({
{"small", 10, 5, 4},
{"medium", 50, 1, 50},
{"large", 200, 20, 100},
}));
const std::vector inputs_2d{region(test_utils::create_random_boxes<2>(grid_size, max_box_size, num_boxes, 13)),
region(test_utils::create_random_boxes<2>(grid_size, max_box_size, num_boxes, 37))};
const std::vector inputs_3d{region_cast<3>(inputs_2d[0]), region_cast<3>(inputs_2d[1])};
test_utils::render_boxes(inputs_2d[0].get_boxes(), fmt::format("{}-input-a", label));
test_utils::render_boxes(inputs_2d[1].get_boxes(), fmt::format("{}-input-b", label));
BENCHMARK(fmt::format("union, {}, native", label)) { return region_union(inputs_2d[0], inputs_2d[1]); };
BENCHMARK(fmt::format("union, {}, embedded in 3d", label)) { return region_union(inputs_3d[0], inputs_3d[1]); };
BENCHMARK(fmt::format("intersection, {}, native", label)) { return region_intersection(inputs_2d[0], inputs_2d[1]); };
BENCHMARK(fmt::format("intersection, {}, embedded in 3d", label)) { return region_intersection(inputs_3d[0], inputs_3d[1]); };
BENCHMARK(fmt::format("difference, {}, native", label)) { return region_difference(inputs_2d[0], inputs_2d[1]); };
BENCHMARK(fmt::format("difference, {}, embedded in 3d", label)) { return region_difference(inputs_3d[0], inputs_3d[1]); };
const auto union_2d = region_union(inputs_2d[0], inputs_2d[1]);
const auto union_3d = region_union(inputs_3d[0], inputs_3d[1]);
const auto intersection_2d = region_intersection(inputs_2d[0], inputs_2d[1]);
const auto intersection_3d = region_intersection(inputs_3d[0], inputs_3d[1]);
const auto difference_2d = region_difference(inputs_2d[0], inputs_2d[1]);
const auto difference_3d = region_difference(inputs_3d[0], inputs_3d[1]);
CHECK(union_3d == region_cast<3>(union_2d));
CHECK(intersection_3d == region_cast<3>(intersection_2d));
CHECK(difference_3d == region_cast<3>(difference_2d));
test_utils::render_boxes(union_2d.get_boxes(), fmt::format("union-{}", label));
test_utils::render_boxes(intersection_2d.get_boxes(), fmt::format("intersection-{}", label));
test_utils::render_boxes(difference_2d.get_boxes(), fmt::format("difference-{}", label));
}
TEST_CASE("performing set operations between randomized regions - 3d", "[benchmark][group:grid]") {
test_utils::benchmark_thread_pinner pinner;
const auto [label, grid_size, max_box_size, num_boxes] = GENERATE(values<std::tuple<const char*, size_t, size_t, size_t>>({
{"small", 10, 5, 4},
{"medium", 50, 1, 50},
{"large", 200, 20, 100},
}));
const std::vector inputs_3d{region(test_utils::create_random_boxes<3>(grid_size, max_box_size, num_boxes, 13)),
region(test_utils::create_random_boxes<3>(grid_size, max_box_size, num_boxes, 37))};
BENCHMARK(fmt::format("union, {}, native", label)) { return region_union(inputs_3d[0], inputs_3d[1]); };
BENCHMARK(fmt::format("intersection, {}, native", label)) { return region_intersection(inputs_3d[0], inputs_3d[1]); };
BENCHMARK(fmt::format("difference, {}, native", label)) { return region_difference(inputs_3d[0], inputs_3d[1]); };
// to attach a profiler
test_utils::black_hole(region_union(inputs_3d[0], inputs_3d[1]));
test_utils::black_hole(region_intersection(inputs_3d[0], inputs_3d[1]));
test_utils::black_hole(region_difference(inputs_3d[0], inputs_3d[1]));
}
box_vector<2> create_interlocking_boxes(const size_t num_boxes_per_side) {
box_vector<2> boxes;
for(size_t i = 0; i < num_boxes_per_side; ++i) {
boxes.emplace_back(id<2>(i, i), id<2>(i + 1, num_boxes_per_side));
boxes.emplace_back(id<2>(i + 1, i), id<2>(num_boxes_per_side, i + 1));
}
return boxes;
}
TEST_CASE("normalizing a fully mergeable, complex tiling of boxes - 2d", "[benchmark][group:grid]") {
test_utils::benchmark_thread_pinner pinner;
const auto [label, n] = GENERATE(values<std::tuple<const char*, size_t>>({
{"small", 10},
{"large", 200},
}));
const auto boxes_2d = create_interlocking_boxes(n);
const auto boxes_3d = boxes_cast<3>(boxes_2d);
BENCHMARK(fmt::format("{}, native", label)) { return grid_detail::normalize(test_utils::copy(boxes_2d)); };
BENCHMARK(fmt::format("{}, embedded in 3d", label)) { return grid_detail::normalize(test_utils::copy(boxes_3d)); };
test_utils::render_boxes(boxes_2d, fmt::format("{}-input", label));
}