Reputation: 127
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
Upvotes: 7
Views: 5092
Reputation: 9499
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
Upvotes: 8