3

I wonder if there is any technique to create parallel sections in OpenMp using a for-loop.

For example, instead of creating n different #pragma omp sections, I want to create them using an n-iteration for-loop with some changing parameters for each section.

#pragma omp parallel sections
{
   #pragma omp section
   {
      /* Executes in thread 1 */
   } 
   #pragma omp section
   {
      /* Executes in thread 2 */
   } 
   #pragma omp section
   {
      /* Executes in thread n */
   } 
}
towi_parallelism
  • 1,421
  • 1
  • 16
  • 38
  • 1
    Yes, look into the `task` directive, available in OpenMP 3.0 and 3.1 (which means that if you use Visual Studio, you are out of luck). – Hristo Iliev Dec 10 '12 at 22:32
  • 1
    This answer may also be useful for you: http://stackoverflow.com/questions/13663750/openmp-for-loop-thread-assignment/13669031 – nat chouf Dec 11 '12 at 12:55

1 Answers1

5

With explicit OpenMP tasks:

#pragma omp parallel
{
   // Let only one thread create all tasks
   #pragma omp single nowait
   {
       for (int i = 0; i < num_tasks; i++)
          #pragma omp task
          {
              // Code for task with parameters, based on i
          }
   }
   // Let the threads process all tasks
   #pragma omp taskwait

   // Further parallel processing ...
}

The code block that follows the OpenMP task directive is an explicit task. Explicit tasks are queued an executed later. The taskwait directive acts similar to barrier, but for tasks. Also see this answer to a similar question.

Tasks can create other tasks recursively. So explicit tasking can be used to process graphs and trees. But beware of the overhead - it is bigger than the overhead from most other constructs and is quite similar to the one that comes from loops with schedule(dynamic). Also the variables from the outer scope, referenced inside the task are by default firstprivate.

Note that explicit tasks are feature, that was added in OpenMP 3.0. Compilers that conform to earlier OpenMP versions might not support the task directive. Almost all modern compilers do support OpenMP 3.0 or later with the notable exception of Microsoft Visual C++, which only supports OpenMP 2.0 (even in VS2012).

Community
  • 1
  • 1
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Does the `nowait` make a difference since there is only one thread creating the tasks? – thatWiseGuy Apr 27 '17 at 00:06
  • It depends - see the other answer I've referred to. Given the sample code here - no. With `nowait` one could put additional code between the `single` constructs and the `taskwait` construct. Without `nowait`, the `taskwait` construct is redundant as the threads will process the tasks while in the implicit barrier. – Hristo Iliev Apr 27 '17 at 11:18