#!/bin/bash
# This is a version of tune-main for launching irace using the --mpi
# option in a SGE Cluster.
set -e
set -o pipefail

if [ $# == 0 ]; then
    echo "Usage: ./tune-main-cluster-mpi <BINDIR> <EXECDIR> --parallel <NB_SLAVES> additional_args_to_irace"
    exit 1
fi

BINDIR=$1
EXECDIR=$2
shift 2
NB_SLAVES=0

PARAMS=
while [ $# -gt 0 ]; do
    case "$1" in
        --parallel) shift; NB_SLAVES="$1"; shift;;
        *) PARAMS="$PARAMS $1"; shift;;# terminate case
  esac
done

if [ $NB_SLAVES -lt 2 ]; then
    echo "$0: error: --parallel must be larger than 1"
    exit 1
fi

QUEUE=long
JOBNAME=irace-$$
#MACHINE=opteron2216
#MACHINE=xeon5410
#MACHINE=opteron6128
MACHINE=opteron6272

MPIRUN=/opt/openmpi/bin/mpirun
#PARALLEL_ENV=mpich_fu
PARALLEL_ENV=mpich_rr
let NB_PARALLEL_PROCESS=NB_SLAVES+1

exec qsub -v PATH <<EOF
#!/bin/sh
#$ -N $JOBNAME
#$ -l $MACHINE 
#$ -l $QUEUE
#$ -pe $PARALLEL_ENV $NB_PARALLEL_PROCESS 
#$ -m ase
#      b     Mail is sent at the beginning of the job.
#      e     Mail is sent at the end of the job.
#      a     Mail is sent when the job is aborted or rescheduled.
#      s     Mail is sent when the job is suspended.
#
#$ -o $EXECDIR/irace-$$.stdout 
#$ -e $EXECDIR/irace-$$.stderr 
#$ -cwd
export OMPI_MCA_plm_rsh_disable_qrsh=1
$MPIRUN -x OMPI_MCA_plm_rsh_disable_qrsh \
        -np 1 \
        $BINDIR/irace --exec-dir=$EXECDIR --parallel $NB_SLAVES --mpi 1 $PARAMS
EOF
