-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Support Apple clang specific compilation/linking flags to enable OpenMP #42
base: main
Are you sure you want to change the base?
Conversation
Log 2022/04/01 1040: remaining problem On my MacOS VM, after installing #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 However, when I try to do the following: 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
which I don't understand. |
Log 2022/04/01 1215: moving a bit forward Running: from setuptools.command.build_ext import new_compiler, customize_compiler
from extension_helpers._openmp_helpers import get_openmp_flags
import distutils.log
distutils.log.set_verbosity(1)
ccompiler = new_compiler()
customize_compiler(ccompiler)
openmp_flags = get_openmp_flags()
compile_flags = openmp_flags.get('compiler_flags')
ccompiler.compile(['test_openmp.c'], extra_postargs=compile_flags) gives the following compilation command:
with the error:
If I move Thus:
Edit: second point solved by
-> a warning should be added |
…ude and lib path (if not aleady setup) for Apple Clang
…sor -fopenmp' not undertood by Apple Clang as postargs but ok as preargs)
Should be ready to merge. Summary of modifications
Edit
End of edit Remaining issue
[1] here is the code used: from setuptools import Extension
from pprint import pprint
from extension_helpers import add_openmp_flags_if_available
import distutils.log
distutils.log.set_verbosity(1)
# 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']
)
# check extension
print("-- Check extension")
pprint(dummy_ext.__dict__)
# add OpenMP flag if available
omp_support = add_openmp_flags_if_available(dummy_ext)
# check OpenMP support
print(f"-- OpenMP support: {omp_support}")
# check extension
print("-- Check extension")
pprint(dummy_ext.__dict__) [2] The compile command is:
It is printed by running the code above because of |
extension_helpers/_openmp_helpers.py
Outdated
) | ||
log.warn(msg) | ||
compile_flags.append('-Xpreprocessor -fopenmp') | ||
if not 'CFLAG' in os.environ and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be CFLAGS
(and LDFLAGS
below)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks! I fixed it in 094b820
extension_helpers/_openmp_helpers.py
Outdated
) | ||
log.warn(msg) | ||
compile_flags.append('-Xpreprocessor -fopenmp') | ||
if not 'CFLAG' in os.environ and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be CFLAGS
(and LDFLAGS
below)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks! I fixed it in 094b820
@lukeolson Seeing your comment #40 (comment) made me think that the code should also check for Edit: introduced by dcca6d7 |
…corresponding environment variable contains one or more
Great! For flags I followed scikit-learn I'll test this with their conda steps too. |
Note: I also modified (c.f e4c01f8 ) Example assuming
Until now, it was only returning the first occurrence. Note: I also added a test for |
extension_helpers/_openmp_helpers.py
Outdated
|
||
compile_flags.append('-fopenmp') | ||
link_flags.append('-fopenmp') | ||
for _ in include_path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be lib_path
? Right now I'm not picking up any LDFLAGS
from the environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is. Thanks. Fixed by e93d695
I left a note on the path above... likely a minor issue. Another possible issue: |
@lukeolson I am not very familiar with MacOS, can we expect Thanks |
probably not, it's a third party tool that provides a community managed kind of repository. It is largely already installed on most buildbots we use though since it's the only sane way to install third party stuff it seems. Regular people probably won't have it out of the box installed because of that I'd say. |
Thanks @samuelstjean, I guess I will have to do a |
Taking a look at cmake [1], one options is to simply encode the right base path -- in this case With homebrew everything is symlinked in Anyway thanks for your work on this! I'll give this version a test. [1] https://github.com/Kitware/CMake/blob/master/Modules/FindOpenMP.cmake |
@lukeolson Thanks for the feedback. It appears that brew/OpenMP configuration does not seem to be standard on every machine. On the two MacOS VMs (10.15.4 and 11.6) that I have access to,
My guess is I will try to cover different possibilities with a combination of As a last resources, the user can setup |
Would this also work in a conda environment on MacOS? |
@MuellerSeb I don't know, I guess it should but I did not test it. |
Potential fix for #40
Apple clang requires
-Xpreprocessor -fopenmp
compile flags (instead of just-fopenmp
) and-lomp
linking flags (instead of-fopenmp
) to support and enable OpenMp.Work in progress, I am still not sure whether it works or not on MacOS.
Edit: should be ready c.f. #42 (comment)