With explicit OpenMP tasks:
#pragma omp parallel
{
#pragma omp single nowait
{
for (int i = 0; i < num_tasks; i++)
#pragma omp task
{
}
}
#pragma omp taskwait
}
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).
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