next up previous contents
Next: Pure Procedures Up: Expressing Parallel Execution Previous: Do-loops and Forall Statements

 

Forall Construct

The FORALL construct, which is also in Fortran 95, is very similar to the FORALL statement except that the indices and mask can be used to control more than one parallel assignment (a bit like the WHERE construct). As well as containing regular assignment statements, the body of a FORALL may also contain a further FORALL block or a WHERE block. This can lead to mind-bendingly complex assignments being expressed very concisely!

It has the following syntax:

 
FORALL(< forall-triplet-list >[,< scalar-mask >])

< assignment-stmt >

....

END FORALL

For example,

    FORALL (i=1:n:2, j=n:1:-2, A(i,j).NE.0)
           A(i,j) = 1/A(i,j) ! s1
           A(i,i) = B(i)     ! s2
    END FORALL

s1 executed first followed by s2

Can also nest FORALL s,

    FORALL (i=1:3, j=1:3, i>j)
     WHERE  (ABS(A(i,i,j,j)) .LT. 0.1) A(i,i,j,j) = 0.0
     FORALL (k=1:3, l=1:j, k+l>i) A(i,j,k,l) = j*k+l
    END FORALL

The two examples given here demonstrate a FORALL containing two assignments and a FORALL containing nested WHERE and FORALL statements.

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Pure Procedures Up: Expressing Parallel Execution Previous: Do-loops and Forall Statements

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.