Consider the following code:
#pragma omp parallel
for (int run = 0; run < 10; run++)
{
std::vector<int> out;
#pragma omp for
for (int i = 0; i < 1'000'000; i++)
{
...
}
}
The intent is to spawn OpenMP threads only once before the outer iterations, which are supposed to run sequentially, and then schedule the inner iterations* multiple times on the same existing parallel threads.
However, the outer iteration is not marked #pragma omp single. Should I assume that it will do something like running the same outer code in all the threads at the same time, thereby a) concurrently modifying the run variable at ill-advised times, and b) having a possible conflict in the instantiation of out. Or are these implicitly private and not shared between threads because they are inside the parallel section?
How would this be executed in practice according to the specification, and what do the various implementations do in practice?
* My actual code is a bit more complex than this ; in practice I intend to put the inner for loop in an orphaned function that will be called by an outer function which handles setting up the parallel execution.
runandoutvariables are thread-private in your case, so there are no conflicts. Live demo: godbolt.org/z/MPocj1acq. The outer loop is run by each thread individually, and the inner loop is run in parallel by all the threads, which seems to be what you want. However, if it logically makes sense, I wouldn't be afraid of puttingomp parallel forjust before the inner loop. On modern system, creation of threads is fast, and OpenMP runtimes are able to reuse threads without creating them repeatedly under the hood.omp for? How does each thread coordinate to determine what batch of inner iterations it should run, particularly whenscheduleis notstatic?#pragma omp paralleldoes not mark outer iterations. But actually it does, and it does not mark inner iterations.#pragma omp paralleldefines the parallel region, and#pragma omp fordistributes the iterations of your inner loop between the threads. Your use case looks correct and should not present any issues.