1

I need to force my Cmake to build and link my MPI code with MPICH. My MPICH is installed using the Ubuntu Package manager, in a standard location /usr/lib/mpich/. However, CMake still looks for the OpenMPI libraries, which I do not use. How can I instruct CMake to look for MPICH instead?

Below, you can see the output of some basic diagnostics:

$ whereis openmpi
openmpi:

$ whereis mpich
mpich: /usr/lib/mpich /usr/include/mpich

$ mpicc -v
mpicc for MPICH version 3.2

Below, I also provide the Cmake script and the errors I get from cmake and the mpirun.mpich. My Cmake is 3.5.1 and I run on Ubuntu Xenial 16.04.

cmake_minimum_required(VERSION 3.0)

message (STATUS "Adding mpiService")

find_package(MPI REQUIRED)

set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILER mpicxx)
set(MPI_GUESS_LIBRARY_NAME MPICH2)

message(STATUS ${MPI_INCLUDE_PATH}) 
message(STATUS ${MPI_C_LIBRARIES}) 

#add_definitions(-DOMPI_SKIP_MPICXX)

add_executable(mpiService main.cpp)

set(CMAKE_VERBOSE_MAKEFILE ON)

include_directories(SYSTEM ${MPI_INCLUDE_PATH})

target_link_libraries(
    mpiService
    PRIVATE
    ${MPI_C_LIBRARIES}
    )

From the Cmake STATUS I get the following output:

/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/include/usr/lib/openmpi/include/usr/lib/openmpi/include/openmpi
/usr/lib/openmpi/lib/libmpi.so

And when I run the binary I get the following:

ubuntu@node1:~$ mpirun.mpich -np 2 --host node1,node2 mpiService
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
user1221647
  • 65
  • 1
  • 11
  • Possible duplicate of [MPI - error loading shared libraries](https://stackoverflow.com/questions/14769599/mpi-error-loading-shared-libraries) – Tsyvarev Dec 18 '17 at 08:16
  • @Tsyvarev thank you for looking into this. The previous post will not help me as it addresses a non standard library installation: _"libmpi and libmpi_cxx are part of the Open MPI installation and in your case are located in a **non-standard** location that must be explicitly included in the linker search path LD_LIBRARY_PATH"_. When I use CMake, I never had to setup any ENV variables for standard installations. I would expect that **CMake** could handle that, given that it supports mpich. I expect that _probably_ the solution would come via the CMake script itself. – user1221647 Dec 26 '17 at 09:09
  • I will try to experiment a bit based on the info I find on the [FindMPI](https://cmake.org/cmake/help/v3.0/module/FindMPI.html) page. It seems that **MPI_C_LIBRARIES** and **MPI_INCLUDE_PATH** are deprecated. Maybe there is a better way to link. – user1221647 Dec 26 '17 at 09:21

2 Answers2

2

How can I instruct CMake to look for MPICH instead?

According to FindMPI documentation, you may set MPI_<lang>_COMPILER variable to the desired MPI compiler:

Set MPI_<lang>_COMPILER to the MPI wrapper (mpicc, etc.) of your choice and reconfigure. FindMPI will attempt to determine all the necessary variables using THAT compiler's compile and link flags.

set(MPI_CXX_COMPILER <path-to-mpich-compiler>)
find_package(MPI REQUIRED)

Alternatively, since CMake version 3.10, variable MPI_EXECUTABLE_SUFFIX can be set instead:

A suffix which is appended to all names that are being looked for. For instance you may set this to .mpich or .openmpi to prefer the one or the other on Debian and its derivatives.

set(MPI_EXECUTABLE_SUFFIX ".mpich")
find_package(MPI REQUIRED)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Unfortunately, this did not solve my problem. If I use this I still get the error: [[22951,1],0]: A high-performance Open MPI point-to-point messaging module was unable to find any relevant network interfaces: Module: OpenFabrics (openib) My answer follows below. – user1221647 Jan 29 '18 at 23:27
1

Herewith my current solution.

find_package(MPI REQUIRED)

# ----------------
# This is the only thing that made it work
# ----------------
set(MPI_C_LIBRARIES "/usr/lib/mpich/lib/libmpich.so")
set(MPI_INCLUDE_PATH "/usr/include/mpich")
# ----------------

add_executable(mpiService main.cpp)

include_directories(SYSTEM ${MPI_INCLUDE_PATH})

target_link_libraries(
    mpiService
    ${MPI_C_LIBRARIES}
    )

I personally do not like this solution, as I have to explicitly specify the path. Any other proposed solution was still building with OpenMPI. If I find a better alternative, I will re-post.

user1221647
  • 65
  • 1
  • 11