#! /bin/sh
# This script is generated by configure for the purpose of reporting on
# the port of MPICH to a new architecture. This does ONLY the performance
# test runs.
#
# To simplify testing and a few special cases, this script takes a few options
# -author="name"        Use instead of 'whoami'
# -nomake               Do not run make
# -noperf               Do not run performance tests
# -file=name            Output LaTeX file (default is doc/doc1.tex)
# -plotonly             Run gnuplot only to create output files
# -testdir=dir          Location of output files from mpptest runs
# -destdir=dir          Location of generated files (by default, uses doc dir)
#                       Should be either relative to testdir or (better)
#                       absolute.
author="$LOGNAME"
nomake=0
noperf=0
plotonly=0
filename="doc/doc1perf.tex"
fileroot="doc1"
testdir=examples/perftest
destdir=../../doc
MAKE="make --no-print-directory"
for arg in "$@" ; do
    case $arg in 
	-author=* )
	author="`echo $arg|sed -e 's/-author=//' -e 's/_/ /'`"
	;;
	-file=* )
	filebase=`echo $arg|sed 's/-file=//'`
        filename="${filebase}perf.tex"
	fileroot="`basename $filebase`"
	;;
 	-nomake )
	nomake=1
	;;
 	-noperf )
	noperf=1
	;;
	-plotonly )
	plotonly=1
	;;
	-testdir=* )
	testdir="`echo a$arg|sed -e 's/a-testdir=//'`"
	;;
	-destdir=* )
	destdir="`echo a$arg|sed -e 's/a-destdir=//'`"
	;;
	-echo)
	set -x
	;;
	*)
	if test -n "$arg" ; then
	    echo "portperf: Unknown argument ($arg)"
	    exit 1
	fi
	;;
    esac
done
#
# RefineGnuplotfile outputname (e.g., RefineGnuplotfile long)
RefineGnuplotfile() {
    if test ! -s mppout.gpl ; then 
        echo "Failed to run performance test!"
    else
	echo "set terminal postscript eps" | cat - $1.gpl > .foo
	echo "exit" | cat .foo - | sed -e '/pause/d' -e '/clear/d' | \
		sed -e "s/mppout/$1-data/g" > $1.gpl
	mv mppout.gpl $1-data.gpl 
    fi
    }

Bverbatim() {
cat >>$1 <<.
\begin{verbatim}
.
}
Everbatim() {
cat >>$1 <<.
\end{verbatim}
.
}

# Remove the old filename
if [ $plotonly = 0 ] ; then
    /bin/rm -f $filename
    if test -z "/usr/rels/mpich-1.2..4pre2/mpe" ; then 
        cat >> $filename <<.
\section{Performance}
The performance test programs require the MPE library.  Reconfigure MPICH
with {\tt -mpe} and rebuild MPICH (you should be able to just run make in the
{\tt mpe} directory, then re-run {\tt port}, adding the {\tt -nomake} 
option).
.
    exit 0
    fi
fi
#
############################################################################
# Some initial comments...
# sh is not reliable about processing redirects of stdin within if...else..fi
# For such cases, we place the output text within a routine, defined
# outside of the if..else..fi, and called conditionally.
############################################################################
############################################################################
# Performance of the system
############################################################################
#
# Put the header in in case there were problems.
if [ $plotonly = 0 ] ; then 
    cat >> $filename <<.
\section{Performance}
The performance of a message passing system is often measured in terms of the
bandwidth and latency for a point-to-point message.  This section reports the
performance of the MPICH version of MPI for a simple ping-pong test between
two nodes.  The time reported is one-half the round-trip time.  
.
fi
############################################################################
# Run the performance tests
############################################################################
#
# i860's don't have which...
# AIX seems to mishandle which.  So, instead of using which to find gnuplot,
# we use the usual "search in user path code"
if test $noperf = 0 ; then
    # Try to find gnuplot.
    IFS="${IFS= 	}"; saveifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
      test -z "$dir" && dir=.
      if test -f $dir/gnuplot; then
        gnuplotloc=$dir/gnuplot
        break
      fi
    done
    IFS="$saveifs"
    # End of code to find executable gnuplot
#    
#    gnuplotloc=`which gnuplot`

    # We can generate the output graphs...
   if test "$plotonly" = 0 ; then
	curdir=`pwd`
	cd $testdir
	# Remove the data files in case we fail
	/bin/rm -rf long.gpl long.err 
	/bin/rm -rf short.gpl short.err 
	/bin/rm -f maketst.log 
 	$MAKE mpptest goptest > maketst.log 2>&1
	
 	if test ! -x mpptest ; then 
	    echo " " >> $filename
	    echo "Failed to build performance test program!" >> $filename
	    if test -s maketst.log ; then
	        echo "Output of \\code{make mpptest goptest} is" >> $filename
	        Bverbatim $filename
	        cat maketst.log >> $filename
	        Everbatim $filename
	    else
		echo "No output from \\code{make mpptest goptest}!" >> \
			$filename
	    fi
	    exit 1
	fi
	mpirun -np 2 -mvhome -mvback mppout.gpl -arch=LINUX mpptest -gnuplot \
		-tgoal 0.1 -auto -givdy > short.gpl 2>short.err
	RefineGnuplotfile "short"
	#
        # Don't use the auto feature for long data.
	mpirun -np 2 -mvhome -mvback mppout.gpl -arch=LINUX mpptest -gnuplot \
	   -tgoal 0.2 -reps 25 -size 16384 65536 2048 \
		-givedy > long.gpl 2>long.err
	RefineGnuplotfile "long"
	#
	# The collective routines need to aggregate over several values of
	# -np ... 
#	/bin/rm -f sync.gpl sync.out
#	for np in 2 4 8 16 ; do
#	    mpirun -np $np -mvhome -mvback mppout.gpl -arch=LINUX goptest \
#			-gnuplot -sync > sync.gpl
#	    cat mppout.gpl >> sync.out
#	done
#        mv sync.out mppout.gpl
#	RefineGnuplotfile "sync"
#	#
#	mpirun -np 4 -mvhome -mvback mppout.gpl -arch=LINUX goptest -gnuplot \
#		-dsum > dsum4.gpl
#	RefineGnuplotfile "dsum4"
#	mpirun -np 8 -mvhome -mvback mppout.gpl -arch=LINUX goptest -gnuplot \
#		-dsum > dsum8.gpl
#	RefineGnuplotfile "dsum8"
#	#
#	mpirun -np 4 -mvhome -mvback mppout.gpl -arch=LINUX mpptest -gnuplot \
#		-bisect -givedy > bisect.gpl
#	RefineGnuplotfile "bisect"
	cd $curdir
    fi
    # 
    # Test that we generated the data.  If not, try to determine why
    #
    if test ! -s $testdir/short-data.gpl -o \
            ! -s $testdir/long-data.gpl ; then
	echo " " >> $filename
	echo "Could not generate the performance test data!" >> $filename
	if test -s $testdir/short.gpl ; then 
	    echo "The output from the short message test was" >> $filename
	    Bverbatim $filename
	    cat $testdir/short.gpl >> $filename
	    Everbatim $filename
	else
	    echo "There was no output from the short message test!" >> \
		$filename
	fi
	if test -s $testdir/long.gpl ; then 
	    echo "The output from the long message test was" >> $filename
	    Bverbatim $filename
	    cat $testdir/long.gpl >> $filename
	    Everbatim $filename
	else
	    echo "There was no output from the long message test!" >> \
		$filename
	fi
	if test -s $testdir/maketst.log ; then
            echo \
	"The output of the make to build the performance test program was" \
			>> $filename
    	    Bverbatim $filename
   	    cat $testdir/maketst.log >> $filename
    	    Everbatim $filename
	else
	    echo "There was no output from the make of mpptest!" >> $filename
	    echo "My current directory is \\code{`pwd`}." >> $filename
	fi
    	if test -s $testdir/short.err ; then
	    echo "The error output of short message test was" >> $filename
            Bverbatim $filename
	    cat $testdir/short.err >>$filename
  	    Everbatim $filename
    	fi
    	if test -s $testdir/long.err ; then
	    echo "The error output of long message test was" >> $filename
            Bverbatim $filename
	    cat $testdir/long.err >>$filename
  	    Everbatim $filename
    	fi
        exit 0
    elif test -f "$gnuplotloc" ; then
        (cd $testdir ; \
	/bin/rm -f short.eps long.eps ; \
        /bin/rm -f $destdir/short.eps $destdir/long.eps ; \
        gnuplot short.gpl > short.eps ; \
        gnuplot long.gpl > long.eps ; \
        mv short.eps $destdir ; mv long.eps $destdir ; )
    fi
fi
if test "$plotonly" = 1 ; then
    exit 0
fi
#
# Extract the performance numbers
if test -s $testdir/short.gpl ; then
    latency="`grep 'startup' $testdir/short.gpl | cut -d' ' -f 4,5`"
    latency="For short messages, the latency is $latency."
else
    latency=""
fi
if test -s $testdir/long.gpl ; then
    rate="`grep 'startup' $testdir/long.gpl | cut -d' ' -f 10,11`"
    rate="For long messages, the transfer rate is $rate."
else
    rate=""
fi

cat >> $filename <<.
Figure \ref{fig-short} shows the performance for short messages.  The dashed
line represents a least-squares (or linear regression) fit to the data with
model \(s + r x\), where \(x\) is the length of the message in bytes, \(s\)
is the latency, and \(r\) is the time to transfer a byte (the inverse of the
transfer rate).  The coefficients for this model are shown in the upper
right-hand corner.  $latency
.

cat >> $filename <<.
\begin{figure}
\newread\graphfilein
\openin\graphfilein=short.eps
\ifeof\graphfilein
\centerline{No graph available}
\else
\closein\graphfilein
\centerline{\epsfbox{short.eps}}
\fi
\caption{Time for short messages}\label{fig-short}
\end{figure}

In Figure \ref{fig-long} we show the time taken by messages of intermediate
length.  These results give a better idea of the effective bandwidth for many
applications.  $rate
Care should, however, be taken when using these results, since
some systems do not reach their full (asymptotic) bandwidth until very large
messages are used. 
In addition, these results measure only the performance of the MPICH
implementation of MPI, not the achievable performance of the MPI standard.

\begin{figure}
\newread\graphfilein
\openin\graphfilein=long.eps
\ifeof\graphfilein
\centerline{No graph available}
\else
\closein\graphfilein
\centerline{\epsfbox{long.eps}}
\fi
\caption{Time for long messages}\label{fig-long}
\end{figure}
.
