Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang fopenmp on macOS #40

Open
lukeolson opened this issue Mar 24, 2022 · 5 comments
Open

clang fopenmp on macOS #40

lukeolson opened this issue Mar 24, 2022 · 5 comments

Comments

@lukeolson
Copy link

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
Copy link

gdurif commented Mar 31, 2022

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
Copy link

gdurif commented Apr 1, 2022

Working on a fix here #42

@gdurif
Copy link

gdurif commented Apr 1, 2022

@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
Copy link

gdurif commented Apr 1, 2022

#42 should fix the issue.

@lukeolson
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants