Parallel sections in OpenMP using a loop

2019-05-23 02:33发布

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 */
   } 
}

1条回答
在下西门庆
2楼-- · 2019-05-23 03:09

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

查看更多
登录 后发表回答