Skip to content

Commit c2e3756

Browse files
Improve and standardize handling of the 'workers' argument
1 parent 590c557 commit c2e3756

22 files changed

+90
-50
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future.batchtools
2-
Version: 0.12.2-9943
2+
Version: 0.12.2-9944
33
Depends:
44
R (>= 3.2.0),
55
parallelly,

R/BatchtoolsFutureBackend-class.R

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
#'
55
#' @param workers (optional) The maximum number of workers the batchtools
66
#' backend may use at any time. Interactive and "local" backends can only
7-
#' process one future at the time (`workers = 1L`), whereas HPC backends,
7+
#' process one future at the time (`workers = 1`), whereas HPC backends,
88
#' where futures are resolved via separate jobs on a scheduler, can have
9-
#' multiple workers. In the latter, the default is `workers = NULL`, which
10-
#' will resolve to
11-
#' \code{getOption("\link{future.batchtools.workers}")}.
12-
#' If neither are specified, then the default is `100`.
9+
#' multiple workers. In the latter, the default is `workers = NULL`, which
10+
#' will resolve to \code{getOption("\link{future.batchtools.workers}", 100)}.
1311
#'
1412
#' @param finalize If TRUE, a future's \pkg{batchtools}
1513
#' \link[batchtools:makeRegistry]{Registry} is automatically deleted when
@@ -42,7 +40,7 @@
4240
#' @keywords internal
4341
#' @export
4442
BatchtoolsFutureBackend <- function(
45-
workers = NULL, resources = list(),
43+
workers = 1L, resources = list(),
4644
finalize = getOption("future.finalize", TRUE),
4745
cluster.functions = NULL,
4846
registry = list(),
@@ -52,23 +50,12 @@ BatchtoolsFutureBackend <- function(
5250
assert_no_positional_args_but_first()
5351

5452
if (is.function(workers)) workers <- workers()
55-
if (is.null(workers)) {
56-
workers <- getOption("future.batchtools.workers", default = 100)
57-
stop_if_not(
58-
is.numeric(workers),
59-
length(workers) == 1L,
60-
!is.na(workers), workers >= 1
61-
)
53+
if (is.numeric(workers)) {
54+
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 1)
55+
} else if (is.character(workers)) {
56+
stop_if_not(length(workers) >= 1L, !anyNA(workers), all(nzchar(workers)))
6257
} else {
63-
stop_if_not(length(workers) >= 1L)
64-
if (is.numeric(workers)) {
65-
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 1)
66-
} else if (is.character(workers)) {
67-
stop_if_not(length(workers) >= 0L, !anyNA(workers))
68-
} else {
69-
stop("Argument 'workers' should be either a numeric or a function: ",
70-
mode(workers))
71-
}
58+
stop("Argument 'workers' should be either a numeric, a character vector, or a function: ", mode(workers))
7259
}
7360

7461
if (!is.null(cluster.functions)) {

R/BatchtoolsTemplateFutureBackend-class.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@
4040
#' @importFrom batchtools makeClusterFunctionsSlurm
4141
#' @importFrom batchtools makeClusterFunctionsTORQUE
4242
#' @export
43-
BatchtoolsTemplateFutureBackend <- function(type = c("lsf", "openlava", "sge", "slurm", "torque"), scheduler.latency = 1.0, fs.latency = 65.0, template = NULL, ...) {
43+
BatchtoolsTemplateFutureBackend <- function(type = c("lsf", "openlava", "sge", "slurm", "torque"), scheduler.latency = 1.0, fs.latency = 65.0, template = NULL, workers = getOption("future.batchtools.workers", default = 100L), ...) {
4444
assert_no_positional_args_but_first()
4545
type <- match.arg(type)
4646

47+
if (is.function(workers)) workers <- workers()
48+
stop_if_not(
49+
is.numeric(workers),
50+
length(workers) == 1L,
51+
!is.na(workers),
52+
is.finite(workers),
53+
workers >= 1
54+
)
4755

4856
dotdotdot <- list(...)
4957

@@ -77,6 +85,7 @@ BatchtoolsTemplateFutureBackend <- function(type = c("lsf", "openlava", "sge", "
7785

7886
args <- dotdotdot
7987
args[["cluster.functions"]] <- cluster.functions
88+
args[["workers"]] <- workers
8089

8190
core <- do.call(BatchtoolsMultiprocessFutureBackend, args = args)
8291

R/batchtools_bash.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ BatchtoolsBashFutureBackend <- function(...,
1111
template = "bash") {
1212
assert_no_positional_args_but_first()
1313

14-
core <- BatchtoolsMultiprocessFutureBackend(
14+
args <- list(...)
15+
if ("workers" %in% names(args)) {
16+
stop("Unknown argument 'workers'")
17+
}
18+
19+
core <- BatchtoolsUniprocessFutureBackend(
1520
...,
1621
cluster.functions = cluster.functions,
1722
template = template
@@ -112,7 +117,7 @@ makeClusterFunctionsBash <- function(template = "bash", fs.latency = 0.0) {
112117
stop_if_not(inherits(jc, "JobCollection"))
113118

114119
script <- cfBrewTemplate(reg, text = template_text, jc = jc)
115-
output <- system2(bin, args = c(script), stdout = TRUE, stderr = TRUE)
120+
output <- system2(bin, args = c(script), stdout = TRUE, stderr = TRUE, wait = TRUE)
116121
debug <- isTRUE(getOption("future.debug"))
117122
if (debug) {
118123
mdebug_push("makeClusterFunctionsBash() ...")

R/batchtools_interactive.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
BatchtoolsInteractiveFutureBackend <- function(fs.latency = 0.0, ...) {
77
assert_no_positional_args_but_first()
88

9+
args <- list(...)
10+
if ("workers" %in% names(args)) {
11+
stop("Unknown argument 'workers'")
12+
}
13+
914
core <- BatchtoolsUniprocessFutureBackend(
1015
cluster.functions = makeClusterFunctionsInteractive(fs.latency = fs.latency, external = FALSE),
1116
...

R/batchtools_local.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
BatchtoolsLocalFutureBackend <- function(fs.latency = 0.0, ...) {
3939
assert_no_positional_args_but_first()
4040

41+
args <- list(...)
42+
if ("workers" %in% names(args)) {
43+
stop("Unknown argument 'workers'")
44+
}
45+
4146
core <- BatchtoolsUniprocessFutureBackend(
4247
cluster.functions = makeClusterFunctionsInteractive(fs.latency = fs.latency, external = TRUE),
4348
...

R/batchtools_lsf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BatchtoolsLsfFutureBackend <- function(...) {
4848
#' * <https://en.wikipedia.org/wiki/IBM_Spectrum_LSF>
4949
#'
5050
#' @export
51-
batchtools_lsf <- function(..., template = "lsf", scheduler.latency = 1.0, fs.latency = 65.0, resources = list()) {
51+
batchtools_lsf <- function(..., template = "lsf", scheduler.latency = 1.0, fs.latency = 65.0, resources = list(), workers = getOption("future.batchtools.workers", default = 100L)) {
5252
stop("INTERNAL ERROR: The future.batchtools::batchtools_lsf() must never be called directly")
5353
}
5454
class(batchtools_lsf) <- c(

R/batchtools_multicore.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ BatchtoolsMulticoreFutureBackend <- function(workers = availableCores(constraint
3636
assert_no_positional_args_but_first()
3737

3838
if (is.function(workers)) workers <- workers()
39-
if (is.null(workers)) {
40-
workers <- getOption("future.batchtools.workers", default = 100L)
41-
}
4239
stop_if_not(
4340
is.numeric(workers),
4441
length(workers) == 1,
45-
!is.na(workers), workers >= 1
42+
!is.na(workers),
43+
is.finite(workers),
44+
workers >= 1
4645
)
4746

4847
## Fall back to batchtools_local if multicore processing is not supported

R/batchtools_openlava.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BatchtoolsOpenLavaFutureBackend <- function(...) {
4848
#' * <https://en.wikipedia.org/wiki/OpenLava>
4949
#'
5050
#' @export
51-
batchtools_openlava <- function(..., template = "openlava", scheduler.latency = 1.0, fs.latency = 65.0, resources = list()) {
51+
batchtools_openlava <- function(..., template = "openlava", scheduler.latency = 1.0, fs.latency = 65.0, resources = list(), workers = getOption("future.batchtools.workers", default = 100L)) {
5252
stop("INTERNAL ERROR: The future.batchtools::batchtools_openlava() must never be called directly")
5353
}
5454
class(batchtools_openlava) <- c(

R/batchtools_sge.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ BatchtoolsSGEFutureBackend <- function(...) {
5252
#' * <https://en.wikipedia.org/wiki/Oracle_Grid_Engine>
5353
#'
5454
#' @export
55-
batchtools_sge <- function(..., template = "sge", scheduler.latency = 1.0, fs.latency = 65.0, resources = list()) {
55+
batchtools_sge <- function(..., template = "sge", scheduler.latency = 1.0, fs.latency = 65.0, resources = list(), workers = getOption("future.batchtools.workers", default = 100L)) {
5656
stop("INTERNAL ERROR: The future.batchtools::batchtools_sge() must never be called directly")
5757
}
5858
class(batchtools_sge) <- c(

0 commit comments

Comments
 (0)