astropy / extension-helpers

Helpers to assist with building Python packages with compiled C/Cython extensions
https://extension-helpers.readthedocs.io
BSD 3-Clause "New" or "Revised" License
16 stars 12 forks source link

clang fopenmp on macOS #40

Open lukeolson opened 2 years ago

lukeolson commented 2 years ago

Great package.

To use OpenMP on a mac with clang, I replaced -fopenmp with -Xpreprocessor -fopenmp in the compile flags and -fopenmp with -lomp in the link flags using the output from get_openmp_flags() here: https://github.com/astropy/extension-helpers/blob/main/extension_helpers/_openmp_helpers.py#L137

This is a bit of a hack (on my part). Are there better ideas (or any plans to add mac support like this)?

gdurif commented 2 years ago

Hi, is there any update on this issue ?

If I run the following code on MacOS:

from setuptools import Extension
from extension_helpers import add_openmp_flags_if_available

# create a dummy extension
dummy_ext = Extension(
    'dummy_ext',
    sources=['dummy.cpp'],
    extra_compile_args=['-DNDEBUG', '-DUSE_BLAS_LIB', '-std=c++11'], 
    language='c++',
    depends=['dummy.h']
)

# add OpenMP flag if available
omp_support = add_openmp_flags_if_available(dummy_ext)

I get the following message:

clang: error: unsupported option '-fopenmp'
Cannot compile Cython/C/C++ extension with OpenMP, reverting to non-parallel code

I was thinking about doing a PR solving this but I am sure how to check for the compiler. setuptools.command.build_ext.new_compiler does not discriminate^[1] between gcc and clang. Any idea?

[1] See

$ python setup.py build_ext --help-compiler
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -c test_openmp.c -o objects/test_openmp.o -fopenmp
x86_64-linux-gnu-gcc -pthread objects/test_openmp.o -o test_openmp -fopenmp
Compiling Cython/C/C++ extension with OpenMP support
List of available compilers:
  --compiler=bcpp     Borland C++ Compiler
  --compiler=cygwin   Cygwin port of GNU C Compiler for Win32
  --compiler=mingw32  Mingw32 port of GNU C Compiler for Win32
  --compiler=msvc     Microsoft Visual C++
  --compiler=unix     standard UNIX-style compiler
gdurif commented 2 years ago

Working on a fix here #42

gdurif commented 2 years ago

@lukeolson What is your OS configuration?

I tried compiling the following code

#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d over %d\n", omp_get_thread_num(), omp_get_num_threads());
}

with clang -Xpreprocessor -fopenmp test.c -lomp but I get a 'omp.h' file not found error.

Thanks

Edit: brew install libomp solved the issue.

gdurif commented 2 years ago

42 should fix the issue.

lukeolson commented 2 years ago

I did the same... using libomp and setting the environment flags:

export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
export CFLAGS="$CFLAGS -I$(brew --prefix libomp)/include"
export CXXFLAGS="$CXXFLAGS -I$(brew --prefix libomp)/include"
export LDFLAGS="$LDFLAGS -Wl,-rpath,$(brew --prefix libomp)/lib -L$(brew --prefix libomp)/lib -lomp"