#pragma omp simd

Purpose

The omp simd directive is applied to a loop to indicate that multiple iterations of the loop can be executed concurrently by using SIMD instructions.

Syntax

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

Read syntax diagramSkip visual syntax diagram
>>-for-loops---------------------------------------------------><

Parameters

clause is any of the following clauses:
aligned(list[:alignment])
Declares that the object to which argument in list points is byte aligned according to the number of bytes expressed by alignment. alignment must be a constant positive integer expression. If alignment is not specified, the alignment is defined by the target platforms. A list item cannot appear in more than one aligned clause.
collapse(n)1
Specifies the number of loops that the omp simd directive applies to. The expression that is represented by n must be a constant positive integer expression. If the collapse clause is not specified, the omp simd directive applies to only the loop that immediately follows.
lastprivate(list)
Declares the data variables in list to be private to each SIMD lane. The final value of each variable in list is as follows:
  • If assigned a value, each variable has the value that is assigned to that variable in the sequentially last iteration.
  • If not assigned a value, each variable has an indeterminate value.
Data variables in list are separated by commas.
linear(list[:linear-step])
Declares the data variables in list to be private to each SIMD lane and to have a linear relationship with the iteration space 2 of a loop. The value of the new list item on each iteration of the associated loops is the result of adding the following values:
  • The value of the original list item before entering the construct
  • The product of the logical number of the iteration and linear-step
The final value of each variable in list has the value that is assigned to that variable in the sequentially last iteration. The default value for linear-step is 1.
private(list)
Declares the data variables in list to be private to each SIMD lane. Data variables in list are separated by commas.
reduction(reduction-identifier:list)
Performs a reduction on each data variable in list according to reduction-identifier. The clause creates a private copy for data variables in list for each SIMD lane, initializes the private copies with the initializer value of reduction-identifier, and updates the original list item after the end of the region with the values of the private copies by using the combiner that is associated with reduction-identifier.

Scalar variables are supported in list. Items in list are separated by commas. reduction-identifier is one of the following operators: +, -, *, &, |, ^, &&, and ||.

safelen(length)1 3
Indicates that no two iterations that are executed concurrently by using SIMD instructions can have a distance in the logical iteration space 2 greater than the value expressed by length. length must be a constant positive integer expression.
simdlen(length)1 3
Specifies the preferred number of iterations to be executed concurrently. The number of SIMD loop iterations that are executed concurrently is implementation defined. Each concurrent iteration is executed by a different SIMD lane. You must preserve lexical forward dependencies in the iterations of the original loop within each set of concurrent iterations. length must be a constant positive integer expression.
Notes:
  1. You can specify this clause only once for each omp simd directive.
  2. Iterations in a SIMD loop are logically numbered from 0 to N-1, where N is the number of loop iterations. The logical numbering denotes the sequence in which the iterations would be executed if the associated loop was executed not using SIMD instructions.
  3. If you specify both the simdlen and safelen clauses, the value of the simdlen parameter must be less than or equal to the value of the safelen parameter.

Rules

All loops that are associated with the construct must be perfectly nested; that is, you cannot insert any intervening code or OpenMP directive between any two loops.

The associated loops must be structured blocks.

A program that branches into or out of a simd region is nonconforming.

An ordered construct with the simd clause is the only OpenMP construct that can be encountered during the execution of a simd region.

Examples

Example 1

The following SIMD construct enables the execution of multiple iterations of the associated loop concurrently by using SIMD instructions.

#pragma omp simd
{
  for (i=0; i<N; i++) {
    a[i] = a[i] + b[i] * c[i];
  }
}
Example 2

The loop in the following example contains a lexically forward loop-carried dependency that prohibits concurrent execution of all iterations of the loop. The omp simd safelen(4) directive specifies that the loop iterations that are at a distance of four or less in the logical iteration space can be executed in parallel by using SIMD instructions.

#pragma omp simd safelen(4)
{
  for (i=0; i<(N-4); i++) {
    a[i] = a[i+4] + b[i] * c[i];
  }
}


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us