OpenMP on OS X
Posted on May 14, 2015 by Paul
At the time of this writing Clang, the default C++ compiler on OS X, doesn’t have an OpenMP implementation. GCC however supports OpenMP 4, but you need to install it yourself if you want to use it. GCC 5 can be built from sources for OS X if you want to have the latest and greatest or, you can install it with Homebrew.
OpenMP ads threading support to a C or C++ code through pragmas, this has the advantage that you can take a serial code and parallelize it with minimal code modifications.
As a side node, starting with C++11, you can directly use the thread header if you want to explicitly parallelize your code and this approach is supported by Clang.
Let’s start with a simple C++14 function that calculates the sum of two vectors:
We can parallelize the above code by adding a single line of code and including the omp.h header, see line 3 from the next piece of code:
the rest of the code doesn’t need to be changed.
We can measure the performance of the above code with the steady_clock from the chrono header:
This code can be compiled with:
On a MacBook Air with a dual-core Intel i7 processor, e.g. the above code takes about 800ms for a serial run versus 400 - 500ms for a parallel run. If we use the -O3 optimization level we get much closer results, about 280ms for a serial run versus about 170ms for a parallel run. Please note that this example was presented for the sake of simplicity, you will need to use a more CPU intensive code if you want to see real differences between a serial versus a parallel run.
If you want to learn more about OpenMP I would recommend reading Using OpenMP by B. Chapman, G. Jost: