OpenMP/Loops

A motivating example
Assume we need to parallelize the following sequential code.

What we can do is assign each thread to carry out a subset of iterations.

The  in above code tells the computer to spawn 24 threads when we enter the parallel region (scope of  ). However, there is no guarantee that we would be given the exact number of threads as we ask for, due to resource limitations and environment constraints. Therefore, we call  within the parallel region to know actual the number of threads spawned. Then we assign the starting and ending iterations for each thread. For example, thread with  0 (returned by  ) will execute all iterations between   and. Each thread will get a private instance of loop index variable, and private instances of variables declared within the parallel region.

We can simplify above function with OpenMP's  worksharing construct. It may be combined with the parallel directive as.

The compiler directive  has no effect unless it is used within a parallel region. Also, it is safe to assume code that uses OpenMP library functions but does not use  anywhere, is not parallelized by OpenMP.