-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefault_shared.cpp
More file actions
66 lines (52 loc) · 1.59 KB
/
Copy pathdefault_shared.cpp
File metadata and controls
66 lines (52 loc) · 1.59 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
#include <cstdio>
#include <omp.h>
#include <faasm/shared_mem.h>
int main(int argc, char* argv[])
{
int count = 0;
int nThreads = 5;
int chunkSize = 20;
int nLoops = nThreads * chunkSize;
int flagsStack[5];
int* flagsHeap = new int[nThreads];
FAASM_REDUCE(count, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel for num_threads(5) default(none) \
shared(nLoops, flagsStack, flagsHeap) reduction(+ : count)
for (int i = 0; i < nLoops; i++) {
int threadNum = omp_get_thread_num();
count += (threadNum + 1);
flagsStack[threadNum] += (threadNum + 1);
flagsHeap[threadNum] += (threadNum + 1);
}
bool success = true;
int expected = 0;
for (int i = 0; i < nThreads; i++) {
// Build up expected sum
expected += (i + 1) * chunkSize;
// Check flag
int expectedFlag = (i + 1) * chunkSize;
if (flagsStack[i] != expectedFlag) {
printf("%ith stack flag not as expected: %i != %i\n",
i,
flagsStack[i],
expectedFlag);
success = false;
}
if (flagsHeap[i] != expectedFlag) {
printf("%ith heap flag not as expected: %i != %i\n",
i,
flagsStack[i],
expectedFlag);
success = false;
}
}
if (count != expected) {
printf("Count not as expected: %i != %i\n", count, expected);
success = false;
}
delete[] flagsHeap;
if (!success) {
return 1;
}
return 0;
}