I have a R code that I want to execute on several nodes using Slurm, with each iteration of my paramater which goes on a node. This is my Slurm code :

#SBATCH -o job-%A_task.out
#SBATCH --job-name=paral_cor
#SBATCH --partition=normal
#SBATCH --time=1-00:00:00
#SBATCH --mem=124G                #I want to use 124Go / node
#SBATCH --cpus-per-task=32        #and 32CPUs / node 
#SBATCH --exclude=hpcsmp01

module load gcc/8.1.0 openblas/0.3.3 R

mkdir -p "$OUTPUT"

echo "Start job :"`date`

Rscript my_scrit.R --subset $i --file $1 > "$OUTPUT"

echo "Stop job :"`date`

The paramater --subset $i will take value from 1 to X (X depends of my input file in my R code) . Then, for each iteration of i , I want that one script which be executed on a node . For example, --subset 1 -> one node , --subset 2 --> another node ... until --subset X -> a last node

I don't want to use jobs arrays because it doesn't work correctly on my cluster. So, I want to create a bash loop which would look like this :

for i in ?
 sbatch slurm_code.sh $i 

I don't know how to make the link between the parameter --subset $i and how to increment it in the for loop from 1 to X.

1 Answer 1


You want to include an srun within a for loop in order to requisition node within your script. If we assume you have five subsets, you can use something along the lines of:

for i in `seq 1 5`; do
  srun \
    -N1 \
    --mem=124G \
    --cpus-per-task=32 \
    Rscript my_script.R --subset $i --file $1 > "$OUTPUT-$i" &


Notice the & as well as the wait command. This will allow these processes to run in parallel, and SLURM will wait until everything in that for loop completes.

You'll also need to make sure that your output file specification can be written to in parallel if you choose this route. Above I added a signifier to your $OUTPUT variable that would give a different file for each subset. You would have to add some code after the wait command to stitch your outputs back together into one large file.

I believe you'll also want to specify a --nodes value in the sbatch file, indicating the total number of nodes your job will use.

Another option would be to include all of your job code in a shell script that takes command-line arguments, and call that from a for loop using srun within your sbatch file. ex.

for i in `seq 1 5`; do
  srun -N1 --mem=124G --cpus-per-task=32 bash runAnalysis.sh --subset $i &


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .