#! /bin/sh
#
# Run some of the tests.  If any arguments are provided, pass them to the
# test programs.
#
# -mvhome is needed for the ANL SP, and is ignored by others
args=
device=
top_srcdir=..
srcdir=../pt2pt
MPICH_VERSION=
STOPFILE=${MPITEST_STOPTEST:-"$HOME/.stopmpichtests"}
mpirun=ampirun
MAKE="make"
MPIRUNMVBACK=""
#
# Load basic procedures
. ${top_srcdir}/runbase
#
# Set mpirun to the name/path of the mpirun program
FindMPIRUN
#
# If the programs are not available, run make.
runtests=1
makeeach=0
writesummaryfile=no
check_at_once=1
quiet=0
check_canrun=0
CheckOutputWhileRunning="yes"
# Using shifts should  remove args from the list.
for arg in "$@" ; do
    case $arg in 
	-checkonly)
	shift
	runtests=0
	;;
        -margs=*)
	shift
	margs=`echo $arg | sed 's/-margs=//'`
	args="$args $margs"
	;;
	-summaryfile=*)
	writesummaryfile=yes
	summaryfile=`echo A$arg | sed 's/A-summaryfile=//'`
	;;
	-echo)
	shift
	set -x
	;;
	-check)
	check_canrun=1
	;;
	-quiet)
	shift
	quiet=1
	;;
	-small)
	shift
	makeeach=1
	;;
	-atend)
	shift
	check_at_once=0
	;;
	-help|-u)
	shift
	echo "runtests [-checkonly] [-margs='...'] [-atend] [-check]"
	echo "run tests in this directory.  If -checkonly set, just run"
	echo "the differences check (do NO rerun the test programs)."
	echo "If -margs is used, these options are passed to mpirun."
	echo "If -small is used, the examples are built, run, and deleted."
	echo "If -atend is used, the success of the tests is checked only"
	echo "at the end of the test rather than also after each test."
	echo "If -check is used, only a single simple test is run; this"
	echo "is used to check that mpirun can run an MPI program."
	exit 1
	;;
	*)
	if test -n "$arg" ; then
	    echo "Passing remaining arguments to programs ($*)"
	    break
        fi
	;;
    esac
done

# Do this because we're writing the output while running
savewritesummaryfile=$writesummaryfile
writesummaryfile=no

# If cannot run test, do that and exit
if [ $check_canrun = 1 ] ; then
    # Make sure that we don't have an old file lying around
    rm -f third third.o third.exe
    MakeExe third
    rm -f third.out
    echo '*** Testing Unexpected messages ***' >> third.out
    $mpirun $args -np 2 ./third </dev/null >> third.out 2>&1
    echo '*** Testing Unexpected messages ***' >> third.out
    rm -f third.stdo
    cat >>third.stdo <<EOF
*** Testing Unexpected messages ***
 No Errors
*** Testing Unexpected messages ***
EOF
    if diff -b third.out third.stdo > /dev/null ; then
	rc=0
    else
	echo "Failed to run simple program!"
	echo "Output from run attempt was"
	cat third.out
        echo "mpirun program was $mpirun"
	echo "mpirun command was "
	echo "$mpirun $args -np 2 ./third </dev/null >>third.out 2>&1"
	rc=1
    fi
    CleanExe third
    rm -f third.out 
    exit $rc
fi

# If the programs are not available, run make.
if [ ! -x sendrecv -a $makeeach = 0 -a $runtests = 1 ] ; then
    $MAKE default
fi

testfiles=""
if [ $runtests = 1 ] ; then
echo '**** Testing MPI Point-to-point routines ****'

RunTest sendrecv 2 "**** Testing MPI_Send and MPI_Recv ****" "" "sendrecv-0.out sendrecv-1.out"

RunTest sendrecv2 2 "**** Testing MPI_Send and MPI_Recv (2) ****"

RunTest sendrecv3 2 "**** Testing MPI_Send and MPI_Recv (3) ****"

RunTest sendrecv4 2 "**** Testing MPI_Send and MPI_Recv (4) ****"

RunTest bsendtest 2 "**** Testing MPI_Bsend and MPI_Recv (4) ****" "" "bsendtest-0.out bsendtest-1.out"

RunTest isndrcv 2 "**** Testing MPI_Isend and MPI_Irecv ****" "" "isndrcv-0.out isndrcv-1.out"

RunTest irsend 2 "**** Testing MPI_Irsend ****"

RunTest irsendinit 2 "**** Testing MPI_Rsend_init ****"

RunTest longmsgs 2 "**** Testing long messages ****"

RunTest testsome 2 "**** Testing MPI_Testsome/Testany/Waitsome ****"

RunTest testall 2 "**** Testing MPI_Testall ****"

RunTest cancel 2 "**** Checking MPI_Cancel (Irecv's) ****"

RunTest cancel2 2 "**** Checking MPI_Cancel (Persistent Recv's) ****"

RunTest cancel3 2 "**** Checking MPI_Cancel (Persistent Send's) ****"

RunTest cancelmessages 2 "**** Checking MPI_Cancel (short/eager/rndv) ****"

RunTest cancelibm 2 "**** Checking MPI_Cancel (successful/nonsuccessful) ****"

# This test exposes a SEGV in the MPICH1 code.  However, this is an
# uncommon situtation.  Users who need this feature should switch to 
# MPICH2 (www.mcs.anl.gov/mpi/mpich2)
#RunTest cancelissend 2 "**** Checking MPI_Cancel and MPI_Issend (short msgs) ****"

RunTest sndrcv 2 "**** Testing MPI_Sendrecv ****"

RunTest sndrcvrep 2 "**** Testing MPI_Sendrecv_replace ****"

RunTest sndrcvrpl2 2 "**** Testing MPI_Sendrecv_replace (long) ****"

RunTest getelm 2 "**** Testing MPI_Get_elements ****"

RunTest overtake 2 "**** Verifying that messages are non-overtaking ****" "" "overtake-0.out overtake-1.out"

RunTest ssendtest 2 "**** Verifying ssend ****"

RunTest ssendtest2 2 "**** Verifying ssend (2) ****"

RunTest issendtest 2 "**** Verifying Issend ****" "" "issendtest-1.out"

RunTest issend2 2 "**** Verifying Issend (2) ****"

RunTest reqcreate 1 "**** Checking the request creation routines ****"

RunTest reqfree 2 "**** Checking request free ****" "-req 2048"

RunTest typebase 1 "**** Checking the type (sizes) routines ****"

RunTest typecreate 1 "**** Checking the type creation routines ****"

RunTest typetest 2 "**** Checking the type routines ****" "" "typetest-0.out typetest-1.out"

RunTest typeub 2 "**** Checking the type routines: MPI_UB ****"

RunTest typeub2 1 "**** Checking the type routines: MPI_UB(2) ****"

RunTest typeub3 1 "**** Checking the type routines: MPI_UB(3) ****"

RunTest typelb 1 "**** Checking the type routines: MPI_LB ****"

RunTest structlb 1 "**** Checking Type_struct routines: MPI_LB ****"

RunTest dtypelife 2 "**** Checking the type routines: MPI_Type_free ****"

RunTest dataalign 2 "**** Checking the type alignment ****"

RunTest dtyperecv 2 "**** Checking the type recv ****"

RunTest commit 1 "**** Checking the type routines: MPI_Type_commit ****"

RunTest third 2 "*** Testing Unexpected messages ***"

RunTest fourth 4 "*** Testing Irecv/Isend/Wait ***"

RunTest fifth 4 "*** Testing Irecv/Isend/Waitall ***"

RunTest sixth 2 "*** Testing attribute manipulation ***"

RunTest nblock 4 "*** Testing Isend/Irecv (large numbers) ***"

RunTest nbtest 4 "*** Testing Isend/Probe/Recv (large numbers) ***"

RunTest sendmany 8 "*** Testing Send (many procs) ***"

RunTest order 2 "*** Testing Recv ordering ***"

RunTest sendorder 2 "**** Checking Message Ordering ****"

RunTest exittest 3 "**** Checking Exit Processing ****"

RunTest trunc 2 "*** Testing Message truncation ***"

RunTest truncmult 2 '*** Testing Message trunction in multiple completions ***'

RunTest nullproc 2 "*** Testing handling of MPI_PROC_NULL ***"

RunTest nullproc2 2 "*** Testing handling of MPI_PROC_NULL in blocking Recv ***"

RunTest relrank 2 "*** Testing handling of relative rank ***"

RunTest hvectest 2 "*** Testing Vector type ***"

RunTest hvectest2 2 "*** Testing struct type for vectors (MPI_UB) ***"

RunTest hvec 2 "*** Testing Type_Hvector ***"

RunTest hindexed 1 "*** Testing Type_Hindexed ***"

RunTest probe 2 "*** Testing Probe and Get_count ***"

RunTest probe1 2 "*** Testing Iprobe and Get_count ***"

RunTest self 1 "*** Testing send to self ***"

RunTest selfvsworld 2 "*** Testing COMM_SELF and COMM_WORLD ***"

RunTest testtest1 2 "*** Testing MPI_Test ***"

RunTest persistent 4 "*** Testing MPI_Recv_init ***"

RunTest persist 4 "*** Testing MPI_Startall/Request_free ***"

RunTest persist2 4 "*** Testing MPI_Startall(Bsend)/Request_free ***"

RunTest waitall 4 "*** Testing MPI_Waitall ***"

RunTest waitall2 2 "*** Testing MPI_Waitall (order) ***"

RunTest waitall3 4 "*** Testing MPI_Waitall (order-irecv) ***"

RunTest waitall4 4 "*** Testing MPI_Waitall (order-isend) ***"

RunTest waitany 4 "*** Testing MPI_Waitany ***"

RunTest pack 2 "*** Testing MPI_Pack ***"

RunTest flood 2 "**** Testing large messages ****"

#RunTest flood2 2 "**** Testing large numbers of messages ****"
#
# Run Fortran tests ONLY if Fortran available
if [ 0 = 1 ] ; then 
    echo "FORTRAN TESTS"
    #
    #echo "*** secondf ***" >> pt2pt.out
    #$mpirun $args -np 2 ./secondf "$@" >> pt2pt.out 2>&1
    #
    RunTest isendf 2 "*** Testing isend from Fortran ***"

    RunTest allpair 2 "*** Testing pt-2-pt from Fortran ***"

    RunTest allpair2 2 "*** Testing pt-2-pt from Fortran (many calls) ***"
    #
    OutTime
    testfiles="$testfiles structf.out"
    rm -f structf.out
    MakeExe structf
    echo '*** Testing Type_struct from Fortran ***'
    echo '*** Testing Type_struct from Fortran ***' >> structf.out
    # This is tricky.  Because of a bug in IRIX64, we need to direct 
    # input from /dev/null (so that we can know that we need a new process
    # group).  This should be ok for everyone, but SunOS 4.1.4 tends to
    # panic (!!) in this case.  Since both behaviors represent broken
    # operating systems, we test for ARCH=IRIX64 
    if [ "freebsd_ppc" = "IRIX64" ] ; then 
      $mpirun $args -np 2 ./structf "$@" >> structf.out 2>&1 < /dev/null
    else
      $mpirun $args -np 2 ./structf "$@" >> structf.out 2>&1
    fi
    echo '*** Testing Type_struct from Fortran ***' >> structf.out
    CheckOutput structf
    CleanExe structf
    #
    RunTest send1 2 "*** Testing pt-2-pt from Fortran (2) ***"

    RunTest sendfort 2 "*** Testing Fortran logical datatype ***"

    #
#    testfiles="$testfiles pingpong.out"
#    rm -f pingpong.out
#    MakeExe pingpong
#    echo '*** Testing pt-2-pt from Fortran (3) ***'
#    echo '*** Testing pt-2-pt from Fortran (3) ***' >> pingpong.out
#    $mpirun $args -np 2 ./pingpong "$@" >> pingpong.out 2>&1
#    echo '*** Testing pt-2-pt from Fortran (3) ***' >> pingpong.out
#    CheckOutput pingpong
#    CleanExe pingpong
    #
    echo "END OF FORTRAN TESTS"
fi
#
else
    # Just run checks
    testfiles=`echo *.out`
    if test "$testfiles" = "*.out" ; then
	echo "No output files remain from previous test!"
	exit 1
    fi
fi
#
writesummaryfile=$savewritesummaryfile
echo '*** Checking for differences from expected output ***'
CheckAllOutput pt2pt.diff
exit 0

