#!/bin/bash
#
#SBATCH --ntasks=100
## --spread-job : spread jobs over the maximum number of worker nodes possible
##                without this, SLURM fills workers before using others
#SBATCH --spread-job
#SBATCH --output=job-tasks-spread.out
#SBATCH --error=job-tasks-spread.err

echo "Job $SLURM_JOB_NAME (jobid $SLURM_JOBID) "
echo
scontrol show job $SLURM_JOBID

echo "List of nodes : $SLURM_JOB_NODELIST)"
echo

DIR=/home/infor/pugnere/encours/slurm/job-tasks-dir

START=$(date +%s)
echo "Start of the loop"
echo

for file in $DIR/*
do
   srun --ntasks=1 job-tasks-exec.sh $file &
done

echo "Waiting the end of all the sub-tasks"
wait

echo
END=$(date +%s)
secs=$(($END-$START))
echo -n "Job $SLURM_JOB_NAME (jobid $SLURM_JOBID) Job-Time (walltime) $secs seconds "
printf "("%dh:%02dm:%02ds")\n" $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))

echo "Job's End"
