#!/bin/sh

MPI=@MPI@

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
    if [ $TESTMODE = 0 ]
    then 
#      echo "mpirun -pg -np $PNUMBER $* ..."
      mpirun -pg -np $PNUMBER $*
    else

      COUNTER=$PNUMBER

      while [ $COUNTER != 0 ]
      do 
        hostname >> machines
        COUNTER=`expr $COUNTER - 1`
      done
#      echo "mpirun -pg -machinefile ./machines -np $PNUMBER $* ..."
      mpirun -pg -machinefile ./machines -np $PNUMBER $*

      rm -f machines

    fi

  fi  

  ;;

  sunhpc)
  echo "tmrun -np $PNUMBER $* ..."
  tmrun -np $PNUMBER $*

  ;;

  sp2)

  if [ -f host.list ]
  then
    $* -procs $PNUMBER
  else
    if [ $TESTMODE = 0 ]
    then
      $* -procs $PNUMBER
    else

      COUNTER=$PNUMBER

      while [ $COUNTER != 0 ]
      do 
        hostname >> host.list
        COUNTER=`expr $COUNTER - 1`
      done

      $* -procs $PNUMBER

      rm -f host.list
    fi
  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
