-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_section.C
54 lines (45 loc) · 1.45 KB
/
test_section.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "StopWatch.h"
#include <omp.h>
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::chrono;
int main(){
const long maxIteration = 100000;
bool debug_output=false;
// This is needed to allow the presence of a parallel for nested in another parallel region
omp_set_nested(1);
int outputCounter = 0;
bool done = false;
#pragma omp parallel sections shared(outputCounter,done) num_threads(2)
{
#pragma omp section
{
if ( omp_get_thread_num()==0 ){
if(debug_output) printf("monitor th_id: %d, n_th: %d\n",omp_get_thread_num(), omp_get_num_threads());
while(!done){
printf("%d/%ld\r",outputCounter,maxIteration);
std::this_thread::sleep_for (microseconds(50));
}
printf("%d/%ld\n",outputCounter,maxIteration);
}
}
#pragma omp section
{
int section = 2;
#pragma omp parallel num_threads(16)
{
if(debug_output) printf("computing th_id: %d, n_th: %d\n",omp_get_thread_num(), omp_get_num_threads());
#pragma omp for
for( int i=0; i<maxIteration; i++){
// This sleep emulates some heavy computation... Feel free to add yours!
std::this_thread::sleep_for (microseconds(1000));
#pragma omp atomic update
outputCounter++;
}
}
done = true;
}
}
return 0;
}