#! /bin/tcsh

##### User adaptable entries

## Set directory for output files
set outdir = "outputdir"

## Set the number of processors we wanna run on
set procnumber = 5

## Set the number of runs to process
set runnumber = 100

## Program to run
set programname = "ALTest"

## Program to run arguments
set programargs = "-size 25000 -min 0 -max 40000 -inc 200"

## Set the command line to run your MPI program
set mpirun = "mpirun -np $procnumber $programname $programargs"

## Set the pause command to use in case of needed re-run
set runpause = "sleep 100"

## Set log file base
set logfb = "ALlog."

## Benchmark temporary output file base
set altmpfb = "ALout."

## Benchmark script to launch
set albench = "al_bench.pl"

## Options to use for Benchmark script
set albenchopt = "-transfer -split"

## Set Final Analyze filename ("" for stdout)
set resultfn = "result"
#set resultfn = ""

## Result Analyzing script
set alanalyze = "al_packettune.pl"

## Options to use for Analyze script
set alanalyzeopt = ""

##### End of user adaptable entries

##### Changes thoses entries at your own risks
set _mkdir = "mkdir"
set _mv = "mv"
set _rm = "rm"
set _cp = "cp"

##### Script

set usage = "$0 [-help] [-verb] [-phase1] [-phase2]"
set scriptversion = "AutoLink v2.11.2 Benchmark Analyze Runner v0.8\nInitial Release by Martial MICHEL (martial.michel@nist.gov), 2/99"

## Arg processing
foreach _arg ( $argv )
    if ("$_arg" == "-help") then
	goto HELP
    else if ("$_arg" == "-verb") then
	set _verb
    else if ("$_arg" == "-phase1") then
	set _phase1
    else if ("$_arg" == "-phase2") then
	set _phase2
    else
	echo "Error : wrong argument ( $_arg )\n\n$usage"
	goto END
    endif
end

if ($?_verb) echo "***** $scriptversion"

if ($?_phase2) then
    goto PHASE2
endif

PHASE1:
if (! -d "$outdir") then
    if ($?_verb) echo " * Creating temporary directory ( $outdir )"
    set run = `$_mkdir $outdir`
endif

@ inc = 0
@ lastlog = $procnumber - 1

while ($inc < $runnumber)

## MPI run
    RUN:
    if ($?_verb) then 
	echo "***** Run $inc (of $runnumber)"
	echo " - Execute ( $mpirun )"
    endif
    set run = `$mpirun`
    if (-f $logfb$lastlog) then
	set tmp = `grep AL_Finalize $logfb$lastlog`
    else
	set tmp = ""
    endif
    if ( "$tmp" == "") then
	if ($?_verb) echo " - Pause ( $runpause )"
	set run = `$runpause`
	goto RUN
    endif

## First script
    set albenchrun = "$albench $albenchopt $procnumber $altmpfb$inc. $logfb"
    if ($?_verb) echo " - Execute ( $albenchrun )"
    set run = `$albenchrun`

    if ($?_verb) echo " - Cleaning outputfiles"
## Move output files
    set run = `$_mv $altmpfb$inc* $outdir`
## Remove created logs
    set run = `$_rm $logfb*`

    @ inc ++
end

if (($?_phase1) && (! $?_phase2)) then
    goto END
endif

PHASE2:
set currentdir = `pwd`
cd $outdir
if ($?_verb) echo "***** Analyzing results"
if ("$resultfn" != "") then
    set alanalyzerun = "$currentdir/$alanalyze $alanalyzeopt -output $resultfn $procnumber $runnumber $altmpfb"
else
    set alanalyzerun = "$currentdir/$alanalyze $alanalyzeopt $procnumber $runnumber $altmpfb"
endif
if ($?_verb) echo " - Execute ( $alanalyzerun )"
set run = `$alanalyzerun`

if ("$resultfn" == "") then
    if ($?_verb) echo " - Showing result"
    echo $run
else
    if ($?_verb) echo " - Moving result file ( $resultfn ) to $currentdir"
    set run = `$_mv $resultfn $currentdir`
endif

END:
exit


HELP:
echo "$usage"
echo "  -help : this help page"
echo "  -verb : run in verbose mode"
echo "  -phase1 : run only phase1"
echo "  -phase2 : run only phase2"
echo "[phase1]"
echo " It is the phase when the run files are generated."
echo " It will run the program, analyze each result and store it for further analyze"
echo "[phase2]"
echo " It is the phase when the run files are analyzed."
echo " It will run the analyze script and extract the PacketSize information"
echo "[Script user configurable variables]"
echo " outdir : Set run files directory"
echo " procnumber : Number of processors to run on"
echo " runnumber : Number of runs to process"
echo " programname : Program to run to create the run files"
echo " mpirun : Command line to run MPI"
echo " runpause : Pause command in case of need of re-run of MPI"
echo " logfb : Log file base (note : must be consistent to define in al_common.h)"
echo " altmpfb : run file temporary file base"
echo " albench : Benchmark script to run"
echo " albenchopt : Options for benchmark script"
echo " resultfn : final analyze filename"
echo " alanalyze : PacketSize tuning script to launch"
echo " alanalyzeopt : Options for PacketSize tuning script"
