Using -O2 and OpenMP Optimization

Is it possible that the -O2 optimization flag reorders the code, thereby making it possible for a multi-threaded application to work as intended?

As an example of what I mean by un-intended behavior when re-compiling code: a variable declared (by the programmer) that must be created for each thread is moved outside of #pragma omp parallal , so that only one single copy is created, a common one for all threads.

+4
source share
1 answer

No, that cannot be. OpenMP is not very useful if the compiler unwraps the loops or if the program crashes when the compiler reorders the loops. The OpenMP directive should specify the dependencies and side effects of variables and parallel areas, and the compiler takes them into account when applying optimization passes.

+3
source

All Articles