Skip to content

Example SLURM scripts

There are many possible configurations for how parallelism can be setup. Here are some example situations and corresponding script:

Example 1: A single task with many cores

This kind of setup is good for running programs that have built in parallelism (can use multiple cores).

1
2
3
4
5
6
7
8
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=8G
#SBATCH --time=01:00:00
echo "Hello, world!"

Example 2: Many tasks running over multiple nodes

This kind of setup will launch a total of 16 tasks (8 tasks per node, 2 nodes). A total of 32 CPU cores will be used (2 for each task), while memory is set to use a total of 16 GB (1 GB per task).

1
2
3
4
5
6
7
8
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=8
#SBATCH --cpus-per-task=2
#SBATCH --mem=1G
#SBATCH --time=01:00:00
echo "Hello, world!"

Example 3: Run a single task many times

This kind of setup will create an array job (numbered 1-12) and will run them in parallel (up to 6 at a time). Each iteration of the array will have 1 task, requesting 2 CPU cores and 8 GB of memory. The array number of accessible using the $SLURM_ARRAY_TASK_ID variable.

This is useful when you want to perform the same computation on different inputs/parameters.

1
2
3
4
5
6
7
8
9
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=2
#SBATCH --mem=8G
#SBATCH --time=01:00:00
#SBATCH --array=1-12%6
echo "Hello, world! Running task number $SLURM_ARRAY_TASK_ID"

Example 4: Simple task with logging

This is the same as example 1, but output is captured and saved into the files specified by “--out" and “--error".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=8G
#SBATCH --time=01:00:00
#SBATCH --out="myjob_%j.out"
#SBATCH --error="myjob_ERROR_%j.out"
echo "Hello, world!"

Example 5: Run job only after dependency finishes first

This is the same as example 1, but will only start after the dependency job (job number 1234) has successfully completed.

1
2
3
4
5
6
7
8
9
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=8G
#SBATCH --time=01:00:00
#SBATCH --dependency=afterok:1234
echo "Hello, world!"

Example 6: Run job on specified nodes and partitions

This is the same as example 1, but will only run of the “standard” partition and “bhri-hpcnce-01” node (multiple values can be given if they are comma separated).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash
#SBATCH --job-name=myjob
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=8G
#SBATCH --time=01:00:00
#SBATCH --nodelist="bhri-hpcn-01"
#SBATCH --partition=standard
echo "Hello, world!"