-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetting_num_threads.cpp
More file actions
172 lines (139 loc) · 4.02 KB
/
Copy pathsetting_num_threads.cpp
File metadata and controls
172 lines (139 loc) · 4.02 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
#include <cstdio>
#include <omp.h>
#include <vector>
#include <faasm/shared_mem.h>
/*
* Sums the numbers up to the total
*/
int getSum(int total)
{
int sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
}
return sum;
}
int main()
{
// Run very overloaded yet simple check
int nThreads = 100;
omp_set_num_threads(nThreads);
auto flags = new bool[nThreads];
const int max = omp_get_max_threads();
if (max != nThreads) {
printf("Expected num threads and max to be equal, %i != %i\n",
nThreads,
max);
return 1;
}
#pragma omp parallel default(none) shared(flags)
{
printf("Setting thread %i\n", omp_get_thread_num());
flags[omp_get_thread_num()] = true;
}
for (int i = 0; i < nThreads; i++) {
if (!flags[i]) {
printf("Basic check at %i failed\n", i);
return 1;
}
}
int actual = 0;
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel default(none) reduction(+ : actual)
{
actual = omp_get_thread_num();
}
int expected = getSum(nThreads);
if (actual != expected) {
printf("Failed to set default num threads. Expected %d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
const int wanted = max - 1;
// Need to run on multicore machine for rest of the test to be ok
if (wanted == 0) {
return EXIT_SUCCESS;
}
// Test setting with num_threads
expected = getSum(wanted);
actual = 0;
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel num_threads(wanted) default(none) reduction(+ : actual)
{
actual += omp_get_thread_num();
}
if (actual != expected) {
printf("Failed to set with pragma argument. Expected %d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
// Test value resets at parallel exit
actual = 0;
expected = getSum(max);
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel default(none) reduction(+ : actual)
{
actual = omp_get_thread_num();
}
if (actual != expected) {
printf("Failed to reset num threads. Expected %d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
// Test setting with omp_set
omp_set_num_threads(wanted);
expected = getSum(wanted);
actual = 0;
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel default(none) reduction(+ : actual)
{
actual = omp_get_thread_num();
}
if (actual != expected) {
printf("Failed to set with omp_set_num_threads. Expected %d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
// Test set value is default even after parallel section
if (wanted != omp_get_max_threads()) {
printf("Max num threads changed. Expected %d, got %d\n",
max,
omp_get_max_threads());
return EXIT_FAILURE;
}
actual = 0;
expected = getSum(wanted);
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel default(none) reduction(+ : actual)
{
actual = omp_get_thread_num();
}
if (actual != expected) {
printf("Failed to keep set num threads. Expected %d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
// Test we can override set value with num_thread
expected = getSum(max);
actual = 0;
FAASM_REDUCE(actual, FAASM_TYPE_INT, FAASM_OP_SUM)
#pragma omp parallel num_threads(max) default(none) reduction(+ : actual)
{
actual = omp_get_thread_num();
}
if (actual != expected) {
printf("Failed to override set value with pragma argument. Expected "
"%d, got %d\n",
expected,
actual);
return EXIT_FAILURE;
}
delete[] flags;
// We're done
return EXIT_SUCCESS;
}