Run an example Fortran program

The mpihello.f90 program is a 'Hello World' example Fortran MPI program:

! mpihello.f90
program mpihello   use mpi   implicit none
   integer :: err, rank, size
  call MPI_Init(ierror = err)
  call MPI_Comm_rank(comm = MPI_COMM_WORLD, rank = rank, ierror = err)
  call MPI_Comm_size(comm = MPI_COMM_WORLD, size = size, ierror = err)
  write (*, 10), rank, size
  call MPI_Finalize(ierror = err)
  10 format(' Hello World, rank: ', I5, ' size: ', I5)
end program mpihello

The program comprises four common Open MPI functions:

  • MPI_Init
  • MPI_Comm_Rank
  • MPI_Comm_Size
  • MPI_Finalize

To run this program with the library build:

  1. Compile the mpihello.f90 program:

    mpifort -O3 -o mpihello mpihello.f90
  2. Execute the mpihello program:

    mpiexec -np 4 ./mpihello

    to return:

     Hello World, rank:     0 size:     4
     Hello World, rank:     2 size:     4
     Hello World, rank:     1 size:     4
     Hello World, rank:     3 size:     4

    Note: The ordering of the output may change due to parallelism.

Previous Next