-
Notifications
You must be signed in to change notification settings - Fork 38
/
ParallelAndSplit.h
68 lines (46 loc) · 1.47 KB
/
ParallelAndSplit.h
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
#pragma once
#include <vector>
#include <list>
#include <unordered_set>
#include <thread>
#include "MultipleAndInterface.h"
#include "SequentialAndSubrange.h"
#include "SequentialAnd.h"
class ParallelAndSplit: public MultipleAndInterface {
std::vector<bitvector*> input;
std::vector<bitvector*> results;
std::vector<std::thread> threads;
size_t thread_count;
public:
ParallelAndSplit(const std::vector<bitvector*>& input_, size_t thread_count_)
: input(input_)
, thread_count(thread_count_) {
assert(input.size() >= 2);
}
virtual std::unique_ptr<bitvector> calculate() override {
threads.reserve(thread_count);
size_t start = 0;
size_t end;
const size_t avg = input.size() / thread_count;
results.resize(thread_count);
for (size_t i=0; i < thread_count; i++) {
if (i == thread_count - 1) {
end = input.size();
} else {
end = start + avg;
}
threads.push_back(std::thread(&ParallelAndSplit::and_pair, this, i, start, end));
start = end;
}
for (auto& thread: threads) {
thread.join();
}
SequentialAnd seq(results);
return seq.calculate();
}
private:
void and_pair(size_t index, size_t start, size_t end) {
SequentialAndSubrange seq(input, start, end);
results[index] = seq.calculate().release();
}
};