#!/bin/sh

# Assumes `mpirun' etc are on path.
# Should configure in full pathname of executables?


MPI=mpich
MACHINEFILE=


if [ $# -lt 1 ]
then
  echo "Usage: prun [-t] <num_processes> <mpioptions> <executable> <program arguments>"
  exit 1
fi

TESTMODE=0
PNUMBER=0


if [ $1 = -t ] 
then 
 TESTMODE=1
 shift
fi

PNUMBER=$1

shift

case $MPI in

  mpich)

  if [ -f machines ]
  then
#    echo "mpirun -pg -machinefile ./machines -np $PNUMBER $* ..."
    mpirun -pg -machinefile ./machines -np $PNUMBER $*
  else
#      echo "mpirun -pg -np $PNUMBER $* ..."
      if test "$MACHINEFILE" != "" 
      then
         mpirun -pg -machinefile $MACHINEFILE -np $PNUMBER $*
      else
         mpirun -pg -np $PNUMBER $*
      fi
  fi  

  ;;

  sunhpc)
#  echo "mprun -np $PNUMBER $* ..."
  mprun -np $PNUMBER $*

  ;;

  lam)
   # echo "mpirun -np $PNUMBER $* ..."
   mpirun -np $PNUMBER $*

  ;;

  sp2)

  if [ -f ./machines ]
  then
    poe $* -procs $PNUMBER -hostfile ./machines
  else
    poe $* -procs $PNUMBER
  fi

  ;;

  chimp)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                         #
    #  CHIMP                                                                  #
    #                                                                         #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


    echo "Not tested"

    exit

    if test -f ./machines ; then
      MACHINES=./machines
    elif test -f ${HOME}/machines ; then
      MACHINES=${HOME}/machines
    else
      echo missing machines file.
      exit 1
    fi
    
    cat $MACHINES | sed "s/^/1($2)host=/" | head -$1 > _config.tmp
    mpirun _config.tmp
    rm _config.tmp

    ;;

esac

