Using -O2 optimization and OpenMP

c++, openmp

Solution

No, this cannot happen. OpenMP would not be very useful if the compiler was unrolling the loops or if the program crashed when the compiler reorders loops. The OpenMP directive must specify the dependencies and side effects of the variables and parallel scopes, and the compiler takes them into account when applying the optimization passes.

Problem

Is it possible that the -O2 optimization flag re-arranges code, thereby possibly making a multi-threaded application work as un-intended? As an example of what I mean by `un-intended behavior` when code is re-arranged: A variable declared (by the programmer) to be created for each thread is moved outside the `#pragma omp parallal` such that only one single copy is created, shared by all threads.

Original source