Fortran/parallel processing

Parallelism is included in the Fortran 2008 standard. To use parallel features, a Fortran program must be compiled with parallelism enabled. For example, the Intel ifort compiler uses the flag -coarray.

Images

edit

Fortran uses a Partitioned Global Address Space (PGAS) model for parallelism. For each processor, the program is executed as a separate duplicate "image" of the program, each with their own separate memory partition. Consider the following program:

program hello
    implicit none
    write (*,*) 'Hello from ', this_image(), 'of', num_images()
end program hello

The intrinsic function this_image returns the number of the image that is being executed, and the intrinsic function num_images returns the total number of images for the program. If the program is compiled and executed with 4 processors, the output might look something like this:

Hello from image 1 of 4
Hello from image 4 of 4
Hello from image 2 of 4
Hello from image 3 of 4

Note that the images are executed asynchronously and so the output may not appear in the order 1, 2, 3 then 4.

Coarrays

edit

Coarrays are a way of communicating data in arrays across images. A coarray is just like a normal array but it has extra codimensions for each image. Codimensions can be declared and indexed using the square brackets []. For example to declare a rank 1 coarray with size of 10 and a codimension of size 4:

real :: coarr(10)[4]
! Or you can use declaration attributes to do the same thing
real, dimension (10), codimension [4] :: another_coarr

Scalar variables can also be coarrays:

integer :: scalar[*]

Here, * denotes the maximum number of available processors. The codimensions can have multiple axes just like normal dimensions, however, there is a limit of rank 15 for codimensions. Transferring data between images is as simple as indexing on the codimensions.

! Set all images to 1
coarr = 1
! Indexing
another_coarr(3)[4] = coarr(3)[3]