OpenMP on Mac fails -- SUCCESS

Has anybody gotten OpenMP to work on a Mac?

I installed OpenMP: brew install libomp
But it has no openmp.cmake file, so cmake cannot find it.

I see /usr/local/include/omp.h and /usr/local/lib/libomp.{a,dylib}.
So how do I build an openmp.cmake file?
Can someone display a working one?

This is macOS Catalina (10.15.7) on an 8-core Mac Pro. I really want to use its 16 threads!

OpenMP is enabled by default in our cmake configuration and should be picked up with the line find_package(OpenMP). What do you see when you run cmake?

This is macOS Catalina (10.15.7) on a Mac Pro with 8 cores / 16 threads, using HomeBrew.

The relevant output lines from cmake are:

– Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
– Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
– Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)

There are two basic problems here:

  1. “brew install libomp” does not install OpenMP-config.cmake. It does install omp.h, libomp.a, and libomp.dyllib into /usr/local. So the code is there, but cmake cannot find it.
  2. Apple’s clang does not support OpenMP, and does not recognize “-fopenmp”

I got it to work by:

  1. brew install llvm
  2. edit CMakeLists.txt to replace the find_package:
    #TJR find_package(OpenMP)
    set(OPENMP_FOUND TRUE)
    set(OpenMP_CXX_FLAGS “-fopenmp”)
    #TJR end edits
  3. use this command in the empty build directory:
    CC=/usr/local/opt/llvm/bin/clang CXX=/usr/local/opt/llvm/bin/clang++ LDFLAGS=-L/usr/local/lib cmake -DCMAKE_INSTALL_PREFIX=$HOME/openmc ..
  4. Note the brew-installed g++-11 did not work (some missing libraries). But the brew-installed clang++ (in package ‘llvm’) does work.

This is clearly very Mac and HomeBrew specific, and required a level of software expertise not often found in physicists. I do not know why neither Apple nor HomeBrew got it right.

@tjrob Glad you were able to figure out a workaround. Out of curiousity what version of CMake are you using? I’m wondering if perhaps they have fixed the OpenMP module to work out of the box in recent releases – ideally the find_package(OpenMP) call should just work!

$ cmake --version
cmake version 3.12.3

That’s quite old – apparently I installed it manually some time ago. So I removed it and did “brew install cmake” and got cmake version 3.23.1.

I removed my edits to CMakeLists.txt, removed and recreated the build directory, and now cmake finds OpenMP. The Apple clang++ compiler still cannot use it, but HomeBrew’s clang++ can; not editing CMakeLists.txt is a big improvement.

For the record, after “brew upgrade; brew install llvm libomp”, with source in $HOME/openmc, the commands are:

cd $HOME/openmc; rm -fr build; mkdir build; cd build; CC=/usr/local/opt/llvm/bin/clang CXX=/usr/local/opt/llvm/bin/clang++ LDFLAGS=-L/usr/local/lib cmake -DCMAKE_INSTALL_PREFIX=$HOME/openmc ..; make install

That puts the executable into $HOME/openmc/bin

Thanks.

2 Likes