Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does OpenMP handle nested loops?

Does the following code just parallelize the first (outer) loops, or it parallelize the entire nested loops?

    #pragma omp parallel for     for (int i=0;i<N;i++)     {        for (int j=0;j<M;j++)       {        //do task(i,j)//       }     } 

I just want to make sure if the above code will parallelize the entire nested for-loops (thus one thread directly related task(i,j)), or it only parallelizes the outer for-loop (thus it ensures that, for each parrallel thread with loop index i, its inner loop will be done sequentially in a single thread, which is very import).

like image 834
user0002128 Avatar asked Nov 13 '12 07:11

user0002128


People also ask

How do I parallelize nested loops in OpenMP?

Parallelizing nested loops. If we have nested for loops, it is often enough to simply parallelize the outermost loop: a(); #pragma omp parallel for for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { c(i, j); } } z();

Is nested parallelism possible in OpenMP?

OpenMP parallel regions can be nested inside each other. If nested parallelism is disabled, then the new team created by a thread encountering a parallel construct inside a parallel region consists only of the encountering thread. If nested parallelism is enabled, then the new team may consist of more than one thread.

Is OpenMP multithreaded?

OpenMP is an implementation of multithreading, a method of parallelizing whereby a primary thread (a series of instructions executed consecutively) forks a specified number of sub-threads and the system divides a task among them.

How does OpenMP parallel for work?

OpenMP in a nutshellParallel code with OpenMP marks, through a special directive, sections to be executed in parallel. The part of the code that’s marked to run in parallel will cause threads to form. The main tread is the master thread. The slave threads all run in parallel and run the same code.


1 Answers

The lines you have written will parallelize only the outer loop. To parallelize both you need to add a collapse clause:

#pragma omp parallel for collapse(2)     for (int i=0;i<N;i++)     {        for (int j=0;j<M;j++)       {        //do task(i,j)//       }     } 

You may want to check OpenMP 3.1 specifications (sec 2.5.1) for more details.

like image 107
Massimiliano Avatar answered Sep 20 '22 18:09

Massimiliano