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 */
   } 
}
2
  • 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). Commented Dec 10, 2012 at 22:32
  • 1
    This answer may also be useful for you: stackoverflow.com/questions/13663750/… Commented Dec 11, 2012 at 12:55

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Does the nowait make a difference since there is only one thread creating the tasks?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.