First calculation

Our goal on this page is to get a serial calculation to run on a compute node.

Note

For each example, the script and its Slurm job file must be in the same working directory before you submit.

Simple example to get started

If you are new to Slurm, start with this minimal Bash sanity check. It confirms that submission and output work before adding R, Python, C, or Fortran.

Simple Bash script (simple_bash.sh):

echo "hello from the bash script!"

Slurm script (simple_bash_job.sh) to run it on Saga:

#!/bin/bash

#SBATCH --account=<your-account>
#SBATCH --job-name=simple-bash
#SBATCH --partition=normal
#SBATCH --time=00:01:00

set -o errexit
set -o nounset

bash simple_bash.sh

Submit with:

$ sbatch simple_bash_job.sh

Now we continue with simple calculation examples in R, Python, C, and Fortran. For C and Fortran, we compile before execution. For simple examples, this can be done directly in the Slurm script.

# assumed to be simple.R
print("hello from the R script!")

We can launch the R, Python, C, and Fortran examples on Saga with the following job scripts. Before submitting, adjust at least the line with --account to match your allocation:

#!/bin/bash

#SBATCH --account=<your-account>
#SBATCH --job-name=example
#SBATCH --partition=normal
#SBATCH --mem=1G
#SBATCH --ntasks=1
#SBATCH --time=00:02:00

# it is good to have the following lines in any bash script
set -o errexit  # make bash exit on any error
set -o nounset  # treat unset variables as errors

module reset
module load R/4.2.1-foss-2022a

Rscript simple.R > simple.Rout

Submit the example job scripts with:

$ sbatch <your_slurm_script>

Longer example

Here is a longer example that approximates pi using a Monte Carlo method. It runs 100 iterations, each throwing 2 million random points. This takes roughly 1 minute in R; C and Fortran will typically be faster.

The Python version uses NumPy, which runs array operations as compiled C code internally. This makes it faster than R here, and much faster than a plain Python loop would be. A pure Python implementation of the same calculation would be an order of magnitude slower than C or Fortran.

# assumed to be sequential.R
library(foreach)


# this function approximates pi by throwing random points into a square
# it is used here to demonstrate a function that takes a bit of time
approximate_pi <- function() {
  # number of points to use
  n <- 2000000

  # generate n random points in the square
  x <- runif(n, -1.0, 1.0)
  y <- runif(n, -1.0, 1.0)

  # count the number of points that are inside the circle
  n_in <- sum(x^2 + y^2 < 1.0)

  4 * n_in / n
}


foreach (i=1:100, .combine=c) %do% {
  approximate_pi()
}

And the corresponding Slurm scripts. Before submitting, adjust at least the line with --account to match your allocation:

#!/bin/bash

#SBATCH --account=<your-account>
#SBATCH --job-name=example
#SBATCH --partition=normal
#SBATCH --mem=2G
#SBATCH --ntasks=1
#SBATCH --time=00:02:00

# it is good to have the following lines in any bash script
set -o errexit  # make bash exit on any error
set -o nounset  # treat unset variables as errors

module restore
module load R/4.2.1-foss-2022a

Rscript sequential.R > sequential.Rout

Next steps

R

Python

  • Use module spider Python to see available Python modules

  • Consider using containers to manage Python environments: Containers with GPU support

C and Fortran