Repository navigation
Expand file tree
/
Copy pathmin_examples.cpp
More file actions
382 lines (346 loc) · 8.86 KB
/
Copy pathmin_examples.cpp
File metadata and controls
382 lines (346 loc) · 8.86 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// Created by eva on 14.01.21.
//
#if defined(_OPENMP)
#include <omp.h>
#endif
#include "min_examples.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
using namespace std;
void parallel() {
//-> runtime_functions()
}
void single() {
#pragma omp parallel
{
printf("Th %i: A - Run this in parallel.\n", omp_get_thread_num());
#pragma omp single
{
printf("--- Th %i: Region run only by one thread, the other are waiting before starting B\n",
omp_get_thread_num());
}
printf("Th %i: B - This is parallel again.\n", omp_get_thread_num());
#pragma omp single nowait
{
printf("--- Th %i: Another region run only by one thread (also not necessarily master)\n",
omp_get_thread_num());
}
printf("Th %i: C - This is parallel again, without the other threads waiting for the single region.\n",
omp_get_thread_num());
}
}
void master() {
#pragma omp parallel
{
printf("Th %i: A - Run this in parallel.\n", omp_get_thread_num());
#pragma omp master
{ printf("--- Th %i: Region run only by the master thread\n", omp_get_thread_num()); }
printf("Th %i: B - This is also parallel.\n", omp_get_thread_num());
#pragma omp master
{
printf("--- Th %i: Another region run only by one thread, because of barrier the others will wait to do C\n",
omp_get_thread_num());
}
#pragma omp barrier
printf("Th %i: C - This is parallel again, after the barrier.\n", omp_get_thread_num());
}
}
void runtime_functions() {
#ifdef _OPENMP
int cores = omp_get_num_procs();
int max_threads = omp_get_max_threads();
int master_id = omp_get_thread_num();
int team_id = omp_get_team_num();
printf("Available cores: %i\n", cores);
printf("Max available threads: %i\n", max_threads);
printf("Master thread: %i\n", master_id);
printf("\n----------\n");
printf("Parallel region!\n");
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
int team_id = omp_get_team_num();
int num_threads = omp_get_num_threads();
printf("Thread %i of %i, in team %i\n", thread_id, num_threads, team_id);
if (thread_id == master_id) {
int num_teams = omp_get_num_teams();
int team_size = omp_get_team_size(omp_get_level());
printf("Per default all available threads are used. Threads in this section: %i\n", num_threads);
printf("Threads are organised in teams, here: %i team(s) à %i threads\n", num_teams, team_size);
}
}
printf("\n----------\n");
printf("Parallel region with more threads!\n");
#pragma omp parallel num_threads(5)
{
int thread_id = omp_get_thread_num();
int num_threads = omp_get_num_threads();
printf("Thread %i of %i\n", thread_id, num_threads);
}
printf("Parallel region with normal threads!\n");
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
int num_threads = omp_get_num_threads();
printf("Thread %i of %i\n", thread_id, num_threads);
}
printf("\n----------\n");
printf("Parallel region with less threads!\n");
omp_set_num_threads(2);
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
int num_threads = omp_get_num_threads();
printf("Thread %i of %i\n", thread_id, num_threads);
}
printf("\n----------\n");
printf("Sequential region!\n");
int thread_id = omp_get_thread_num();
int num_threads = omp_get_num_threads();
printf("Only master thread remains: Thread %i of %i, in team %i\n", thread_id, num_threads, team_id);
omp_set_num_threads(4);
#else
printf("No OpenMP supported");
#endif
}
void parallel_for() {
#pragma omp parallel for
for (int i = 0; i < 6; i++)
printf("Th %i: iteration %i\n", omp_get_thread_num(), i);
printf("\n");
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < 6; i++)
printf("Th %i: iteration %i\n", omp_get_thread_num(), i);
}
}
void parallel_nested() {
omp_set_nested(2);
#pragma omp parallel num_threads(2)
{
int thread_id = omp_get_thread_num();
int team_id = omp_get_team_num();
int level = omp_get_level();
printf("Lvl %i: Thread %i in team %i\n", level, thread_id, team_id);
#pragma omp parallel num_threads(3)
{
int thread_id_nst = omp_get_thread_num();
int team_id_nst = omp_get_team_num();
int level_nst = omp_get_level();
printf("-- Lvl %i: Thread %i in team %i\n", level_nst, thread_id_nst, team_id_nst);
}
thread_id = omp_get_thread_num();
team_id = omp_get_team_num();
level = omp_get_level();
printf("Lvl %i: And back to thread %i in team %i\n", level, thread_id, team_id);
}
}
void nowait() {
int i;
printf("count upwards - rather randomly:\n");
#pragma omp parallel
{
#pragma omp for nowait
for (i = 10; i < 15; i++)
printf("%i ", i);
#pragma omp for nowait
for (i = 20; i < 25; i++)
printf("%i ", i);
}
printf("\n");
}
void memory() {
int x = 5;
int y = 20;
int s = 1;
printf("s = %i, x = %i, y = %i\n", s, x, y);
#pragma omp parallel private(x) firstprivate(y) shared(s)
{
// x is undefined on entry, but now set to 10
x = 10;
// y was pre-initialized to a value of 20
int z = x + y;
// (first)private variables may be modified
// y = 11;
s++;
if (omp_get_thread_num() != 0) {
y += 3;}
printf("\tTh %i: s=%i, x=%i, y=%i\n", omp_get_thread_num(), s, x, y);
}
printf("s = %i, x = %i, y = %i\n", s, x, y);
}
void parallel_for_collapse() {
#pragma omp parallel for collapse(2)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
printf("Th %i: %i - %i\n", omp_get_thread_num(), i, j);
}
}
printf("\n");
}
void parallel_for_reduction() {
int m = 0;
#pragma omp parallel for reduction(max:m)
for (int i = 0; i <= 6; i++) {
m = max(m, (rand() % 10));
printf("Th %i: m = %i\n", omp_get_thread_num(), m);
}
printf("Max of all m = %i\n", m);
}
void parallel_for_scheduling() {
printf("static\n");
#pragma omp parallel for schedule(static)
for (int i = 0; i < 12; i++) {
printf("%i: Th %i sleeping\n", i, omp_get_thread_num());
sleep(i);
}
printf("\ndynamic\n");
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < 12; i++) {
printf("%i: Th %i sleeping\n", i, omp_get_thread_num());
sleep(i);
}
printf("\nguided\n");
#pragma omp parallel for schedule(guided)
for (int i = 0; i < 12; i++) {
printf("%i: Th %i sleeping\n", i, omp_get_thread_num());
sleep(i);
}
printf("\nauto\n");
#pragma omp parallel for schedule(auto)
for (int i = 0; i < 12; i++) {
printf("%i: Th %i sleeping\n", i, omp_get_thread_num());
sleep(i);
}
}
void sections(bool addCont) {
#pragma omp parallel
if (addCont) omp_set_num_threads(2);
{
// Gets all in the way:
// printf("Parallel before: Thread %i\n", omp_get_thread_num());
#pragma omp sections
{
{ printf("A "); }
#pragma omp section
{ printf("race "); } // Task 1
#pragma omp section
{ printf("car "); } // Task 2
}
#pragma omp single
{ printf("is fun to watch.\n"); }
if (addCont) {
printf("Parallel after: Thread %i\n", omp_get_thread_num());
omp_set_num_threads(4);
}
}
}
void tasks(bool addCont) { // \cite[\pg{108}]{pas_using_2017}
if (addCont) omp_set_num_threads(2);
#pragma omp parallel
{
if (addCont) printf("Parallel before: Thread %i\n", omp_get_thread_num());
#pragma omp single
{
printf("A ");
#pragma omp task
{ printf("race "); } // Task 1
#pragma omp task
{ printf("car "); } // Task 2
#pragma omp taskwait
printf("is fun to watch.\n");
}
if (addCont) {
printf("Parallel after: Thread %i\n", omp_get_thread_num());
omp_set_num_threads(4);
}
}
}
void syncing() {
//\cite[\pg{33}]{pas_using_2017}
int x;
#pragma omp parallel
{
#pragma omp atomic
x += 1;
}
// \cite[\pg{88}]{pas_using_2017}
omp_lock_t my_lock;
(void) omp_init_lock(&my_lock);
#pragma omp parallel
{
#pragma omp master
{
(void) omp_set_lock(&my_lock);
//locked region
(void) omp_unset_lock(&my_lock);
} // End of master region
} // End of parallel region
(void) omp_destroy_lock(&my_lock);
}
void say_what(string s) {
printf("\n\n%s\n-------\n", s.c_str());
}
/**
* Show minimal examples for some omp directives
*/
void minimal_examples(bool schedule) {
printf("--------------------\n"
"| Minimal Examples |\n"
"--------------------\n\n");
// say_what("single");
// single();
//
// say_what("master");
// master();
//
// say_what("Runtime Functions");
// runtime_functions();
//
// say_what("For");
// parallel_for();
//
// say_what("Nested Teams");
// parallel_nested();
//
// say_what("nowait");
// nowait();
//
say_what("Memory");
memory();
//
// say_what("Collapse");
// parallel_for_collapse();
//
// say_what("Reduction");
// parallel_for_reduction();
//
// if (schedule) {
// say_what("Scheduling");
// parallel_for_scheduling();
// }
//
// say_what("Sections");
// //muatations:
// for (int i=0; i<5; i++)
// sections(false);
// printf("\n");
// //Executions Context to show order:
// sections(true);
//
// say_what("Tasks");
// //muatations:
// for (int i=0; i<5; i++)
// tasks(false);
// printf("\n");
// //Executions Context to show order:
// tasks(true);
//
// say_what("Syncing (without output)");
// syncing();
}