<< Chapter < Page | Chapter >> Page > |
FORTRAN 90 includes some new control features, including a conditional
assignment primitive called
WHERE
, that puts shape-conforming array assignments under control of a mask as in the following example. Here's an example of the
WHERE
primitive:
REAL A(2,2), B(2,2), C(2,2)
DATA B/1,2,3,4/, C/1,1,5,5/...
WHERE (B .EQ. C)A = 1.0
C = B + 1.0ELSEWHERE
A = -1.0ENDWHERE
In places where the logical expression is
TRUE
,
A
gets
1.0
and
C
gets
B+1.0
. In the
ELSEWHERE
clause,
A
gets
-1.0
. The result of the operation above would be arrays
A
and
C
with the elements:
A = 1.0 -1.0 C = 2.0 5.0
-1.0 -1.0 1.0 5.0
Again, no order is implied in these conditional assignments, meaning they can be done in parallel. This lack of implied order is critical to allowing SIMD computer systems and SPMD environments to have flexibility in performing these computations.
Every program needs temporary variables or work space. In the past, FORTRAN programmers have often managed their own scratch space by declaring an array large enough to handle any temporary requirements. This practice gobbles up memory (albeit virtual memory, usually), and can even have an effect on performance. With the ability to allocate memory dynamically, programmers can wait until later to decide how much scratch space to set aside. FORTRAN 90 supports dynamic memory allocation with two new language features: automatic arrays and allocatable arrays.
Like the local variables of a C program, FORTRAN 90's automatic arrays are assigned storage only for the life of the subroutine or function that contains them. This is different from traditional local storage for FORTRAN arrays, where some space was set aside at compile or link time. The size and shape of automatic arrays can be sculpted from a combination of constants and arguments. For instance, here's a declaration of an automatic array,
B
, using FORTRAN 90's new specification syntax:
SUBROUTINE RELAX(N,A)
INTEGER NREAL, DIMENSION (N) :: A, B
Two arrays are declared:
A
, the dummy argument, and
B
, an automatic, explicit shape array. When the subroutine returns,
B
ceases to exist. Notice that the size of
B
is taken from one of the arguments,
N
.
Allocatable arrays give you the ability to choose the size of an array after examining other variables in the program. For example, you might want to determine the amount of input data before allocating the arrays. This little program asks the user for the matrix's size before allocating storage:
INTEGER M,N
REAL, ALLOCATABLE, DIMENSION (:,:) :: X...
WRITE (*,*) 'ENTER THE DIMENSIONS OF X'READ (*,*) M,N
ALLOCATE (X(M,N))...
do something with X...
DEALLOCATE (X)...
The
ALLOCATE
statement creates an
M × N
array that is later freed by the
DEALLOCATE
statement. As with
C
programs, it's important to give back allocated memory when you are done with it; otherwise, your program might consume all the virtual storage available.
Notification Switch
Would you like to follow the 'High performance computing' conversation and receive update notifications?