#!/bin/csh -f

if ( $#argv < 3 ) then
  echo "Usage: $0 program_name run_directory num_cpus1 [num_cpus2 ...]"
  exit 1
endif

set PROG=$1
set RUNDIR=$2
shift
shift
set numcpus="$*"

if ( ! -d $PROG ) then
  echo "Executable directory '`pwd`/$PROG' does not exist. Check config."
  exit 2
endif

if ( ! -d $RUNDIR ) then
  echo "Run directory '$RUNDIR' does not exist. Trying to create it ..."
  mkdir $RUNDIR || exit 3
endif

cd $PROG || exit 4

set EXEDIR=`pwd`
set files=`ls`

cd $RUNDIR || exit 5
if ( -d $PROG ) then
  if ( -d old.$PROG ) then
    /bin/rm -rf old.$PROG
  endif
  mv $PROG old.$PROG
endif
mkdir $PROG
cd $PROG || exit 5
set RUNDIR=$RUNDIR/$PROG

foreach NUMCPUS ( $numcpus ) 

echo "Running $PROG with $NUMCPUS processors ..."

/bin/rm -rf * >& /dev/null

foreach FILE ( $files )
  if ( -f $EXEDIR/$FILE ) then
    cp $EXEDIR/$FILE .
  endif
end

pwd
ls -l

${PROG}.paral.sh $NUMCPUS < /dev/null

set OUTFILE=${PROG}.ncpus$NUMCPUS

if ( -f ${OUTFILE} ) then
  cp ${OUTFILE} $EXEDIR
  set timing=`tail -3 ${OUTFILE}`
  echo ">>>${PROG}@${NUMCPUS}: $timing"
else
  echo "Can't find the output file '${OUTFILE}' after ${PROG}'s execution"
  exit 6
endif

end

exit 0





