#!/bin/ksh
# $Id: CPU_time_sx8brodie.ksh 1301 2009-02-09 17:15:38Z ctlod $
##############################################################
# Original : C. Talandier for NEMO team
# Contact  : nemo_st@locean-ipsl.upmc.fr
#
# It is called by the assessment.ksh script for a 
# given configuration 
#
# INPUT ARGS: 6
#     - zrunt  : run type to treat
#     - zfile1 : mon run timing input file name
#     - zfile2 : mpi run timing input file name
#     - zout   : output file name
#     - ztag   : tag name to which are compared files
#     - zxis   : (0/1) reference timing files (do/do not) exist
#
# WORK: It aims to :
#
#      - Treat output timing from the sx8brodie target for 
#        mon, mpi & omp runs
#     *- compare it to a reference version 
#
# * this step is performed only if a reference tag 
#   is specified with the -t option and if files exist.
#   See the Makefile under ./config/NVTK directory
#
##############################################################
#set -xv
##############################################################

# Get the name of the present script
nscript=$( (basename $0) )

#
# help_CPU_time() To print help
#
function help_CPU_time
{
  echo "Usage: $nscript [-r] [-a] [-b] [-o] [-t] [-x]"
  echo "Options: These are optional argument"
  echo " -r : run type to treat"
  echo " -a : current version timing input file name"
  echo " -b : reference version timing input file name"
  echo " -o : output file name"
  echo " -t : tag name to which are compared files"
  echo " -x : (0/1) reference timing files (do/do not) exist"
  echo " their values are not taken)"
  exit 1
}

# Initialization
zrunt=
zfile1=
zfile2=
zout=
ztag=
zxis=1

# Get arguments
while getopts r:a:b:o:t:x: opt
do
  case "$opt" in
    r) zrunt="$OPTARG";;
    a) zfile1="$OPTARG";;
    b) zfile2="$OPTARG";;
    o) zout="$OPTARG";;
    t) ztag="$OPTARG";;
    x) zxis="$OPTARG";;
    *) help_CPU_time;;
  esac
done

# Check if results must be compare to the reference version
test -n "${ztag}"
evaltag=$?

case "${zrunt}" in

     'mon' | 'omp' )

           # OUTPUT TIMING OF THE MON/OMP RUN
           ###################################
           
           if [ ${evaltag} == 0 -a ${zxis} == 0 ] ; then
               # Get values from the reference tag
               zreal=$( (awk '/Real/        {print $NF}' ${zfile2} ) )
               zuser=$( (awk '/User/        {print $NF}' ${zfile2} ) )
               zmflo=$( (awk '/MFLOPS/ && ! /concurrent/      {print $NF}' ${zfile2} ) )
               [ ${zrunt} = 'omp' ] && zmfli=$( (awk '/MFLOPS/ &&   /concurrent/      {print $NF}' ${zfile2} ) )
               [ ${zrunt} = 'omp' ] && zmprc=$( (awk '/Max Concurrent Proc/           {print $NF}' ${zfile2} ) )
               zvlen=$( (awk '/VLEN/        {print $NF}' ${zfile2} ) )
               zmemo=$( (awk '/Memory Size/ {print $NF}' ${zfile2} ) )
               # Print: Variable long name | current version values | reference version values | variations
               echo "                                Current version    ${ztag} version    Variation " >> ${zout}
               awk '/Real/         { creal=$5 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((creal-invar)/invar)*100 ) }' invar=${zreal} ${zfile1} >> ${zout}
               awk '/User/         { cuser=$5 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((cuser-invar)/invar)*100 ) }' invar=${zuser} ${zfile1} >> ${zout}
               awk '/MFLOPS/ && ! /concurrent/      { cmflo=$3 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((cmflo-invar)/invar)*100 ) }' invar=${zmflo} ${zfile1} >> ${zout}
               [ ${zrunt} = 'omp' ] && awk '/MFLOPS/ &&   /concurrent/      { cmfli=$4 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((cmfli-invar)/invar)*100 ) }' invar=${zmfli} ${zfile1} >> ${zout}
               awk '/VLEN/         { cvlen=$3 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((cvlen-invar)/invar)*100 ) }' invar=${zvlen} ${zfile1} >> ${zout}
               awk '/Memory Size/  { cmemo=$5 ; printf("%-5s %.25s  %10.2f      | %10.2f    |   %7.2f % \n", " ", $0, $NF, invar, ((cmemo-invar)/invar)*100 ) }' invar=${zmemo} ${zfile1} >> ${zout} 
               [ ${zrunt} = 'omp' ] && awk '/Max Concurrent Proc/  { cmprc=$5 ; printf("%-5s %.25s  %10i      | %10i    |   %7i \n", " ", $0, $NF, invar, (cmprc-invar) ) }' invar=${zmprc} ${zfile1} >> ${zout} 
           else
               # Print: Variable long name | current version values
               echo "                                 Current version " >> ${zout}
               awk '/Real/        {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               awk '/User/        {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               awk '/MFLOPS/ && ! /concurrent/     {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               [ ${zrunt} = 'omp' ] && awk '/MFLOPS/ &&   /concurrent/     {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               awk '/VLEN/        {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               awk '/Memory Size/ {printf("%-5s %.25s  %10.2f\n", " ", $0, $NF ) }' ${zfile1} >> ${zout}
               [ ${zrunt} = 'omp' ] && awk '/Max Concurrent Proc/  { cmprc=$5 ; printf("%-5s %.25s  %10i\n", " ", $0, $NF ) }' ${zfile1} >> ${zout} 
           fi 

           ;;

     'mpi' )
           # OUTPUT TIMING OF THE MPI RUN
           ##############################
           
           if [ ${evaltag} == 0 -a ${zxis} == 0 ] ; then
               # Get reference tag information
               # Syntax: 1) search pattern between // symbol, 2) print text of the last column (identified by $NF)
               zreal=$( (awk '( /\[/ && /\]/ ) && /Real/                           {print $NF}' ${zfile2} ) )
               zuser=$( (awk '( /\[/ && /\]/ ) && /User/                           {print $NF}' ${zfile2} ) )
               zmflo=$( (awk '( /\[/ && /\]/ ) && /MFLOPS/                         {print $NF}' ${zfile2} ) )
               zvlen=$( (awk '( /\[/ && /\]/ ) && /Average Vector Length/          {print $NF}' ${zfile2} ) )
               zmemo=$( (awk '( /\[/ && /\]/ ) && ! /Global/ && /Memory size used/ {print $NF}' ${zfile2} ) )
           
               # Print: Variable long name | current version values | reference version values | variations
               # Syntax: 1) search pattern between // symbol, 2) print text & values following a format (printf) explicited between "" symbol
               awk '/Global Data/ { printf( "        %.29s  Current version   %s version      Variation \n", $0, tag) }' tag=${ztag} ${zfile1} >> ${zout}
               awk '/==========================/ { printf("        %s \n", $0) }' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Real/                  { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zreal} ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /User/                  { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zuser} ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /MFLOPS/                { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zmflo} ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Average Vector Length/ { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zvlen} ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Memory size used/ && !/Global/ { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zmemo} ${zfile1} >> ${zout}
               echo " " >> ${zout} 
           
               # Get reference tag information (Overall data)
               # Syntax: 1) search pattern between // symbol, 2) print text of the last column (identified by $NF)
               zreal=$( (awk '!( /\[/ && /\]/ ) && /Real/                           {print $NF}' ${zfile2} ) )
               zuser=$( (awk '!( /\[/ && /\]/ ) && ! /OPS/ && /User/                {print $NF}' ${zfile2} ) )
               zgflo=$( (awk '!( /\[/ && /\]/ ) && /GFLOPS/                         {print $NF}' ${zfile2} ) )
               zmemo=$( (awk '!( /\[/ && /\]/ ) && ! /Global/ && /Memory size used/ {print $NF}' ${zfile2} ) )
           
               # Print: Variable long name | current version values 
               # Syntax: 1) search pattern between // symbol, 2) print text & values following a format (printf) explicited between "" symbol
               awk '/Overall Data/ { printf( "        %-28s  Current version   %s version       Variation \n", $0, tag) }' tag=${ztag} ${zfile1} >> ${zout}
               echo "        =============" >> ${zout} 
               awk '!( /\[/ && /\]/ ) && /Real/             { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zreal} ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && ! /OPS/ && /User/  { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zuser} ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && /GFLOPS/           { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zgflo} ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && /Memory size used/ && !/Global/ { printf( "%-7s %.29s  %10.2f     |   %10.2f    |    %7.2f %\n", " ", $0 , $NF, invar, (($NF-invar)/invar)*100 ) }' invar=${zmemo} ${zfile1} >> ${zout}
               echo " " >> ${zout} 
           
           else
           
               # Print: Variable long name | current version values 
               awk '/Global Data/ { printf( "        %.29s   Current version\n ", $0) }' ${zfile1} >> ${zout}
               awk '/==========================/ { printf("%-7s %-.26s \n", " ", $0) } ' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Real/                  { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF) }' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /User/                  { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF) }' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /MFLOPS/                { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF) }' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Average Vector Length/ { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF) }' ${zfile1} >> ${zout}
               awk '( /\[/ && /\]/ ) && /Memory size used/ && ! /Global/ { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF) }' ${zfile2} >> ${zout}
               echo " " >> ${zout} 
           
               # Print: Variable long name | current version values (Overall data)
               awk '/Overall Data/ { printf( "        %-28s   Current version\n ", $0 ) }' ${zfile1} >> ${zout}
               echo "        =============" >> ${zout} 
               awk '!( /\[/ && /\]/ ) && /Real/             { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF ) }' ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && ! /OPS/ && /User/  { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF ) }' ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && /GFLOPS/           { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF ) }' ${zfile1} >> ${zout}
               awk '!( /\[/ && /\]/ ) && /Memory size used/  && ! /Global/ { printf( "%-7s %.29s  %10.2f \n", " ", $0 , $NF ) }' ${zfile1} >> ${zout}
               echo "" >> ${zout} 
           fi
           ;;
esac
