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 */
}
}
With explicit OpenMP tasks:
The code block that follows the OpenMP
task
directive is an explicit task. Explicit tasks are queued an executed later. Thetaskwait
directive acts similar tobarrier
, 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 defaultfirstprivate
.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).