Difference between revisions of "Job Arrays"
Jump to navigation
Jump to search
(64 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | == | + | == Introduction == |
+ | Job Arrays allow you to run the same job on different datasets without having to create an individual job script for each dataset. Thus you are easily able to submit hundreds, even thousands of jobs for each different datasets. | ||
+ | This is accomplished by using the #SBATCH --array parameter in the '''SBATCH''' script. Then using the '''$SLURM_ARRAY_TASK_ID''' environmental variable to specify which dataset to use. The resources (number of processors, memory, etc) you specify in the job script will be identical for each job in the array. More details on SLURM job arrays can be found at [https://slurm.schedmd.com/job_array.html https://slurm.schedmd.com/job_array.html] | ||
− | + | == --array parameter == | |
+ | *This will create 10 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 10 (1,2,3,4,...,8,9.10) | ||
+ | <pre>#SBATCH --array 1-10</pre> | ||
+ | *This will create 10 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 19 with step size 2 (1,3,5,7,...,17,19) | ||
+ | <pre>#SBATCH --array 1-20:2</pre> | ||
+ | *This will create 5 jobs with the $SLURM_ARRAY_TASK_ID set to the 5 specified values (1,5,10,15,20) | ||
+ | <pre>#SBATCH --array 1,5,10,15,20</pre> | ||
+ | *This will create 20 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 20 (1,2,3,4...,18,19,20) but only run 4 of the jobs at time. | ||
+ | <pre>#SBATCH --array=1-20%4</pre> | ||
− | + | == Example Script == | |
− | + | This script will submit 10 jobs. Each job will do the following | |
− | + | *wait for 10 seconds (sleep 10) | |
− | + | *Output the hostname of the node it ran on (echo "Hostname: `hostname`") | |
− | + | *Output the $SLURM_ARRAY_TASK_ID | |
− | + | *The output file slurm-%A_%a.out will have that information. '''%A''' is the SLURM job number. '''%a''' is value of '''$SLURM_ARRAY_TASK_ID''' | |
− | + | ||
− | + | <pre>#!/bin/bash | |
− | + | # ----------------SBATCH Parameters----------------- # | |
− | + | #SBATCH -p normal | |
− | + | #SBATCH -n 1 | |
+ | #SBATCH -N 1 | ||
+ | #SBATCH --mail-user youremail@illinois.edu | ||
+ | #SBATCH --mail-type BEGIN,END,FAIL | ||
+ | #SBATCH -J example_array | ||
+ | #SBATCH -D /home/a-m/USERNAME | ||
+ | #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out | ||
+ | #SBATCH --array 1-10 | ||
− | + | # ----------------Your Commands------------------- # | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | sleep 10 | |
+ | echo "Hostname: `hostname`" | ||
+ | echo "Job Array Number: $SLURM_ARRAY_TASK_ID" | ||
− | + | </pre> | |
+ | *10 different output files will be created. The output in each one will be like below | ||
+ | <pre> | ||
+ | Hostname: compute-0-16 | ||
+ | Job Array Number: 10 | ||
+ | </pre> | ||
− | + | == Example - Ordered List == | |
+ | *Say you have a directory with input data files that are numbered sequentially like below. | ||
+ | *You want to run the program FastQC against these files. | ||
+ | *The input data is located in the directory '''input_data''' | ||
+ | *The results will be placed in the directory '''results''' | ||
+ | <pre>yeast_1_50K.fastq | ||
+ | yeast_2_50K.fastq | ||
+ | yeast_3_50K.fastq | ||
+ | yeast_4_50K.fastq | ||
+ | yeast_5_50K.fastq | ||
+ | yeast_6_50K.fastq | ||
+ | </pre> | ||
+ | *The job array script would be like below. | ||
<pre>#!/bin/bash | <pre>#!/bin/bash | ||
− | # ---------------- | + | # ----------------SBATCH Parameters----------------- # |
− | # | + | #SBATCH -p normal |
− | # | + | #SBATCH -n 1 |
− | # | + | #SBATCH -N 1 |
− | # | + | #SBATCH --mail-user youremail@illinois.edu |
− | # | + | #SBATCH --mail-type BEGIN,END,FAIL |
− | # | + | #SBATCH -J example_array |
− | # | + | #SBATCH -D /home/a-m/USERNAME |
− | # ----------------Load Modules-------------------- | + | #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out |
− | module load | + | #SBATCH --array 1-6 |
+ | # ----------------Load Modules-------------------- | ||
+ | module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 | ||
# ----------------Your Commands------------------- # | # ----------------Your Commands------------------- # | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | echo "Starting FastQC Job" | |
− | + | fastqc -o results/ input_data/yeast_${SLURM_ARRAY_TASK_ID}_50K.fastq | |
− | + | echo "Finishing FastQC Job" | |
− | |||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
+ | == Example - Unordered List == | ||
+ | *This is for a list of data files that do not have sequential numbers in the filename. | ||
+ | *The input data is located in the directory '''input_data''' | ||
+ | *The results will be located in the directory '''results''' | ||
+ | *This grabs a file from the '''input_data''' directory ('''ls input_data/ | sed -n ${SLURM_ARRAY_TASK_ID}p''') , and then puts that in the variable '''$file'''. | ||
+ | *Then runs fastqc using the '''$file''' variable | ||
− | + | <pre>#!/bin/bash | |
− | <pre> | + | # ----------------SBATCH Parameters----------------- # |
− | + | #SBATCH -p normal | |
− | + | #SBATCH -n 1 | |
− | + | #SBATCH -N 1 | |
− | + | #SBATCH --mail-user youremail@illinois.edu | |
− | + | #SBATCH --mail-type BEGIN,END,FAIL | |
− | .. | + | #SBATCH -J example_array |
− | + | #SBATCH -D /home/a-m/USERNAME | |
+ | #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out | ||
+ | #SBATCH --array 1-6 | ||
+ | # ----------------Load Modules-------------------- | ||
+ | module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 | ||
+ | # ----------------Your Commands------------------- # | ||
+ | echo "Starting FastQC Job" | ||
+ | file=$(ls input_data/ | sed -n ${SLURM_ARRAY_TASK_ID}p) | ||
+ | fastqc -o results input_data/${file} | ||
+ | echo "Finishing FastQC Job" | ||
</pre> | </pre> | ||
− | |||
− | == | + | == Example - Combining jobs == |
+ | *What if you have 650 datasets you want to analyze, but you can only submit 80 jobs at a time. Instead of submitting 80 jobs, and waiting for them to finish to submit the next batch, submit a single job array job that can handle all of the datasets. | ||
+ | *You need to divide your datasets into groups. Below is an example bash code that can do it. | ||
+ | *First, all of your input datasets need to be a folder called '''input_data''' | ||
+ | *The variable '''$ITEMS_TO_PROCESS''' specifies the size of each group of jobs should be | ||
+ | *The line '''JOBLIST=$(ls input_data/)''' will store the list of the files in the variable '''$JOB_LIST''' | ||
+ | *Then it will calculate which jobs go into which group using '''$SLURM_ARRAY_TASK_ID''' and '''$ITEMS_TO_PROCESS'' to calculate the '''$START_LINE''' and '''$END_LINE''' | ||
+ | <pre>ITEMS_TO_PROCESS=10 | ||
+ | JOBLIST=$(ls input_data/) | ||
+ | START_LINE=$(( ${SLURM_ARRAY_TASK_ID} * ${ITEMS_TO_PROCESS}) +1 ) | ||
+ | END_LINE=$(( ${START_LINE] + ${ITEMS_TO_PROCESS} ) -1 ) | ||
− | '' | + | </pre> |
+ | *Below is the full script | ||
+ | *In order to use the following script, you will need to properly set | ||
+ | *'--array' (the number of array elements you want) | ||
+ | *ITEMS_TO_PROCESS (the size of each group of jobs) | ||
− | + | <pre>#!/bin/bash | |
+ | # ----------------SBATCH Parameters----------------- # | ||
+ | #SBATCH -p normal | ||
+ | #SBATCH -n 1 | ||
+ | SSBATCH -N 1 | ||
+ | #SBATCH --mail-user youremail@illinois.edu | ||
+ | #SBATCH --mail-type BEGIN, END, FAIL | ||
+ | #SBATCH -J array_of_jobs | ||
+ | #SBATCH --array 1-10 | ||
+ | #SBATCH -D /home/a-m/USERNAME | ||
− | + | # ----------------Load Modules-------------------- # | |
+ | module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 | ||
+ | # ----------------Your Commands------------------- # | ||
− | + | ITEMS_TO_PROCESS=10 | |
+ | JOBLIST=$(ls input_data/) | ||
− | == | + | START_LINE=$(( ${SLURM_ARRAY_TASK_ID} * ${ITEMS_TO_PROCESS}) + 1) |
+ | END_LINE=$(( ${START_LINE] + ${ITEMS_TO_PROCESS} ) -1) | ||
− | |||
− | + | #Iteration through START_LINE and END_LINE | |
− | + | for line in `seq ${START_LINE} ${END_LINE}` | |
− | + | do | |
− | + | DATA_FILE=$( head -n $line ${JOBLIST} | tail -n 1 ) | |
− | + | fastqc -o results input_data/${DATA_FILE} | |
− | <pre> | + | done |
− | + | </pre> | |
− | |||
− | + | == Resources == | |
− | + | [https://slurm.schedmd.com/job_array.html https://slurm.schedmd.com/job_array.html] | |
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 12:00, 2 October 2019
Contents
Introduction[edit]
Job Arrays allow you to run the same job on different datasets without having to create an individual job script for each dataset. Thus you are easily able to submit hundreds, even thousands of jobs for each different datasets. This is accomplished by using the #SBATCH --array parameter in the SBATCH script. Then using the $SLURM_ARRAY_TASK_ID environmental variable to specify which dataset to use. The resources (number of processors, memory, etc) you specify in the job script will be identical for each job in the array. More details on SLURM job arrays can be found at https://slurm.schedmd.com/job_array.html
--array parameter[edit]
- This will create 10 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 10 (1,2,3,4,...,8,9.10)
#SBATCH --array 1-10
- This will create 10 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 19 with step size 2 (1,3,5,7,...,17,19)
#SBATCH --array 1-20:2
- This will create 5 jobs with the $SLURM_ARRAY_TASK_ID set to the 5 specified values (1,5,10,15,20)
#SBATCH --array 1,5,10,15,20
- This will create 20 jobs with the $SLURM_ARRAY_TASK_ID iterating 1 through 20 (1,2,3,4...,18,19,20) but only run 4 of the jobs at time.
#SBATCH --array=1-20%4
Example Script[edit]
This script will submit 10 jobs. Each job will do the following
- wait for 10 seconds (sleep 10)
- Output the hostname of the node it ran on (echo "Hostname: `hostname`")
- Output the $SLURM_ARRAY_TASK_ID
- The output file slurm-%A_%a.out will have that information. %A is the SLURM job number. %a is value of $SLURM_ARRAY_TASK_ID
#!/bin/bash # ----------------SBATCH Parameters----------------- # #SBATCH -p normal #SBATCH -n 1 #SBATCH -N 1 #SBATCH --mail-user youremail@illinois.edu #SBATCH --mail-type BEGIN,END,FAIL #SBATCH -J example_array #SBATCH -D /home/a-m/USERNAME #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out #SBATCH --array 1-10 # ----------------Your Commands------------------- # sleep 10 echo "Hostname: `hostname`" echo "Job Array Number: $SLURM_ARRAY_TASK_ID"
- 10 different output files will be created. The output in each one will be like below
Hostname: compute-0-16 Job Array Number: 10
Example - Ordered List[edit]
- Say you have a directory with input data files that are numbered sequentially like below.
- You want to run the program FastQC against these files.
- The input data is located in the directory input_data
- The results will be placed in the directory results
yeast_1_50K.fastq yeast_2_50K.fastq yeast_3_50K.fastq yeast_4_50K.fastq yeast_5_50K.fastq yeast_6_50K.fastq
- The job array script would be like below.
#!/bin/bash # ----------------SBATCH Parameters----------------- # #SBATCH -p normal #SBATCH -n 1 #SBATCH -N 1 #SBATCH --mail-user youremail@illinois.edu #SBATCH --mail-type BEGIN,END,FAIL #SBATCH -J example_array #SBATCH -D /home/a-m/USERNAME #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out #SBATCH --array 1-6 # ----------------Load Modules-------------------- module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 # ----------------Your Commands------------------- # echo "Starting FastQC Job" fastqc -o results/ input_data/yeast_${SLURM_ARRAY_TASK_ID}_50K.fastq echo "Finishing FastQC Job"
Example - Unordered List[edit]
- This is for a list of data files that do not have sequential numbers in the filename.
- The input data is located in the directory input_data
- The results will be located in the directory results
- This grabs a file from the input_data directory (ls input_data/ | sed -n ${SLURM_ARRAY_TASK_ID}p) , and then puts that in the variable $file.
- Then runs fastqc using the $file variable
#!/bin/bash # ----------------SBATCH Parameters----------------- # #SBATCH -p normal #SBATCH -n 1 #SBATCH -N 1 #SBATCH --mail-user youremail@illinois.edu #SBATCH --mail-type BEGIN,END,FAIL #SBATCH -J example_array #SBATCH -D /home/a-m/USERNAME #SBATCH -o /home/a-m/USERNAME/slurm-%A_%a.out #SBATCH --array 1-6 # ----------------Load Modules-------------------- module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 # ----------------Your Commands------------------- # echo "Starting FastQC Job" file=$(ls input_data/ | sed -n ${SLURM_ARRAY_TASK_ID}p) fastqc -o results input_data/${file} echo "Finishing FastQC Job"
Example - Combining jobs[edit]
- What if you have 650 datasets you want to analyze, but you can only submit 80 jobs at a time. Instead of submitting 80 jobs, and waiting for them to finish to submit the next batch, submit a single job array job that can handle all of the datasets.
- You need to divide your datasets into groups. Below is an example bash code that can do it.
- First, all of your input datasets need to be a folder called input_data
- The variable $ITEMS_TO_PROCESS specifies the size of each group of jobs should be
- The line JOBLIST=$(ls input_data/) will store the list of the files in the variable $JOB_LIST
- Then it will calculate which jobs go into which group using $SLURM_ARRAY_TASK_ID' and $ITEMS_TO_PROCESS to calculate the $START_LINE and $END_LINE
ITEMS_TO_PROCESS=10 JOBLIST=$(ls input_data/) START_LINE=$(( ${SLURM_ARRAY_TASK_ID} * ${ITEMS_TO_PROCESS}) +1 ) END_LINE=$(( ${START_LINE] + ${ITEMS_TO_PROCESS} ) -1 )
- Below is the full script
- In order to use the following script, you will need to properly set
- '--array' (the number of array elements you want)
- ITEMS_TO_PROCESS (the size of each group of jobs)
#!/bin/bash # ----------------SBATCH Parameters----------------- # #SBATCH -p normal #SBATCH -n 1 SSBATCH -N 1 #SBATCH --mail-user youremail@illinois.edu #SBATCH --mail-type BEGIN, END, FAIL #SBATCH -J array_of_jobs #SBATCH --array 1-10 #SBATCH -D /home/a-m/USERNAME # ----------------Load Modules-------------------- # module load FastQC/0.11.5-IGB-gcc-4.9.4-Java-1.8.0_152 # ----------------Your Commands------------------- # ITEMS_TO_PROCESS=10 JOBLIST=$(ls input_data/) START_LINE=$(( ${SLURM_ARRAY_TASK_ID} * ${ITEMS_TO_PROCESS}) + 1) END_LINE=$(( ${START_LINE] + ${ITEMS_TO_PROCESS} ) -1) #Iteration through START_LINE and END_LINE for line in `seq ${START_LINE} ${END_LINE}` do DATA_FILE=$( head -n $line ${JOBLIST} | tail -n 1 ) fastqc -o results input_data/${DATA_FILE} done