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