Accessing task id for array jobs

Viewed 452

I usually use something like the following for submitting array jobs,

#!/bin/bash
sbatch <<EOT
#!/bin/sh
#SBATCH --job-name=$1                      
#SBATCH --time=$2                           
#SBATCH --mem-per-cpu=$3                   
#SBATCH --array 0-$4                        

./myprogram.o ${SLURM_ARRAY_TASK_ID} <-- SLURM_ARRAY_TASK_ID not visible.. (A)
./script_that_runs_myprogram.sh      <-- SLURM_ARRAY_TASK_ID visible inside script (B)

EOT

In case (B), script_that_runs_myprogram.sh can see the environment variable that defines the task ID. Is it possible to access the array index somehow in case (A)? It seems unnecessary to have another script which runs the program. The alternative case where SBATCH parameters are defined manually is very tedious.

1 Answers

The ${SLURM_ARRAY_TASK_ID} should be visible in option A. The problem is, that the variable is evaluated by bash too early. Bash interprets the variable the moment you send that script. At that point, the variable will still be empty. Try escaping it:

#!/bin/bash
sbatch <<EOT
#!/bin/sh
#SBATCH --job-name=$1                      
#SBATCH --time=$2                           
#SBATCH --mem-per-cpu=$3                   
#SBATCH --array 0-$4                        

./myprogram.o \${SLURM_ARRAY_TASK_ID}

EOT
Related