manodeep / Corrfunc

⚡️⚡️⚡️Blazing fast correlation functions on the CPU.
https://corrfunc.readthedocs.io
MIT License
163 stars 50 forks source link

OpenMP on Mac OS #172

Open yipihey opened 5 years ago

yipihey commented 5 years ago

Significant warnings are issued suggesting that Mac OS openMP does not work with the system installed clang toc comile Corrfunc.

Following, https://iscinumpy.gitlab.io/post/omp-on-high-sierra/ explains how to use the existing clang on Mac OS and still use openMP. I tested this on Mojave 10.4.1 and it works well.

I used home-brew: brew install libomp

And added in common.mk

Add any compiler specific flags you want

CFLAGS ?= -Xpreprocessor -fopenmp

Add any compiler specific link flags you want

CLINK ?= -lomp

If a kernel crashes it can happen when two openMP libs are installed. To avoid this you can add this in your python script: import os os.environ['KMP_DUPLICATE_LIB_OK']='True'

Or set the variable in your shell environment.

Speed up on MacBook pro is as expected.

Perhaps common.mk could be adjusted to suggest this possibility given how many astronomers use Macs?

Best, Tom

lgarrison commented 5 years ago

Thanks for the report! common.mk should currently print out the following advice:

../common.mk:302: Compiler is Apple clang and does not support OpenMP
If you want OpenMP support, please install clang with OpenMP support
For homebrew, use "brew update && (brew outdated xctool || brew upgrade xctool) && brew tap homebrew/versions && brew install clang-omp"
For Macports, use "sudo port install clang-3.8 +assertions +debug + openmp"

But possibly we'd prefer to just install libomp instead of clang-omp? If the latter happens to fix the duplicate OpenMP library issue, then that would be a strong argument in its favor (seems unlikely though). I've also found that -Wl\,--as-needed in the linker args can help avoid a lot of mischief in loading duplicate OpenMP libs.

I don't have a Mac, but if you or @manodeep could test that linker arg, it could help us decide on a path for stable OpenMP for clang.

manodeep commented 5 years ago

Thanks Tom!

One of the goals of Corrfunc is to ensure that the code compiles on all systems. Supporting OpenMP automatically in a Mac is a complex task, and that's why we only try to do the minimum and leave the responsibility on the user. For reference, I use Corrfunc with OpenMP regularly on my MAC laptop, but I have installed the necessary clang with the OpenMP support.

That being said, the section of the common.mk dealing with clang-omp, and the OpenMP checking + info messages can certainly be improved. And better yet, we could add in documentation on how to install libomp (or libgomp?) on OSX, and then enable OMP support at compile time.

@lgarrison The linker on OSX does not support the --as,needed flag :(

manodeep commented 3 years ago

I can compile and run with multiple threads even with Apple clang with the following steps:

$ conda install llvm-openmp

Then adding the following to the `common.mk` file
CC :=/usr/bin/clang

#### Add any compiler specific flags you want                                                                                                                          
CFLAGS ?=-Xpreprocessor -fopenmp -I$(CONDA_PREFIX)/include

#### Add any compiler specific link flags you want                                                                                                                     
CLINK ?=-L$(CONDA_PREFIX)/lib -lomp -Xlinker -rpath -Xlinker $(CONDA_PREFIX)/lib

Of course, we will need to detect apple-clang, and then conda (and possibly brew if conda is unavailable), install the llvm-openmp package, and add those options to CFLAGS and CLINK.

Noting the solution here for incorporating into the v2.4

manodeep commented 3 years ago

If we were thinking of supporting cases where conda is not available, then this page might be useful