Skip to contents

Generate a SLURM batch script for running an R script with configurable computational resources, logging, email notifications, optional Conda environment activation, and automatic resource-usage reporting.

Usage

slurm(
  script,
  ncores = 1,
  name = NULL,
  email = "filippo.gambarota@unipd.it",
  conda = "r460",
  partition = "queue1",
  mem_per_cpu = "2G",
  time = "00:00:00",
  out = NULL,
  clip = FALSE
)

Arguments

script

Character scalar. Path to the R script to execute.

ncores

Numeric scalar. Number of CPUs requested through `–cpus-per-task`. Defaults to `1`.

name

Character scalar or `NULL`. SLURM job name. If `NULL`, the job name is derived from the basename of `script`, without its file extension.

email

Character scalar or `NULL`. Email address used for SLURM job notifications. If `NULL`, email notifications are omitted.

conda

Character scalar or `NULL`. Name of the Conda environment to activate before running the R script. If `NULL`, the Conda activation block is omitted.

partition

Character scalar. SLURM partition to use. Defaults to `"queue1"`.

mem_per_cpu

Character scalar. Memory requested per CPU, passed to `–mem-per-cpu`. Defaults to `"2G"`.

time

Character scalar. SLURM time limit passed to `–time`. Defaults to `"00:00:00"`.

out

Character scalar or `NULL`. Optional path where the generated batch script should be written. If `NULL`, no file is written.

clip

Logical scalar. If `TRUE`, copy the generated batch script to the system clipboard using [clipr::write_clip()]. Defaults to `FALSE`.

Value

Invisibly returns a character scalar containing the generated SLURM batch script. The script is always printed to the console. If `out` is supplied, it is also written to that file. If `clip = TRUE`, it is also copied to the clipboard.

Details

The generated script disables automatic multi-threading in common numerical libraries so that parallelism can be managed explicitly by R, for example through [parallel::mclapply()].

Standard output and error are initially written to the submission directory using the SLURM job name and job ID. When the job terminates, resource-usage statistics are printed and the log files are moved to a `logs` directory, which is created automatically if needed.

`out` and `clip = TRUE` are mutually exclusive.

The generated batch script requests one node and one task, with the number of CPUs controlled by `ncores`. Numerical-library threading is restricted to one thread per process by setting common BLAS, OpenMP, MKL, BLIS, VECLIB, and NUMEXPR environment variables.

At job termination, the generated script reports wall-clock time and, when available, resource-usage statistics from `sstat` and `sacct`, including CPU usage, resident memory, virtual memory, disk I/O, requested memory, and exit status. A short description of each reported metric is appended to the log.

Examples

if (FALSE) { # \dontrun{
# Print a SLURM script
slurm(
  script = "scripts/simulation.R",
  ncores = 32
)

# Write the generated script to a file
slurm(
  script = "scripts/simulation.R",
  ncores = 32,
  out = "simulation.slurm"
)

# Copy the generated script to the clipboard
slurm(
  script = "scripts/simulation.R",
  ncores = 32,
  clip = TRUE
)

# Generate a script without Conda activation
slurm(
  script = "scripts/analysis.R",
  conda = NULL
)
} # }