Co-array Fortran on the GPC

From oldwiki.scinet.utoronto.ca
Jump to navigation Jump to search

Versions 12 and higher of the Intel Fortran compiler, and version 5.1 and up of the GNU Fortran compiler, support almost all of Co-array Fortran, and are installed on the GPC.

This page will briefly sketch how to compile and run Co-array Fortran programs using these compilers.

Example

Here is an example of a co-array fortran program: <source lang="fortran"> program Hello_World

 integer :: i ! Local variable
 integer :: num[*] ! scalar coarray
 if (this_image() == 1) then
   write(*,'(a)') 'Enter a number: '
   read(*,'(i80)') num
   ! Distribute information to other images
   do i = 2, num_images()
     num[i] = num
   end do
 end if
 sync all ! Barrier to make sure the data has arrived
 ! I/O from all nodes
 write(*,'(a,i0,a,i0)') 'Hello ',num,' from image ', this_image()

end program Hello_world </source> (Adapted from [1]).

Compiling, linking and running co-array fortran programs is different depending on whether you will run the program only on a single node (with 8 cores), or on several nodes, and depends on which compiler you are using, Intel, or GNU.

Intel compiler instructions for Coarray Fortran

Loading necessary modules

First, you need to load the module for version 12 or greater of the Intel compilers, as well as Intel MPI.

module load intel/14.0.1 intelmpi/4.1.2.040

Note: For multiple node usage, it makes sense to have to load the IntelMPI module, since Intel's implementation of Co-array Fortran uses MPI. However, the Intel MPI module is needed even for single-node usage, just in order to link successfully.

Single node usage

Compilation

<source lang="bash"> ifort -O3 -xHost -coarray=shared -c [sourcefile] -o [objectfile] </source>

Linking

<source lang="bash"> ifort -coarray=shared [objectfile] -o [executable] </source>

Running

To run this co-array program on one node with 16 images (co-array version for what openmp calls a thread and mpi calls a process), you simply put <source lang="bash"> ./[executable] </source> in your job submission script. The reason that this gives 16 images is that HyperThreading is enabled on the GPC nodes, which makes it seem to the system as if there are 16 computing units on a node, even though physically there are only 8.

To control the number of images, you can change the FOR_COARRAY_NUM_IMAGES environment variable: <source lang="bash"> export FOR_COARRAY_NUM_IMAGES=2 ./[executable] </source> This can be useful for testing.

An example submission script would look as follows:

<source lang="bash">

  1. !/bin/bash
  2. MOAB/Torque submission script for SciNet GPC (Intel Coarray Fortran)
  3. PBS -l nodes=1:ppn=8,walltime=1:00:00
  4. PBS -N test
  1. DIRECTORY TO RUN - $PBS_O_WORKDIR is directory job was submitted from

cd $PBS_O_WORKDIR

  1. LOAD MODULES THAT THE APPLICATION WAS COMPILED WITH

module load intel/14.0.1 intelmpi/4.1.2.040

  1. RUN THE APPLICATION WITH 16 IMAGES

export FOR_COARRAY_NUM_IMAGES=16 ./[executable] </source>

Multiple nodes usage

Compilation

<source lang="bash"> ifort -O3 -xHost -coarray=distributed -c [sourcefile] -o [objectfile] </source>

Linking

<source lang="bash"> ifort -coarray=distributed [objectfile] -o [executable] </source>

Running

Because distributed co-array fortran is based on MPI, we need to launch the mpi processes on different nodes. The easiest way to do this is to set the number of images using FOR_COARRAY_NUM_IMAGES and then to use mpirun without an -np parameter: <source lang="bash"> export I_MPI_PROCESS_MANAGER=mpd export FOR_COARRAY_NUM_IMAGES=32 mpirun ./[executable] </source> Note that the total number of images is set explicitly, and should not be given to mpirun. You can still pass other parameters to mpirun, though. Also the default process manager for INTELMPI is not MPD, however Coarray needs MPD which is why I_MPI_PROCESS_MANAGER needs to be set to mpd.

An example submission script would look as follows:

<source lang="bash">

  1. !/bin/bash
  2. MOAB/Torque submission script for SciNet GPC (ethernet)
  3. PBS -l nodes=4:ppn=8,walltime=1:00:00
  4. PBS -N test
  1. DIRECTORY TO RUN - $PBS_O_WORKDIR is directory job was submitted from

cd $PBS_O_WORKDIR

  1. LOAD MODULES THAT THE APPLICATION WAS COMPILED WITH

module load intel/14.0.1 intelmpi/4.1.2.040

  1. EXECUTION export COMMAND; FOR_COARRAY_NUM_IMAGES = nodes*ppn

export I_MPI_PROCESS_MANAGER=mpd export FOR_COARRAY_NUM_IMAGES=32 mpirun ./[executable] </source>

Features known not to work

There are a few features that are known not to work in the current version of the Intel Fortran compiler (v12.0), such as character coarrays. See section 3.2.3.3 of the official release notes. These issues may get fixed in later releases.


GNU compiler instructions for Coarray Fortran

Coarray fortran is supported in the GNU compiler suite (GCC) starting from version 5.1. To implement coarrays, it uses the opencoarray library, which in turns uses openmpi (or at least, that's how it has been setup on the GPC).

Loading necessary modules

First, you need to load the module for version 5.2 or greater of the GNU compilers (version 5.1 would've worked, but we skipped that release on the GPC), as well as OpenMPI.

module load gcc/5.2.0 openmpi/gcc/1.8.3

The gcc/5.2.0 modules comes with a compilation and linking wrapper called caf and a wrapper for running the application called cafrun

Compilation

<source lang="bash"> caf -O3 -march=native -c [sourcefile] -o [objectfile] </source>

Linking

<source lang="bash"> caf [objectfile] -o [executable] </source>

Running

To run this co-array program on one node with 8 images (co-array version for what openmp calls a thread and mpi calls a process), you simply put <source lang="bash"> cafrun ./[executable] </source> in your job submission script. In contrast with the Intel compiler, this does not runs 16 images, but only 8. The reason is that the gcc/opencoarray implementation uses MPI, and MPI is not aware of HyperThreading.

To control the number of images, you can change the run command to <source lang="bash"> cafrun -np 2 ./[executable] </source> This can be useful for testing, or to exploit HyperThreading.

An example submission script would look as follows:

<source lang="bash">

  1. !/bin/bash
  2. MOAB/Torque submission script for SciNet GPC (GCC Coarray Fortran)
  3. PBS -l nodes=1:ppn=8,walltime=1:00:00
  4. PBS -N test
  1. DIRECTORY TO RUN - $PBS_O_WORKDIR is directory job was submitted from

cd $PBS_O_WORKDIR

  1. LOAD MODULES THAT THE APPLICATION WAS COMPILED WITH

module load gcc/5.2.0 openmpi/gcc/1.8.3

  1. RUN WITH 16 IMAGES ON 1 NODE

cafrun -np 16 ./[executable] </source>

Multiple nodes usage

Because the GNU implementation of Coarray Fortran in the gcc/5.2.0 module is based on MPI, running on multiple nodes is no different from the single-node usage. An example multi-node submission script would look as follows:

<source lang="bash">

  1. !/bin/bash
  2. MOAB/Torque submission script for SciNet GPC (ethernet)
  3. PBS -l nodes=4:ppn=8,walltime=1:00:00
  4. PBS -N test
  1. DIRECTORY TO RUN - $PBS_O_WORKDIR is directory job was submitted from

cd $PBS_O_WORKDIR

  1. LOAD MODULES THAT THE APPLICATION WAS COMPILED WITH

module load gcc/5.2.0 openmpi/gcc/1.8.3

  1. EXECUTION with 32 images (nodes*ppn)

cafrun -np 32 ./[executable] </source>