TASKLOOP / END TASKLOOP

Purpose

The TASKLOOP directive specifies 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.

Required option

-qsmp

Syntax

Read syntax diagramSkip visual syntax diagramTASKLOOP ,taskloop_clause
Read syntax diagramSkip visual syntax diagramblock
Read syntax diagramSkip visual syntax diagramEND TASKLOOP

where taskloop_clause is:

Read syntax diagramSkip visual syntax diagramcollapse_clausedefault_clausefinal_clausefirstprivate_clausegrainsize_clauseif_clauselastprivate_clausemergeable_clausenogroup_clausenum_tasks_clausepriority_clauseprivate_clauseshared_clauseuntied_clause
collapse_clause
Specifies the number of loops that are associated with the TASKLOOP construct. The syntax is COLLAPSE(n). n must be a constant positive integer expression.
If no collapse_clause is specified, only the loop that immediately follows the TASKLOOP directive is associated with the TASKLOOP construct.
default_clause
Defines the default data scope of variable in each generated task. The syntax is DEFAULT(FIRSTPRIVAT|PRIVATE|SHARED|NONE).
Only one default_clause can be specified on a TASKLOOP directive.
final_clause
Generates a final task if the specified expression evaluates to a nonzero value. The syntax is FINAL(exp).
If exp is a variable, an implicit reference to the variable in all enclosing constructs occurs.
firstprivate_clause
Declares the scope of the listed data variables 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. The syntax is FIRSTPRIVATE(list). Data variables in list are separated by commas.
grainsize_clause
Controls how many logical loop iterations are assigned to each created task. The syntax is GRAINSIZE(grain-size). 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_clause
Generates an undeferred task if the scalar expression evaluates to a nonzero value. The syntax is IF([TASKLOOP:]exp). Only one IF clause can be specified.
lastprivate_clause
Declares the scope of the listed data variables to be private to each task. The final value of each listed variable, if assigned, will be the value assigned to that variable in the last iteration. Variables not assigned a value will have an indeterminate value. The syntax is LASTPRIVATE(list). Data variables in list are separated by commas.
mergeable_clause
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. The syntax is MERGEABLE.
nogroup_clause
Removes the implicit TASKGROUP region that encloses the TASKLOOP construct. The syntax is NOGROUP.
num_tasks_clause
Creates as many tasks as the minimum of the specified number and the number of logical loop iterations. The syntax is NUM_TASKS(num-tasks). num-tasks must evaluate to a positive integer.
priority_clause
Applies the specified priority value to the generated tasks as if it was specified for each task individually. The syntax is PRIORITY(priority-value). If priority_clause is not specified, the generated tasks have the default zero task priority.
private_clause
Declares the scope of the listed data variables to be private to each task. The syntax is PRIVATE(list). Data variables in list are separated by commas.
shared_clause
Declares the scope of the comma-separated listed data variables to be shared across all tasks. The syntax is SHARED(list).
untied_clause
Specifies that all the created tasks are untied tasks. The syntax is UNTIED.

Examples

Example 1

The TASKLOOP construct generates as many as 20 tasks. The iterations of the DO loop are distributed among the tasks generated for the TASKLOOP construct.

!$OMP PARALLEL
!$OMP SINGLE
!$OMP TASKLOOP NUM_TASKS(20)
  DO i= 1, N
    A(i) = i * i
  END DO
!$OMP END TASKLOOP
!$OMP END SINGLE
!$OMP END PARALLEL
Example 2

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

!$OMP PARALLEL
!$OMP SINGLE
!$OMP TASKGROUP
!$OMP TASK
 CALL compute_update(data1)
!$OMP END TASK

!$OMP TASKLOOP COLLAPSE(2) NOGROUP
 DO i = 1, n
 DO j = 1, m
 data2(i,j) = data2(i,j) + 1.3
 END DO
 END DO
!$OMP END TASKLOOP
!$OMP END TASKGROUP
! Both data1 and data2 are updated
!$OMP END SINGLE
!$OMP END PARALLEL