#!/bin/bash


#
# Usage:  speedup2 [--log] [--inv] reference-xxxMHz cpu1-yyyMHz cpu2-zzzMHz....
#
# it will plot how much fater cpu1 and cpu2 is compared to reference CPU
# [--log] turns on the logarithmical Y-scale. It plots both the fast and
# slow versions of cpu1 and cpu2 but it compares it to the best result
# of reference cpu, be it the fast or the slow version.
# [--inv] will show slowdown which allows to use the fastest machine as
# a reference which is better as faster machines usually have wider
# x-axis range, so they do not limit the span of the machines we compare to
#

do_plot()
{
  fname=$1 ; shift
  ylabel=$1 ; shift
  clocks=$1 ; shift
  (
    cat <<-
      set grid
      set title "Sieve of Eratosthenes Benchmark"
      set logscale x
      set mxtics 10
      set mytics 10
      set xlabel "sieve length in bytes (i.e. the max tested number / 8)"
      set key left Left
      set terminal postscript eps color solid "Helvetica-Bold" 
      set output "$fname"
      set ylabel "$ylabel"
      min(a,b)=a<b?a:b

    if [ $invsw = 1 ] ; then 
      echo "fn(x)=x"
    else
      echo "fn(x)=1/x"
    fi
    if [ $logsw = 1 ] ; then
      echo set logscale y
      echo -n "p [:] "
    else
      echo -n "p [:] [0:] "
    fi
    cat <<-
      1 title "$reference best(slow,fast)" with lines, \\

    ct=0;
    reference_freq=$( echo $reference | tr 'M-' '\n\n' | tail -2 | head -1 )
    grep -v ^\# $reference | tr '#' ' ' > /tmp/plot_speedup_ref
    
    for i in $* ; do 
      freq=$( echo $i | tr 'M-' '\n\n' | tail -2 | head -1 )
      grep -v ^\# $i | tr '#' ' ' > /tmp/plot_speedup
      
      ( sort -n -m -b --key=1 -s /tmp/plot_speedup /tmp/plot_speedup_ref ; echo EOF ) | (
        bts0=none
        while true ; do
          read bts1 sspd1 fspd1 inr1
          if [ "$bts1" = EOF ] ; then break ; fi
          if [ "$bts1" = "$bts0" ] ; then
            echo $bts0 $sspd0 $sspd1 $fspd0 $fspd1
          fi
          bts0=$bts1;
          sspd0=$sspd1;
          fspd0=$fspd1;
        done
      ) > /tmp/plot_speedup-$i

      echo -n \"/tmp/plot_speedup-$i\" using '($1):(fn((min($2,$4)/min($3,$5))'
      if $clocks ; then
        echo -n "*($freq/(1.0*$reference_freq))"
      fi
      echo '))' title \"$i\" with lines, \\
            
      if [ $ct -eq 3 ] ; then   # to skip yellow color
        echo "log(0) notitle, \\"
      fi
      ct=$(( ct + 1 ))
    done 
    echo "log(0) notitle"
    ) | gnuplot
  gv $fname &
  ps2pdf14 -sPAPERSIZE=a4 $fname &
}

logsw=0
if [ "$1". = --log. ] ; then
  logsw=1;
  shift 1
fi
invsw=0
if [  "$1". = --inv. ] ; then
  invsw=1
  shift 1
fi
reference=$1 
shift 1

if [ $invsw = 0 ] ; then
  do_plot absolute_speedup.eps "Absolute speedup over $reference" false $*
  do_plot clock_speedup.eps "Instruction per clock speedup over $reference" true $*
else
  do_plot absolute_speedup.eps "Absolute slowdown over $reference" false $*
  do_plot clock_speedup.eps "Instruction per clock slowdown over $reference" true $*
fi

