-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelAdd.cpp
More file actions
78 lines (51 loc) · 1.68 KB
/
ParallelAdd.cpp
File metadata and controls
78 lines (51 loc) · 1.68 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
#include <iostream>
#include <random>
#include <chrono>
#include <thread>
#include <mutex>
#include <assert.h>
constexpr int N_THREADS = 4;
constexpr int A_SIZE = ((100 / N_THREADS) * N_THREADS);
int reduce(int* a, int size) {
int ret = 0;
for (int i = 0; i < size; ++i)
ret += a[i];
return ret;
}
int reduceWithThreads(int* a, int size) {
std::mutex mu; // you may need this for synchronisation
std::thread threads[N_THREADS];
int ret = 0;
// TODO implement with threads
return ret;
}
int reduceWithOMP(int* a, int size) {
int ret = 0;
// TODO implement OMP with either atomic or critical
return ret;
}
int reduceWithOMPReduce(int* a, int size) {
int ret = 0;
// TODO implement OMP with reduction
return ret;
}
int main()
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, 100);
generator.seed((unsigned)std::chrono::system_clock::now().time_since_epoch().count());
int a[A_SIZE];
// fill array
for (int i = 0; i < sizeof(a) / sizeof(int); ++i) { // extra question: why does sizeof know the array size here ?
a[i] = distribution(generator);
}
int redcSerial = reduce(a, A_SIZE);
int redcThreads = reduceWithThreads(a, A_SIZE);
assert((redcSerial == redcThreads) && "threading implementation failed");
int redcOMP = reduceWithOMP(a, A_SIZE);
assert((redcSerial == redcOMP) && "OMP implementation failed");
// TODO comment in and implement
// int redcOMPRed = reduceWithOMPReduce(a, A_SIZE);
// assert((redcSerial == redcOMPRed) && "OMP reduce implementation failed");
std::cout << "works :)!" << std::endl;
}