Skip to content

Commit

Permalink
[R-package] Add GPU install options (fixes #3765) (#3779)
Browse files Browse the repository at this point in the history
* [R-package] Add GPU install options (fixes #3765)

* whitespace

* linting

* Update build_r.R

Co-authored-by: Nikita Titov <[email protected]>

Co-authored-by: Nikita Titov <[email protected]>
  • Loading branch information
jameslamb and StrikerRUS authored Jan 19, 2021
1 parent c871496 commit 8593f85
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
21 changes: 21 additions & 0 deletions R-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ After installing these other libraries, follow the steps in ["Installing from So
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

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.
Expand Down
5 changes: 5 additions & 0 deletions R-package/src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ build_cmd <- "make"
build_args <- "_lightgbm"
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(
"MinGW" = c(
build_tool = "mingw32-make.exe"
Expand Down
70 changes: 66 additions & 4 deletions build_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,58 @@ INSTALL_AFTER_BUILD <- !("--skip-install" %in% args)
TEMP_R_DIR <- file.path(getwd(), "lightgbm_r")
TEMP_SOURCE_DIR <- file.path(TEMP_R_DIR, "src")

USING_GPU <- "--use-gpu" %in% args
USING_MINGW <- "--use-mingw" %in% args
USING_MSYS2 <- "--use-msys2" %in% args
# [description]
# Parse the content of commandArgs() into a structured
# 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(
"--skip-install"
, "--use-gpu"
, "--use-mingw"
, "--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) {
msg <- paste0(
"Unrecognized arguments: "
Expand All @@ -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_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
# breaks. Let's fix that
.handle_result <- function(res) {
Expand Down

0 comments on commit 8593f85

Please sign in to comment.