@@ -1,28 +1,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 |
// ...
|
18 |
-
#pragma omp parallel num_threads(thread_count)
|
19 |
-
|
|
|
20 |
// ...
|
21 |
#pragma omp critical(cout)
|
22 |
-
std::cout <<
|
23 |
-
<<
|
24 |
// ...
|
25 |
}
|
26 |
// ...
|
27 |
return 0;
|
28 |
}
|
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 |
+
std::cout << omp_get_thread_num() << '/' << omp_get_num_threads()
|
29 |
+
<< ": iteration " << iteration << '/' << iteration_count << std::endl;
|
30 |
// ...
|
31 |
}
|
32 |
// ...
|
33 |
return 0;
|
34 |
}
|