-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjb_parallel.hpp
More file actions
250 lines (224 loc) · 7.7 KB
/
jb_parallel.hpp
File metadata and controls
250 lines (224 loc) · 7.7 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef CS207_JB_PARALLEL_HPP
#define CS207_JB_PARALLEL_HPP
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <omp.h>
#include <algorithm>
#include <climits>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include "CS207/Util.hpp"
/** @file jb_parallel.hpp
* @brief Define the key functions built on top of
* OpenMP to be used to optimize code which iterates
* over a random access data structure.
*/
namespace jb_parallel {
// RAII helper class for timing code.
// Cris wrote this.
struct Timer {
std::string msg;
CS207::Clock clock;
Timer(const std::string& s) : msg(s) {
clock.start();
}
~Timer() {
double elapsed = clock.seconds();
std::cout << msg << ": " << elapsed << "s" << std::endl;
}
};
// Simple function which tells the user how many
// threads OMP is making available.
int threads_available() {
int p;
// This compiler directive instructs OpenMP
// to make this block of code parallel.
#pragma omp parallel
{
p = omp_get_num_threads();
}
return p;
}
/** A function that sorts a range in parallel
* @param begin, end the range of what is to be sored
* @tparam IteratorType must meet the requirements of RandomAccessIterator
* @pre begin < end and begin and end are in the same range
* @pre IteratorType::value_type supports <, + int (looser than random access!)
* @pre Machine has exactly 1, 2, or 4 available cores
* @post For all i in [begin, end] !(i+1 < i)
* Runtime O((nlog(n))/p)
* std::sort used in this function uses a quicksort/heapsort hybrid known as intro sort
*/
template<typename IteratorType>
void parallel_sort(IteratorType begin, IteratorType end) {
int sz = end - begin;
int nt = threads_available();
if (nt == 1) {
std::sort(begin, end);
return;
}
std::vector<IteratorType> bounds(nt*2);
#pragma omp parallel shared(bounds)
{
int id = omp_get_thread_num();
int nthreads = omp_get_num_threads();
auto start = begin + id * sz / nthreads;
auto finish = end;
if (id != nthreads - 1){
finish = begin + (id + 1) * sz / nthreads;
}
// Remember which subsections of the array are sorted
// so that they can be inplace merged later
bounds[2*id] = start;
bounds[2*id + 1] = finish;
std::sort(start, finish);
}
// Relies on operator overload of vector iterator < operator.
std::sort(bounds.begin(), bounds.end());
// Time to do some in_place merges
// This section hardcoded for four cores.
if (nt == 4) {
std::inplace_merge(bounds[0],bounds[2], bounds[3]);
std::inplace_merge(bounds[4],bounds[6], bounds[7]);
std::inplace_merge(bounds[0], bounds[4], bounds[7]);
}
// This sectino hardcoded for 2 cores: only a single inplace
// merge necessary.
if (nt == 2) {
std::inplace_merge(bounds[0],bounds[2], bounds[3]);
}
}
/** A function that finds the minimum in a range in parallel
* @param begin, end the range of what is to be sored
* @tparam IteratorType must meet the requirements of RandomAccessIterator
* @pre begin < end and begin and end are in the same range
* @pre IteratorType::value_type supports the < function
* @post For all i in [begin, end] !(i+1 < i)
* Runtime O((nlog(n))/4 + 2n)
* std::sort used in this function uses a quicksort/heapsort hybrid known as intro sort
*/
template <typename IteratorType>
typename std::iterator_traits<IteratorType>::value_type
parallel_min(IteratorType begin, IteratorType end) {
int nt = threads_available();
std::vector<int> mins(nt);
auto sz = end - begin;
#pragma omp parallel shared(mins)
{
int id = omp_get_thread_num();
int nthreads = omp_get_num_threads();
auto start = id * sz / nthreads;
int finish = sz;
if (id != nthreads - 1){
finish = (id + 1) * sz / nthreads;
}
auto min_seen = *(begin + start);
for (auto i = begin + start; i < begin + finish; ++i) {
min_seen = std::min(min_seen, *i);
}
mins[id] = min_seen;
}
auto min_total = mins[0];
for (int i = 0; i < mins.size(); ++i) {
min_total = std::min(min_total, mins[i]);
}
return min_total;
}
/** A function that applies
* @param first, last the range to apply the function to
* @param f the unary function object to be applied
* @tparam Iter must meet the requirements of RandomAccessIterator
* @tparam UnaryFunction must meet the requirements of MoveConstructible
* @pre first < last and first and last are in same range
* @pre
* @post for all i in [first,last] *(new first) = f(* old first)
* Runtime O(O(f)*(last - first)/nthreads)
*/
template<class Iter, class UnaryFunction>
void for_each(Iter first, Iter last, UnaryFunction f) {
int dist = last - first;
// Parallelize blockwise
#pragma omp parallel
{
int id = omp_get_thread_num();
int nthreads = omp_get_num_threads();
auto start = first + id * dist / nthreads;
auto end = last;
if (id != nthreads - 1){
end = first + (id + 1) * dist / nthreads;
}
for (auto it = start; it != end; ++it) {
f(*it);
}
}
}
/** A function that applies a unary operator to every element
* in a range in parallel.
*
* @param first, last the range to apply the function to
* @param f the unary function object to be applied
* @tparam Iter must have an operator + (int) in addition to standard
* iterator abstraction requirements.
* @tparam UnaryFunction must meet the requirements of MoveConstructible
* @pre first < last and first and last are in same range
* @post for all i in [0, last - first] [new] first + i= f[old first + i]
* Runtime O(O(f)*(last - first)/nthreads)
*/
template<class Iter, class UnaryFunction>
void parallel_transform(Iter first, Iter last, UnaryFunction f) {
int dist = last - first;
// Parallelize blockwise
#pragma omp parallel
{
int id = omp_get_thread_num();
int nthreads = omp_get_num_threads();
auto start = first + id * dist / nthreads;
auto end = last;
if (id != nthreads - 1){
end = first + (id + 1) * dist / nthreads;
}
for (auto it = start; it != end; ++it) {
*it = f(*it);
}
}
}
/** Increments a counter according to a function in parallel, also
* known as 'reduce' functionality
* @param first, last the range to apply the function to
* @param f the unary function object to be applied
* @tparam Iter must have an operator + (int) in addition to standard
* iterator abstraction requirements.
* @tparam UnaryFunction must meet the requirements of MoveConstructible
* @pre first < last and first and last are in same range
* @post counter contains \sum_i=0^{last-first} f(*(first + i))
*
* Runs in time O(O(f)*(last - first)/nthreads), should achieve
* speedup roughly linear in the number of cores.
*/
template<class Iter, class UnaryFunction, class T>
void parallel_reduction(Iter first, Iter last, UnaryFunction f, T& counter) {
int dist = last - first;
int nt = threads_available();
std::vector<T> counts(nt, 0);
// Parallelize blockwise
#pragma omp parallel shared(counts)
{
int id = omp_get_thread_num();
int nthreads = omp_get_num_threads();
auto start = first + id * dist / nthreads;
auto end = last;
if (id != nthreads - 1){
end = first + (id + 1) * dist / nthreads;
}
for (auto it = start; it != end; ++it) {
counts[id] += f(*it);
}
}
for (int i = 0; i < nt; ++i) {
counter += counts[i];
}
}
} // namespace jb_parallel
#endif