#!/bin/sh
#
# This script is part of ML's benchmarking facility.  It is one of the scripts
# that allows benchmarking to run under the pbs batch scheduling software.
# qbench is called from the script "run_one". 
#
# This script submits the script SCRIPTNAME to the pbs batch system.  The
# script will not finish until the job has completed.
#
# usage: qbench [number_of_processors] 
#
# The value of number_of_processors defaults to one if a value is not supplied.
#
# Note:   It is assumed that number_of_processors is either one or an even
#         number.
#
################################################################################
SCRIPTNAME="mpibench.pbs"
WALLTIME='0:15:00'
################################################################################
#
rm -f nproc_file
temp=`echo xx$1xx | sed "s/ *//g"`
#if test `expr ${temp}` = 'xxxx'
if test `expr ${temp}` = 'xxxx' -o \
        `expr ${temp}` = 'xx1xx'
then
  NNODES=1
  echo ${NNODES} > nproc_file
else
  echo $1 > nproc_file
  NNODES=`expr $1 / 2`
fi
SESSARGS="-l nodes=${NNODES}:ppn=2 -l walltime=${WALLTIME} ${SCRIPT_DIRECTORY}/${SCRIPTNAME}"
#echo "session args: ${SESSARGS}"
JOBID=`qsub ${SESSARGS}`
JOBNUM=`echo ${JOBID} | sed "s/.puffin.ca.sandia.gov//g"`
touch ${JOBID}
#echo "qsub has returned"
while ! test -e ${SCRIPTNAME}.e${JOBNUM}
    do
    sleep 15
done
#echo "job has finished"
sleep 5
rm -f nproc_file
JOBNUM=`echo ${JOBID} | sed "s/.puffin.ca.sandia.gov//g"`
cat ${SCRIPTNAME}.o${JOBNUM} | grep -v "Warning: no access" > xxxxxxx
rm -f ${SCRIPTNAME}.o${JOBNUM}
cat xxxxxxx | grep -v "Thus no job" > ${SCRIPTNAME}.o${JOBNUM}
rm -f xxxxxxx
SCRATCH_SUBDIR=`cat scratch_subdirectory`
cp ${SCRIPTNAME}.o${JOBNUM} ${SCRATCH_SUBDIR}/xxxxxx${ttt}
mv ${SCRIPTNAME}.o${JOBNUM} ${SCRATCH_SUBDIR}/
mv ${SCRIPTNAME}.e${JOBNUM} ${SCRATCH_SUBDIR}/
