-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto1.cpp
198 lines (154 loc) · 4.04 KB
/
proto1.cpp
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
#include <string>
#include <iostream>
#include <mutex>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cassert>
#include <chrono>
using Clock = std::chrono::high_resolution_clock;
#include <omp.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range.h>
using namespace std;
Clock::duration no_mp(size_t max, size_t nprimes) {
auto start = Clock::now();
vector<size_t> primes;
for (size_t i=0; i<max; i++) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
primes.push_back(i);
not_a_prime:;
}
assert(primes.size() == nprimes);
return (Clock::now() - start);
}
Clock::duration no_mp_count(size_t max, size_t nprimes) {
auto start = Clock::now();
size_t count = 0;
for (size_t i=0; i<max; i++) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
count++;
not_a_prime:;
}
assert(count == nprimes);
return (Clock::now() - start);
}
Clock::duration open_mp(size_t max, size_t nprimes) {
auto start = Clock::now();
mutex m;
vector<size_t> primes;
size_t nthreads = 8;
size_t chunksize = max/nthreads;
size_t extra = max%nthreads;
#pragma omp parallel num_threads(nthreads)
{
size_t thread = omp_get_thread_num();
vector<size_t> buffer;
size_t i = thread*chunksize;
size_t end = ((thread+1)*chunksize) + (thread+1==nthreads? extra : 0);
for (; i<end; i++) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
buffer.push_back(i);
not_a_prime:;
}
m.lock();
primes.reserve(buffer.size());
copy(buffer.begin(), buffer.end(), back_inserter(primes));
m.unlock();
}
assert(primes.size() == nprimes);
return (Clock::now() - start);
}
Clock::duration open_mp_count(size_t max, size_t nprimes) {
auto start = Clock::now();
size_t count = 0;
#pragma omp parallel for
for (size_t i=0; i<max; i++) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
#pragma omp atomic
count++;
not_a_prime:;
}
assert(count == nprimes);
return (Clock::now() - start);
}
Clock::duration intel_tbb(size_t max, size_t nprimes) {
auto start = Clock::now();
vector<size_t> primes = tbb::parallel_reduce(
tbb::blocked_range<size_t>(0, max), // Range
vector<size_t>(), // Identity
[](const tbb::blocked_range<size_t>& r, vector<size_t> buffer) -> vector<size_t> {
for (size_t i=r.begin(); i<r.end(); ++i) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
buffer.push_back(i);
not_a_prime:;
}
return buffer;
},
[](const vector<size_t>& a, const vector<size_t>& b) -> vector<size_t> {
vector<size_t> c = a;
c.reserve(b.size());
copy(b.begin(), b.end(), back_inserter(c));
return c;
}
);
assert(primes.size() == nprimes);
return (Clock::now() - start);
}
Clock::duration intel_tbb_count(size_t max, size_t nprimes) {
auto start = Clock::now();
size_t count = tbb::parallel_reduce(
tbb::blocked_range<size_t>(0, max), // Range
0, // Identity
[](const tbb::blocked_range<size_t>& r, size_t temp) -> size_t {
for (size_t i=r.begin(); i<r.end(); ++i) {
for (size_t j=2; j<i; j++) {
if (i%j == 0) goto not_a_prime;
}
temp++;
not_a_prime:;
}
return temp;
},
plus<size_t>()
);
assert(count == nprimes);
return (Clock::now() - start);
}
int main() {
map<string, function<Clock::duration(size_t, size_t)>> funcs;
funcs["Nope"] = no_mp;
funcs["OMP"] = open_mp;
funcs["TBB"] = intel_tbb;
funcs["NopeC"] = no_mp_count;
funcs["OMPC"] = open_mp_count;
funcs["TBBC"] = intel_tbb_count;
vector<size_t> sizes = {5000, 50000, 75000, 100000};
vector<size_t> primes = {671, 5135, 7395, 9594};
cout << "max";
for (auto& it: funcs) {
cout << "\t" << it.first;
}
cout << "\n";
for (size_t i=0; i<sizes.size(); i++) {
cout << sizes[i] << "\t";
for (auto& it: funcs) {
auto dur = it.second(sizes[i], primes[i]);
cout << chrono::duration_cast<chrono::milliseconds>(dur).count() << "\t";
}
cout << "\n";
}
}