Batchtools futures for custom batchtools configuration

batchtools_custom(
  expr,
  envir = parent.frame(),
  substitute = TRUE,
  globals = TRUE,
  label = NULL,
  resources = list(),
  workers = NULL,
  conf.file = findConfFile(),
  cluster.functions = NULL,
  registry = list(),
  ...
)

Arguments

expr

The R expression to be evaluated

envir

The environment in which global environment should be located.

substitute

Controls whether expr should be substitute():d or not.

globals

(optional) a logical, a character vector, a named list, or a Globals object. If TRUE, globals are identified by code inspection based on expr and tweak searching from environment envir. If FALSE, no globals are used. If a character vector, then globals are identified by lookup based their names globals searching from environment envir. If a named list or a Globals object, the globals are used as is.

label

(optional) Label of the future (where applicable, becomes the job name for most job schedulers).

resources

(optional) A named list passed to the batchtools template (available as variable resources). See Section 'Resources' in batchtools::submitJobs() more details.

workers

(optional) The maximum number of workers the batchtools backend may use at any time. Interactive and "local" backends can only process one future at the time (workers = 1L), whereas HPC backends, where futures are resolved via separate jobs on a scheduler, can have multiple workers. In the latter, the default is workers = NULL, which will resolve to getOption("future.batchtools.workers"). If neither are specified, then the default is 100.

conf.file

(character) A batchtools configuration file as for instance returned by batchtools::findConfFile().

cluster.functions

A ClusterFunctions object.

registry

(optional) A named list of settings to control the setup of the batchtools registry.

...

Additional arguments passed to BatchtoolsFuture().

Value

An object of class BatchtoolsFuture.

Examples

options(error = function(...) {
  print(traceback())
})

cf <- batchtools::makeClusterFunctionsInteractive(external = TRUE)
print(cf)
#> ClusterFunctions for mode: Interactive
#>   List queued Jobs : FALSE
#>   List running Jobs: FALSE
#>   Kill Jobs        : FALSE
#>   Hooks            : -
str(cf)
#> List of 11
#>  $ name                : chr "Interactive"
#>  $ submitJob           :function (reg, jc)  
#>  $ killJob             : NULL
#>  $ listJobsQueued      : NULL
#>  $ listJobsRunning     : NULL
#>  $ array.var           : chr NA
#>  $ store.job.collection: logi TRUE
#>  $ store.job.files     : logi FALSE
#>  $ scheduler.latency   : num 0
#>  $ fs.latency          : num 0
#>  $ hooks               : list()
#>  - attr(*, "class")= chr "ClusterFunctions"
plan(batchtools_custom, cluster.functions = cf)
print(plan())
#> batchtools_custom:
#> - args: function (..., cluster.functions = list(name = "Interactive", submitJob = function (reg, jc) { assertRegistry(reg, writeable = TRUE) assertClass(jc, "JobCollection") if (external) { runOSCommand(Rscript(), sprintf("-e \"batchtools::doJobCollection('%s', output = '%s')\"", jc$uri, jc$log.file)) } else { doJobCollection(jc, output = jc$log.file) } makeSubmitJobResult(status = 0L, batch.id = "cfInteractive") }, killJob = NULL, listJobsQueued = NULL, listJobsRunning = NULL, array.var = NA_character_, store.job.collection = TRUE, store.job.files = FALSE, scheduler.latency = 0, fs.latency = 0, hooks = list()), workers = NULL, envir = parent.frame())
#> - tweaked: TRUE
#> - call: plan(batchtools_custom, cluster.functions = cf)
print(nbrOfWorkers())
#> [1] 1

## Create explicit future
f <- future({
  cat("PID:", Sys.getpid(), "\n")
  42L
})
print(f)
#> BatchtoolsCustomFuture:
#> Label: ‘<none>’
#> Expression:
#> {
#>     cat("PID:", Sys.getpid(), "\n")
#>     42L
#> }
#> Lazy evaluation: FALSE
#> Asynchronous evaluation: TRUE
#> Local evaluation: TRUE
#> Environment: 0x625e2b3a5160
#> Capture standard output: TRUE
#> Capture condition classes: ‘condition’ (excluding ‘nothing’)
#> Globals: <none>
#> Packages: <none>
#> L'Ecuyer-CMRG RNG seed: <none> (seed = FALSE)
#> Resolved: TRUE
#> Value: <not collected>
#> Conditions captured: <none>
#> Early signaling: FALSE
#> Owner process: 17fdc8f2-90b2-3e55-5ed9-de73cafd72d7
#> Class: ‘BatchtoolsCustomFuture’, ‘BatchtoolsFuture’, ‘Future’, ‘environment’
#> batchtools configuration file: <NA>
#> batchtools cluster functions: ‘Interactive’
#> batchtools cluster functions template: <NA>
#> batchtools status: ‘defined’, ‘finished’, ‘started’, ‘submitted’
#> batchtools Registry:
#>   File dir exists: TRUE
#>   Work dir exists: TRUE
#> Job Registry
#>   Backend  : Interactive
#>   File dir : /tmp/henrik/RtmphzSAJj/future.batchtools/docs/reference/.future/20240418_180222-fRU2Z8/batchtools_808909453
#>   Work dir : /tmp/henrik/RtmphzSAJj/future.batchtools/docs/reference
#>   Jobs     : 1
#>   Seed     : 13290
#>   Writeable: TRUE
v <- value(f)
#> PID: 463628 
print(v)
#> [1] 42

options(error = NULL)


## Create explicit future
f <- future({
  cat("PID:", Sys.getpid(), "\n")
  42L
})
print(f)
#> BatchtoolsCustomFuture:
#> Label: ‘<none>’
#> Expression:
#> {
#>     cat("PID:", Sys.getpid(), "\n")
#>     42L
#> }
#> Lazy evaluation: FALSE
#> Asynchronous evaluation: TRUE
#> Local evaluation: TRUE
#> Environment: 0x625e2b3a5160
#> Capture standard output: TRUE
#> Capture condition classes: ‘condition’ (excluding ‘nothing’)
#> Globals: <none>
#> Packages: <none>
#> L'Ecuyer-CMRG RNG seed: <none> (seed = FALSE)
#> Resolved: TRUE
#> Value: <not collected>
#> Conditions captured: <none>
#> Early signaling: FALSE
#> Owner process: 17fdc8f2-90b2-3e55-5ed9-de73cafd72d7
#> Class: ‘BatchtoolsCustomFuture’, ‘BatchtoolsFuture’, ‘Future’, ‘environment’
#> batchtools configuration file: <NA>
#> batchtools cluster functions: ‘Interactive’
#> batchtools cluster functions template: <NA>
#> batchtools status: ‘defined’, ‘finished’, ‘started’, ‘submitted’
#> batchtools Registry:
#>   File dir exists: TRUE
#>   Work dir exists: TRUE
#> Job Registry
#>   Backend  : Interactive
#>   File dir : /tmp/henrik/RtmphzSAJj/future.batchtools/docs/reference/.future/20240418_180222-fRU2Z8/batchtools_765094972
#>   Work dir : /tmp/henrik/RtmphzSAJj/future.batchtools/docs/reference
#>   Jobs     : 1
#>   Seed     : 9231
#>   Writeable: TRUE
v <- value(f)
#> PID: 463648 
print(v)
#> [1] 42



## Create explicit future
f <- future({
  cat("PID:", Sys.getpid(), "\n")
  42L
})
v <- value(f)
#> PID: 463669 
print(v)
#> [1] 42