-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path04_find.cpp
More file actions
220 lines (184 loc) · 6.87 KB
/
04_find.cpp
File metadata and controls
220 lines (184 loc) · 6.87 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
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <execution>
#include <future>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>
#include <atomic>
#include <execution>
#include <future>
#include <thread>
#include <vector>
using std::milli;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
// ===================================================================
// Helper Elements
// ===================================================================
// Joins threads on destruction
class join_threads {
std::vector<std::thread> &threads;
public:
explicit join_threads(std::vector<std::thread> &_threads)
: threads(_threads) {}
~join_threads() {
for (std::size_t i = 0; i < threads.size(); i++) {
if (threads[i].joinable())
threads[i].join();
}
}
};
// Prints benchmark results
void print_results(const char *const tag,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime) {
printf("%s: Time: %fms\n", tag,
duration_cast<duration<double, milli>>(endTime - startTime).count());
}
// ===================================================================
// Implementation A: Multiple Promises and Atomic Boolean
// ===================================================================
template <typename Iterator, typename MatchType>
Iterator parallel_find_promise(Iterator first, Iterator last, MatchType match) {
struct find_element {
void operator()(Iterator begin, Iterator end, MatchType match,
std::promise<Iterator> *result, // promise to fill
std::atomic<bool> *done_flag // atomic for early stop
) {
try {
// Actual Work
// Check atomic on every cycle for early stop
for (; (begin != end) && !std::atomic_load(done_flag); ++begin) {
if (*begin == match) {
result->set_value(begin);
std::atomic_store(done_flag, true);
return;
}
}
} catch (...) {
result->set_exception(std::current_exception());
done_flag->store(true);
}
}
};
unsigned long const length = std::distance(first, last);
if (!length) {
return last;
}
// Calculate the optimized number of threads
unsigned long const min_per_thread = 25;
unsigned long const max_threads =
(length + min_per_thread - 1) / min_per_thread;
unsigned long const hardware_threads = std::thread::hardware_concurrency();
unsigned long const num_threads =
std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
unsigned long const block_size = length / num_threads;
// Declare thread objects
std::promise<Iterator> result;
std::atomic<bool> done_flag(false);
std::vector<std::thread> threads(num_threads - 1);
// Split data into threads
{
join_threads joiner(threads);
Iterator block_start = first;
for (unsigned long i = 0; i < (num_threads - 1); i++) {
Iterator block_end = block_start;
std::advance(block_end, block_size);
threads[i] = std::thread(find_element(), block_start, block_end, match,
&result, &done_flag);
block_start = block_end;
}
// perform the find operation for final block in this thread.
find_element()(block_start, last, match, &result, &done_flag);
}
// Threads are joined at this point!
// Then, a done_flag==false, means not found.
if (!done_flag.load()) {
return last;
}
return result.get_future().get();
}
// ===================================================================
// Implementation B: Divide and Conquer Async
// ===================================================================
template <typename Iterator, typename MatchType>
Iterator parallel_find_async_impl(Iterator first, Iterator last,
MatchType match,
std::atomic<bool> *done_flag) {
try {
unsigned long const length = std::distance(first, last);
unsigned long const min_per_thread = 25;
printf("<parallel_find_async_impl> length: %lu\n", length);
if (length < 2 * min_per_thread) {
// Base Case
for (; (first != last) && !std::atomic_load(done_flag); ++first) {
if (*first == match) {
std::atomic_store(done_flag, true);
printf("<parallel_find_async_impl> found\n");
return first;
}
}
return last;
} else {
// Divide And Conquer: recurse and async
Iterator const mid_point = first + length / 2;
// async upper half
std::future<Iterator> async_result =
std::async(¶llel_find_async_impl<Iterator, MatchType>, mid_point,
last, match, std::ref(done_flag));
// recurse lower half
Iterator const direct_result =
parallel_find_async_impl(first, mid_point, match, done_flag);
return (direct_result == mid_point) ? async_result.get() : direct_result;
}
} catch (const std::exception &) {
std::atomic_store(done_flag, true);
throw;
}
}
template <typename Iterator, typename MatchType>
Iterator parallel_find_async(Iterator first, Iterator last, MatchType match) {
printf("parallel_find_async init\n");
std::atomic<bool> done_flag{false};
return parallel_find_async_impl(first, last, match, &done_flag);
}
// ===================================================================
// Benchmark
// ===================================================================
const size_t testSize = 1000;
using std::milli;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
int main() {
std::vector<int> ints(testSize);
for (size_t i = 0; i < testSize; i++) {
ints[i] = i;
}
int looking_for = 45;
auto startTime = high_resolution_clock::now();
parallel_find_promise(ints.begin(), ints.end(), looking_for);
auto endTime = high_resolution_clock::now();
print_results("Parallel-promise_atomic_impl :", startTime, endTime);
startTime = high_resolution_clock::now();
parallel_find_async(ints.begin(), ints.end(), looking_for);
endTime = high_resolution_clock::now();
print_results("Parallel-divide-and-conquer-async :", startTime, endTime);
startTime = high_resolution_clock::now();
std::find(ints.begin(), ints.end(), looking_for);
endTime = high_resolution_clock::now();
print_results("STL sequntial :", startTime, endTime);
startTime = high_resolution_clock::now();
std::find(std::execution::par, ints.begin(), ints.end(), looking_for);
endTime = high_resolution_clock::now();
print_results("STL parallel-par :", startTime, endTime);
startTime = high_resolution_clock::now();
std::find(std::execution::seq, ints.begin(), ints.end(), looking_for);
endTime = high_resolution_clock::now();
print_results("STL parallel-seq :", startTime, endTime);
return 0;
}