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

[R] Enable OpenMP with AppleClang in XGBoost R package #5240

Merged
merged 2 commits into from
Jan 28, 2020

Conversation

hcho3
Copy link
Collaborator

@hcho3 hcho3 commented Jan 28, 2020

Follow-up to #5232. This PR uses Autoconf to detect the availability of OpenMP library (libomp) on Mac OSX. This is done by compiling the following program with command ${CC} -o conftest conftest.c /usr/local/lib/libomp.dylib -Xclang -fopenmp:

/* conftest.c */
#include <omp.h>

int main(void) {
  return (omp_get_max_threads() <= 1);
    // returns 0 when there's only one thread
    // returns 1 when there's more than one thread
}

Implication: Now Mac users can simply run install.packages('xgboost') and enjoy the performance of multiple threads, as long as they first install libomp package from Homebrew.

I tested this change as follows:

  • I ran R CMD INSTALL . on my Macbook Pro. A sample XGBoost training job runs with all 4 threads:
library(xgboost)
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
train <- agaricus.train
test <- agaricus.test
bst <- xgboost(data = train$data, label = train$label, max.depth = 1, eta = 1,
               nthread = 4, nrounds = 1000, objective = "binary:logistic", verbose = 1,
               eval.metric = 'auc')

Screen Shot 2020-01-28 at 12 34 11 AM

  • Then I uninstalled libomp Homebrew and ran R CMD INSTALL . again. A prominent warning shows up, as I intended:
checking whether OpenMP will work in a package... no
*****************************************************************************************
WARNING: OpenMP is unavailable on this Mac OSX system. Training speed may be suboptimal.
         To use all CPU cores for training jobs, you should install OpenMP by running

             brew install libomp
*****************************************************************************************

Aside from the warning, XGBoost compiles successfully. The sample job ran without a problem, albeit this time with a single thread.

Note to reviewer: you should only review configure.ac. The configure script is generated from configure.ac when autoconf is run.

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.

Awesome! Love it when everything is working out of box.

@hcho3 hcho3 merged commit cb3ed40 into dmlc:master Jan 28, 2020
@hcho3 hcho3 deleted the r_osx_openmp branch January 28, 2020 20:37
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 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.

3 participants