-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathparallelalgo.hpp
215 lines (175 loc) · 5.7 KB
/
parallelalgo.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
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <algorithm>
#include <vector>
#include <thread>
#include <iterator>
#include <mutex>
#include <atomic>
#include <unistd.h>
namespace sweet {
inline unsigned getNumberOfCores() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
template<typename Iterator, typename UnaryFunction>
UnaryFunction for_each_impl(Iterator first, Iterator second,
UnaryFunction f, std::random_access_iterator_tag, size_t numThreads) {
std::vector<std::thread> threads;
const size_t dist = std::distance(first, second);
const size_t stepWidth = dist/numThreads;
for(size_t i = 0; i < numThreads; ++i) {
int fillUp = (i+1 == numThreads) && (dist % 2 != 0) ? 1 : 0;
threads.push_back(
std::thread(
std::for_each<Iterator, UnaryFunction>,
first+(i*stepWidth), first+((i+1)*stepWidth) + fillUp, f));
}
for(size_t i = 0; i < numThreads; ++i) {
threads[i].join();
}
return f;
}
template<typename Iterator, typename UnaryFunction>
UnaryFunction for_each(Iterator first, Iterator second, UnaryFunction f,
size_t numThreads = getNumberOfCores()) {
typedef typename std::iterator_traits<Iterator>::iterator_category category;
return for_each_impl(std::forward<Iterator>(first),
std::forward<Iterator>(second), std::forward<UnaryFunction>(f),
category(), numThreads);
}
template<typename Iterator, typename OIterator, typename UnaryFunction>
OIterator transform_impl(Iterator first, Iterator second, OIterator out,
UnaryFunction f, size_t numThreads, std::random_access_iterator_tag,
std::random_access_iterator_tag) {
std::vector<std::thread> threads;
const size_t dist = std::distance(first, second);
const size_t stepWidth = dist/2u;
for(size_t i = 0; i < numThreads; ++i) {
int fillUp = (i+1 == numThreads) && (dist % 2 != 0) ? 1 : 0;
threads.push_back(
std::thread(
std::transform<Iterator, OIterator, UnaryFunction>,
first+(i*stepWidth), first+((i+1)*stepWidth) + fillUp,
out+(i*stepWidth), f
)
);
}
for(size_t i = 0; i < numThreads; ++i) {
threads[i].join();
}
return out;
}
template<typename Iterator, typename OIterator, typename UnaryFunction>
OIterator transform(Iterator first, Iterator second, OIterator out,
UnaryFunction f, size_t numThreads = getNumberOfCores()) {
typedef typename std::iterator_traits<Iterator>::iterator_category iType;
typedef typename std::iterator_traits<OIterator>::iterator_category oType;
return transform_impl(std::forward<Iterator>(first),
std::forward<Iterator>(second), std::forward<OIterator>(out),
std::forward<UnaryFunction>(f), numThreads, iType(), oType());
}
template<typename Iterator, typename OIterator, typename UnaryFunction>
UnaryFunction transform(Iterator first, Iterator second, OIterator oit,
UnaryFunction f, size_t numThreads = 2) {
typedef typename std::iterator_traits<Iterator>::iterator_category category;
return transform_impl(std::forward<Iterator>(first),
std::forward<Iterator>(second), std::forward<OIterator>(oit),
std::forward<UnaryFunction>(f), category(), numThreads);
}
template<typename Iterator, typename OIterator, typename Tmp, typename UnaryFunction>
OIterator mapReduce(Iterator first, Iterator second, OIterator out,
UnaryFunction f, size_t numThreads = getNumberOfCores()) {
struct Job {
Iterator b;
Iterator e;
OIterator outIter;
Tmp tmpStore;
UnaryFunction func;
std::mutex& jmutex;
inline Job(Iterator nb, Iterator ne, OIterator nOutIter, UnaryFunction nFunc,
std::mutex& nJmutex) : b(nb), e(ne), outIter(nOutIter),
func(nFunc), jmutex(nJmutex) {
}
inline void operator()() {
/*std::insert_iterator<Tmp> tmpOut = std::inserter(tmpStore,
tmpStore.end());*/
auto tmpOut = std::inserter(this->tmpStore, this->tmpStore.end());
for(; this->b != this->e; ++this->b) {
this->func(*this->b, tmpOut);
}
this->jmutex.lock();
std::copy(this->tmpStore.begin(), this->tmpStore.end(), this->outIter);
this->jmutex.unlock();
}
};
std::mutex joinMutex;
size_t curSize = std::distance(first, second);
size_t advanceSize = curSize/numThreads;
if(advanceSize == 0) {
++advanceSize;
}
Iterator cur = first;
std::vector<std::thread> jobs;
jobs.reserve(numThreads);
for(size_t i = 0; i < numThreads && cur != second; ++i) {
Iterator next = cur;
std::advance(next, advanceSize);
jobs.push_back(std::thread(Job(cur, next, out, f, joinMutex)));
cur = next;
}
if(cur != second) {
jobs.push_back(std::thread(Job(cur, second, out, f, joinMutex)));
}
for(auto& it : jobs) {
it.join();
}
return out;
}
template<typename T>
struct FindEqual {
const T& value;
inline FindEqual(const T& v) : value(v) {}
inline bool operator()(const T& other) {
return this->value == other;
}
};
template<typename Iterator, typename Unary>
Iterator find_if(Iterator begin, Iterator end, Unary pred,
size_t numThreads = getNumberOfCores())
{
size_t curSize = std::distance(begin, end);
size_t advanceSize = curSize/numThreads;
if(advanceSize == 0) {
++advanceSize;
}
std::atomic_bool die(false);
std::vector<std::thread> jobs;
jobs.reserve(numThreads);
Iterator cur = begin;
Iterator found = end;
for(size_t i = 0; i < numThreads && cur != end; ++i) {
Iterator next = cur;
std::advance(next, advanceSize);
jobs.push_back(std::thread([&](Iterator be, Iterator en) {
for(; be != en && !die; ++be) {
if(pred(*be)) {
found = be;
die = true;
break;
}
}
}, cur, next));
cur = next;
}
for(auto& it : jobs) {
it.join();
}
return found;
}
template<typename Iterator, typename T>
Iterator find(Iterator begin, Iterator end, const T& value,
size_t numThreads = getNumberOfCores())
{
return find_if(begin, end, FindEqual<T>(value), numThreads);
}
} // namespace sweet