#!/bin/sh

EXAMLDIR=/usr/lib/examl/bin
if echo $0 | grep -qi omp ; then
  EXE=examl-OMP
else
  EXE=examl
fi

NPROC=`nproc`

if echo "$@" | grep -q -- -np ; then
  NP=`echo "$@" | sed 's/^.* *-np *\([0-9]\+\) *.*/\1/'`
  echo "Run MPI on $NP processors as requested"
  CMDARGS=`echo "$@" | sed 's/\(^.* *\)-np *[0-9]\+\( *.*\)/\1\2/'`
else
  NP=$((NPROC/2)) # leave 50% processors for other jobs - if you want to force more just use -np option
  if [ $NP -eq 1 ] ; then
    NP=$NPROC
  fi
  echo "Run MPI on $NP of $NPROC available processors"
  CMDARGS="$@"
fi

if grep -q avx /proc/cpuinfo; then
    echo "Use $EXE with AVX support and $NP processors"
    mpirun -np $NP ${EXAMLDIR}/${EXE}-AVX $CMDARGS
else
    echo "Use $EXE without AVX support and $NP processors"
    mpirun -np $NP ${EXAMLDIR}/${EXE} $CMDARGS
fi
