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 | /*
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
* Creates a secondary thread that greets in the standard output
*/
#include <omp.h>
#include <cstdlib>
#include <iostream>
int main(int argc, char* argv[]) {
int thread_count = omp_get_max_threads();
if (argc >= 2) {
thread_count = atoi(argv[1]);
}
int iteration_count = thread_count;
if (argc >= 3) {
iteration_count = atoi(argv[2]);
}
#pragma omp parallel num_threads(thread_count) \
default(none) shared(std::cout, iteration_count)
{
#pragma omp for schedule(static,2)
for (int iteration = 0; iteration < iteration_count; ++iteration) {
#pragma omp critical(cout)
std::cout << "stage 1: " << omp_get_thread_num() << '/'
<< omp_get_num_threads() << ": iteration " << iteration << '/'
<< iteration_count << std::endl;
}
#pragma omp single
std::cout << std::endl;
#pragma omp for schedule(dynamic,2)
for (int iteration = 0; iteration < iteration_count; ++iteration) {
#pragma omp critical(cout)
std::cout << "stage 2: " << omp_get_thread_num() << '/'
<< omp_get_num_threads() << ": iteration " << iteration << '/'
<< iteration_count << std::endl;
}
#pragma omp single
std::cout << std::endl;
#pragma omp for schedule(guided,2)
for (int iteration = 0; iteration < iteration_count; ++iteration) {
#pragma omp critical(cout)
std::cout << "stage 3: " << omp_get_thread_num() << '/'
<< omp_get_num_threads() << ": iteration " << iteration << '/'
<< iteration_count << std::endl;
}
}
return 0;
}
|