-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_sections_single.cpp
63 lines (56 loc) · 1.46 KB
/
base_sections_single.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*!
* \brief Sections and Single.
*/
#include <stdio.h>
#include <omp.h>
int main() {
////////////////////////////////////////
// Sections
////////////////////////////////////////
printf("\nTest sections: \n");
#pragma omp parallel sections num_threads(3)
{
// Each section has a thread to call.
printf("thread_num = %d, num_threads = %d.\n",
omp_get_thread_num(), omp_get_num_threads());
#pragma omp section
{
printf("section 0,tid=%ld\n", omp_get_thread_num());
}
#pragma omp section
{
printf("section 1,tid=%ld\n", omp_get_thread_num());
}
#pragma omp section
{
printf("section 2,tid=%ld\n", omp_get_thread_num());
}
}
////////////////////////////////////////
// Single
////////////////////////////////////////
printf("\nTest single: \n");
#pragma omp parallel num_threads(3)
{
// It will only take a thread to execute it.
#pragma omp single
printf("Beginning work1.\n");
printf("Running work1 with thread %d\n", omp_get_thread_num());
// It also play the role of a barrier.
#pragma omp single
{
printf("Finishing work1.\n");
for (int i = 0; i < 20; i++)
printf("-");
}
// nowait.
#pragma omp single nowait
{
printf("Running work2 with thread %d\n", omp_get_thread_num());
for (int i = 0; i < 20; i++)
printf(".");
}
printf("work on 2 parallelly.%d\n", omp_get_thread_num());
}
return 0;
}