5  Single Independent R Job

Consider the need to obtain random numbers across a single sample size under a specific mean.

\[X_{N} = \mu + Z \sim \mathcal{N}_{N} \left( \mu, 1 \right)\]

5.1 Sample Job Script

sim_job.R

# Expect command line args at the end.
args = commandArgs(trailingOnly = TRUE)
# Skip args[1] to prevent getting --args

# Extract and cast as numeric from character
rnorm(n = as.numeric(args[2]), mean = as.numeric(args[3]))

Download a copy and run it on the cluster with:

# Download a copy of the script onto the cluster
wget https://hpc.thecoatlessprofessor.com/slurm/scripts/sim_job.R

# Execute the script with parameter values
Rscript $HOME/sim_job.R --args 5 10
# [1]  9.006482 11.288477 11.109700 12.280027  9.500943

5.2 Sample Slurm Submission File

sim_single_launch.slurm

#!/bin/bash

## Describe requirements for computing ----

## Name the job to ID it in squeue -u $USER
#SBATCH --job-name=myjobarray

## Send email on any change in job status (NONE, BEGIN, END, FAIL, ALL)
## Note: To be notified on each task on the array use: ALL,ARRAY_TASKS
#SBATCH --mail-type=ALL

## Email address of where the notification should be sent.
#SBATCH --mail-user=netid@illinois.edu

## Amount of time the job should run
## Note: specified in hour:min:sec, e.g. 01:30:00 is a 1 hour and 30 min job.
#SBATCH --time=00:10:00
## Request a single node
#SBATCH --ntasks=1
## Specify number of CPU cores for parallel jobs
## Note: Leave at 1 if not running in parallel.
#SBATCH --cpus-per-task=1
## Request a maximum amount of RAM per CPU core
## Note: For memory intensive work, set to a higher amount of ram.
#SBATCH --mem-per-cpu=5gb

## Setup computing environment for job ----

## Create a directory for the data output based SLURM_JOBID assigned to job
mkdir ${SLURM_SUBMIT_DIR}/${SLURM_JOBID}

## Switch directory into job ID (puts all output here)
cd ${SLURM_SUBMIT_DIR}/${SLURM_JOBID}

## Run simulation ----

## Load a pre-specified version of R
module load R/3.6.2

## Run R script in batch mode without file output
Rscript $HOME/sim_job.R --args 5 10
# Download a copy of the script onto the cluster
wget https://hpc.thecoatlessprofessor.com/slurm/scripts/sim_single_launch.slurm

# Queue the job on the Cluster
sbatch sim_single_launch.slurm