#pragma omp taskloop
Purpose
The taskloop pragma is used to specify that the iterations of one or more associated loops are executed in parallel using OpenMP tasks. The iterations are distributed across tasks that are created by the construct and scheduled to be executed.
Syntax
Parameters
The clause parameter can
be any of the following types of clauses:
- collapse(n)
- Specifies the number of loops that are associated with the omp taskloop construct. n must be a constant positive integer expression.
- If no collapse clause is specified, only the loop that immediately follows the omp taskloop directive is associated with the omp taskloop construct.
- default(firstprivate|private|shared|none)
- Defines the default data scope of variable in each generated task. Only one default clause can be specified on an omp taskloop directive.
- final(exp)
- Generates a final task if exp evaluates to a nonzero value. If exp is a variable, an implicit reference to the variable in all enclosing constructs occurs.
- firstprivate(list)
- Declares the scope of the data variables in list to be private to each task. Each new private object is initialized with the value of the original variable as if there was an implied declaration within the statement block. Data variables in list are separated by commas.
- grainsize(grain-size)
- Controls how many logical loop iterations are assigned to each created task. The number of logical loop iterations assigned to each created task is greater than or equal to the minimum of the value of grain-size and the number of logical loop iterations, but less than two times the value of grain-size. grain-size must be a positive integer expression.
- if([taskloop:]exp)
- Generates an undeferred task if the scalar expression exp evaluates to a nonzero value. Only one if clause can be specified.
- lastprivate(list)
- Declares the scope of the data variables in list to be private to each task. The final value of each variable in list, if assigned, will be the value assigned to that variable in the last iteration. Variables not assigned a value will have an indeterminate value. Data variables in list are separated by commas.
- mergeable
- Specifies that each generated task is a mergeable task. mergeable task is a task that might be a merged task if it is an undeferred task or an included task.
- nogroup
- Removes the implicit taskgroup region that encloses the taskloop construct.
- num_tasks(num-tasks)
- Creates as many tasks as the minimum of num-tasks and the number of logical loop iterations. num-tasks must evaluate to a positive integer.
- priority(priority-value)
- Applies priority-value to the generated tasks as if it was specified for each task individually. If the priority clause is not specified, the generated tasks have the default zero task priority.
- private(list)
- Declares the scope of the data variables in list to be private to each task. Data variables in list are separated by commas.
- shared(list)
- Declares the scope of the comma-separated data variables in list to be shared across all tasks.
- untied
- Specifies that all the created tasks are untied tasks.
Examples
Example 1The taskloop construct generates as many as 20 tasks. The iterations of the for loop are distributed among the tasks generated for the taskloop construct.
#pragma omp parallel
#pragma omp single
#pragma omp taskloop num_tasks(20)
for (i=0; i<N; i++) {
arr[i] = i*i;
}
Example 2The task to call the compute_update routine is independent from the calculation in the taskloop construct. The computation can be distributed to different tasks. Because the taskgroup construct ensures that all sibling tasks complete execution, the nogroup clause on the taskloop construct is to remove the implicit taskgroup in the taskloop construct.
#pragma omp parallel
{
#pragma omp single
#pragma omp taskgroup
{
#pragma omp task
compute_update(data1);
#pragma omp taskloop collapse(2) nogroup
for (i=0; i<N; i++)
for (j=0; j<M; j++)
data2[i,j] = data2[i,j] + 1.3;
}
// Both data1 and data2 are updated
}



