@@ -1,59 +1,57 @@
|
|
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 |
-
|
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 |
}
|
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 schedule(static,2)
|
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 for schedule(dynamic,2)
|
|
|
|
|
37 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
38 |
#pragma omp critical(cout)
|
39 |
std::cout << "stage 2: " << omp_get_thread_num() << '/'
|
40 |
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
41 |
<< iteration_count << std::endl;
|
42 |
}
|
43 |
|
44 |
#pragma omp single
|
45 |
std::cout << std::endl;
|
46 |
|
47 |
+
#pragma omp for schedule(guided,2)
|
48 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
49 |
#pragma omp critical(cout)
|
50 |
std::cout << "stage 3: " << omp_get_thread_num() << '/'
|
51 |
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
52 |
<< iteration_count << std::endl;
|
53 |
}
|
54 |
}
|
55 |
|
56 |
return 0;
|
57 |
}
|