##############################################################
#        Below this nothing should have to be changed        #
##############################################################
 
echo Running MPI from $MPI_HOME
echo 
 
set nodes = `cat $PBS_NODEFILE` 
set nnodes = $#nodes 
echo Nodes: $nodes 
 
set confile = /tmp/$PBS_JOBID.conf 

set prev = ""
# Work out if running single or dual processor 
foreach i ( $nodes ) 
 if ( ${prev} != ${i} ) then 
    set pp = ":1"
  else 
    set pp = ":2"
  endif 
  set prev = ${i} 
end 
 
set prev = ""
# Now create the confile 
foreach i ( $nodes )  
   if ( ${i} != ${prev} ) then 
      echo "$i$pp" >> $confile 
   endif 
   set prev = ${i} 
end 
 
#echo MPICH_GM Configuration file: 
cat $confile 

#need to make local directory on all slave nodes
echo "making rundirectory"
foreach i ( $nodes )
   rsh ${i} "mkdir ${RUNDIR}"
end


echo Changing to $RUNDIR
cd $RUNDIR

# copy the input file for the run over to the current directory
cp $FROMDIR$RUNFLAGS .
cp $FROMDIR$run_name.cell .
# are we checkpointing??
if ( $?checkp == 1) then
    # yes we are. Do we have a checkpoint file from a particular directory?
    if ( $?checkf != 1) then
	# no we don't - use the default one from home program dir
	cp $FROMDIR$run_name.check .
    else
	# yes we do: copy it across
	cp $FROMDIR$checkp.erik.beo.org/$run_name.$checkf $run_name.check
    endif
endif

# now we actually get to run the code! Yay!
echo "Will run command: mpirun.ch_gm  -np $nnodes -machinefile $confile $APPLICATION $RUNFLAGS"
echo Starting job at `date`
time mpirun.ch_gm  -np $nnodes -machinefile $confile $APPLICATION $RUNFLAGS > $run_name.screen 
echo ... finished at `date`

# Clean up at the end
rm -rf $confile