Skip to content
lukasMega edited this page Oct 9, 2014 · 9 revisions

How to code parallel stuff in C/C++ using MPI with CLion on Windows

1. Install Cygwin

  1. Download Cygwin - https://cygwin.com/install.html for 64-bit versions of Windows, if you running on 64-bit Windows
  2. Install Cygwin for example into C:\cygwin64 with packages:

Packages for compilling C/C++ (and for CLion system requirements):

  • search gcc  for package: Devel -> gcc-core
  • search gcc for package: Devel -> gcc-g++
  • search gcc for package: Libs  -> libgcc1
  • search make for package: Devel -> cmake
  • search make for package: Devel -> make
  • search gdb for package: Devel -> gdb

Packages for MPI:

  • search mpi for package: Libs  -> libopenmpi
  • search mpi for package: Libs -> libopenmpi-devel
  • search mpi for package: Libs -> libopenmpicxx1
  • search mpi for package: Libs -> libopenmpifh2
  • search mpi for package: Libs -> openmpi
  1. Now in Cygwin Terminal run: $ mpicc and $ mpirun

  2. If result is:

     gcc: fatal error: no input files
     compilation terminated.
    

and:

    --------------------------------------------------------------------------
    mpirun could not find anything to do.
    It is possible that you forgot to specify how many processes to run
    via the "-np" argument.
    --------------------------------------------------------------------------
  1. Then all is OK. Now you can compile and run MPI programs.

2. Download CLion

  1. Download CLion EAP Build - http://confluence.jetbrains.com/display/CLION/Early+Access+Program
  2. You can install .exe version or extract portable .zip version (-;

3. Set external tool for mpicc and mpirun

create new file MPI.xml

<?xml version="1.0" encoding="UTF-8"?>
<toolSet name="MPI">
    <tool name="mpicc" description="MPI C compiler" showInMainMenu="true" showInEditor="true" showInProject="true" showInSearchPopup="true" disabled="false" useConsole="true" showConsoleOnStdOut="false" showConsoleOnStdErr="false" synchronizeAfterRun="true">
    <exec>
      <option name="COMMAND" value="C:\cygwin64\bin\sh.exe" />
      <option name="PARAMETERS" value="-l -c &quot;cd /cygdrive/d/Dropbox/Dev/CLion_projects/$FileDirName$; mpicc -std=c99 $FileName$ -o $FileNameWithoutExtension$.exe&quot;" />
      <option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
    </exec>
  </tool>
  <tool name="mpirun" description="Runner for MPI" showInMainMenu="true" showInEditor="true" showInProject="true" showInSearchPopup="true" disabled="false" useConsole="true" showConsoleOnStdOut="false" showConsoleOnStdErr="false" synchronizeAfterRun="true">
    <exec>
      <option name="COMMAND" value="C:\cygwin64\bin\sh.exe" />
      <option name="PARAMETERS" value="-l -c &quot;cd /cygdrive/d/Dropbox/Dev/CLion_projects/$FileDirName$; mpirun -np $Prompt$ -bind-to core:overload-allowed $FileNameWithoutAllExtensions$&quot;   " />
      <option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
    </exec>
  </tool>
</toolSet>
  1. in this file MPI.xml find and replace 2 occurences of d/Dropbox/Dev/CLion_projects with your path to directory with CLion projects.

  2. and make sure that option COMMAND has correct value (path to sh.exe in cygwin64\bin folder)

NOTICE: D:\something must be d/something

NOTICE: avoid spaces in path!

  1. save MPI.xml file to .clion\config\tools

default path is: C:\Users\<yourUserName>\.clion\config\tools

4. Create new project in CLion

  1. Create new project (for example with name MPI_Project)
  2. wait for end of process at background (updating indicies, building symbols, etc. in CLion)
  3. edit file CMakeLists.txt

append this at the end of this file:

  # 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_Project ${MPI_LIBRARIES})
  1. now you can use #include <mpi.h> and you have code completition for MPI

5. Check settings in CLion

Run CLion and:

  1. open File > Settings (CTRL + ALT + S)
  2. open Tools > External Tools
  3. now you can see a tree:
  • MPI
    • mpicc
    • mpirun
  1. you can see that settings here are the same as in MPI.xml. CLion has loaded our MPI.xml file.

6. Compile!

Right click on source file > MPI > mpicc

compile via mpicc

after successfull compilation:

after successfull compilation

7. Run!

Right click on source file > MPI > mpirun

run

now you type number of processes for running (np parameter)

Done!

created by Lukáš Melega