#!/bin/bash
# This runs on the compute nodes...
# Args: wd [-b CMDS] [-i input] CMDS

N=$SLURM_JOB_NUM_NODES
n=$SLURM_NTASKS
c=$SLURM_CPUS_PER_TASK
nodename=$SLURMD_NODENAME
local=$SLURM_LOCALID
node=$SLURM_NODEID
task=$SLURM_PROCID

cd "$1"
shift

threadsPerTask=2
pjobs=$((c / $threadsPerTask))

echo $(date) task $n.$task on $nodename:$N.$node start $(pwd) 1>&2

PATH=$W/$USER/bin:$W/shared/bin:$PATH
export task PATH n

if [ "$1" = "-b" ]
then
  shift
  eval "$1"
  shift
fi

if [ "$1" = "-i" ]
then
 shift
 input="$1"
 shift
fi

export cmd="$1"
shift

doit () {
 arg="$1"
 echo $(date) start $task $PARALLEL_SEQ $arg
 eval "$cmd"
 echo $(date) end $task $PARALLEL_SEQ
}

export -f doit

eval "$input" | \
   parallel -j $pjobs doit '{}'

echo $(date) task $n.$task on $nodename:$N.$node end 1>&2

