Much troubled by these two questions when implementing my openMP programs
Q1: when does the parallel region and different construct stop?
OpenMP seems to promote using {} as the separator between construct or parallel regions, it can sometimes get confusing or against its original intention when conflicting with the {} used by for loop or in cases we purposely choose not to use it for code simplicity
example 1:
int main() {
int i, j;
int t =0;
int a[sizeA];
for (i=0;i<sizeA;i++)
a[i] =1;
double elapsed =-omp_get_wtime();
#pragma omp parallel for reduction(+: t)
for(j=0; j<sizeA; j++)
t=t+a[j];
--------------------1-----------------------------------------------------
#pragma omp master
printf("The sum of the array %d\n", t);
---------------------2-------------------------------------------------------
elapsed+=omp_get_wtime();
printf("The sum of the array In [REDUCTION] is %d: \n", t);
printf("The time cost is %f: \n", elapsed);
-----------------------------3--------------------------------------
}
In the above example, does the parallel region stop at 1 or 2 or 3 (as marked in the program)? According to the test result, it stops at location 2. cause section between 2-3 is executed only once, I find this rather confusing, why this?
I am also quite against the use of combined directive like
#pragma omp parallel for bla bla
, which messed the situation even more, the same code, a little different, added {} for for loop
#pragma omp parallel for reduction(+: t)
for(j=0; j<sizeA; j++)
{ //================difference added here================
t=t+a[j];
printf("hi, everyone\n");
} //===============difference added here ==================
//--------------------1-----------------------------------------------------
#pragma omp master
printf("The sum of the array %d\n", t);
//---------------------2-------------------------------------------------------
elapsed+=omp_get_wtime();
printf("The sum of the array In [REDUCTION] is %d: \n", t);
printf("The time cost is %f: \n", elapsed);
// -----------------------------3--------------------------------------
}
In the second example, does the parallel region stops in 1? in 2? if I want to make the parallel region include the #pragma omp master
construct, do I have to add extra brackets for the parallel region? and consequently, break the combined directive#pragma omp parallel for
, like folloing: or there is a better way(if any, would be super happy)
#pragma omp parallel
{
#pragma omp for reduction(+: t)
for(j=0; j<sizeA; j++)
{
t=t+a[j];
printf("hi, everyone\n");
}
#pragma omp master
bla bla
}
===================================================================== **Q2: which kinds of construct can rest inside the same parallel region? **
Like in the first example, #pragma omp for
and #pragma omp master
share the same parallel region by default, however, anything following the #pragma omp master
is not even though there is no {} explicitly saying this, what kind of construct can share the same parallel region? like working sharing construct vs Synchronization Constructs
Any reference on this?
Many thanks!