-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark_parallel.cpp
More file actions
235 lines (189 loc) · 7.25 KB
/
benchmark_parallel.cpp
File metadata and controls
235 lines (189 loc) · 7.25 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
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
// Phase 0: Parallel/Multithreaded Benchmark
// Measures thread scaling for parallel construction
#include "workloads.h"
#include "benchmark_utils.h"
#include <iostream>
#include <vector>
#include <array>
#include <thread>
#include <algorithm>
#include <unistd.h>
// Simple BB class
template <int D = 2>
class BB {
private:
float values[2 * D];
public:
BB() {
for (int i = 0; i < 2 * D; i++) values[i] = 0.0f;
}
BB(const float *minima, const float *maxima) {
for (int i = 0; i < D; i++) {
values[i] = minima[i];
values[i + D] = maxima[i];
}
}
inline float min(int i) const { return values[i]; }
inline float max(int i) const { return values[i + D]; }
};
// Simple DataType class
// Phase 7: Thread-local buffers eliminate need for alignas(64)
template <class T, int D = 2>
class DataType {
public:
T first;
BB<D> second;
DataType() = default;
DataType(const T &f, const BB<D> &s) : first(f), second(s) {}
};
// Parallel construction benchmark
template<typename T, int D>
class ParallelPRTreeBenchmark {
public:
using BBox = std::array<float, D * 2>;
using Data = DataType<T, D>;
void construct_parallel(const std::vector<BBox>& data, size_t n_threads) {
elements_.clear();
// Phase 7: Use thread-local buffers to eliminate contention
const size_t chunk_size = (data.size() + n_threads - 1) / n_threads;
std::vector<std::vector<Data>> thread_local_buffers(n_threads);
std::vector<std::thread> threads;
// Phase 1: Parallel data conversion (thread-local)
for (size_t t = 0; t < n_threads; ++t) {
threads.emplace_back([&, t]() {
size_t start = t * chunk_size;
size_t end = std::min(start + chunk_size, data.size());
// Reserve space in thread-local buffer
auto& local_buffer = thread_local_buffers[t];
local_buffer.reserve(end - start);
for (size_t i = start; i < end; ++i) {
float minima[D], maxima[D];
for (int d = 0; d < D; ++d) {
minima[d] = data[i][d];
maxima[d] = data[i][d + D];
}
BB<D> bb(minima, maxima);
// Write to thread-local buffer (no contention!)
local_buffer.emplace_back(static_cast<T>(i), bb);
}
});
}
for (auto& thread : threads) {
thread.join();
}
// Phase 2: Merge thread-local buffers
size_t total_size = 0;
for (const auto& buffer : thread_local_buffers) {
total_size += buffer.size();
}
elements_.reserve(total_size);
for (auto& buffer : thread_local_buffers) {
elements_.insert(elements_.end(),
std::make_move_iterator(buffer.begin()),
std::make_move_iterator(buffer.end()));
}
// Sort phase (single-threaded for simplicity)
std::sort(elements_.begin(), elements_.end(),
[](const Data& a, const Data& b) {
return a.second.min(0) < b.second.min(0);
});
// Simulate tree building
build_tree_structure();
}
size_t size() const { return elements_.size(); }
private:
std::vector<Data> elements_;
void build_tree_structure() {
if (elements_.size() <= 6) return;
float total = 0.0f;
for (const auto& elem : elements_) {
for (int d = 0; d < D; ++d) {
total += elem.second.min(d) + elem.second.max(d);
}
}
if (total < 0) std::cout << total;
}
};
void run_parallel_benchmark(const benchmark::WorkloadConfig& config,
size_t n_threads,
benchmark::BenchmarkReporter& reporter) {
std::cout << "\n" << std::string(60, '-') << "\n";
std::cout << "Threads: " << n_threads << "\n";
// Generate data
benchmark::DataGenerator<2> generator;
auto data = generator.generate(config);
// Benchmark parallel construction
ParallelPRTreeBenchmark<int64_t, 2> tree;
benchmark::Timer timer;
tree.construct_parallel(data, n_threads);
double elapsed_ms = timer.elapsed_ms();
// Calculate throughput
double throughput = (config.n_elements / elapsed_ms) * 1000.0;
std::cout << "Time: " << elapsed_ms << " ms\n";
std::cout << "Throughput: " << throughput << " elements/sec\n";
// Record results
benchmark::BenchmarkResult result;
result.workload_name = config.name + "_threads_" + std::to_string(n_threads);
result.operation = "parallel_construction";
result.n_elements = config.n_elements;
result.n_queries = 0;
result.time_ms = elapsed_ms;
result.throughput = throughput;
result.memory_bytes = 0;
reporter.add_result(result);
}
int main(int argc, char** argv) {
std::cout << "PRTree Phase 0: Parallel/Thread Scaling Benchmark\n";
std::cout << "==================================================\n\n";
benchmark::BenchmarkReporter reporter;
// Use large_uniform workload for thread scaling
auto workloads = benchmark::get_standard_workloads();
auto it = std::find_if(workloads.begin(), workloads.end(),
[](const auto& w) { return w.name == "large_uniform"; });
if (it == workloads.end()) {
std::cerr << "large_uniform workload not found\n";
return 1;
}
const auto& config = *it;
// Thread counts to test
std::vector<size_t> thread_counts = {1, 2, 4, 8};
size_t hw_threads = std::thread::hardware_concurrency();
if (hw_threads > 8) {
thread_counts.push_back(16);
}
std::cout << "Workload: " << config.name << "\n";
std::cout << "Elements: " << config.n_elements << "\n";
std::cout << "Hardware threads: " << hw_threads << "\n\n";
// Baseline (single-threaded)
double baseline_time = 0.0;
for (size_t n_threads : thread_counts) {
run_parallel_benchmark(config, n_threads, reporter);
if (n_threads == 1) {
baseline_time = reporter.get_results().back().time_ms;
}
}
// Print scaling analysis
std::cout << "\n" << std::string(60, '=') << "\n";
std::cout << "THREAD SCALING ANALYSIS\n";
std::cout << std::string(60, '=') << "\n\n";
std::cout << std::fixed << std::setprecision(2);
std::cout << "Threads | Time (ms) | Speedup | Efficiency\n";
std::cout << std::string(50, '-') << "\n";
for (const auto& result : reporter.get_results()) {
// Extract thread count from workload name
size_t pos = result.workload_name.find("_threads_");
if (pos != std::string::npos) {
size_t threads = std::stoul(result.workload_name.substr(pos + 9));
double speedup = baseline_time / result.time_ms;
double efficiency = (speedup / threads) * 100.0;
std::cout << std::setw(7) << threads << " | "
<< std::setw(9) << result.time_ms << " | "
<< std::setw(7) << speedup << "x | "
<< std::setw(6) << efficiency << "%\n";
}
}
std::cout << "\n";
// Save results
reporter.save_csv("parallel_benchmark_results.csv");
return 0;
}