Submitting R Jobs#

Listing 1 Serial \(\pi\) Monte Carlo R Script.#
1monte_carlo_pi <- function(n) {
2  x=runif(n)
3  y=runif(n)
4  z=sqrt(x^2+y^2)
5  length(which(z<=1))*4/n
6}
7
8args = commandArgs(trailing=TRUE)
9monte_carlo_pi(strtoi(args[1]))
Listing 2 Parallel \(\pi\) Monte Carlo R Script.#
 1library(parallel)
 2
 3monte_carlo_pi <- function(n) {
 4  z = sqrt(runif(1)^2+runif(1)^2)
 5  return(if(z<=1) 1 else 0)
 6}
 7
 8args = commandArgs(trailing=TRUE)
 9n = strtoi(args[1])
10c = strtoi(args[2])
11cl = makeCluster(getOption("cl.cores", c))
12result = clusterApply(cl, 1:n, fun=monte_carlo_pi)
13sum(unlist(result))*4/n
14stopCluster(cl)
Listing 3 Interactive sessions using srun.#
1module load r/4.0.2
2srun -p htc --mem=6G --pty $SHELL
Listing 4 Using sbatch --wrap.#
1module load r/4.0.2
2sbatch -p htc --mem=6G  --wrap "sleep 30; time Rscript pi_monte_carlo_serial.R 1000"
Listing 5 Serial job using using sbatch.#
 1#!/bin/bash
 2#SBATCH -J R
 3#SBATCH -o R_%j.out
 4#SBATCH -p htc
 5#SBATCH --mem=6G
 6
 7module purge
 8module load r/4.0.2
 9
10time Rscript pi_monte_carlo_serial.R 1000
Listing 6 Parallel job using using sbatch.#
 1#!/bin/bash
 2#SBATCH -J pi
 3#SBATCH -o pi_%j.out
 4#SBATCH -p development
 5#SBATCH --cpus-per-task=2
 6#SBATCH --mem=6G
 7
 8module purge
 9module load r/4.0.2
10
11time Rscript pi_monte_carlo_parallel.R 10000000 ${SLURM_CPUS_PER_TASK}
Listing 7 Array job using using sbatch.#
 1#!/bin/bash
 2#SBATCH -J pi_array
 3#SBATCH -o pi_array_%a-%A.out
 4#SBATCH --array=1-4%2
 5#SBATCH -p development
 6#SBATCH --mem=6G
 7
 8module purge
 9module load r/4.0.2
10
11time Rscript pi_monte_carlo_serial.R $((100**${SLURM_ARRAY_TASK_ID}))