Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4.5 and OpenMP with Clang (Apple LLVM) uses only one core

We are using Xcode 4.5 on a C++11 project where we use OpenMP to speed up our computation:

#pragma omp parallel for
for (uint x=1; x<grid.width()-1; ++x) {
    for (uint y=1; y<grid.height()-1; ++y) {
         // code
    }
}

Although the Activity Monitor shows multiple threads being used by the program we observed that only one core is used:

Screenshot of the Activity Monitor running my code

We also run the same code on Ubuntu using GCC 4.7 and we observed contention on all cores. Could it be that the OpenMP support has been removed in the Apple LLVM? Is there an alternative to OpenMP? We can't switch to GCC since we use C++11 features.

like image 356
Pascal Avatar asked Dec 05 '12 09:12

Pascal


People also ask

Does Apple Clang support OpenMP?

The default C compiler on macOS, Apple clang (confusingly aliased as /usr/bin/gcc ), does not directly support OpenMP.

Does Xcode use LLVM?

In Xcode, the LLVM compiler uses the Clang front end (a C-based languages project on LLVM.org) to parse source code and turn it into an interim format. Then the LLVM code generation layer (back end) turns that interim format into final machine code.

Does Apple Clang support C ++ 17?

You can use Clang in C++17 mode with the -std=c++17 option (use -std=c++1z in Clang 4 and earlier).

Is LLVM made by Apple?

LLVM was released under the University of Illinois/NCSA Open Source License, a permissive free software licence. In 2005, Apple Inc. hired Lattner and formed a team to work on the LLVM system for various uses within Apple's development systems.


1 Answers

Edit: This answer is now partially outdated. Modern Clang does support OpenMP, just not Apple's build distributed with Xcode. It is possible to get an OpenMP-enabled compiler from Homebrew or another prepackaged repository.

Clang does not yet support OpenMP (it has not been removed - it never existed in the first place). You could use Apple's Grand Central Dispatch (GCD) tasking system or you could use Intel's Threading Building Blocks (TBB) instead.

like image 181
Hristo Iliev Avatar answered Sep 29 '22 11:09

Hristo Iliev