View source: R/batchtools_bash.R
| batchtools_bash | R Documentation |
The batchtools_bash backend was added to illustrate how to write a
custom future.batchtools backend that uses a templated job script.
Please see the source code, for details.
batchtools_bash(
...,
template = "bash",
fs.latency = 0,
resources = list(),
delete = getOption("future.batchtools.delete", "on-success"),
registry = list(),
finalize = getOption("future.finalize", TRUE)
)
makeClusterFunctionsBash(template = "bash", fs.latency = 0, ...)
template |
(optional) Name of job-script template to be searched
for by |
fs.latency |
[ |
resources |
(optional) A named list passed to the batchtools
job-script template as variable
|
delete |
Controls if and when the batchtools job registry folder is
deleted.
If |
registry |
(optional) A named list of settings applied to each
future's batchtools Registry.
This is a more convenient alternative to using argument |
finalize |
If TRUE, a future's batchtools Registry is automatically deleted when the future is garbage collected, otherwise not. |
... |
Not used. |
Batchtools Bash futures use batchtools cluster functions
created by makeClusterFunctionsBash() and requires that bash is
installed on the current machine and the timeout command is available.
The default template script templates/bash.tmpl can be found in:
system.file("templates", "bash.tmpl", package = "future.batchtools")
and comprises:
#!/bin/bash
######################################################################
# A batchtools launch script template
#
# Author: Henrik Bengtsson
######################################################################
## Bash settings
set -e # exit on error
set -u # error on unset variables
set -o pipefail # fail a pipeline if any command fails
trap 'echo "ERROR: future.batchtools job script failed on line $LINENO" >&2; exit 1' ERR
## Redirect stdout and stderr to the batchtools log file
exec > <%= log.file %> 2>&1
<%
## Work on a copy of 'resources', because this template is evaluated
## in the environment of the batchtools JobCollection, and modifying
## 'resources' would modify the resources of the job itself
job_resources <- resources
## Should job "details" be seen?
details <- isTRUE(job_resources[["details"]])
job_resources[["details"]] <- NULL
## Shell "startup" code to evaluate
startup <- job_resources[["startup"]]
job_resources[["startup"]] <- NULL
## Shell "shutdown" code to evaluate
shutdown <- job_resources[["shutdown"]]
job_resources[["shutdown"]] <- NULL
## Environment modules specifications
modules <- job_resources[["modules"]]
job_resources[["modules"]] <- NULL
## Environment variables to be set
envs <- job_resources[["envs"]]
if (length(envs) > 0) {
stopifnot(is.character(envs), !is.null(names(envs)))
}
job_resources[["envs"]] <- NULL
## Custom "Rscript" command and Rscript arguments
rscript <- job_resources[["rscript"]]
if (is.null(rscript)) {
rscript <- "Rscript"
} else if (length(rscript) == 0 || !nzchar(rscript)[1]) {
stop("Argument 'resources' specifies an empty 'rscript' field")
}
job_resources[["rscript"]] <- NULL
## Maximum runtime?
timeout <- job_resources[["timeout"]]
if (is.null(timeout)) timeout <- job_resources[["runtime"]]
job_resources[["timeout"]] <- NULL
job_resources[["runtime"]] <- NULL
if (length(timeout) > 0) {
rscript <- c("timeout", timeout, rscript)
}
rscript_args <- job_resources[["rscript_args"]]
job_resources[["rscript_args"]] <- NULL
rscript_call <- paste(c(rscript, rscript_args), collapse = " ")
%>
<% if (length(startup) > 0) {
writeLines(startup)
} %>
<% if (length(modules) > 0) {
writeLines(c(
'echo "Load environment modules:"',
sprintf('echo "- modules: %s"', paste(modules, collapse = ", ")),
sprintf("module load %s", paste(modules, collapse = " ")),
"module list"
))
} %>
<% if (length(envs) > 0) {
writeLines(c(
sprintf("echo 'Setting environment variables: [n=%d]'", length(envs)),
sprintf("echo ' - %s=%s'", names(envs), shQuote(envs)),
sprintf("export %s=%s", names(envs), shQuote(envs))
))
} %>
echo "Session information:"
echo "- timestamp: $(date +"%Y-%m-%d %H:%M:%S%z")"
echo "- hostname: $(hostname)"
echo "- Rscript: <%= paste(rscript, collapse = " ") %>"
echo "- Rscript args: <%= paste(rscript_args, collapse = " ") %>"
echo "- Rscript call: <%= rscript_call %>"
if ! command -v <%= rscript[1] %> &> /dev/null; then
>&2 echo "ERROR: Argument 'resources' specifies a non-existing 'Rscript' launch command: <%= rscript[1] %>. Maybe you need to specify which environment modules to load in the 'resources' argument, e.g. 'plan(future.batchtools::batchtools_bash, resources = list(modules = c(\"r\")))'. The search PATH for '<%= rscript[1] %>' was ${PATH}"
exit 1
fi
echo "- Rscript version: $(<%= paste(rscript, collapse = " ") %> --version)"
echo "- R_LIBS_USER=${R_LIBS_USER:-<not set>}"
echo "- R_LIBS_SITE=${R_LIBS_SITE:-<not set>}"
echo "- R_LIBS=${R_LIBS:-<not set>}"
echo "- Rscript library paths:"
<% if (details) { %>
<%= rscript_call %> -e "for (p in .libPaths()) cat(' - ', shQuote(p), ' [n=', sum(file.exists(file.path(list.dirs(p, recursive = FALSE), 'DESCRIPTION'))), ']\n', sep = '')"
<% } else { %>
<%= rscript_call %> -e "cat(sprintf(' - %s\n', shQuote(.libPaths())), sep = '')"
<% } %>
echo
# Launch R and evaluate the batchtools R job
echo "Calling 'batchtools::doJobCollection()' ..."
echo "- job name: '<%= job.name %>'"
echo "- job log file: '<%= log.file %>'"
echo "- job uri: '<%= uri %>'"
<%= rscript_call %> -e 'batchtools::doJobCollection("<%= uri %>")'
res=$?
echo " - exit code: ${res}"
echo "Calling 'batchtools::doJobCollection()' ... done"
echo
<% if (length(shutdown) > 0) {
writeLines(shutdown)
} %>
echo "End time: $(date +"%Y-%m-%d %H:%M:%S%z")"
## Relay the exit code from Rscript
exit "${res}"
Nothing.
makeClusterFunctionsBash() returns a
ClusterFunctions object.
library(future)
# Limit runtime to 30 seconds per future
plan(future.batchtools::batchtools_bash, resources = list(runtime = 30))
message("Main process ID: ", Sys.getpid())
f <- future({
data.frame(
hostname = Sys.info()[["nodename"]],
os = Sys.info()[["sysname"]],
cores = unname(parallelly::availableCores()),
pid = Sys.getpid(),
modules = Sys.getenv("LOADEDMODULES")
)
})
info <- value(f)
print(info)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.