#!/bin/bash

set -euo pipefail

if [[ ! -e o2_nrConfs.txt ]]; then
  echo "o2_nrConfs.txt not found, exiting"
  exit 1
fi
NR_CONFS=$(cat o2_nrConfs.txt)
DEP=$(cat o4.sid)
NAME=$(basename $(pwd))

rm -f o5.sid
find "scratch/o5/" -mindepth 1 -maxdepth 1 -name "run*-*.log" -not -name "*ref*" -print0 | xargs -0 -P 3 -n 100 -r rm

REPEAT=5
BSIZE=10
sbatch -C chimaira -n 1 -c 20 --array=0-$(( (${NR_CONFS} / ${BSIZE} + 1) * ${REPEAT} - 1)) --nice=700 -o scratch/o5/run_blob-%a.log -e scratch/o5/run_blob-%a.log --parsable --job-name "pExpl run ${NAME}" --time=150 --dependency=afterany:${DEP} "$@" <<EOF | tee -a o5.sid
#!/bin/bash

ITERATION=\$(( \$SLURM_ARRAY_TASK_ID % ${REPEAT} + 1))
BLOCK=\$(( \$SLURM_ARRAY_TASK_ID / ${REPEAT} ))
ID=\$(( \$BLOCK * ${BSIZE} ))
MAX=\$(( (\$ID + ${BSIZE} - 1) >= $NR_CONFS ? $NR_CONFS : (\$ID + ${BSIZE} - 1) ))
for j in \$(seq \$ID \$MAX); do
  DIR=scratch/\$(printf "var%05d" \$j)
  srun --time=10 taskset -c 0-9 \$DIR/exastencils.exe &> scratch/o5/run\$ITERATION-\$j.log
done
EOF
