HP Fortran Programmer's Guide (B3908-90031; September 2011)

Performance and optimization
Vectorization
Chapter 6 171
inc_x = 1
inc_y = 1
! initialize the two arrays x and y
DO i = 1, 5
y(i) = i
x(i) = i + 3.0
END DO
PRINT *, y
! add a scalar multiple of x to y
DO i = 1, 5
y(i) = y(i) + b * x(i)
END DO
PRINT *, y
END PROGRAM main
The following command lines compile and execute the program, and show the output from a sample run:
$ f90 saxpy.f90
$ a.out
1.0 2.0 3.0 4.0 5.0
13.0 17.0 21.0 25.0 29.0
As an alternative, you could replace the second loop with the following call to the saxpy routine in the
BLAS library:
CALL saxpy(dim_num, b, x, inc_x, y, inc_y)
When you compile the revised program, you must add the -lblas option to the end of the command line to
link in the BLAS library. The following show the command lines to compile and execute the revised
program as well as the output from a sample run:
$ f90 saxpy_blas.f90 -lblas
$ a.out
1.0 2.0 3.0 4.0 5.0
13.0 17.0 21.0 25.0 29.0
If you call a BLAS routine that is a function, be sure to declare the return value of the routine in a data
declaration statement and specify the EXTERNAL attribute, as in the following:
REAL, EXTERNAL :: sdot
Fortran uses implicit typing by default. Unless a function is explicitly declared as having a certain type, the
type is determined by the first character of the BLAS routine. If that character implies a type other than that
of the returned value, the result will be meaningless.
See the HP Fortran Programmer's Reference for information about the BLAS library.