Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: set path to MPI headers and binaries manually

Tags:

c++

cmake

mpi

I am developing an MPI application which requires to be run with a specific implementation of MPI (let's call it MPIvA). On my workstation, another implementation of MPI (let's call it MPIvB) is installed.

My application is built using CMake and the find_library(MPI) obviously points to MPIvB. It compiles and runs without hassle.

I compiled MPIvA on my workstation. How can I make CMake use these headers and binaries?

like image 645
M4urice Avatar asked Feb 08 '23 11:02

M4urice


1 Answers

CMake comes with a FindMPI module, that does all the heavy lifting for you.

In your CMakeLists.txt, instead of calling find_library(MPI), use find_package like so:

#### MPI
find_package(MPI REQUIRED)
if (MPI_FOUND)
    include_directories(SYSTEM ${MPI_INCLUDE_PATH})
else (MPI_FOUND)
    message(SEND_ERROR "This application cannot compile without MPI")
endif (MPI_FOUND)

Then wherever you link your application, link against the ${MPI_LIBRARIES}:

target_link_libraries(example-app ${MPI_LIBRARIES})

Now cmake will automatically find a MPI implementation in your system. If you have multiple different MPI versions, and want to specify which one to compile with, you can set the MPI_C_COMPILER and MPI_CXX_COMPILER variables to the corresponding mpicc and mpicxx compiler wrappers. The CMake module will then use those to figure out all the required compiler and linker flags itself.

Example:

cmake -DMPI_C_COMPILER=/usr/share/mvapich/bin/mpicc your-project-dir

To make sure cmake is using the correct MPI, start in a new empty build directory.

More information on the FindMPI module here: https://cmake.org/cmake/help/v3.0/module/FindMPI.html

like image 192
Patrick Avatar answered Feb 16 '23 02:02

Patrick