## Numerical Computing with Modern Fortran## Richard J. Hanson and Tim Hopkins |

# Chapter 14: Coarrays in Standard Fortran

Source Code:

These executable routines use Fortran coarrays to compute quantities that benefit from using parallel resources. There are two simple examples in two separate source files. The compiler must support coarrays and should allow more than one image in the work group.

- approximatePi uses set_precision.f90 and approximatePi.f90 to
approximate Pi by numerical quadrature using the mid-point formula. The user will input the number of quadrature points for the integrand function f(x)=1/(1+x**2).

- matvecmult uses set_precision.f90 and matvecmult.f90 to compute
a matrix-vector product, y=A*x. The matrix dimensions are input. The columns of A are partitioned to the images and the matrix entries are randomly generated with values on [0,1]. The vector x is randomly generated on one image and the part of x corresponding to the columns of A is sent to each image.

Each image computes a part of the product with the data it owns. This step is done in parallel. Then these partial results are accumulated and compared with the result obtained at a single image to establish correctness. There is an allowance for rounding errors.

### Sample Output from approximatePi using 8 images

Input the number of quadrature points: 1000000 Using 8 images, and n = 1000000 Approximate Value of PI = 3.14159265358989 Relative error = -3.095739889957823E-014

### Sample Output from matvecmult using 8 images

8 images being used Input m, the number of rows (<=0 to quit): 10000 Input n, the number of cols (<=0 to quit): 1000 Satisfactory Error Check on IMAGE 1 = 3.0314E-15