@@ -1,23 +1,51 @@
|
|
1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
2 |
#include <omp.h>
|
3 |
#include <iostream>
|
4 |
|
5 |
int main(int argc, char* argv[]) {
|
6 |
int thread_count = omp_get_max_threads();
|
7 |
if (argc >= 2) {
|
8 |
thread_count = atoi(argv[1]);
|
9 |
}
|
10 |
|
11 |
int iteration_count = thread_count;
|
12 |
if (argc >= 3) {
|
13 |
iteration_count = atoi(argv[2]);
|
14 |
}
|
15 |
|
16 |
-
#pragma omp parallel
|
17 |
default(none) shared(iteration_count, std::cout)
|
|
|
|
|
18 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
19 |
#pragma omp critical(stdout)
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
}
|
1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
2 |
#include <omp.h>
|
3 |
#include <iostream>
|
4 |
|
5 |
int main(int argc, char* argv[]) {
|
6 |
int thread_count = omp_get_max_threads();
|
7 |
if (argc >= 2) {
|
8 |
thread_count = atoi(argv[1]);
|
9 |
}
|
10 |
|
11 |
int iteration_count = thread_count;
|
12 |
if (argc >= 3) {
|
13 |
iteration_count = atoi(argv[2]);
|
14 |
}
|
15 |
|
16 |
+
#pragma omp parallel num_threads(thread_count) \
|
17 |
default(none) shared(iteration_count, std::cout)
|
18 |
+
{
|
19 |
+
#pragma omp for
|
20 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
21 |
#pragma omp critical(stdout)
|
22 |
+
std::cout << "stage 1: " << omp_get_thread_num() << '/'
|
23 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
24 |
+
<< iteration_count << std::endl;
|
25 |
+
}
|
26 |
+
|
27 |
+
#pragma omp single
|
28 |
+
std::cout << /*omp_get_thread_num() <<*/ std::endl;
|
29 |
+
// #pragma omp barrier
|
30 |
+
|
31 |
+
#pragma omp for
|
32 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
33 |
+
#pragma omp critical(stdout)
|
34 |
+
std::cout << "stage 2: " << omp_get_thread_num() << '/'
|
35 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
36 |
+
<< iteration_count << std::endl;
|
37 |
+
}
|
38 |
+
|
39 |
+
#pragma omp single
|
40 |
+
std::cout << /*omp_get_thread_num() <<*/ std::endl;
|
41 |
+
// #pragma omp barrier
|
42 |
+
|
43 |
+
#pragma omp for
|
44 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
45 |
+
#pragma omp critical(stdout)
|
46 |
+
std::cout << "stage 3: " << omp_get_thread_num() << '/'
|
47 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
48 |
+
<< iteration_count << std::endl;
|
49 |
+
}
|
50 |
}
|
51 |
}
|