Skip to content

Commit

Permalink
#253 Add .export_vars to parallel_start()
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Oct 22, 2024
1 parent b4f6824 commit bcbb0a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# modeltime 1.3.0.9000

Parallel Computation:
- `parallel_start()`: New parameters `.export_vars` and `.packages` allows passing environment variables and packages to the parallel workers.

Fixes:
- Adam (`adam_reg()`): Fixes #254

# modeltime 1.3.0

#### Overview
Expand Down
31 changes: 27 additions & 4 deletions R/utils-control-par.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#'
#' - "parallel" - Uses the `parallel` and `doParallel` packages
#' - "spark" - Uses the `sparklyr` package
#' @param .export_vars Environment variables that can be sent to the workers
#' @param .packages Packages that can be sent to the workers
#'
#'
#' @details
Expand Down Expand Up @@ -44,7 +46,8 @@

#' @export
#' @rdname parallel_start
parallel_start <- function(..., .method = c("parallel", "spark")) {
parallel_start <- function(..., .method = c("parallel", "spark"),
.export_vars = NULL, .packages = NULL) {

meth <- tolower(.method[1])

Expand All @@ -53,11 +56,31 @@ parallel_start <- function(..., .method = c("parallel", "spark")) {
}

if (meth == "parallel") {
# Step 1: Create the cluster
cl <- parallel::makeCluster(...)

# Step 2: Register the cluster
doParallel::registerDoParallel(cl)
invisible(
parallel::clusterCall(cl, function(x) .libPaths(x), .libPaths())
)

# Step 3: Export variables (if provided)
if (!is.null(.export_vars)) {
parallel::clusterExport(cl, varlist = .export_vars)
}

# Step 4: Load .packages (if provided)
if (!is.null(.packages)) {
parallel::clusterCall(cl, function(pkgs) {
lapply(pkgs, function(pkg) {
if (!requireNamespace(pkg, quietly = TRUE)) {
stop(paste("Package", pkg, "is not installed."))
}
library(pkg, character.only = TRUE)
})
}, .packages)
}

# Step 5: Set the library paths for each worker
invisible(parallel::clusterCall(cl, function(x) .libPaths(x), .libPaths()))
}

if (meth == "spark") {
Expand Down
11 changes: 10 additions & 1 deletion man/parallel_start.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bcbb0a9

Please sign in to comment.