Running Open MPI on macOS

I am, generally, against all these automation stuff (brew, etc.) at macOS. I prefer to install everything from sources (as long as it is possible).

So, here are few steps to get Open MPI up and running at macOS. Please make sure to download source package from: https://www.open-mpi.org/software/ompi/v2.0/downloads/openmpi-2.0.2.tar.gz.

# put this file somewhere inside your $HOME
# and untar it
> tar xf openmpi-2.0.2.tar
> cd openmpi-2.0.2/

# Make sure you have XCode and command line tools
# installed - all of these if free of charge
# If you want to have Fortran support, make sure to install Fortran from here
#
# https://gcc.gnu.org/wiki/GFortran
#
> ./configure --prefix=$HOME/opt/usr/local
> make all
> make install

# After installation is done, you can 
# verify it
> $HOME/opt/usr/local/bin/mpirun --version
mpirun (Open MPI) 2.0.2

Report bugs to http://www.open-mpi.org/community/help/

Now, it’s time for Hello world! code.

/* Put this text inside hello.c file */
#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
    int rank;
    int world;

    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world);
    printf("Hello: rank %d, world: %d\n",rank, world);
    MPI_Finalize();
}

And, eventually, we can compile and run it

$HOME/opt/usr/local/bin/mpicc -o hello ./hello.c
$HOME/opt/usr/local/bin/mpirun -np 2 ./hello
Hello: rank 0, world: 2
Hello: rank 1, world: 2

As for the Fortran code, you can compile it as well

! Put this text inside hello_f.f90 file
program main
  use mpi

  integer error, id, p
  call MPI_Init ( error )
  call MPI_Comm_size ( MPI_COMM_WORLD, p, error )
  call MPI_Comm_rank ( MPI_COMM_WORLD, id, error )
  write (*,*) 'Hello: ', id, '/', p
  call MPI_Finalize ( error )
end

and you can compile and run it

$HOME/opt/usr/local/bin/mpif90 -o hello_f ./hello_f.f90
$HOME/opt/usr/local/bin/mpirun -np 2 ./hello_f
 Hello:            0 /           2
 Hello:            1 /           2

That’s all.

Leave a comment

Your comment