Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile an MPI included c program using cmake

Tags:

c

cmake

mpi

I am trying to apply openmp and mpi techniques to an open source C program which requires "cmake . && make" to be built. I already found at How to set linker flags for OpenMP in CMake's try_compile function how to include the -fopenmp flags but still confused about mpi. What can I do about that?

like image 622
user3403765 Avatar asked Apr 18 '14 22:04

user3403765


1 Answers

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)

If you want to run your program directly from CLion (with a specified number of processes), check also this answer: https://stackoverflow.com/a/66992251/9723204

like image 181
Hawklike Avatar answered Oct 15 '22 16:10

Hawklike