9

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

Joey
  • 809
  • 1
  • 10
  • 24

3 Answers3

20

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.

Michelrandahl
  • 3,365
  • 2
  • 26
  • 41
3

The number of processors you use has nothing to do with the compilation process and thus has nothing to do with your CMakeLists.txt (besides when using CTest, but that is a different topic).

You just compile the executable, either using mpicxx or the way you do now, and then run it with

mpirun -np 4 nameOfExe

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

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Thanks! But do you know how to do it in CLion (IDE)? I mean I tried to add "-np 4" to the program arguments, but it didn't help. – Joey Jul 07 '15 at 15:22
  • @Joey I do not know CLion, though I think you need to run it through the mpi wrapper. Passing `-np 4` to the `nameOfExe` will not work. – luk32 Jul 07 '15 at 15:27
  • @luk32 Thank you, but I do not know how to do it like you mentioned :( But I found this here: "set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")" -> https://github.com/realstolz/powerlyra/blob/master/cmake/FindMPICH2.cmake So can you make it still in the CMakeList? – Joey Jul 07 '15 at 15:31
  • @Joey Again, this has nothing to do with compilation and CMake, but with how you execute the final binary. I don't know CLion and don't know if it can handle this special way of starting your binary. If not, just start it in a command line. – Baum mit Augen Jul 07 '15 at 15:33
  • @BaummitAugen You're right. Thank you, in a terminal it's working of course. But it would be more pleasent if it would work with CLion :P – Joey Jul 07 '15 at 15:47
  • 2
    @joey Google "CLion mpirun" it yields some results how to define custom launcher or something like that. – luk32 Jul 07 '15 at 15:59
3

OpenMP and MPI together

For those who would like to use both OpenMP and MPI within a single CMake file:

cmake_minimum_required(VERSION 3.9.1)
project(parallel_task)

set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -pedantic -lm -O3 -funroll-loops")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

add_executable(parallel_task example.cpp example.h)

# OpenMP
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
    target_link_libraries(parallel_task PUBLIC OpenMP::OpenMP_CXX)
endif()

# MPI
find_package(MPI REQUIRED)
target_link_libraries(parallel_task PUBLIC MPI::MPI_CXX)

or even simpler:

cmake_minimum_required(VERSION 3.9.1)
project(parallel_task)

set(CMAKE_CXX_STANDARD 14)

# -fopenmp flag (enables OpenMP)
set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -pedantic -lm -O3 -funroll-loops -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

add_executable(parallel_task example.cpp example.h)

# MPI
find_package(MPI REQUIRED)
target_link_libraries(parallel_task PUBLIC MPI::MPI_CXX)

Execute program from CLion

In order to run the program directly from CLion, click on Edit Configurations... and as executable set mprirun (location of the executable may be found by using the command whereis from a command line).

How to edit configurations

Inside program arguments set -np <number of processes> <some other arguments>. For example -np 4 /home/desktop/parallel_task will execute the program with 4 processes.

Set executable and program arguments

Hawklike
  • 952
  • 16
  • 23