How is the ordered clause in OpenMP supposed to be correctly used? There is this test code to check if the loop will be executed by increasing values of n but it is not always the case.
Did I misinterpret the definition of the ordered clause?
The ordered construct specifies a structured block in a loop region that will be executed in the order of the loop iterations. This sequentializes and orders the code within an ordered region while allowing code outside the region to run in parallel.
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(){
int n;
omp_set_num_threads(4);
#pragma omp parallel
{
#pragma omp for ordered
for (n=0;n<10;n++)
printf("n = %d\n",n);
}
return 0;
}
When compiling with
gcc -Wall -Wextra -fopenmp test_par.c
The output is
./a.out
n = 0
n = 1
n = 2
n = 9
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
Summary The order clause specifies an expected order of execution for the iterations of the associated loops of a loop-associated directive. Description The order clause specifies an expected order of execution for the iterations of the associated loops of a loop-associated directive.
15.6 nowait Clause The nowait clause overrides any synchronization that would otherwise occur at the end of a construct. It can also specify that an interoperability requirement set includes the nowait property. If the construct includes an implicit barrier, the nowait clause specifies that the barrier will not occur.
The default(firstprivate) clause causes all variables in the construct that have implicitly determined data-sharing attributes to be firstprivate. The default(private) clause causes all variables referenced in the construct that have implicitly determined data-sharing attributes to be private.
The OpenMP ordered
clause is supposed to be used in two distinct stages:
for
directive, to indicate some possible ordering of iterations; and thenSo in essence, your example should be:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(){
int n;
omp_set_num_threads(4);
#pragma omp parallel
{
#pragma omp for ordered
for (n=0;n<10;n++)
#pragma omp ordered
printf("n = %d\n",n);
}
return 0;
}
Which gives:
$ g++ -fopenmp order.cc
$ ./a.out
n = 0
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9
However, two remarks here:
ordered
directiveordered
directive, you should play with the schedule
directive. A good candidate for an "effective" scheduling in the case of an ordered loop would be a schedule( static, 1 )
for example.And finally, Hristo Iliev explains it all better than I'll ever be able to here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With