FORALL

Purpose

The FORALL statement performs assignment to groups of subobjects, especially array elements. Unlike the WHERE statement, assignment can be performed on an elemental level rather than on an array level. The FORALL statement also allows pointer assignment.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-FORALL--forall_header--forall_assignment--------------------><

forall_header
Read syntax diagramSkip visual syntax diagram
>>-(--+-------------------+--forall_triplet_spec_list--+---------------------+--)-><
      |               (1) |                            '-,--scalar_mask_expr-'      
      '-type_spec--::-----'                                                         

Notes:
  1. Fortran 2008
forall_triplet_spec
Read syntax diagramSkip visual syntax diagram
>>-index_name-- = --subscript-- : --subscript------------------->

>--+-------------+---------------------------------------------><
   '- : --stride-'   

forall_assignment
is either assignment_statement or pointer_assignment_statement
Fortran 2008 begins type_spec
specifies an integer type Fortran 2008 ends
scalar_mask_expr
is a scalar logical expression
subscript, stride
are each scalar integer expressions

Rules

Only pure procedures can be referenced in the mask expression of forall_header and in a forall_assignment (including one referenced by a defined operation, assignment, or finalization).

index_name must be a scalar integer variable. It is also a statement entity; that is, it does not affect and is not affected by other entities in the scoping unit.

Fortran 2008 begins
You must explicitly declare the FORALL index variables in the following cases:
  • The value range of index_name exceeds the range of the default integer type.
  • The IMPLICIT NONE statement is in effect. See Example 2.

You can specify type_spec to declare index_name within the scope of the FORALL statement. If you specify type_spec in forall_header, you can reuse any accessible identifier for index_name.

Fortran 2008 ends

In forall_triplet_spec_list, neither a subscript nor a stride can contain a reference to any index_name in the forall_triplet_spec_list. Evaluation of any expression in forall_header must not affect evaluation of any other expression in forall_header.

Given the forall_triplet_spec
     index1 = s1:s2:s3
the maximum number of index values is determined by:
     max = INT((s2-s1+s3)/s3)
If the stride (s3 above) is not specified, a value of 1 is assumed. If max ≤ 0 for any index, forall_assignment is not executed. For example,
     index1 = 2:10:3    !  The index values are 2,5,8.
                           max = INT((10-2+3)/3) = 3.

     index2 = 6:2:-1    !  The index values are 6,5,4,3,2.
     index2 = 6:2       !  No index values.

If the mask expression is omitted, a value of .TRUE. is assumed.

No atomic object can be assigned to more than once. Assignment to a nonatomic object assigns to all subobjects or associates targets with all subobjects.

Examples

Example 1
INTEGER A(1000,1000), B(200)
I=17
FORALL (I=1:1000,J=1:1000,I.NE.J) A(I,J)=A(J,I)
PRINT *, I    ! The value 17 is printed because the I
              ! in the FORALL has statement scope.
FORALL (N=1:200:2) B(N)=B(N+1)
END
Fortran 2008 begins
Example 2
IMPLICIT NONE
INTEGER, PARAMETER :: limit=60000
INTEGER(1) :: flag(limit)
flag=0
FORALL (INTEGER(4) :: i=2:limit:2) flag(i)=1
PRINT *, flag(limit)
END
Fortran 2008 ends


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