Intel® C++ Compiler 16.0 User and Reference Guide
Specifies a code block in a worksharing loop that will be run in the order of the loop iterations.
#pragma omp ordered |
structured-block
None.
One thread at a time is allowed in an ordered section. Threads are allowed to enter in the order of the loop iterations. No thread can enter an ordered section until it can be guaranteed that all previous iterations have completed or will never execute an ordered section. This sequentializes and orders code within ordered sections while allowing code outside the section to run in parallel.
The following example demonstrates how to use this pragma to allow each thread to calculate a private value for the variable intermediate, then sums up the private intermediate values into the shared variable running_calc in the same order as would have been done in the serial version of the for loop. Preserving the order of operations can make sure that the floating point result will be the same as it would have been in a serial version of the code:
Example |
---|
#include <omp.h> float running_calc = 0.0; float intermediate; #pragma omp parallel for ordered private(intermediate) { for (i=0; i<N; i++) { // compute private “intermediate” value #pragma omp ordered { running_calc = intermediate + running_calc; } } } |