60

when I compile my script with only

#include <mpi.h>

it tells me that there is no such file or directory. But when i include the path to mpi.h as

#include "/usr/include/mpi/mpi.h"

(the path is correct) it returns:

In file included from /usr/include/mpi/mpi.h:2087:0,
                 from lbm.cc:7:
/usr/include/mpi/openmpi/ompi/mpi/cxx/mpicxx.h:35:17: fatal error: mpi.h: No such file or directory
 #include "mpi.h"
                 ^
compilation terminated.

Anyone know how to fix this?

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
user2804865
  • 976
  • 2
  • 9
  • 15
  • What is your compiler and operating system? – muradin Nov 13 '14 at 23:04
  • g++ on linux. and doesn't work either. – user2804865 Nov 13 '14 at 23:34
  • 1
    Program using the Message Passing Interface should be compiled by using something like `mpicc main.c -o main` or `mpiCC main.cpp -o main`. Is it your case ? Regarding the second message : you may have two implementations of the MPI standards on your computer. You may try to know more by typing `which mpicc`, `which mpirun`, `mpirun --version` or `module avail`. The implementation (openmpi or mpich2 or...) of compiler command must be the same as the command to execute the program `mpirun -np 2 main`. – francis Nov 13 '14 at 23:38

11 Answers11

51

The problem is almost certainly that you're not using the MPI compiler wrappers. Whenever you're compiling an MPI program, you should use the MPI wrappers:

  • C - mpicc
  • C++ - mpiCC, mpicxx, mpic++
  • FORTRAN - mpifort, mpif77, mpif90

These wrappers do all of the dirty work for you of making sure that all of the appropriate compiler flags, libraries, include directories, library directories, etc. are included when you compile your program.

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
  • 1
    The conundrum lies in the fact that openmpi does not have MPI wrappers for mpicc, mpicxx, etc. – Marc J. Driftmeyer Dec 26 '14 at 19:43
  • 1
    There may be some implementations that don't provide them, but Open MPI isn't one of them. It definitely has at least `mpicc` and `mpicxx` and `mpifortan`. – Wesley Bland Dec 26 '14 at 19:53
  • 7
    can you explain how to fix this? i am trying to make someone's program and it gives the error in the original post. it is looking for mpi.h. I installed api using apt-get. Still get the same error. – user391339 May 28 '16 at 22:20
  • 3
    No info were given as to how to use those wrappers; adding them to the compiler like `-mpiCC`, installing a wrapper and including it in source file? – Terry Oct 05 '19 at 10:27
  • 1
    @user391339 and Romario By default your IDE uses cmake or so to compile the .cpp file, right?.. Instead you will use the mpi wrapper (which is simply a compiling/executing command) to compile/execute your program. An example would be: $ mpic++ main.cpp and an example to run your program would be $ mpirun -np 2 a.out – Hasnaa Ibraheem Nov 15 '19 at 21:46
49

On my system, I was just missing the Linux package.

sudo apt install libopenmpi-dev
pip install mpi4py

(example of something that uses it that is a good instant test to see if it succeeded)

Succeded.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • The first line of this solution will also work if you are getting this error while building lammps: $ cmake ../cmake -- Running check for auto-generated files from make-based build system CMake Error in /home/name/lammps-23Jun2022/build/CMakeFiles/CMakeScratch/TryCompile-Iqf2dm/CMakeLists.txt: Imported target "MPI::MPI_CXX" includes non-existent path "/usr/lib/x86_64-linux-gnu/openmpi/include" in its INTERFACE_INCLUDE_DIRECTORIES. – user30850 Jul 06 '23 at 06:45
13

You can execute:

$ mpicc -showme 

result :

gcc -I/Users/<USER_NAME>/openmpi-2.0.1/include -L/Users/<USER_NAME>/openmpi-2.0.1/lib -lmp

This command shows you the necessary libraries to compile mpicc

Example:

$ mpicc -g  -I/Users/<USER_NAME>/openmpi-2.0.1/include -o [nameExec] [objetcs.o...] [program.c] -lm


$ mpicc -g  -I/Users/<USER_NAME>/openmpi-2.0.1/include -o example file_object.o my_program.c otherlib.o -lm

this command generates executable with your program in example, you can execute :

$ ./example
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Bvacavar
  • 141
  • 1
  • 2
6

On my system Ubuntu 16.04. I installed :

sudo apt install libopenmpi-dev

after I used mpiCC to compile and it works

5

As suggested above the inclusion of

/usr/lib/openmpi/include 

in the include path takes care of this (in my case)

jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47
3

Debian appears to include the following:

  • mpiCC.openmpi
  • mpic++.openmpi
  • mpicc.openmpi
  • mpicxx.openmpi
  • mpif77.openmpi
  • mpif90.openmpi

I'll test symlinks of each for mpic, etc., and see if that helps the likes of HDF5-openmpi enabled find mpi.h.

Take that back Debian includes symlinks via their alternatives system and it still cannot find the proper paths between HDF5 openmpi packages and mpi.h referenced in the H5public.h header.

2

On Ubuntu 18.04 I had to install:

sudo apt install lam4-dev
Felix Crazzolara
  • 982
  • 9
  • 24
1

On Fedora:

dnf install openmpi-devel
Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
Hung Tran
  • 11
  • 1
1

On Ubuntu 20.04, this worked for me:

apt -y install lam-runtime
jindalab
  • 11
  • 2
0

On Mac 12.2, I installed with brew install openmpi. The header file is under /opt/homebrew/Cellar/open-mpi/x.x.x/include.

yyFred
  • 775
  • 9
  • 13
0

once you have mpi installed:

$ sudo apt install mpich

see where the library is installed, each case is different:

$ mpicc -show

in my case: (Ubuntu 20.0)

and add...

#include </usr/lib/x86_64-linux-gnu/openmpi/include/openmpi>

:-)

Carlos Noé
  • 103
  • 3
  • 11