Skip to content

Commit 3fd804d

Browse files
makeCluster(): Validate 'specs' argument
1 parent cf10109 commit 3fd804d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

R/utils_api-makeClusterFuture.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#' particularly because of the stateless nature of the cluster._
77
#'
88
#'
9-
#' @param specs Ignored. This exists only to support
9+
#' @param specs Ignored.
10+
#' If specified, the value should equal `nbrOfWorkers()` (default).
11+
#' A missing value corresponds to specifying `nbrOfWorkers()`.
12+
#' This argument exists only to support
1013
#' `parallel::makeCluster(NA, type = future::FUTURE)`.
1114
#'
1215
#' @param \ldots Named arguments passed to [future::future()].
@@ -81,7 +84,24 @@
8184
#'
8285
#' @importFrom future nbrOfWorkers
8386
#' @rawNamespace if (getRversion() >= "4.4") export(makeClusterFuture)
84-
makeClusterFuture <- function(specs = NA_integer_, ...) {
87+
makeClusterFuture <- function(specs = nbrOfWorkers(), ...) {
88+
stop_if_not(length(specs) == 1L)
89+
90+
backend <- plan("backend")
91+
n <- nbrOfWorkers(backend)
92+
if (is.na(specs)) specs <- n
93+
94+
if (is.numeric(specs)) {
95+
if (specs <= 0) {
96+
stop("Argument 'specs' must be a positive integer: %s", specs)
97+
}
98+
if (specs != n) {
99+
stop(sprintf("Value of argument 'specs' does not match the number of workers in the registered future backend (%s:%s): %g != %g", class(backend)[1], backend[["uuid"]], specs, n))
100+
}
101+
} else {
102+
stop("Unknown type of argument 'specs': ", typeof(specs))
103+
}
104+
85105
options <- list(...)
86106
if (length(options) > 0L) {
87107
names <- names(options)
@@ -90,8 +110,7 @@ makeClusterFuture <- function(specs = NA_integer_, ...) {
90110
}
91111
}
92112

93-
backend <- plan("backend")
94-
cl <- vector("list", length = nbrOfWorkers(backend))
113+
cl <- vector("list", length = n)
95114
for (kk in seq_along(cl)) {
96115
node <- new.env(parent = emptyenv())
97116
node[["options"]] <- options

man/makeClusterFuture.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)