MPI: changing the number of processors in CMakelists

I am 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}) 

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

I already tried adding -np 4 and -n 4 to the program arguments in CLion. But still I'm just getting

Hello World process 0 of 1

+7
c ++ mpi mpich clion
source share
2 answers

You cannot specify the number of processes that will be used in CMakeLists.txt. The number of processes is the argument that you specify when running the program using mpirun.

To compile the mpi C project, I use the 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}) 

To run the program from Clion, I first changed the (obscure) location that Clion defaults to compiling files. You can specify a different location for the compiled files in the settings "Build, Execution and Deployment" → "CMake". I just changed it to the project folder.

Next, I edited the launch configurations. "Run" → "Edit configurations" → install the executable file in mpirun. (location of mpirun on your computer)

Next, I edited Programmatic Arguments as

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

To execute my program using 4 processes.

+9
source share

The number of processors you use has nothing to do with the compilation process and therefore has nothing to do with your CMakeLists.txt (besides, when using CTest, but that's another question).

You simply compile the executable using mpicxx or the method you are currently using, and then run it with

 mpirun -np 4 nameOfExe 

Note that -np 4 is an argument to mpirun , not your program.

+3
source share

All Articles