Search code examples
c++mpimpichclion

MPI: Change number of processors in CMakelists


I'm using CLion. My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.2)

project(MPI)

add_executable(MPI main.cpp)

# Require MPI for this project:
find_package(MPI REQUIRED)

set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})

include_directories(MPI_INCLUDE_PATH)
target_link_libraries(MPI ${MPI_LIBRARIES})

The MPI - Hello World runs well. But how do I change the number of the processors in the cmakelists?

I already tried to add -np 4 and -n 4 to the program arguments in CLion. But still I just get

Hello World process 0 of 1


Solution

  • You cannot specify number of processes to use in the CMakeLists.txt. Number of processes is an argument you specify when executing the program with mpirun.

    To compile a mpi C project I use following CMakeLists.txt

    cmake_minimum_required(VERSION 3.3)
    project(hellompi)
    
    find_package(MPI REQUIRED)
    include_directories(${MPI_INCLUDE_PATH})
    
    SET(CMAKE_C_COMPILER mpicc)
    SET(CMAKE_CXX_COMPILER mpicxx)
    
    set(SOURCE_FILES main.c)
    add_executable(hellompi ${SOURCE_FILES})
    

    In order to execute the program from Clion, I first changed the (obscure) location which Clion by default outputs compiled files to. You can specify another location for the compiled files under the settings in "Build, Execution and Deployment" -> "CMake". I just changed it to the project folder.

    Next I edited the run configurations. "Run" -> "Edit Configurations" -> set Executable to mpirun. (the location of mpirun on your machine)

    Next I edited the "Program arguments" to be

    -np 4 /home/mitzh/ClionProjects/hellompi/Debug/hellompi
    

    To execute my program using 4 processes.