#! /bin/csh -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
#
if ($1 == '-h' | $1 == '--help') then
  echo "Usage: petscmpiexec <-valgrind> -n <numberprocessors> programname <programopions>"
  exit
endif

set valgrind=0
if ( $1 == "-valgrind" ) then
  set valgrind = 1
  shift
endif

if ( $1 !=  "-n" ) then
  echo "Error in  mpiexec command: must start with -n <numberprocessors> or -valgrind -n <numberprocessors>"
  exit 1
endif
shift
set np=$1
shift

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

#echo $MPI_BUILD_HOME
#echo $MPI_HOME

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

#echo $qrun $*

if ( ${valgrind} == "1" ) then
  if ( `uname` == 'Darwin') then
    $qrun -n $np `which valgrind` -q --tool=memcheck --dsymutil=yes --num-callers=20  $*
    rm -rf *.dSYM
  else 
    $qrun -n $np `which valgrind` -q --tool=memcheck --num-callers=20  $*
  endif
else
  $qrun -n $np $*
endif


