#! /bin/sh
#
# Run the tests for the Jacobi-like iteration, including the profile files
#
#MACH=anlsp2
MACH=`uname -s`
Show=eval
OPT="-O"
donopoll=1
do_html=0
#PATH=/usr/local/mpi/lib/rs6000/ch_mpl:$PATH
#PATH=/home/MPI/mpich/lib/rs6000/ch_mpl:$PATH
#export PATH
NPLIST="01 04 16 32 64"
MLIST="5 4096"
do_base=0
do_log=0
do_update=0
mpirunargs=""
# in Words
maxmempernode=8000000
#
for arg in "$@" ; do
    case $arg in 
	-echo) set -x ;;
        -np=*) NPLIST=`echo a$arg | sed "s/a-np=//"` ;;
	-mlist=*) MLIST=`echo a$arg | sed "s/a-mlist=//"` ;;
	-base) do_base=1 ;;
	-show) Show=echo ;;
	-logs) do_log=1 ;;
	-update) do_update=1 ;;
	-nopoll) mpirunargs="-nopoll" ;;
	-html) do_html=1 ;;
	-mach=*) MACH=`echo a$arg | sed "s/a-mach=//"` ;;
	-maxmem=*) maxmempernode=`echo a$arg | sed "s/a-maxmem=//"` ;;
        *) echo "Unrecognized argument $arg" ; exit 1 ;;
    esac
done
#
# By using constant length strings, the output can be more easily
# sorted.
# Originally, this did a cross product of np, n, m, leading to 
# 5 * 5 * 3 = 75 runs for EACH case.  This has been trimmed by
#    Running scaling test (over and under rendezvous limit)
#    Keeping work per processor LOW (accentuate communication problems)
# These test are then:
# (same NP range)
#    n = np * 2 + 2
#    m = eager, rendezvous
if [ $do_base = 1 ] ; then 
    # Just run the basic programs, though at several sizes and numbers
    # of processors
    curdir=`pwd`
    for dir in . `cat related.lst` ; do 
	cd $dir 
	if [ $do_html = 1 ] ; then 
	    echo "<H2>`cat title.txt`</H2>"
	    echo "<PRE>"
	else 
	    cat title.txt
	fi
	if [ $do_log = 1 ] ; then
	    $Show make jacobi.log
	else
            $Show make jacobi
	fi
        for NP in $NPLIST ; do
	    if [ -n "$MPIEXMPL_MAX_NP" ] ; then 
		if [ $NP -gt $MPIEXMPL_MAX_NP ] ; then 
		    continue
		fi
	    fi
	    maxn=`expr $NP \* 2 + 2`
	    for mbase in $MLIST ; do 
		mempernode=`expr \( \( $maxn - 2 \) / $NP \) \* $mbase`
		if [ $mempernode -gt $maxmempernode ] ; then
		    continue
		fi
		maxm=`expr $mbase + 2`
		if [ $do_log = 1 ] ; then
		     if [ $do_update = 0 -o \
			! -s jacobi.$MACH-$NP-$maxm-$maxn.alog ] ; then
			$Show mpirun $mpirunargs -np $NP -mvhome -maxtime 5 \
			  -mvback jacobi.log_profile.log jacobi.log \
			  -maxit 25 -m $maxm -n $maxn 
			$Show /bin/mv jacobi.log_profile.log \
			  jacobi.$MACH-$NP-$maxm-$maxn.alog
			fi
		else
		     $Show mpirun $mpirunargs -np $NP -mvhome -maxtime 5 jacobi \
			-maxit 25 -m $maxm -n $maxn
		fi
	    done
	done
	if [ $do_html = 1 ] ; then
	    echo "</PRE>"
	    # Generate the links to the log files
	    if [ $do_log = 1 ] ; then
	        echo "<UL>"
		for NP in $NPLIST ; do
		    maxn=`expr $NP \* 2 + 2`
		    for mbase in $MLIST ; do 
			maxm=`expr $mbase + 2`
			if [ -s jacobi.$MACH-$NP-$maxm-$maxn.alog ] ; then
			    echo "<LI><A HREF="'"'"jacobi/C/$dir/jacobi.$MACH-$NP-$maxm-$maxn.alog"'"'">Logs for jacobi -np $NP -m $maxm -n $maxn</A>"
			fi
		    done
		done
	        echo "</UL>"
	    fi
	fi
        cd $curdir
    done
    exit 0
fi
#
# The rest of this creates the profiles (.alog files)
for NP in $NPLIST ; do
    for nbase in 0064 0256 1024 4096 ; do
	MAXN=`expr $nbase + 2`
	if [ $MAXN -lt 100 ] ; then
	    MAXN="00$MAXN"
	elif [ $MAXN -lt 1000 ] ; then
	    MAXN="0$MAXN"
	fi
	MAXM=$MAXN
	echo "Test for $NP processes and $MAXN mesh size"
        make doall MACH=$MACH OPT=$OPT NP=$NP MAXM=$MAXM MAXN=$MAXN
	if [ $donopoll = 1 ] ; then
 	    make doallnopoll MACH=$MACH OPT=$OPT NP=$NP MAXM=$MAXM MAXN=$MAXN
        fi
        make doallexplicit MACH=$MACH OPT=$OPT NP=$NP MAXM=$MAXM MAXN=$MAXN
    done
done
