I’ve been taking the course Parallel Computing this semester, therefore I want to configure Open MP and MPI on my Mac. This post describes how I configured OpenMP using Homebrew and built from MPI source successfully.

1. Enable Open MP on OS X 10.11

Remember to run brew update before you continue. If you haven’t installed, please refer to Homebrew.

One thing you should know is that OpenMP is not really a package that you install on your computer. Instead, it’s part of the compiler. Therefore, what we need to do is to compile a compiler with OpenMP support. We’ll use Homebrew to build the GNU Compiler Collection (GCC) on our Mac from source, with OpenMP support. To do that, type in the following command:

$ brew install gcc --without-multilib

If you have installed gcc before:

$ brew reinstall gcc --without-multilib

According to my experience, it takes about half an hour to one hour to build gcc since it’s quite a large piece of program. Just be patient. After you installed it, you should be able to compile programs with OpenMP support with:

gcc-6 -fopenmp

Configure OpenMP in Clion

Once you’ve successfully built it, you can either compile programs in Terminal, or can you configure it in CLion to write and debug more convenient.

What you need to do is add the following lines in CMakeList.txt:

set(CMAKE_CXX_COMPILER /usr/local/bin/g++-6)
set(CMAKE_C_COMPILER /usr/local/bin/gcc-6)
# enable openmp using -fopenmp
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")

before add_executable.

You can test if it works correctly by #include "omp.h" :

#include <iostream>
#include "omp.h"

void hello()
    int my_rank=omp_get_thread_num();
    int thread_count=omp_get_num_threads();

//    std::cout << "hello from rank " << my_rank << " of " << thread_count << std::endl;
    printf("hello from rank %d of %d \n",my_rank,thread_count);

int main(int argc, char* argv) {

    int thread_count=12;
#	pragma omp parallel num_threads(thread_count)

    return 0;

2. Enable MPI on OS X 10.11

MPI is only an interface. Therefore, there are several MPI implementations, such as MPICH, OpenMPI, and Intel MPI. In this post, we’ll install OpenMPI. Here we also want to build it from source because then our MPI compiler is using the gcc we built just now with OpenMP support. If you installed the binary distribution, it’s compiled with clang without any OpenMP support. To build OpenMPI, type in the following command

$ brew install openmpi --build-from-source --cc=gcc-6

Currently, the newest system firmware is macOS 10.12, but I need to run Tensorflow and Caffe on my Mac, they requires Xcode 7.3.1, so I couldn’t update to the latest version of Xcode and Xcode Command Line Tool. So I can’t install it using the above command, instead, I need to build from source.

On some versions of Mac OS X / MacOS Sierra, the default temporary directory location is sufficiently long that it is easy for an application to create file names for temporary files which exceed the maximum allowed file name length. With Open MPI v2.0.x, this can lead to errors like the following at application startup:

$ mpirun ... my_mpi_app
[[53415,0],0] ORTE_ERROR_LOG: Bad parameter in file ../../orte/orted/pmix/pmix_server.c at line 264
[[53415,0],0] ORTE_ERROR_LOG: Bad parameter in file ../../../../../orte/mca/ess/hnp/ess_hnp_module.c at line

Or you may see something like this (v2.1.x):

$ mpirun ... my_mpi_app
PMIx has detected a temporary directory name that results
in a path that is too long for the Unix domain socket:
    Temp dir: /var/folders/mg/q0_5yv791yz65cdnbglcqjvc0000gp/T/openmpi-sessions-502@anlextwls026-173_0/53422
Try setting your TMPDIR environmental variable to point to
something shorter in length.

The workaround for the Open MPI 2.0.x and v2.1.x release series is to set the TMPDIR environment variable to /tmp or other short directory name.

Configure MPI in CLion

Add the following lines in CMakeList.txt:

set(CMAKE_C_COMPILER /opt/openmpi/bin/mpicc)
set(CMAKE_CXX_COMPILER /opt/openmpi/bin/mpic++)

If it doesn’t work, try #include_directories({MPI_INCLUDE_PATH}) in CMakeList.txt.

If it still doesn’t work, try:

This will reload CLion cache, and it should work out then. ^_^

Then you can use #inlcude "mpi.h" to write MPI programs.

The detailed MPI Tutorial is here.