2

I was trying to install openpi on ubuntu. I have an Ubuntu 18.04.4 LTS (64 bit) desktop. I installed openmpi using

sudo apt-get install openmpi-bin openmpi-common openmpi-doc libopenmpi2 libopenmpi-dev openssh-client openssh-server

Then, in the .bashrc, I have added the following two lines:

echo export PATH="$PATH:/home/$USER/.openmpi/bin" >> /home/$USER/.bashrc

echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/$USER/.openmpi/lib/" >> /home/$USER/.bashrc

After that, I used dartmouth hello world mpi to write a small program, compile it and run it to check that the openmpi installation is OK by typing gfortran ubuntu.f90

which leads to the following error

ubuntu.f90:2: Error: Can't open included file 'mpif.h'

the code in ubuntu.f90 :

program hello
include 'mpif.h'
integer rank, size, ierror, tag, status(MPI_STATUS_SIZE)

call MPI_INIT(ierror) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror) print*, 'node', rank, ': Hello world' call MPI_FINALIZE(ierror) end

I have tried installing libblacs-mpi-dev as in the answer to this question. This question does not seem relevant.

1 Answers1

1

To include a C-style header file in a Fortran program, you need to use a C-style pre-processor directive

#include <mpif.h>

rather than a native Fortran include statement, and then tell gfortran to run the pre-processor by adding the -cpp command-line switch (or change the source file suffix to upper-case F90 which causes the pre-processor to be run by default). See for example

However including mpif.h is apparently deprecated, and instead you should probably use the MPI module, and compile your program using mpif90 instead of invoking gfortran directly.

Ex.

$ cat ubuntu.f90 
program hello
use mpi
integer rank, size, ierror, tag, status(MPI_STATUS_SIZE)

call MPI_INIT(ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
print*, 'node', rank, ': Hello world'
call MPI_FINALIZE(ierror)
end

$ mpif90 ubuntu.f90

$ ./a.out 
 node           0 : Hello world

FWIW, your changes to PATH and LD_LIBRAY_PATH likely have no effect since you have installed openmpi into system directories using apt

steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • Basically, I am an idiot. I should have been running 'mpif90 ubuntu.f90 -o ubuntu.out', followed by 'mpirun -n 8 ubuntu.out' – Tejas Shetty Mar 02 '20 at 15:42