Unverified Commit 8593f850 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] Add GPU install options (fixes #3765) (#3779)



* [R-package] Add GPU install options (fixes #3765)

* whitespace

* linting

* Update build_r.R
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent c871496a
...@@ -169,6 +169,27 @@ After installing these other libraries, follow the steps in ["Installing from So ...@@ -169,6 +169,27 @@ After installing these other libraries, follow the steps in ["Installing from So
Rscript build_r.R --use-gpu Rscript build_r.R --use-gpu
``` ```
You may also need or want to provide additional configuration, depending on your setup. For example, you may need to provide locations for Boost and OpenCL.
```shell
Rscript build_r.R \
--use-gpu \
--opencl-library=/usr/lib/x86_64-linux-gnu/libOpenCL.so \
--boost-librarydir=/usr/lib/x86_64-linux-gnu
```
The following options correspond to the [CMake FindBoost options](https://cmake.org/cmake/help/latest/module/FindBoost.html) by the same names.
* `--boost-root`
* `--boost-dir`
* `--boost-include-dir`
* `--boost-librarydir`
The following options correspond to the [CMake FindOpenCL options](https://cmake.org/cmake/help/latest/module/FindOpenCL.html) by the same names.
* `--opencl-include-dir`
* `--opencl-library`
### Installing Precompiled Binaries ### Installing Precompiled Binaries
Precompiled binaries for Mac and Windows are prepared by CRAN a few days after each release to CRAN. They can be installed with the following R code. Precompiled binaries for Mac and Windows are prepared by CRAN a few days after each release to CRAN. They can be installed with the following R code.
......
...@@ -135,6 +135,11 @@ build_cmd <- "make" ...@@ -135,6 +135,11 @@ build_cmd <- "make"
build_args <- "_lightgbm" build_args <- "_lightgbm"
lib_folder <- file.path(source_dir, fsep = "/") lib_folder <- file.path(source_dir, fsep = "/")
# add in command-line arguments
# NOTE: build_r.R replaces the line below
command_line_args <- NULL
cmake_args <- c(cmake_args, command_line_args)
WINDOWS_BUILD_TOOLS <- list( WINDOWS_BUILD_TOOLS <- list(
"MinGW" = c( "MinGW" = c(
build_tool = "mingw32-make.exe" build_tool = "mingw32-make.exe"
......
...@@ -10,17 +10,58 @@ INSTALL_AFTER_BUILD <- !("--skip-install" %in% args) ...@@ -10,17 +10,58 @@ INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)
TEMP_R_DIR <- file.path(getwd(), "lightgbm_r") TEMP_R_DIR <- file.path(getwd(), "lightgbm_r")
TEMP_SOURCE_DIR <- file.path(TEMP_R_DIR, "src") TEMP_SOURCE_DIR <- file.path(TEMP_R_DIR, "src")
USING_GPU <- "--use-gpu" %in% args # [description]
USING_MINGW <- "--use-mingw" %in% args # Parse the content of commandArgs() into a structured
USING_MSYS2 <- "--use-msys2" %in% args # list. This returns a list with two sections.
# * "flags" = a character of vector of flags like "--use-gpu"
# * "keyword_args" = a named character vector, where names
# refer to options and values are the option values. For
# example, c("--boost-librarydir" = "/usr/lib/x86_64-linux-gnu")
.parse_args <- function(args) {
out_list <- list(
"flags" = character(0L)
, "keyword_args" = character(0L)
)
for (arg in args) {
if (any(grepl("=", arg))) {
split_arg <- strsplit(arg, "=")[[1L]]
arg_name <- split_arg[[1L]]
arg_value <- split_arg[[2L]]
out_list[["keyword_args"]][[arg_name]] <- arg_value
} else {
out_list[["flags"]] <- c(out_list[["flags"]], arg)
}
}
return(out_list)
}
parsed_args <- .parse_args(args)
USING_GPU <- "--use-gpu" %in% parsed_args[["flags"]]
USING_MINGW <- "--use-mingw" %in% parsed_args[["flags"]]
USING_MSYS2 <- "--use-msys2" %in% parsed_args[["flags"]]
# this maps command-line arguments to defines passed into CMake,
ARGS_TO_DEFINES <- c(
"--boost-root" = "-DBOOST_ROOT"
, "--boost-dir" = "-DBoost_DIR"
, "--boost-include-dir" = "-DBoost_INCLUDE_DIR"
, "--boost-librarydir" = "-DBOOST_LIBRARYDIR"
, "--opencl-include-dir" = "-DOpenCL_INCLUDE_DIR"
, "--opencl-library" = "-DOpenCL_LIBRARY"
)
recognized_args <- c( recognized_args <- c(
"--skip-install" "--skip-install"
, "--use-gpu" , "--use-gpu"
, "--use-mingw" , "--use-mingw"
, "--use-msys2" , "--use-msys2"
, names(ARGS_TO_DEFINES)
)
given_args <- c(
parsed_args[["flags"]]
, names(parsed_args[["keyword_args"]])
) )
unrecognized_args <- setdiff(args, recognized_args) unrecognized_args <- setdiff(given_args, recognized_args)
if (length(unrecognized_args) > 0L) { if (length(unrecognized_args) > 0L) {
msg <- paste0( msg <- paste0(
"Unrecognized arguments: " "Unrecognized arguments: "
...@@ -47,6 +88,27 @@ install_libs_content <- .replace_flag("use_gpu", USING_GPU, install_libs_content ...@@ -47,6 +88,27 @@ install_libs_content <- .replace_flag("use_gpu", USING_GPU, install_libs_content
install_libs_content <- .replace_flag("use_mingw", USING_MINGW, install_libs_content) install_libs_content <- .replace_flag("use_mingw", USING_MINGW, install_libs_content)
install_libs_content <- .replace_flag("use_msys2", USING_MSYS2, install_libs_content) install_libs_content <- .replace_flag("use_msys2", USING_MSYS2, install_libs_content)
# set up extra flags based on keyword arguments
keyword_args <- parsed_args[["keyword_args"]]
if (length(keyword_args) > 0L) {
cmake_args_to_add <- NULL
for (i in seq_len(length(keyword_args))) {
arg_name <- names(keyword_args)[[i]]
define_name <- ARGS_TO_DEFINES[[arg_name]]
arg_value <- shQuote(keyword_args[[arg_name]])
cmake_args_to_add <- c(cmake_args_to_add, paste0(define_name, "=", arg_value))
}
install_libs_content <- gsub(
pattern = paste0("command_line_args <- NULL")
, replacement = paste0(
"command_line_args <- c(\""
, paste(cmake_args_to_add, collapse = "\", \"")
, "\")"
)
, x = install_libs_content
)
}
# R returns FALSE (not a non-zero exit code) if a file copy operation # R returns FALSE (not a non-zero exit code) if a file copy operation
# breaks. Let's fix that # breaks. Let's fix that
.handle_result <- function(res) { .handle_result <- function(res) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment