Scalarization of the Up: The FORALL Construct Previous: Interpretation of the

Examples of the FORALL Construct

FORALL ( i=2:n-1, j=2:n-1 ) a(i,j) = a(i,j-1) + a(i,j+1) + a(i-1,j) + a(i+1,j) b(i,j) = a(i,j) END FORALL This FORALL is equivalent to the two Fortran 90 statements

a(2:n-1,2:n-1) = a(2:n-1,1:n-2)+a(2:n-1,3:n) & +a(1:n-2,2:n-1)+a(3:n,2:n-1) b(2:n-1,2:n-1) = a(2:n-1,2:n-1) In particular, note that the assignment to array uses the values of array computed in the first statement, not the values before the FORALL began execution.

FORALL ( i=1:n-1 ) FORALL ( j=i+1:n ) a(i,j) = a(j,i) END FORALL END FORALL This FORALL construct assigns the transpose of the lower triangle of array (i.e., the section below the main diagonal) to the upper triangle of . For example, if and originally contained the matrix then after the FORALL it would contain This cannot be done using array expressions without introducing mask expressions.

FORALL ( i=1:5 ) WHERE ( a(i,:) .NE. 0.0 ) a(i,:) = a(i-1,:) + a(i+1,:) ELSEWHERE b(i,:) = a(6-i,:) END WHERE END FORALL This FORALL construct, when executed with the input arrays will produce as results Note that, as with WHERE statements in ordinary Fortran 90, assignments in the WHERE branch may affect computations in the ELSEWHERE branch.
Thu Dec 8 16:17:11 CST 1994