Next: Scalarization of the Up: The FORALL Statement Previous: Interpretation of Element

Examples of the FORALL Statement

FORALL (j=1:m, k=1:n) x(k,j) =index.html y(j,k) FORALL (k=1:n) x(k,1:m) = y(1:m,k) These statements both copy columns 1 through index.html of array index.html into rows 1 through index.html of array index.html. This is equivalent to the standard Fortran 90 statement

x(1:n,1:m) =index.html TRANSPOSE(y(1:m,1:n)) FORALL (i=1:n, j=1:n) x(i,j) = 1.0 / REAL(i+j-1) This FORALL sets array element index.html to the value index.html for values of index.html and index.html between 1 and index.html. In Fortran 90, the same operation can be performed by the statement

x(1:n,1:n) =index.html 1.0/REAL( SPREAD((/(i,i=1,n)/),DIM=2,NCOPIES=n) & + SPREAD((/(j,j=1,n)/),DIM=1,NCOPIES=n) - 1 ) Note that the FORALL statement does not imply the creation of temporary arrays and is much more readable.

FORALL (i=1:n, j=1:n, y(i,j).NE.0.0) x(i,j) =index.html 1.0 / y(i,j) This statement takes the reciprocal of each nonzero element of array index.html and assigns it to the corresponding element of array index.html. Elements of index.html that are zero do not have their reciprocal taken, and no assignments are made to the corresponding elements of index.html. This is equivalent to the standard Fortran 90 statement

WHERE (y(1:n,1:n) .NE. 0.0) x(1:n,1:n) =index.html 1 / y(1:n,1:n) TYPE monarch INTEGER, POINTER :: p END TYPE monarch TYPE(monarch) :: a(n) INTEGER, TARGET :: b(n)

! Set up a butterfly pattern FORALL (j=1:n) a(j)This FORALL statement sets the elements of array index.html to point to a permutation of the elements of index.html. When index.html and index.html, then elements 1 through 8 of index.html point to elements 3, 4, 1, 2, 7, 8, 5, and 6 of index.html, respectively. This requires a DO loop or other control flow in Fortran 90.

FORALL ( i=1:n ) x(indx(i)) =index.html x(i) This FORALL statement is equivalent to the Fortran 90 array assignment

x(indx(1:n)) =index.html x(1:n) If index.html contains a permutation of the integers from 1 to index.html, then the final contents of index.html will be a permutation of the original values. If index.html contains repeated values, neither the behavior of the FORALL nor the array assignment are defined by their respective standards.

FORALL (i=2:4) x(i) =index.html x(i-1) + x(i) + x(i+1) If this statement is executed with

index.html

then after execution the new values of array index.html will be

index.html

This has the same effect as the Fortran 90 statement

x(2:4) =index.html x(1:3) + x(2:4) + x(3:5) Note that it does not have the same effect as the Fortran 90 loop

DO i =index.html 2, 4 x(i) = x(i-1) + x(i) + x(i+1) END DO FORALL (i=1:n) a(i,i) = x(i) This FORALL statement sets the elements of the main diagonal of matrix index.html to the elements of vector index.html. This cannot be done by an array assignment in Fortran 90 unless EQUIVALENCE or WHERE is also used.

FORALL (i=1:4) a(i,ix(i)) =index.html x(i) This FORALL statement sets one element in each row of matrix index.html to an element of vector index.html. The particular elements in index.html are chosen by the integer vector index.html. If

index.html index.html

and array index.html represents the matrix index.html before execution of the FORALL, then index.html will represent index.html after its execution. This operation cannot be accomplished with a single array assignment in Fortran 90.

FORALL (k=1:9) x(k) =index.html SUM(x(1:10:k)) This FORALL statement computes nine sums of subarrays of x. (SUM is allowed in a FORALL because Fortran 90 intrinsic functions are pure; see Section .) If before the FORALL

index.html

then after the FORALL

index.html

This computation cannot be done by Fortran 90 array expressions alone.



Next: Scalarization of the Up: The FORALL Statement Previous: Interpretation of Element


paula@erc.msstate.edu
Thu Dec 8 16:17:11 CST 1994