OpenMP with nested loops

Use this forum to discuss the book: Using OpenMP - Portable Shared Memory Parallel Programming, by Barbara Chapman, Gabriele Jost and Ruud van der Pas Read the for book info and to download the examples. Post your feedback about the book and examples to this forum
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help:
Posts: 1
Joined: Fri May 23, 2014 2:25 pm

OpenMP with nested loops

Post by ecdogrul »

Figure 5.26 on page 150 of the book shows an efficient way of parallelizing the inner loop. I don't understand why this approach does not create nested threads. With the #pragma omp parallel enclosing the entire loop, I would expect that each thread would run its own nested loop and when #pragma omp for is encountered, each thread would further split into multiple threads. Essentially, wouldn't we end up calculating the entire nested loop as many times as the number of threads? I would appreciate any clarification.


Posts: 808
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: OpenMP with nested loops

Post by MarkB »

Hi John,

Worksharing directives such as #pragma omp for do not create threads: they share work out between the existing threads in the enclosing parallel region.
(Note that #pragma omp parallel for does create threads, as it is just shorthand for #pragma omp parallel followed by #pragma omp for - this is a common source of confusion).

Hope that helps,