List

All screenshots are clickable.

For certain purposes, it is very beneficial to use OpenMP (shared memory parallelization) for the development.
This page covers the necessary steps to use OpenMP in Xcode (via Clang compiler).

The Clang compiler that is installed on Mac with Xcode does not have all the necessary features (at least at the moment of this post publication). I suggest installation of llvm compiler via Homebrew.

  1. Install Homebrew if it is not installed on your Mac machine.
  2. Install llvm by pasting the following command in your Terminal:
    brew install llvm

The following steps will be explained based on the “dummy project”; however, it should work for the existing project as well. The following code example is a standard one-thread HelloWorld.

#include <iostream>
int main(int argc, const char * argv[]) {
    std::cout << "Greetings from thread 1"<<std::endl;
    return 0;
}

  1. Specify a user-defined setting CC with the following value /usr/local/opt/llvm/bin/clang
    (If Homebrew changes the location of the llvm installation, the value of the user-defined setting CC has to be changed accordingly.)
    In order to do that:

    1. Select the project in the Project Explorer in the left and select Build Settings
    2. Select from the main Xcode menuEditor -> Add Build Setting -> Add User-Defined Setting
    3. Add the CC setting with the value /usr/local/opt/llvm/bin/clang
  2. Search for the setting Enable Modules (C and Objective-C) and switch it from Yes to No
  3. Add to the Library Search Paths the value /usr/local/opt/llvm/lib to allow for the precompiled libraries (for example, libiomp5) to be dynamically linked to your executable.
  4. Add to the Header Search Paths the value /usr/local/opt/llvm/lib/clang/4.0.1/include to allow inclusion of headers provided by llvm installation (for example, omp.h). Note, the installed version of Clang compiler might be different, so the value of the Header Search Path should be changed accordingly.
  5. Add the -fopenmp compilation flag to Other C Flags
  6. Link llvm OpenMP library dynamically to your executable. To do that:
    1. Select Build Phases -> Link Binary With Libraries options in the main Xcode window
    2. Open a Finder window and select Go To Folder option from the main menu.
    3. Navigate to /usr/local/opt/llvm/lib/ folder and select libimpl5.dylib file.
    4. Drag it to Xcode main window into the Add Frameworks and Libraries Here

Now the code should compile successfully, but already with llvm-provided Clang compiler.
Slightly editing the code to demonstrate that OpenMP is actually active:

#include <iostream>
#include "omp.h"
int main(int argc, const char * argv[]) {
#pragma omp parallel
#pragma omp critical
    std::cout << "Greetings from thread "<<omp_get_thread_num()<<std::endl;
    return 0;
}

Now the code greets us from all the threads (omp critical section is used so that the output is not “scrambled”)

The number of threads being used is usually controlled via environmental variable that can be edited via Terminal:

export OMP_NUM_THREADS=8

Leave a Reply

Your email address will not be published. Required fields are marked *