Skip to content

Commit

Permalink
ensure the number of workers cannot be less than 1 in suitableParalle…
Browse files Browse the repository at this point in the history
…lPlan()
  • Loading branch information
jasenfinch committed Apr 26, 2023
1 parent e4376a5 commit dc2a04f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ suitableParallelPlan <- function(strategy = NULL,workers = NULL,RAM_per_worker =
as.numeric()

suitable_workers <- floor(total_RAM / RAM_per_worker)
max_workers <- availableCores() * proportion_max_workers
max_workers <- ceiling(availableCores() * proportion_max_workers)

if (suitable_workers > max_workers) {
workers <- max_workers
Expand All @@ -34,6 +34,10 @@ suitableParallelPlan <- function(strategy = NULL,workers = NULL,RAM_per_worker =
}
}

if (workers < 1){
workers <- 1
}

plan(strategy,workers = workers)

if (!is.character(strategy)) strategy <- class(strategy)[1]
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ test_that("a suitable parallel plan generated when strategy specified as a funct

test_that("a suitable parallel plan generated when suitable workers exceeds the maximum available", {
expect_message(suitableParallelPlan(RAM_per_worker = 0.1))
})

test_that("a suitable parallel plan generated when the number of workers is less than 1", {
expect_message(suitableParallelPlan(workers = 0.1))
})

0 comments on commit dc2a04f

Please sign in to comment.