Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X 10.8, llvm, OpenMP with CMake

I have just upgraded to OS X 10.8 "Mountain Lion" and I start regretting having done that.

I am no longer able to build my (simple) parallel code that uses OpenMP.

By looking here and there I understood that Apple is no more using 'gcc' but 'llvm' - quite likely due to licence-related issues. Unfortunately 'llvm' does not (yet?) support OpenMP.

I cannot believe many programs based on OpenMP will not run any more on OS X, so my question is: how can I enable OpenMP in Mountain Lion?


After reading the excellent answer of Hristo Iliev, I think I missing more than I initially thought.

I am not using XCode. The building is based on CMake and make/gcc. Yesterday I installed gcc 4.2 via macports, but - as of today - my code is still in source form because - for some strange reasons - I am not able to switch the compilation to gcc (same errors related to OpenMP).

The problem now is that CMake keeps using the llvm compiler. In the root CMakeLists.txt I added the following two lines:

set(CMAKE_C_COMPILER "/opt/local/bin/gcc-apple-4.2")  
set(CMAKE_CXX_COMPILER "/opt/local/bin/gcc-apple-4.2")

and I also added two environments variables:

export CXX=/opt/local/bin/gcc-apple-4.2
export CC=/opt/local/bin/gcc-apple-4.2

When CMake tries to find OpenMP ( FIND_PACKAGE(OpenMP) ) I get the following output:

-- The compiler is /opt/local/bin/gcc-apple-4.2
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
[...]
CMake Error at /opt/local/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (message):
Could NOT find OpenMP (missing: OpenMP_C_FLAGS OpenMP_CXX_FLAGS)
Call Stack (most recent call first):
/opt/local/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:291    (_FPHSA_FAILURE_MESSAGE)
/opt/local/share/cmake-2.8/Modules/FindOpenMP.cmake:159 (find_package_handle_standard_args)
demo/CMakeLists.txt:8 (FIND_PACKAGE)

I tried to run /opt/local/bin/gcc-apple-4.2 manually and it fails due to a missing libgomp.

Regards

like image 428
Antonio Sesto Avatar asked Feb 28 '13 18:02

Antonio Sesto


2 Answers

LLVM is a compiler framework that Apple uses extensively in OS X (most notably in the OpenGL implementation), not a compiler itself. There are two LLVM frontends available in Xcode - the old GCC-based one llvm-gcc (which supports OpenMP 2.5) and the new clang (which does not support OpenMP). clang has extensive static code analysis capabilities and also much nicer error reporting, especially with C++ code. Mostly that's the reason Apple is investing in it and moving away from GCC (the FreeBSD project, on the other side, is seeking to replace GCC with clang for purely license-related reasons). There is an ongoing project clomp (not to be mistaken with Intel's Cluster OpenMP) that seeks to create an OpenMP-aware version of clang, but it is far from mature.

If you do not need new OpenMP constructs like explicit tasks and can live with some bugs, you can still use the old GCC frontend. Just switch the compiler suite in the project settings to LLVM GCC 4.2:

Compiler suite chooser

This one is based on the really ancient GCC 4.2.1 and no longer seems to be developed. Should you need a newer version of GCC, simply build one. Unfortunately I cannot provide you with information on how to integrate it into Xcode. It works for me on the command line with GCC 4.7.1 and OS X 10.8.2 (and I don't use Xcode much).

like image 197
Hristo Iliev Avatar answered Sep 23 '22 20:09

Hristo Iliev


Just adding few words to excellent Hristo's answer: you can install gcc via MacPorts. This is much simpler than compiling everything by hands (and compiling gcc from scratch might be really non-trivial task due to dependencies, etc).

So, install MacPorts, do "sudo port install gcc47", wait few hours and you'll end with gcc-mp-4.7 which is able to compile your OpenMP code.

like image 44
Anton Korobeynikov Avatar answered Sep 19 '22 20:09

Anton Korobeynikov