OpenMP on Mac fails -- SUCCESS

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.