Using MPI in Fortran

From EGEE-see WIki

Jump to: navigation, search

MPI under Fortran is mostly the same as MPI in C, except that instead of returning the status value as a function, most MPI calls in Fortran are procedures with an extra output parameter at the end of the parameter list.

       PROGRAM mpitest
       INCLUDE 'mpif.h'
       INTEGER ierr, rank, size

       CALL MPI_INIT(ierr)
       CALL MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
       CALL MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
       PRINT *, 'I am ', rank, ' of ', size
       CALL MPI_FINALIZE(ierr)
       END

(Be careful that each line begins with six spaces, if using Fortran 77 compiler.)

The example can be compiled with the following command (providing MPI is configured for Fortran):

 mpif77 --static -o mpitest mpitest.f

or, if using a Fortran 90 compiler:

 mpif90 --static -o mpitest mpitest.f90

This program can now be executed just like MPI programs written in C. For example, if it is executed on 3 processors using mpirun, the following JDL can be used:

 JobType = "mpich";
 NodeNumber = 5;
 Executable = "mpitest";
 StdOutput = "out";
 StdError = "err";
 InputSandbox = {"mpitest"};
 OutputSandbox = {"out", "err"};

When run, the program should produce the following output:

 I am  0 of  3
 I am  2 of  3
 I am  1 of  3

In case the cluster uses mpiexec to start MPI executables, the program needs to be started through a simple shell script:

 #!/bin/sh
 chmod +x mpitest
 mpiexec $PWD/mpitest

JDL then needs to include and run this script instead of the MPI executable:

 JobType = "mpich";
 NodeNumber = 5;
 Executable = "mpitest.sh";
 StdOutput = "out";
 StdError = "err";
 InputSandbox = {"mpitest", "mpitest.sh"};
 OutputSandbox = {"out", "err"};

Since some sites in SEE-GRID support mpiexec and some mpirun (or both), it is prudent to try to use both, for example:

 #!/bin/sh
 chmod +x mpitest
 if ! mpiexec $PWD/mpitest; then
     CPUS=`cat $PBS_NODEFILE | wc -l`
     mpirun -np $CPUS -machinefile $PBS_NODEFILE mpitest
 fi
Personal tools