forked from fanspaceshow/CodeTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omp_clause.c
178 lines (153 loc) · 4.02 KB
/
omp_clause.c
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
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
/**
* 记录openmp中子句的用法
*
* 包括shared, private, default, firstprivate, lastprivate, nowait, schedule, if, reduction, copyin, copyprivate, num_threads
*/
void omp_shared_private_default() {
int i, n = 10;
#pragma omp parallel for default(none) \
private(i) shared(n)
for(i = 0; i < n; i++) {
printf("thread %d execute loop %d\n", omp_get_thread_num(), i);
}
}
void omp_firstprivate() {
int n = 8;
int i=0, a[n];
for(i = 0; i < n ;i++) {
a[i] = i+1;
}
#pragma omp parallel for private(i) firstprivate(a)
for ( i = 0; i<n; i++)
{
printf("thread %d: a[%d] is %d\n", omp_get_thread_num(), i, a[i]);
}
}
void omp_lastprivate() {
int n = 8;
int i, a = 3;
// lastprivate 将for中最后一次循环(i == n-1) a 的值赋给a
#pragma omp parallel for private(i) lastprivate(a)
for ( i = 0; i<n; i++)
{
a = i+1;
printf("In for: thread %d has a value of a = %d for i = %d\n", omp_get_thread_num(),a,i);
}
printf("\n");
printf("Out for: thread %d has a value of a = %d\n", omp_get_thread_num(),a);
printf("\n");
// lastprivate 将最后一个包含a的section的中a 的值赋给a
#pragma omp parallel sections private(i) lastprivate(a)
{
#pragma omp section
{
a = 1;
}
#pragma omp section
{
a = 5;
}
}
printf("after section: thread %d has a value of a = %d\n", omp_get_thread_num(),a);
}
void omp_nowait() {
int i, n =6;
#pragma omp parallel
{
#pragma omp for nowait
for(i = 0; i < n; i++) {
printf("thread %d: ++++\n", omp_get_thread_num());
}
#pragma omp for
for(i = 0; i < n; i++) {
printf("thread %d: ----\n", omp_get_thread_num());
}
}
}
void omp_schedule() {
int i, n = 10;
// schedule 取值 static, dynamic, guided, auto, runtime
#pragma omp parallel for default(none) schedule(static, 2) \
private(i) shared(n)
for(i = 0; i < n; i++) {
printf("Iteration %d executed by thread %d\n", i, omp_get_thread_num());
}
}
void omp_if() {
int n = 1, tid;
printf("n = 1\n");
#pragma omp parallel if(n>5) default(none) \
private(tid) shared(n)
{
tid = omp_get_thread_num();
printf("thread %d is running\n", tid);
}
printf("\n");
n = 10;
printf("n = 10\n");
#pragma omp parallel if(n>5) default(none) \
private(tid) shared(n)
{
tid = omp_get_thread_num();
printf("thread %d is running\n", tid);
}
}
void omp_reduction() {
int sum, i;
int n = 10;
int a[n];
for(i = 0; i < n; i++) {
a[i] = (i+1);
}
#pragma omp parallel for default(none) \
private(i) shared(a,n) reduction(+:sum)
for(i = 0; i < n; i++) {
sum += a[i];
}
printf("sum is %d\n", sum);
}
int counter = 10;
#pragma omp threadprivate(counter)
void omp_copyin() {
printf("counter is %d\n", counter);
#pragma omp parallel copyin(counter)
{
counter = omp_get_thread_num() + counter + 1;
printf("thread %d : counter is %d\n", omp_get_thread_num(), counter);
}
printf("counter is %d\n", counter);
}
void omp_copyprivate() {
int i;
#pragma omp parallel private(i)
{
#pragma omp single copyprivate(i, counter)
{
i = 50;
counter = 100;
printf("thread %d execute single\n", omp_get_thread_num());
}
printf("thread %d: i is %d and counter is %d\n",omp_get_thread_num(), i, counter);
}
}
void omp_num_threads() {
#pragma omp parallel num_threads(2)
{
printf("thread %d is running\n", omp_get_thread_num());
}
}
int main() {
// omp_shared_private_default();
// omp_firstprivate();
// omp_lastprivate();
// omp_nowait();
// omp_schedule();
// omp_if();
// omp_reduction();
// omp_copyin();
// omp_copyprivate();
omp_num_threads();
}