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

Enable OpenMP with Apple Clang (Mac default compiler) #5146

Merged
merged 14 commits into from
Dec 26, 2019

Conversation

hcho3
Copy link
Collaborator

@hcho3 hcho3 commented Dec 22, 2019

Simplify logic for importing OpenMP into the CMake build by treating OpenMP as a target. We no longer have to set compiler flags; all it takes is to import target OpenMP::OpenMP_CXX via target_link_libraries(). See https://cliutils.gitlab.io/modern-cmake/chapters/packages/OpenMP.html for more information.

This will be especially useful for Mac OSX users. The old way requires them to use Homebrew GCC, which is a heavy dependency (*). The new way allows them to use Apple Clang (default system compiler) instead, provided that they install OpenMP library via Homebrew (brew install libomp). Furthermore, we can release XGBoost 1.0.0 on Homebrew with OpenMP enabled.

CMake version is raised to 3.9 3.12 to access OpenMP::OpenMP_CXX target. 3.12 is needed to use target_link_libraries() with object target objxgboost. Note that we've been already requiring 3.12 for the GPU algorithms.

Closes #4477.
Depends on dmlc/dmlc-core#586

(*) See discussion at Homebrew/homebrew-core#43246, where Homebrew maintainers asked to remove GCC dependency from XGBoost.

@hcho3 hcho3 changed the title Add OpenMP as CMake target Enable OpenMP with Apple Clang (Mac default compiler) Dec 22, 2019
src/CMakeLists.txt Outdated Show resolved Hide resolved
src/CMakeLists.txt Outdated Show resolved Hide resolved
src/CMakeLists.txt Outdated Show resolved Hide resolved
@StrikerRUS
Copy link
Contributor

@hcho3 Just want to make sure that you are aware of the following issues that have been fixed only in the latest CMake version (3.16) released recently.
https://gitlab.kitware.com/cmake/cmake/issues/18520
https://gitlab.kitware.com/cmake/cmake/issues/18098
https://gitlab.kitware.com/cmake/cmake/issues/18470

Fix: https://gitlab.kitware.com/cmake/cmake/merge_requests/3916

In case you do not want to bump CMake version and keep it 3.12 the following workaround works fine for OpenMP installed via Homebrew:

# For Mojave or newer (>=10.14)
cmake \
  -DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp -I$(brew --prefix libomp)/include" \
  -DOpenMP_C_LIB_NAMES="omp" \
  -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp -I$(brew --prefix libomp)/include" \
  -DOpenMP_CXX_LIB_NAMES="omp" \
  -DOpenMP_omp_LIBRARY=$(brew --prefix libomp)/lib/libomp.dylib \
  ..

# For High Sierra or earlier (<= 10.13)
cmake ..

@hcho3
Copy link
Collaborator Author

hcho3 commented Dec 23, 2019

@StrikerRUS Indeed, the build fails for CMake 3.12. I had tested this PR on my Macbook Pro and it was using CMake 3.16. Switching to CMake 3.12 resulted in error Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES).

Since Homebrew makes it easy to install latest versions of packages, I'm inclined to require CMake 3.16+ for Mac OSX users.

CMakeLists.txt Outdated Show resolved Hide resolved
@hcho3 hcho3 force-pushed the modernize_openmp branch 3 times, most recently from 3309906 to c2b582a Compare December 23, 2019 07:13
Copy link
Member

@trivialfis trivialfis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know whether the OMP flags are correctly set for CUDA files? One pain point in CMake is, flags for C++ and CUDA are separately managed.

tests/travis/run_test.sh Outdated Show resolved Hide resolved
@hcho3
Copy link
Collaborator Author

hcho3 commented Dec 23, 2019

@trivialfis

Do you know whether the OMP flags are correctly set for CUDA files?

Yes, I'm pretty sure OMP flags are set correctly. See fec00b7. Otherwise, you'd see build failure on Windows like this

Copy link
Member

@trivialfis trivialfis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for cleaning up the code along with the fix.

@hcho3
Copy link
Collaborator Author

hcho3 commented Dec 23, 2019

Thanks for reviewing. Don't merge this before dmlc/dmlc-core#586, as without it this PR will break.

@trivialfis
Copy link
Member

Ready for merging?

@hcho3
Copy link
Collaborator Author

hcho3 commented Dec 26, 2019

I'll merge dmlc/dmlc-core#587 soon (today). Then I'll merge this PR.

@hcho3
Copy link
Collaborator Author

hcho3 commented Dec 26, 2019

Done. Please merge this when all tests are done running.

@trivialfis
Copy link
Member

@hcho3 @StrikerRUS Thanks!

@trivialfis trivialfis merged commit 9b0af6e into dmlc:master Dec 26, 2019
@hcho3 hcho3 deleted the modernize_openmp branch December 26, 2019 16:18
@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better XGBoost installation on Mac OSX?
3 participants