#! /bin/sh -f
# $Id: petscmpiexec,v 1.3 2001/09/04 16:18:05 bsmith Exp $ 
#
#    Uses PETSC_ARCH and PETSC_DIR plus the MPIEXEC command in
#  packages to run mpiexec
#    Use PETSCVALGRIND_OPTIONS and MPI_BINDING from environment
#
if [ x$1 = 'x-h' ] || [ x$1 = 'x--help' ]; then
  echo "Usage: petscmpiexec <-valgrind> <-debugger> -n <numberprocessors> programname <programoptions>"
  exit
fi

valgrind=0
debugger=0
if [ x$1 = x"-valgrind" ]; then
  valgrind=1
  echo $2
  if [ x$2 = x"-debugger" ]; then
    debugger=1
    shift
  fi
  shift
fi

if [ $1 !=  "-n" ]; then
  echo "Error in  mpiexec command: must start with -n <numberprocessors> or -valgrind -n <numberprocessors>"
  exit 1
fi
shift
np=$1
shift

hosts=""
if [ x$1 =  x"-hosts" ]; then
  shift
  hosts="-hosts $1"
  shift
fi

MPI_BUILD_HOME=$( grep MPI_BUILD_HOME ${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables | grep -v mpiexec | grep -v include | grep -v lib | grep -v "#MPI_HOME" | cut -f2 -d= )
MPI_HOME=$( grep "MPI_HOME " ${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables | grep -v build | grep -v include | grep -v "#MPI_HOME" | grep -v mpe  | cut -f2 -d= )
run=$( grep MPIEXEC ${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables | grep -v "#MPIEXEC" | cut -f2 -d= )

#echo MPI_BUILD_HOME=$MPI_BUILD_HOME
#echo MPI_HOME=$MPI_HOME

#echo run=$run
trun=$( echo ${run} | sed -e sz\$MPI_BUILD_HOMEz${MPI_BUILD_HOME}z )
#echo trun=$trun
nrun=$( echo ${trun} | sed -e sz\$MPI_HOMEz${MPI_HOME}z )
#echo nrun=$nrun
qrun=$( echo ${nrun} | sed -e sz\$PETSC_DIRz${PETSC_DIR}z )
#echo qrun=$qrun

#echo $qrun $*

if [ ${valgrind} = "1" ]; then
  VALGRIND_CMD=$(which valgrind)
  VALGRIND_OPTIONS="-q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes ${PETSCVALGRIND_OPTIONS}"
  if [ ${debugger} = "1" ]; then
    VALGRIND_CMD="xterm -e ${VALGRIND_CMD}"
    VALGRIND_OPTIONS="${VALGRIND_OPTIONS} --db-attach=yes"
  fi
  if [ `uname` = 'Darwin' ]; then
    VALGRIND_OPTIONS="${VALGRIND_OPTIONS} --dsymutil=yes"
  fi
  $qrun -n $np $hosts ${VALGRIND_CMD} ${VALGRIND_OPTIONS} $*
  if [ `uname` = 'Darwin' ]; then
    rm -rf *.dSYM
    rm -rf ${PETSC_DIR}/${PETSC_ARCH}/lib/lib*.dSYM
  fi
else
  $qrun -n $np $hosts ${MPI_BINDING} $*
fi

