Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPI: Change number of processors in CMakelists

Tags:

c++

clion

mpi

mpich

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

like image 550
Joey Avatar asked Jul 07 '15 15:07

Joey


People also ask

How do I edit CMakeLists?

You can edit CMakeLists. txt files right in the Editor. Make sure to reload the project after editing. Automatic reload feature is disabled by default, you can turn it on by selecting the Automatically reload CMake project on editing checkbox in Settings / Preferences | Build, Execution, Deployment | CMake.

What is CMakeLists used for?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.

What is MPI c++?

MPI is a directory of C++ programs which illustrate the use of the Message Passing Interface for parallel programming. MPI allows a user to write a program in a familiar language, such as C, C++, FORTRAN, or Python, and carry out a computation in parallel on an arbitrary number of cooperating computers.

How do I add MPI to CLion?

Go to Settings -> Build, Execution, Deployment -> CMake . Click on "Plus" sign and a new "Release" configuration should be created.


2 Answers

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.

like image 146
Michelrandahl Avatar answered Oct 18 '22 03:10

Michelrandahl


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.

like image 3
Baum mit Augen Avatar answered Oct 18 '22 01:10

Baum mit Augen