HP Fortran Programmer's Guide (March 2010)
Performance and optimization
Vectorization
Chapter 6 175
INTEGER :: i, inc_x, inc_y, dim_num
REAL, DIMENSION(5) :: x, y
REAL :: b
b = 3.0
dim_num = 5
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