#pragma omp task

Purpose

The task pragma can be used to explicitly define a task.

Use the task pragma when you want to identify a block of code to be executed in parallel with the code outside the task region. The task pragma can be useful for parallelizing irregular algorithms such as pointer chasing or recursive algorithms. The task directive takes effect only if you specify the SMP compiler option.

Syntax

Read syntax diagramSkip visual syntax diagram
                        .-,------.   
                        V        |   
>>-#--pragma--omp task----clause-+-----------------------------><

Parameters

The clause parameter can be any of the following types of clauses:
default (shared | none)
Defines the default data scope of variable in each task. Only one default clause can be specified on an omp task directive.

Specifying default(shared) is equivalent to stating each variable in a shared(list) clause.

Specifying default(none) requires that each data variable visible to the construct must be explicitly listed in a data scope clause, with the exception of variables with the following attributes:
  • Threadprivate
  • Automatic and declared in a scope inside the construct
  • Objects with dynamic storage duration
  • Static data members
  • The loop iteration variables in the associated for-loops for a work-sharing for or parallel for construct
  • Static and declared in a scope inside the construct
final (exp)
If you specify a final clause and exp evaluates to a nonzero value, the generated task is a final task. All task constructs encountered inside a final task create final and included tasks.

You can specify only one final clause on the task pragma.

firstprivate (list)
Declares the scope of the data variables in list to be private to each thread. 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.
if (exp)
When the if clause is specified, an undeferred task is generated if the scalar expression exp evaluates to a nonzero value. Only one if clause can be specified.
mergeable
If you specify a mergeable clause and the generated task is an undeferred task or included task, a merged task might be generated.
private (list)
Declares the scope of the data variables in list to be private to each thread. 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 threads.
untied
When a task region is suspended, untied tasks can be resumed by any thread in a team. The untied clause on a task construct is ignored if either of the following conditions is met:
  • A final clause is specified on the same task construct and the final clause expression evaluates to a nonzero value.
  • The task is an included task.

Usage

A final task is a task that makes all its child tasks become final and included tasks. A final task is generated when either of the following conditions is a nonzero value:
  • A final clause is specified on a task construct and the final clause expression evaluates to nonzero value.
  • The generated task is a child task of a final task.

An undeferred task is a task whose execution is not deferred with respect to its generating task region. In other words, the generating task region is suspended until the undeferred task has finished running. An undeferred task is generated when an if clause is specified on a task construct and the if clause expression evaluates to zero.

An included task is a task whose execution is sequentially included in the generating task region. In other words, an included task is undeferred and executed immediately by the encountering thread. An included task is generated when the generated task is a child task of a final task.

A merged task is a task that has the same data environment as that of its generating task region. A merged task might be generated when both the following conditions nonzero values:
  • A mergeable clause is specified on a task construct.
  • The generated task is an undeferred task or an included task.

The if clause expression and the final clause expression are evaluated outside of the task construct, and the evaluation order cannot be specified.