#!/bin/tcsh -f
#
# choose_strategy_by_simulation <cpus> <recording> [<options>]
#
# Runs the thread simulation on the given recording for the given
# number of CPUs for ACTIVE, KEEP and DEPTH parallelization strategies.
# Examines the results of the simuation's evaluation heuristics and 
# recommends a strategy accordingly if it's evaluated OK, otherwise
# increases the strategy's parameter up to a maximum of 20.
# The simulated number of steps is used to rank the three strategy
# classes.
#
# The recording file has to be in the properly filtered format.
#
# Stefan Haenssgen  05-feb-98
#
# 05-feb-98	1st version
#

set simulation=simulation	# Name of simulation executable
set tmpfile=/tmp/cstrat_$$

if ($#argv < 2) then
  echo "$0 - need two parameters (cpu number and recording file)"
  echo "  and optionally the simulation options"
  echo "  e.g.: $0 4 recorded_barnes_16384_filtered '-t B'"
  exit 1
endif

set cpus=$1			# Read parameters
shift
set recfile=$1
if (! -f $recfile) then
  echo "Error, couldn't open recording $recfile"
  exit 2
endif
shift
set options="$*"

echo "Performing simulations for $cpus CPUs with recording"
echo "  $recfile and options '$options'"
echo ""

set classnames=(keep active depth)
set classabrevs=(-k -a -d)
set simparams=(0 0 0)
set simsteps=(0 0 0)

# Run simulations for each class until satisfactory evaluation heuristic
# results or maximum parameter value reached.
#
foreach class (1 2 3)

  set param=1				# Strategy paramameter
  while ( $param <= 20 )

    # Run simulation
    #
    echo "Simulating $classnames[$class] $param ..."
    $simulation -q $classabrevs[$class] -c $cpus -p $param $options \
       < $recfile > $tmpfile

    # Extract results from simulation's output, h1-6 are the
    # different heuristics (h6, load, can be ignored)
    #
    set steps=`fgrep "steps elapsed" $tmpfile | cut -d' ' -f1`
    set h1=`fgrep "Not less threads than CPUs" $tmpfile | cut -d':' -f2`
    set h2=`fgrep "Node percentage of largest" $tmpfile | cut -d':' -f2`
    set h3=`fgrep "Leaf percentage of largest" $tmpfile | cut -d':' -f2`
    set h4=`fgrep "Subnode percentage of largest" $tmpfile | cut -d':' -f2`
    set h5=`fgrep "Subleaf percentage of largest" $tmpfile | cut -d':' -f2`
    set h6=`fgrep "Each CPU used at least" $tmpfile | cut -d':' -f2`
    #echo $h1 $h2 $h3 $h4 $h5 $h6
    rm -f $tmpfile
    if ( ($h1 == "OK") && ($h2 == "OK") && \
         ($h3 == "OK") && ($h4 == "OK") ) then
      break
    endif
    set param=`expr $param + 1`

  end #while

  # Remember recommendation and corresponding number of steps,
  # resp. mark as unsuccessful if no parameter recommended
  #
  if ( $param <= 20 ) then
    echo "Recommended: $classnames[$class] $param with $steps steps"
  else
    echo "No $classnames[$class] strategy recommended at all"
    set param=0
    set steps=9999999999999
  endif
  set simparams[$class]=$param
  set simsteps[$class]=$steps

end # class

echo ""
echo "Overall ranking:"
set sk=$simsteps[1]			# Look at elapsed steps
set sa=$simsteps[2]
set sd=$simsteps[3]
set pk=$simparams[1]
set pa=$simparams[2]
set pd=$simparams[3]
if ($pk == 0) then			# Build result strings
  set nk="[Keep not recommended]"
else
  set nk="Keep $pk    ($sk steps)"
endif
if ($pa == 0) then
  set na="[Active not recommended]"
else
  set na="Active $pa  ($sa steps)"
endif
if ($pd == 0) then
  set nd="[Depth not recommended]"
else
  set nd="Depth $pd   ($sd steps)"
endif
if ($sk < $sa) then			# Sort the three classes and
  if ($sk < $sd) then			#  output ranking
    if ($sa < $sd) then
      echo "Rank 1: $nk"		# k < a < d
      echo "Rank 2: $na"
      echo "Rank 3: $nd"
    else # sd <= sa
      echo "Rank 1: $nk"		# k < d < a
      echo "Rank 2: $nd"
      echo "Rank 3: $na"
    endif
  else # sd<=sk
    if ($sa < $sd) then
      echo "Rank 1: $nk"		# =
      echo "Rank 2: $na"
      echo "Rank 3: $nd"
    else # sd <= sa
      echo "Rank 1: $nd"		# d < k < a
      echo "Rank 2: $nk"
      echo "Rank 3: $na"
    endif
  endif
else # sa <= sk
  if ($sk < $sd) then
    if ($sa < $sd) then
      echo "Rank 1: $na"		# a < k < d
      echo "Rank 2: $nk"
      echo "Rank 3: $nd"
    else # sd <= sa
      echo "Rank 1: $na"		# =
      echo "Rank 2: $nd"
      echo "Rank 3: $nk"
    endif
  else # sd<=sk
    if ($sa < $sd) then
      echo "Rank 1: $na"		# a < d < k
      echo "Rank 2: $nd"
      echo "Rank 3: $nk"
    else # sd <= sa
      echo "Rank 1: $nd"		# d < a < k
      echo "Rank 2: $na"
      echo "Rank 3: $nk"
    endif
  endif
endif

