-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto2.cpp
265 lines (209 loc) · 6.18 KB
/
proto2.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <string>
#include <iostream>
#include <mutex>
#include <vector>
#include <map>
#include <type_traits>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <cassert>
#include <chrono>
using Clock = std::chrono::high_resolution_clock;
// flags = -ltbb -fopenmp -lomp -O3
#include <omp.h>
#include <tbb/parallel_for.h>
#include "unipar/tbb.h"
#include "unipar/openmp.h"
#include "unipar/dummy.h"
using namespace std;
// This file tries to emulate the main OpenMP usage of stride better:
/*
#pragma omp parallel num_threads(m_num_threads)
{
const unsigned int thread = omp_get_thread_num();
#pragma omp for schedule(runtime)
for (int i = 0; i < m_households.size(); i++) {
Infector<log_level, track_index_case>::execute(
m_households[i], m_disease_profile, m_rng_handler[thread], m_calendar);
}
#pragma omp for schedule(runtime)
for (int i = 0; i < m_school_clusters.size(); i++) {
Infector<log_level, track_index_case>::execute(
m_school_clusters[i], m_disease_profile, m_rng_handler[thread], m_calendar);
}
...3 more of those ...
}
*/
struct Cluster {
vector<int> numbers;
Cluster(int size) {
for (int i=0; i<size; i++) {
numbers.push_back(i);
}
}
};
struct Infector {
/// Will update each number to the next prime
static void execute(Cluster& c) {
for (int& i: c.numbers) {
for (int j=i+1; ; j++) {
for (int k=2; k<j; k++) {
if (j%k == 0) goto not_prime;
}
// Found the next prime
i = j;
break;
not_prime:;
}
}
}
};
using Clusters = vector<Cluster>;
Clusters make_clusters(int amount, int size) {
vector<Cluster> res;
for (int i=0; i<amount; i++) {
res.push_back(Cluster(size));
}
return res;
}
Clock::duration none(Clusters families, Clusters schools, Clusters communities) {
auto start = Clock::now();
for (int step=0; step<5; step++) {
for (int i=0; i<families.size(); i++) {
Infector::execute(families[i]);
}
for (int i=0; i<schools.size(); i++) {
Infector::execute(schools[i]);
}
for (int i=0; i<communities.size(); i++) {
Infector::execute(communities[i]);
}
}
return (Clock::now() - start);
}
Clock::duration openmp(Clusters families, Clusters schools, Clusters communities) {
auto start = Clock::now();
for (int step=0; step<5; step++) {
//cout << "Step " << step << endl;
#pragma omp parallel num_threads(8)
{
#pragma omp for schedule(runtime)
for (int i=0; i<families.size(); i++) {
Infector::execute(families[i]);
}
#pragma omp for schedule(runtime)
for (int i=0; i<schools.size(); i++) {
Infector::execute(schools[i]);
}
#pragma omp for schedule(runtime)
for (int i=0; i<communities.size(); i++) {
Infector::execute(communities[i]);
}
}
}
return (Clock::now() - start);
}
Clock::duration intel_tbb(Clusters families, Clusters schools, Clusters communities) {
auto start = Clock::now();
for (int step=0; step<5; step++) {
tbb::parallel_for<int>(0, families.size(), [&](size_t i) {
Infector::execute(families[i]);
});
tbb::parallel_for<int>(0, schools.size(), [&](size_t i) {
Infector::execute(schools[i]);
});
tbb::parallel_for<int>(0, communities.size(), [&](size_t i) {
Infector::execute(communities[i]);
});
}
return (Clock::now() - start);
}
template <typename UniParallel, int nthreads>
Clock::duration unified(Clusters families, Clusters schools, Clusters communities) {
auto start = Clock::now();
auto region = UniParallel(nthreads);
for (int step=0; step<5; step++) {
region.for_(0, families.size(), [&](size_t i) {
Infector::execute(families[i]);
});
region.for_(0, schools.size(), [&](size_t i) {
Infector::execute(schools[i]);
});
region.for_(0, communities.size(), [&](size_t i) {
Infector::execute(communities[i]);
});
}
return (Clock::now() - start);
}
// Making this a proper class to simulate more 'serious' resources
class Adder {
public:
Adder(int add): m_add(add) {}
int m_add;
int do_add(int a, int b) {
return a+b+m_add;
}
};
template <typename UniParallel, int nthreads>
Clock::duration unified_res(Clusters families, Clusters schools, Clusters communities) {
auto start = Clock::now();
UniParallel par(nthreads);
auto region = par.template with<Adder>(2);
for (int step=0; step<5; step++) {
region.for_(0, families.size(), [&](Adder& add, size_t i) {
Infector::execute(families[add.do_add(-2, i)]);
});
region.for_(0, schools.size(), [&](Adder& add, size_t i) {
Infector::execute(schools[add.do_add(-2, i)]);
});
region.for_(0, communities.size(), [&](Adder& add, size_t i) {
Infector::execute(communities[add.do_add(-2, i)]);
});
}
return (Clock::now() - start);
}
template <typename Func>
Clock::duration measure(const Func& f, uint total=20, uint best=10) {
vector<Clock::duration> times;
for (int i=0; i<total; i++) {
times.push_back(f());
}
sort(times.begin(), times.end());
return accumulate(times.begin(), times.begin()+best, Clock::duration()) / best;
}
int main() {
map<string, function<Clock::duration(Clusters, Clusters, Clusters)>> funcs;
funcs["no"] = none;
funcs["omp"] = openmp;
funcs["tbb"] = intel_tbb;
funcs["up_no"] = unified<unipar::DummyParallel, 1>;
funcs["up_omp"] = unified<unipar::OpenmpParallel, 8>;
funcs["up_tbb"] = unified<unipar::TbbParallel, 8>;
// funcs["upr_no"] = unified_res<unipar::DummyParallel, 1>;
// funcs["upr_omp"] = unified_res<unipar::OpenmpParallel, 8>;
// funcs["upr_tbb"] = unified_res<unipar::TbbParallel, 8>;
//vector<int> sizes = {1, 2, 4, 8, 16, 32};
vector<int> sizes = {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};
cout << "mulpl";
for (auto& it: funcs) {
cout << "\t" << it.first;
}
cout << "\n";
for (int i=0; i<sizes.size(); i++) {
int size = sizes[i];
cout << size << "\t";
auto families = make_clusters(size*356, 6);
auto schools = make_clusters(size*50, 224);
auto communities = make_clusters(size*5, 724);
for (auto& it: funcs) {
auto dur = measure([&](){
return it.second(families, schools, communities);
}, 20, 10);
cout << chrono::duration_cast<chrono::milliseconds>(dur).count() << "\t";
}
cout << "\n";
}
}