#!/bin/bash
#
#SBATCH --ntasks=100
#SBATCH --output=job-tasks.out
#SBATCH --error=job-tasks.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
  # -ntasks=1 : each srun is 1 task, 100 tasks maximum at a time (see #SBATCH --ntasks=100) whatever the number of files
  srun --ntasks=1 job-tasks-exec.sh $file  &
done

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

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"

# pareil que :
# find /path/to/data -print0 | xargs -0 -n1 -P $SLURM_NTASKS srun -n1 --exclusive ./myprog
