Configures parallel processing with future::plan()
or stops an existing
plan. When stopping, it resets to sequential mode.
Usage
set_parallel(
n_cores = 1L,
strategy = "multisession",
stop_cluster = FALSE,
show_log = TRUE,
future_max_size = 500L,
...
)
Arguments
- n_cores
Integer. Number of cores to use. If
NULL
, defaults to sequential mode. Default is1
.- strategy
Character. The parallel processing strategy to use. Valid options are "sequential", "multisession" (default), "multicore", and "cluster". See
future::plan()
andset_parallel()
for details.- stop_cluster
Logical. If
TRUE
, stops any parallel cluster and resets to sequential mode. IfFALSE
(default), sets up a new plan.- show_log
Logical. If
TRUE
(default), logs messages viacat_time()
.- future_max_size
Numeric. Maximum allowed total size (in megabytes) of global variables identified. See
future.globals.maxSize
argument of future::future.options for more details. Default is500L
for 500 MB.- ...
Additional arguments to pass to cat_time.
Examples
load_packages(future)
# number of workers available
future::nbrOfWorkers()
#> [1] 1
# ---------------------------------------------
# `multisession`
# ---------------------------------------------
# Prepare working in parallel
set_parallel(n_cores = 2)
#> Setting up parallel processing using 2 cores (strategy: `multisession`) - 14:50:19
future::plan("list")
#> List of future strategies:
#> 1. multisession:
#> - args: function (..., workers = 2L, gc = TRUE, envir = parent.frame())
#> - tweaked: TRUE
#> - call: future::plan(strategy = strategy, workers = n_cores, gc = TRUE)
future::nbrOfWorkers()
#> [1] 2
# Stopping parallel processing
set_parallel(stop_cluster = TRUE)
#> Stopping parallel processing - 14:50:20
future::plan("list")
#> List of future strategies:
#> 1. sequential:
#> - args: function (..., gc = TRUE, envir = parent.frame(), workers = "<NULL>")
#> - tweaked: TRUE
#> - call: future::plan(strategy = "sequential", gc = TRUE)
future::nbrOfWorkers()
#> [1] 1
# ---------------------------------------------
# `cluster`
# ---------------------------------------------
# Prepare working in parallel
set_parallel(n_cores = 2, strategy = "cluster")
#> Setting up parallel processing using 2 cores (strategy: `cluster`) - 14:50:20
future::plan("list")
#> List of future strategies:
#> 1. cluster:
#> - args: function (..., workers = 2L, gc = TRUE, envir = parent.frame())
#> - tweaked: TRUE
#> - call: future::plan(strategy = strategy, workers = n_cores, gc = TRUE)
future::nbrOfWorkers()
#> [1] 2
# Stopping parallel processing
set_parallel(stop_cluster = TRUE)
#> Stopping parallel processing - 14:50:21
future::plan("list")
#> List of future strategies:
#> 1. sequential:
#> - args: function (..., gc = TRUE, envir = parent.frame(), workers = "<NULL>")
#> - tweaked: TRUE
#> - call: future::plan(strategy = "sequential", gc = TRUE)
future::nbrOfWorkers()
#> [1] 1
# ---------------------------------------------
# `multicore`
# ---------------------------------------------
# Prepare working in parallel
set_parallel(n_cores = 2, strategy = "multicore")
#> Setting up parallel processing using 2 cores (strategy: `multicore`) - 14:50:21
future::plan("list")
#> List of future strategies:
#> 1. multicore:
#> - args: function (..., workers = 2L, gc = TRUE, envir = parent.frame())
#> - tweaked: TRUE
#> - call: future::plan(strategy = strategy, workers = n_cores, gc = TRUE)
future::nbrOfWorkers()
#> [1] 2
#> attr(,"class")
#> [1] "integer"
# Stopping parallel processing
set_parallel(stop_cluster = TRUE)
#> Stopping parallel processing - 14:50:21
future::plan("list")
#> List of future strategies:
#> 1. sequential:
#> - args: function (..., gc = TRUE, envir = parent.frame(), workers = "<NULL>")
#> - tweaked: TRUE
#> - call: future::plan(strategy = "sequential", gc = TRUE)
future::nbrOfWorkers()
#> [1] 1
# ---------------------------------------------
# `sequential`
# ---------------------------------------------
set_parallel(n_cores = 1, strategy = "sequential")
future::nbrOfWorkers()
#> [1] 1