-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_synchronous.cpp
49 lines (44 loc) · 1.18 KB
/
base_synchronous.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
/*!
* \brief Synchronous operation in openmp, including
* barrier, ordered and master.
*/
#include <omp.h>
#include <stdio.h>
int main() {
////////////////////////////////////////
// barrier
////////////////////////////////////////
printf("\nbarrier: \n");
#pragma omp parallel
{
int sumb = 0;
for (int i = 0; i < 100000; i++)
sumb++;
// Wait for all threads to execute here.
#pragma omp barrier
printf("sum=%d, thread_id=%d\n", sumb, omp_get_thread_num());
}
////////////////////////////////////////
// ordered
////////////////////////////////////////
// Execute the threads sequentially.
// "ordered" must be bound to "for".
printf("\nordered: \n");
#pragma omp parallel for ordered num_threads(6)
for (int i = 0; i < 10; i++) {
#pragma omp ordered
printf("i=%d, thread_id=%d\n", i, omp_get_thread_num());
}
////////////////////////////////////////
// master
////////////////////////////////////////
printf("\nmaster: \n");
#pragma omp parallel
{
// Specify the master thread to execute.
#pragma omp master
for (int i = 0; i<5; i++) {
printf("i=%d, thread_id=%d\n", i, omp_get_thread_num());
}
}
}