FORALL statement is included in Fortran 95. Syntax:
FORALL(forall-triplet-list
[,
scalar-mask
])&
assignment-stmt
![]()
It:
For example,
FORALL (i=1:n,j=1:m,A(i,j).NE.0) & A(i,j) = 1/A(i,j)
The stated assignment is performed in parallel for all specified values of i and j for which the mask expression is .TRUE..
When used without the mask, the FORALL statement simply performs conceptually parallel assignment -- any assignment that can be expressed in array syntax can also be expressed by a FORALL statement (or construct). The FORALL statement can also be used to express parallel assignments which it is impossible or very tricky to code using Fortran 90 features. Note that although the index variables look like the DO loop equivalent -- the colons imply that this is array syntax.
If a mask is added, such as in the given example, FORALL can be made to resemble a WHERE statement. Indeed, the example could actually be expressed using a WHERE statement:
WHERE ( A.NE.0 ) A = 1/A
Again, very complex masked assignments can be made syntactically simpler by the use of a masked FORALL statement.