Skip to content

Instantly share code, notes, and snippets.

@tochanenko
Last active May 16, 2025 10:49
Show Gist options
  • Select an option

  • Save tochanenko/10d4c1dc7888b035b55192faaf678458 to your computer and use it in GitHub Desktop.

Select an option

Save tochanenko/10d4c1dc7888b035b55192faaf678458 to your computer and use it in GitHub Desktop.
How to create and run MPI projects in CLion

How to create and run MPI projects in CLion

  1. Download and install MPI from Microsoft Website. You need to download both msmpisetup.exe and msmpisdk.msi.

Note that you shouldn't have spaces or cyrillic characters in path to MPI installation.

  1. Open Windows Terminal and try running mpiexec:
Microsoft MPI Startup Program [Version 10.1.12498.18]

...

If the mpiexec command isn't recognised by your system try restarting your PC and check if MPI system variables are installed correctly:

Name Value
MSMPI_BENCHMARKS path_to_mpi\Benchmarks\
MSMPI_BIN path_to_mpi\Bin\
MSMPI_INC path_to_mpi\SDK\Include\
MSMPI_LIB32 path_to_mpi\SDK\Lib\x86\
MSMPI_LIB64 path_to_mpi\SDK\Lib\x64\

And path_to_mpi\Bin\ path is added to Path variable.

To view system variables open Windows Search -> Input "Variables" -> Click on "Edit the system environment variables" -> Click on "Environemnt Variables on "System Properties" Window.

path_to_mpi is a path to your Microsoft MPI installation.

  1. Open CLion and create new C++ Executable Project.

  2. Open CMakeLists.txt where you should see something like this: (MY_PROJECT is project name)

cmake_minimum_required(VERSION 3.20)
project(MY_PROJECT)

set(CMAKE_CXX_STANDARD 14)

add_executable(MY_PROJECT main.cpp)
  1. Add MPI Package and MPI Library to project
cmake_minimum_required(VERSION 3.20)
project (MY_PROJECT)

set(CMAKE_CXX_STANDARD 14)

# Add MPI Package to Project
find_package(MPI REQUIRED)

add_executable(MY_PROJECT main.cpp)
# Add libraries for code completion and compiling
target_link_libraries(MY_PROJECT PUBLIC MPI::MPI_CXX)
  1. Add following run configurations:

1) CMake Application

Property Value
Name Compile
Tagret MY_PROJECT
Executable MY_PROJECT

IMPORTANT! Replace MY_PROJECT with the name of your project!

2) Shell Script

Property Value
Name 2 Cores
Execute Script text
Script text mpiexec -n 2 ./cmake-build-debug/MY_PROJECT
Working directory project_directory/MY_PROJECT
Execute in the terminal true

Add more shell scripts for number of cores you would like to use while executing your project. Replace number 2 in Name and Script text properties with number of cores you would like to use.

MY_PROJECT is a name of .exe file in ./cmake-build-debug folder.

  1. Add following code to main.cpp:
#include <mpi.h>
#include <cstdio>

int main(int argc, char** argv) {
	// Initialize the MPI environment
	MPI_Init(NULL, NULL);

	// Get the number of processes
	int world_size;
	MPI_Comm_size(MPI_COMM_WORLD, &world_size);

	// Get the rank of the process
	int world_rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

	// Get the name of the processor
	char processor_name[MPI_MAX_PROCESSOR_NAME];
	int name_len;
	MPI_Get_processor_name(processor_name, &name_len);

	// Print off a hello world message
	printf("Hello world from processor %s, rank %d out of %d processors\n",
		   processor_name, world_rank, world_size);

	// Finalize the MPI environment.
	MPI_Finalize();
}
  1. Setup CMake "Release" configuration. Go to Settings -> Build, Execution, Deployment -> CMake. Click on "Plus" sign and a new "Release" configuration should be created. Click on "OK" or "Apply".

  2. Select "Release" under "Configurations" dropdown.

  3. Run Compile configuration before executing any of X Cores configurations.

If you are getting an error "Cannot start process, the working directory '../cmake-build-release' does not exist" then RMC on your project folder and select "Reload CMake Project".

@ngDuyAnh
Copy link

Blessed.

@hoi-bj
Copy link

hoi-bj commented Oct 2, 2022

Thanks!
The MPI inclusion directories must be added to the CMake file
include_directories(SYSTEM ${MPI_INCLUDE_PATH})

@sanyavertolet
Copy link

Thank you for good guidelines!

@JReesW
Copy link

JReesW commented Mar 27, 2023

Had everything I needed, thank you!

@iheathers
Copy link

https://www.jetbrains.com/help/clion/openmpi.html You can refer to documentation. It's clear and easy

@GeorgeVe
Copy link

GeorgeVe commented Nov 8, 2023

Thanks!
For anyone else who still runs into a "Could NOT find MPI". Check the ./cmake-build-debug/CMakeFiles/CMakeOutput.log and if it says "fatal error: sal.h: No such file or directory", check your toolchain (for CLion: settings->Build,Execution,Deployment->Toolchains). For me it worked by switching from MinGW (version: 5.0) to Bundeld MinGW (version: w64 9.0).

Another tip:
If you are compiling with the help of an IDEA, such as Clion, make sure that the system variables are also up to date there. I did restart my CLion (and even invalidated caches) however, it turns out this is not enough. You need to restart your computer after installing MPI/adding system variables before they are updated in CLion as well. You can check if you have the system environment variables in CLion by going to settings->Build,Execution,Deployment->CMake->click the list icon in the environment tab, and look if the MSMPI_BIN and the rest are in there.

@bogdansanduu
Copy link

Thanks a lot for this!

@FAST-Technologies
Copy link

Tried your guide, and yes, its actually good works for Windows 11 system

@wacns
Copy link

wacns commented Dec 4, 2024

-- Could NOT find MPI_C (missing: MPI_C_LIB_NAMES MPI_C_HEADER_DIR MPI_C_WORKS)
CMake Error at C:/Users/---------------/AppData/Local/Programs/CLion/bin/cmake/win/x64/share/cmake-3.30/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
Could NOT find MPI (missing: MPI_C_FOUND MPI_CXX_FOUND)
Call Stack (most recent call first):
C:/Users/---------/AppData/Local/Programs/CLion/bin/cmake/win/x64/share/cmake-3.30/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
C:/Users/--------------/AppData/Local/Programs/CLion/bin/cmake/win/x64/share/cmake-3.30/Modules/FindMPI.cmake:1841 (find_package_handle_standard_args)
CMakeLists.txt:5 (find_package)

cmake_minimum_required(VERSION 3.30)
project(untitled)

set(CMAKE_CXX_STANDARD 26)
find_package(MPI REQUIRED)

target_link_libraries(untitled PUBLIC MPI::MPI_CXX)

include_directories(SYSTEM ${MPI_INCLUDE_PATH})

add_executable(untitled main.cpp)

@Frebbers
Copy link

To get this working standard C, use the following CMakeLists.txt template instead:

cmake_minimum_required(VERSION 3.20)
project(MY_PROJECT)
set(CMAKE_C_STANDARD 11)
find_package(MPI REQUIRED)
add_executable(MY_PROJECT main.c)
target_link_libraries(MY_PROJECT PUBLIC MPI::MPI_C)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment