Liangfu Chen's Homepage
  • Home
  • About
  • Categories
  • Tags
  • Archives

Notes on Programming with OpenMP on MacOSX

Contents

  • Setup Compiler To Work With OpenMP
    • Conclusion
  • Configure CMAKE to Create Xcode Project with OpenMP enabled

Setup Compiler To Work With OpenMP

OpenMP bring us an efficient way to parallelize what we have already done with normal C/C++ programming. Before moving to MacOSX, I personally prefer programming with gcc under Debian.

Unfortunately, the default compiler XCode bring to me was llvm-clang, which don't officially support OpenMP yet (as in Xcode7.3.1). I searched for solutions on Google, the best solution seems to be installing clang-omp through brew.

However, as pointed out in the comment on the stackoverflow post [1]. clang-omp have been removed from brew, now being directly supported by upstream llvm, which exist on brew as well.

So, I can't find a clang-omp on brew. What I found is that openmprtl.org provide open-source library, which provide runtime for default clang-omp feature in Xcode. By configuring with cmake, which can be installed through brew, and compile the library with default compiler, libiomp5.dylib is generated. Then, by running make install, I install the library into my local path, which exist at /usr/local.

When compiling a simple openmp example (like the one provided at https://clang-omp.github.io), add -Xclang -fopenmp to replace the default option -fopenmp in gcc, which is provided by installing Xcode. For instance, by running:

$ gcc -Xclang -fopenmp hello.c -o hello -liomp5

the simple example can be sucessfully compiled without install extra compiler through brew.

Conclusion

  • The option -Xclang -fopenmp should be used instead of normal -fopenmp.
  • The openmp runtime library should be downloaded from https://www.openmprtl.org/download .
  • The runtime library should be configured with cmake and installed into local path /usr/local.
  • During link stage of compiling your program, directly link to libiomp5.dylib, instead of gomp which exists in linux.

Configure CMAKE to Create Xcode Project with OpenMP enabled

More recently, I found out a way to configure my project with CMake in MacOSX with OpenMP enabled. And ported my private fork of OpenCV to MacOSX.

Because clang-omp is no longer available in brew, therefore I downloaded a fresh pre-built version of clang (v3.8.1, with native OpenMP support) at http://llvm.org/releases/download.html , so that I can ensure I have a copy of clang program that have OpenMP support built-in, and install the program into /usr/local.

Then configure the project with cmake:

$ cd $PROJECT_ROOT && mkdir build && cd build
$ CC=/usr/local/bin/clang cmake -G Xcode ..

This way, cmake generates Xcode project files that compile programs with the clang program I just downloaded.

Additionally, at runtime, the compiled program built by Xcode look for DYLD_LIBRARY_PATH for shared libraries. We can either set the value before running the program

$ DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:lib/Debug ./bin/Debug/myprogram

or tell cmake directly that we want load what we linked to:

if(APPLE)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
else()
# ...
endif()

See also:

[1]SO post on 'clang-omp in Xcode under El Capitan'
[2]clang-omp official website
Comments
comments powered by Disqus

  • « Notes on using Git
  • Notes on Programming with MXNet »

Published

Jul 24, 2016

Category

Articles

Tags

  • openmp 1
  • parallel-programming 1
  • xcode 1

Stay in Touch

  • Liangfu Chen's Homepage - Keep Calm and Carry On!
  • Powered by Pelican. Theme: Elegant by Talha Mansoor