-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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-package] [c++] add tighter multithreading control, avoid global OpenMP side effects (fixes #4705, fixes #5102) #6226
Changes from 10 commits
7f0de8f
b62e46d
651dbc8
600f9ec
c931d3c
6bf188b
a8e666b
921fecb
b068106
ebf61f1
8106f4a
af20a53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -917,6 +917,8 @@ NULL | |
#' the factor levels not being present in the output. | ||
#' @examples | ||
#' \donttest{ | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
#' data(agaricus.train, package = "lightgbm") | ||
#' train <- agaricus.train | ||
#' dtrain <- lgb.Dataset(train$data, label = train$label) | ||
|
@@ -1082,6 +1084,8 @@ predict.lgb.Booster <- function(object, | |
#' \link{predict.lgb.Booster}. | ||
#' @examples | ||
#' \donttest{ | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
#' library(lightgbm) | ||
#' data(mtcars) | ||
#' X <- as.matrix(mtcars[, -1L]) | ||
|
@@ -1224,6 +1228,8 @@ summary.lgb.Booster <- function(object, ...) { | |
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
#' data(agaricus.train, package = "lightgbm") | ||
#' train <- agaricus.train | ||
#' dtrain <- lgb.Dataset(train$data, label = train$label) | ||
|
@@ -1289,6 +1295,8 @@ lgb.load <- function(filename = NULL, model_str = NULL) { | |
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
#' library(lightgbm) | ||
#' data(agaricus.train, package = "lightgbm") | ||
#' train <- agaricus.train | ||
|
@@ -1346,6 +1354,8 @@ lgb.save <- function(booster, filename, num_iteration = NULL) { | |
#' @examples | ||
#' \donttest{ | ||
#' library(lightgbm) | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per https://cran.r-project.org/doc/manuals/R-exts.html
These Thanks to @jangorecki for the suggestion (Rdatatable/data.table#5658 (comment)). |
||
#' data(agaricus.train, package = "lightgbm") | ||
#' train <- agaricus.train | ||
#' dtrain <- lgb.Dataset(train$data, label = train$label) | ||
|
@@ -1396,6 +1406,8 @@ lgb.dump <- function(booster, num_iteration = NULL) { | |
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' \dontshow{setLGBMthreads(2L)} | ||
#' \dontshow{data.table::setDTthreads(1L)} | ||
#' # train a regression model | ||
#' data(agaricus.train, package = "lightgbm") | ||
#' train <- agaricus.train | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' @name setLGBMThreads | ||
#' @title Set maximum number of threads used by LightGBM | ||
#' @description LightGBM attempts to speed up many operations by using multi-threading. | ||
#' The number of threads used in those operations can be controlled via the | ||
#' \code{num_threads} parameter passed through \code{params} to functions like | ||
#' \link{lgb.train} and \link{lgb.Dataset}. However, some operations (like materializing | ||
#' a model from a text file) are done via code paths that don't explicitly accept thread-control | ||
#' configuration. | ||
#' | ||
#' Use this function to set the maximum number of threads LightGBM will use for such operations. | ||
#' | ||
#' This function affects all LightGBM operations in the same process. | ||
#' | ||
#' So, for example, if you call \code{setLGBMthreads(4)}, no other multi-threaded LightGBM | ||
#' operation in the same process will use more than 4 threads. | ||
#' | ||
#' Call \code{setLGBMthreads(-1)} to remove this limitation. | ||
#' @param num_threads maximum number of threads to be used by LightGBM in multi-threaded operations | ||
#' @return NULL | ||
#' @seealso \link{getLGBMthreads} | ||
#' @export | ||
setLGBMthreads <- function(num_threads) { | ||
.Call( | ||
LGBM_SetMaxThreads_R, | ||
num_threads | ||
) | ||
return(invisible(NULL)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are currently just setters and getters for
|
||
|
||
#' @name getLGBMThreads | ||
#' @title Get default number of threads used by LightGBM | ||
#' @description LightGBM attempts to speed up many operations by using multi-threading. | ||
#' The number of threads used in those operations can be controlled via the | ||
#' \code{num_threads} parameter passed through \code{params} to functions like | ||
#' \link{lgb.train} and \link{lgb.Dataset}. However, some operations (like materializing | ||
#' a model from a text file) are done via code paths that don't explicitly accept thread-control | ||
#' configuration. | ||
#' | ||
#' Use this function to see the default number of threads LightGBM will use for such operations. | ||
#' @return number of threads as an integer. \code{-1} means that in situations where parameter \code{num_threads} is | ||
#' not explicitly supplied, LightGBM will choose a number of threads to use automatically. | ||
#' @seealso \link{setLGBMthreads} | ||
#' @export | ||
getLGBMthreads <- function() { | ||
out <- 0L | ||
.Call( | ||
LGBM_GetMaxThreads_R, | ||
out | ||
) | ||
return(out) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I chose not to add similar functions to the Python interface, since: