Installing HomeBrew/OpenMP on MacOS

You have the option to compile the OOFEM with OpenMP in order to enable multithreading option (-t #number_of_threads).

In CLion preferences, add a new CMake profile with the corresponding CMake option to enable OpenMP library for multi-threading. For OOFEM you need to add the following CMake option:

-DUSE_OPENMP=ON

(the default is OFF)

If the OpenMP is not found, the compilation may continue without OpenMP despite the options you have given to cmake but you cannot use -t argument with OOFEM. To successfully compile OOFEM with OpenMP, you need to first make sure that the OpenMP library is available on your machine. If you use Clang compiler, you need to make sure that the compiler supports OpenMP otherwise even if you install OpenMP library you still won’t be able to compile the code successfully. One solution with Clang is to use OpenMP-enabled version of Clang (clang-omp).

Installing Homebrew

Unlike Linux, MacOS does not come with a useful package manager. Homebrew is a third-party package manager that can help us install some packages easily. You probably don’t need it on Linux. To install Homebrew on MacOS:

1. Make sure that the Xcode is already installed.

2. Open terminal and run the following Bash command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installing OpenMP on MacOS

Install OpenMP if you are using a compiler that supports OpenMP but does not come with it. With Homebrew installed you can use the following command in the terminal to install OpenMP on MacOS:

brew install libomp

For MacOS, this should add the OpenMP library to the following location :

Header files (location of omp.h):
/usr/local/opt/libomp/include

Library files (location of libomp.dylib):
/usr/local/opt/libomp/lib/

Compiler Errors with OpenMP

One common error with OpenMP is when the compiler fails to locate the OpenMP header files directory:

fatal error: 'omp.h' file not found

You may need to provide OpenMP header files/ library location to the compiler. There are several ways to do this.

If you need to provide the location of header files to the compiler (error is some .h file is not found) you can use the following CMake variable (for C++):

-DOpenMP_CXX_FLAGS="-I/usr/local/opt/libomp/include"

Alternatively (above is safer to avoid overriding other flags but for me it didn’t work with Apple’s Clang compiler):

-DCMAKE_CXX_FLAGS="-I/usr/local/opt/libomp/include"

Or

-DCMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES="/usr/local/opt/libomp/include"
  • Note that the location is different for different operating systems. Try to locate the OpenMP files in your system to make sure you are pointing at the correct directory. If you are using homebrew’s OpenMP on MacOS, run (brew --prefix libomp) in the terminal to locate OpenMP
  • These settings may override some flags or maybe ignored (especially for the case of environment variables described below)
  • For the case of environment variable you need to clear the cmake cache beside any change to the vairables to see the effect (CLion tools menu> cmake>Reset Cache and Reload Project)
  • For some flags adding -I or -L is mandatory before the address
  • -I points at header files location, -L points to library

Using Environment variables to point at OpenMP locations

Alternatively one easy way with CLion is to add the environment variables. As you can see, CMake profiles dialog also provides that option.

For example instead of using CMake flags described above you could also define this environment variable:

CXXFLAGS="-I/usr/local/opt/libomp/include"

You may get some errors about architecture symbols not being found with Clang trying to compile using OpenML:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

Providing the location of the library (which contains .dylib file) is supposed to solve this problem. With environment variable LDFLAGS you can provide the location of library:

LDFLAGS="-L/usr/local/opt/libomp/lib/"

I did not find any other way to point at the library location except with the environment variable above.

If despite providing the library location and still getting this error using Apple’s Clang (comes with Xcode), then using mainstream Clang may resolve the problem.

Using mainstream Clang instead of Apple’s Clang (last resort for linker error)

-Installing mainstream Clang

As for Apple’s Clang to successful compile the OpenMP library should be properly linked with OOFEM in CMakeLists.txt file. At present the OOFEM source does not do that. It seems the Apple’s Clang is more likely to generate errors when something goes wrongwhile the mainstream Clang can still compile the code.

So you may want to try mainstream Clang compiler. OpenMP-enabled Clang is now a part of LLVM package that can be installed using Homebrew.

To eview the version and info you can run this code in the terminal:

brew info llvm

Download and install LLVM package including Clang (takes time):

brew install llvm

-Making CLion Toolchain profile for mainstream Clang

Now we create a separate toolchain for the mainstream Compiler so that we can choose which compiler (Apple’s Clang, mainstream Clang & etc.) should use to compile the project.

The location of Homebrew’s LLVM can be checked using

brew --prefix llvm 

The outcome is something like:

/usr/local/opt/llvm

The Clang compiler is in bin directory of LLVM location. Go to the CLion preferences Build, Execution, Deployment > Toolchains

Make a new toolchain, name it as LLVM Clang point to clang and clang++ for C and C++ compilers

Making Toolchain for new Clang compiler

-Creating Cmake profile for the new Toolchain

Now make a new CMake profile with the new Toolchain profile and the corresponding settings and flags for OpenMP. Go to the CLion preferences Build, Execution, Deployment > Toolchains

You can name new profiles like LC_Release_OMP for Release Build type and LC_Debug_OMP for Debug type.

Make a new Cmake profile (pay close attention to selected Toolchain, CMake options and Environment variables)

You can switch to new CMake profile on run/debug configuration drop-down list.

You can quickly switch cmake profiles to compile with the desired compiler

https://iscinumpy.gitlab.io/post/omp-on-high-sierra/

Installing LLVM/Clang on OS X

https://stackoverflow.com/questions/69732120/

Leave a comment

Design a site like this with WordPress.com
Get started