Skip to content

Commit 265a337

Browse files
committed
use parallelly
1 parent 012e2fd commit 265a337

13 files changed

+16
-22
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Imports:
2424
methods,
2525
testthat,
2626
parallel,
27+
parallelly,
2728
snow,
2829
dplyr,
2930
sessioninfo,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ importFrom(dplyr,mutate)
8181
importFrom(dplyr,select)
8282
importFrom(future.apply,future_lapply)
8383
importFrom(graphics,abline)
84+
importFrom(parallelly,availableCores)
8485
importFrom(pbapply,pblapply)
8586
importFrom(pbapply,pboptions)
8687
importFrom(progressr,progressor)

R/SimDesign.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#' @author Phil Chalmers \email{rphilip.chalmers@@gmail.com}
2626
#' @import methods parallel stats
2727
#' @importFrom sessioninfo session_info
28+
#' @importFrom parallelly availableCores
2829
#' @importFrom RPushbullet pbPost
2930
#' @importFrom graphics abline
3031
#' @importFrom R.utils withTimeout

R/SimSolve.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,14 @@ SimSolve <- function(design, interval, b, generate, analyse, summarise,
464464
integer = TRUE, formula = y ~ poly(x, 2), family = 'binomial',
465465
parallel = FALSE, cl = NULL, save = TRUE, resume = TRUE,
466466
method = 'ProBABLI', wait.time = NULL,
467-
ncores = parallel::detectCores() - 1L,
467+
ncores = parallelly::availableCores() - 1L,
468468
type = ifelse(.Platform$OS.type == 'windows', 'PSOCK', 'FORK'),
469469
maxiter = 100L, check.interval = TRUE,
470470
verbose = TRUE, control = list(),
471471
predCI = .95, predCI.tol = NULL, ...){
472472

473473
# robust <- FALSE
474474
org.opts <- options()
475-
ncores <- check_ncores(ncores)
476475
options(warn = 1)
477476
on.exit(options(org.opts), add = TRUE)
478477
if(is.null(control$print_RAM)) control$print_RAM <- FALSE

R/runArraySimulation.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,10 @@ runArraySimulation <- function(design, ..., replications,
296296
filename_suffix = paste0("-", array2row(arrayID)),
297297
addArrayInfo = TRUE,
298298
parallel = FALSE, cl = NULL,
299-
ncores = parallel::detectCores() - 1L,
299+
ncores = parallelly::availableCores() - 1L,
300300
save_details = list(),
301301
control = list()){
302302
dots <- list(...)
303-
ncores <- check_ncores(ncores)
304303
if(parallel && ncores == 1L) parallel <- FALSE
305304
if(!is.null(dots$save_results) && isTRUE(dots$save_results))
306305
stop('save_results not supported for array jobs. Please use store_results only')

R/runSimulation.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,13 @@ runSimulation <- function(design, replications, generate, analyse, summarise,
978978
fixed_objects = NULL, packages = NULL, filename = NULL,
979979
debug = 'none', load_seed = NULL, save = any(replications > 2),
980980
store_results = TRUE, save_results = FALSE,
981-
parallel = FALSE, ncores = parallel::detectCores() - 1L,
981+
parallel = FALSE, ncores = parallelly::availableCores() - 1L,
982982
cl = NULL, notification = 'none', beep = FALSE, sound = 1,
983983
CI = .95, seed = NULL, boot_method='none', boot_draws = 1000L,
984984
max_errors = 50L, resume = TRUE, save_details = list(),
985985
control = list(), progress = TRUE, verbose = TRUE)
986986
{
987987
stopifnot(!missing(analyse))
988-
ncores <- check_ncores(ncores)
989988
if(length(control)){
990989
stopifnot("Argument(s) to control list invalid"=
991990
all(names(control) %in% valid_control.list()))

R/util.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ get_packages <- function(packages){
2525
sapply(packages, function(x) as.character(packageVersion(x)))
2626
}
2727

28-
check_ncores <- function(ncores){
29-
ncores <- ifelse(is.na(ncores), 1L, ncores)
30-
ncores <- ifelse(ncores < 1L, 1L, ncores)
31-
ncores
32-
}
33-
3428
# base-code borrowed and modified from pbapply
3529
timeFormater_internal <- function(time, decimals = TRUE){
3630
dec <- time - floor(time)

man/SimSolve.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/runArraySimulation.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/runSimulation.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests/test-01-core.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test_that('SimDesign', {
100100
expect_equal(nrow(out), nrow(Design) * 2)
101101

102102
Final <- runSimulation(Design, generate=mysim, analyse=mycompute, summarise=mycollect,
103-
replications = parallel::detectCores(),
103+
replications = parallelly::availableCores(),
104104
parallel=TRUE, ncores=2L, save=FALSE, verbose = FALSE)
105105
expect_is(Final, 'data.frame')
106106

@@ -116,15 +116,15 @@ test_that('SimDesign', {
116116

117117
#seeds
118118
Final <- runSimulation(Design, generate=mysim, analyse=mycompute, summarise=mycollect, seed = 1:8,
119-
replications = parallel::detectCores(),
119+
replications = parallelly::availableCores(),
120120
parallel=TRUE, ncores=2L, save=FALSE, verbose = FALSE)
121121
Final2 <- runSimulation(Design, generate=mysim, analyse=mycompute, summarise=mycollect, seed = 1:8,
122-
replications = parallel::detectCores(),
122+
replications = parallelly::availableCores(),
123123
parallel=TRUE, ncores=2L, save=FALSE, verbose = FALSE)
124124
Final <- runSimulation(Design, generate=mysim, analyse=mycompute, summarise=mycollect, seed = 1:8,
125-
replications = parallel::detectCores(), parallel=FALSE, save=FALSE, verbose = FALSE)
125+
replications = parallelly::availableCores(), parallel=FALSE, save=FALSE, verbose = FALSE)
126126
Final2 <- runSimulation(Design, generate=mysim, analyse=mycompute, summarise=mycollect, seed = 1:8,
127-
replications = parallel::detectCores(), parallel=FALSE, save=FALSE, verbose = FALSE)
127+
replications = parallelly::availableCores(), parallel=FALSE, save=FALSE, verbose = FALSE)
128128

129129
mycompute <- function(condition, dat, fixed_objects){
130130

vignettes/HPC-computing.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ To start, the structure of the simulation code used later on to distribute the j
4949

5050
Suppose the following simulation was to be evaluated, though for time constraint reasons would not be possible to execute on a single computer (or a smaller network of computers) and therefore should be submitted to an HPC cluster.
5151

52-
The following script (hypothetically written in a file called `SimDesign_simulation.R`) contains a simulation experiment whose instructions are to be submitted to the Slurm scheduler. To do so, the `sbatch` utility is used along with the set of instructions specifying the type of hardware required in the file `slurmInstructions.slurm`. In the R side of the simulation, the defined code must grab all available cores (minus 1) that are detectable via `parallel::detectCores()`, which occurs automatically when using `runSimulation(..., parallel=TRUE)`.
52+
The following script (hypothetically written in a file called `SimDesign_simulation.R`) contains a simulation experiment whose instructions are to be submitted to the Slurm scheduler. To do so, the `sbatch` utility is used along with the set of instructions specifying the type of hardware required in the file `slurmInstructions.slurm`. In the R side of the simulation, the defined code must grab all available cores (minus 1) that are detectable via `parallelly::availableCores()`, which occurs automatically when using `runSimulation(..., parallel=TRUE)`.
5353
```{r}
5454
# SimDesign::SimFunctions()
5555
library(SimDesign)

vignettes/Parallel-computing.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The logic presented within this document is based on the notion that the front-e
3535

3636
## Local parallel computing
3737

38-
By default, `SimDesign`'s `runSimulation()` function is executed with only a single core. However, setting the argument `runSimulation(..., parallel = TRUE)` will automatically define a cluster object using one core less than the system has available (detect via `parallel::detectCores()`). This allows a straightforward way to construct a suitable, locally supported cluster object for parallel processing on just the active computer. Depending on the `verbose` and `progress` flags, the progress of each distributed replication will also be printed to the console to indicate the amount of estimated time remaining for the selection simulation condition to complete. This process is then repeated again for each condition in the supplied `design` object until all rows have been evaluated.
38+
By default, `SimDesign`'s `runSimulation()` function is executed with only a single core. However, setting the argument `runSimulation(..., parallel = TRUE)` will automatically define a cluster object using one core less than the system has available (detect via `parallelly::availableCores()`). This allows a straightforward way to construct a suitable, locally supported cluster object for parallel processing on just the active computer. Depending on the `verbose` and `progress` flags, the progress of each distributed replication will also be printed to the console to indicate the amount of estimated time remaining for the selection simulation condition to complete. This process is then repeated again for each condition in the supplied `design` object until all rows have been evaluated.
3939

4040
This setup is the most painless way to construct and distribute the independent replications per condition, where within each evaluated condition (i.e., each row of the `design` object) high-quality random numbers are automatically used via Pierre L'Ecuyer's (1999) multiple streams method, limited only by the number of cores that are available. Alternatively, though with a bit of extra effort, users may also define their own cluster computing object by way of the `runSimulation(..., cl)` object, which can be used to link computing resources that are able to communicate via `ssh`, thereby expanding the number of available computing cores detected by `parallel::detectCores()` and friends.
4141

0 commit comments

Comments
 (0)