|
@@ -1,34 +1,59 @@
|
|
| 1 |
/*
|
| 2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
| 3 |
* Creates a secondary thread that greets in the standard output
|
| 4 |
*/
|
| 5 |
|
| 6 |
#include <omp.h>
|
| 7 |
|
| 8 |
#include <cstdlib>
|
| 9 |
#include <iostream>
|
| 10 |
|
| 11 |
int main(int argc, char* argv[]) {
|
| 12 |
int thread_count = omp_get_max_threads();
|
| 13 |
if (argc >= 2) {
|
| 14 |
thread_count = atoi(argv[1]);
|
| 15 |
}
|
| 16 |
|
| 17 |
int iteration_count = thread_count;
|
| 18 |
if (argc >= 3) {
|
| 19 |
iteration_count = atoi(argv[2]);
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
#pragma omp parallel for num_threads(thread_count) \
|
| 24 |
default(none) shared(std::cout, iteration_count)
|
|
|
|
|
|
|
| 25 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 26 |
-
// ...
|
| 27 |
#pragma omp critical(cout)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
}
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return 0;
|
| 34 |
}
|
| 1 |
/*
|
| 2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
| 3 |
* Creates a secondary thread that greets in the standard output
|
| 4 |
*/
|
| 5 |
|
| 6 |
#include <omp.h>
|
| 7 |
|
| 8 |
#include <cstdlib>
|
| 9 |
#include <iostream>
|
| 10 |
|
| 11 |
int main(int argc, char* argv[]) {
|
| 12 |
int thread_count = omp_get_max_threads();
|
| 13 |
if (argc >= 2) {
|
| 14 |
thread_count = atoi(argv[1]);
|
| 15 |
}
|
| 16 |
|
| 17 |
int iteration_count = thread_count;
|
| 18 |
if (argc >= 3) {
|
| 19 |
iteration_count = atoi(argv[2]);
|
| 20 |
}
|
| 21 |
|
| 22 |
+
#pragma omp parallel num_threads(thread_count) \
|
|
|
|
| 23 |
default(none) shared(std::cout, iteration_count)
|
| 24 |
+
{
|
| 25 |
+
#pragma omp for
|
| 26 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
|
|
|
| 27 |
#pragma omp critical(cout)
|
| 28 |
+
std::cout << "stage 1: " << omp_get_thread_num() << '/'
|
| 29 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 30 |
+
<< iteration_count << std::endl;
|
| 31 |
}
|
| 32 |
+
|
| 33 |
+
#pragma omp single
|
| 34 |
+
std::cout << std::endl;
|
| 35 |
+
|
| 36 |
+
// #pragma omp barrier
|
| 37 |
+
|
| 38 |
+
#pragma omp for
|
| 39 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 40 |
+
#pragma omp critical(cout)
|
| 41 |
+
std::cout << "stage 2: " << omp_get_thread_num() << '/'
|
| 42 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 43 |
+
<< iteration_count << std::endl;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
#pragma omp single
|
| 47 |
+
std::cout << std::endl;
|
| 48 |
+
|
| 49 |
+
#pragma omp for
|
| 50 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 51 |
+
#pragma omp critical(cout)
|
| 52 |
+
std::cout << "stage 3: " << omp_get_thread_num() << '/'
|
| 53 |
+
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 54 |
+
<< iteration_count << std::endl;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
return 0;
|
| 59 |
}
|