Online Book Reader

Home Category

High Performance Computing - Charles Severance [104]

By Root 1277 0
Intrinsics MATMUL and TRANSPOSE can manipulate whole matrices.

Constructing or reshaping arrays: RESHAPE allows you to create a new array from elements of an old one with a different shape. SPREAD replicates an array along a new dimension. MERGE copies portions of one array into another under control of a mask. CSHIFT allows an array to be shifted in one or more dimensions.

Inquiry functions: SHAPE, SIZE, LBOUND, and UBOUND let you ask questions about how an array is constructed.

Parallel tests: Two other new reduction intrinsics, ANY and ALL, are for testing many array elements in parallel.


New Control Features

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.0

ELSEWHERE

A = -1.0

ENDWHERE

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.


Automatic and Allocatable Arrays

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 N

REAL, 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.


Heat Flow in FORTRAN 90

The heat flow problem is an ideal program to use to demonstrate how nicely FORTRAN 90 can express regular array programs:

PROGRAM HEATROD

PARAMETER(MAXTIME=200)

INTEGER TICKS,I,MAXTIME

REAL*4 ROD(10)

ROD(1) = 100.0

DO I=2,9

ROD(I) = 0.0

ENDDO

ROD(10) = 0.0

DO TICKS=1,MAXTIME

IF ( MOD(TICKS,20) .EQ. 1 ) PRINT 100,TICKS,(ROD(I),I=1,10)

ROD(2:9) = (ROD(1:8) + ROD(3:10) ) / 2

ENDDO

100 FORMAT(I4,10F7.2)

END

The program is identical,

Return Main Page Previous Page Next Page

®Online Book Reader