Use Cmake in an MPI C++ program

Hello all,
I’m a PhD student in AME department, and is taking CSCI596 this semester. I have a question about a problem I met when I’m trying to compile a C++ program using Cmake. This program reqiures MPI for running in parallel.

I have load these modules before I compile the code

module load gcc/8.3.0
module load openmpi/4.0.2
module load cmake
export LD_PRELOAD=/spack/apps/gcc/8.3.0/lib64/

And when I tried to check mpirun --version
it seems no problem:

mpirun (Open MPI) 4.0.2

However, when I tried to use Cmake , it fails at the line
find_package(MPI REQUIRED)

ibtorch;/home1/haotianh/rl_new/eigen-3.4.0;/spack/apps/linux-centos7-x86_64/gcc-8.3.0" ..
-- Could NOT find MPI_C (missing: MPI_C_WORKS) 
-- Could NOT find MPI_CXX (missing: MPI_CXX_WORKS) 
CMake Error at /spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find MPI (missing: MPI_C_FOUND MPI_CXX_FOUND)
Call Stack (most recent call first):
  /spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
  /spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/share/cmake-3.16/Modules/FindMPI.cmake:1688 (find_package_handle_standard_args)
  CMakeLists.txt:4 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home1/haotianh/rl_new/build/CMakeFiles/CMakeOutput.log".
See also "/home1/haotianh/rl_new/build/CMakeFiles/CMakeError.log".

I’d like to ask you whether you can help me with this? Thank you so much!

Yours sincerely,

Hi Haotian,

Does your program have a documentation page? If not, could you send me the CMakeLists.txt file you’re using along with the cmake command you used?


Hello Cesar,
Thank you so much for your reply. Hear is the GitHub Repositories of our code.
The link for the CMakeLists.txt is as follows (The building procedure is in the same folder, sorry for I cannot put another link).
Thank you so much for your help.
Yours sincerely,

I was able to reproduce this issue. When using CMake, the configure stage will pick up the system compilers by default. This compiler is not compatible with any MPI implementation we have available which is probably why it fails to find a working MPI_C and MPI_CXX.

You can override this behavior by setting CC and CXX environment variables or by adding -DCMAKE_C_COMPILER=gcc and -DCMAKE_CXX_COMPILER=g++ cmake options. I was able to compile example-app like so

module purge
module load usc
module load cudnn/
module load eigen/3.3.7
module load cmake/3.16.2
module load python/3.9.2

cd RL3linkFishCppPortMerge
mkdir build
cd build
CC=gcc CXX=g++ cmake -DTorch_DIR=/home1/csul/.local/lib/python3.9/site-packages/torch/share/cmake/Torch/ ..

Please try that and let me know the result

Hello Cesar,
It works! Thank you so much!
Yours sincerely,

1 Like