-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_barrier.cpp
More file actions
59 lines (50 loc) · 1.29 KB
/
Copy pathsimple_barrier.cpp
File metadata and controls
59 lines (50 loc) · 1.29 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
#include <cstdio>
#include <omp.h>
static int x1 = 0;
static int x2 = 0;
static int x3 = 0;
static int x4 = 0;
static int result1 = 0;
static int result2 = 0;
static int result3 = 0;
static int result4 = 0;
int main(int argc, char* argv[])
{
#pragma omp parallel num_threads(2) default(none) \
shared(x1, x2, x3, x4, result1, result2, result3, result4)
{
int threadNum = omp_get_thread_num();
threadNum == 0 ? x1 = 5 : x2 = 5;
#pragma omp barrier
if (threadNum == 0) {
result1 = x1 + x2;
} else {
result2 = x1 * x2;
}
threadNum == 0 ? x3 = 5 : x4 = 5;
#pragma omp barrier
if (threadNum == 0) {
result3 = x3 + x4;
} else {
result4 = x3 * x4;
}
}
if (result1 != 10 || result2 != 25) {
printf("Barrier failed: result1: %d, result2: %d, x1: %d, x2: %d\n",
result1,
result2,
x1,
x2);
return EXIT_FAILURE;
}
if (result3 != 10 || result4 != 25) {
printf(
"Second Barrier failed: result3: %d, result4: %d, x3: %d, x4: %d\n",
result3,
result4,
x3,
x4);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}