#! /bin/sh
# This version puts the output from each program into a separate file.
# -mvhome is needed for the ANL SP, and is ignored by others
args=
device=
MPICH_VERSION=
STOPFILE=${MPITEST_STOPTEST:-"$HOME/.stopmpichtests"}

MAKE="make --no-print-directory"
MPIRUNMVBACK=''
#

# Set mpirun to the name/path of the mpirun program
#FindMPIRUN
#
runtests=1
makeeach=0
writesummaryfile=no
have_fortran=0
quiet=0
MAKE="make --no-print-directory"
for arg in "$@" ; do
    case $arg in
    -basedir=* )
	basedir=`echo $arg | sed 's/-basedir=//'`
	;; 
    -srcdir=* )
	srcdir=`echo $arg | sed 's/-srcdir=//'`
	;; 
	-fort=* )
	have_fortran=`echo $arg | sed 's/-fort=//'`
	;;
	-checkonly )
	runtests=0
	;;
    -margs=*)
	margs=`echo $arg | sed 's/-margs=//'`
	args="$args $margs"
	;;
	-small)
	shift
	makeeach=1
	;;
	-summaryfile=*)
	writesummaryfile=yes
	summaryfile=`echo A$arg | sed 's/A-summaryfile=//'`
	;;
	-quiet)
	shift
	quiet=1
	;;
	-help|-u)
	echo "runtests [-checkonly] [-margs='...']"
	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."
	exit 1
	;;
	*)
	if test -n "$arg" ; then
   	    echo "runtests: Unknown argument ($arg)"
	    exit 1
        fi
	;;
    esac
done
# Load basic procedures
. ${srcdir}/../runbase
#
# If the programs are not available, run make.
if [ ! -x coll1 -a $makeeach = 0 -a $runtests = 1 ] ; then
    $MAKE
fi

mpirun=" ${basedir}/bin/smpirun -platform ${srcdir}/../../../../examples/msg/small_platform_with_routers.xml -hostfile ${srcdir}/../../hostfile  --log=root.thres:critical"
testfiles=""
if [ $runtests = 1 ] ; then
echo '**** Testing MPI Collective routines ****'

RunTest barrier 4 "*** Barrier Test ***" "" "barrier-0.out"

RunTest bcast_mpich 4 "*** Broadcast Test ***" "" "bcast-0.out bcast-1.out bcast-2.out bcast-3.out"

RunTest bcastvec 4 "*** Broadcast Datatype Test ***" "" "bcastvec-0.out bcastvec-1.out bcastvec-2.out bcastvec-3.out"


#TODO : handle MPI_BOTTOM to allow som operations to use absolute addresses
RunTest coll1 4

RunTest coll2 5

RunTest coll3 5

RunTest coll4 4

RunTest coll5 4

RunTest coll6 5

RunTest coll7 5

RunTest coll8 4

RunTest coll9 4

#smpi does not handle non commutative operations, removed
#RunTest coll10 4

#smpi does not handle non commutative operations, removed
RunTest coll11 4

#weird manipulations of ranks in split, and comms -> deadlock, removed
#RunTest scantst 4

RunTest coll12 4

# coll13 is very picky about arguments
RunTest coll13 14

RunTest longuser 4

# Some implementations (e.g., IBM's) forget to handle the np = 1 case.
#RunTest longuser 1 "*** longuser (np == 1) ***"
MakeExe longuser
cp longuser longuser1
RunTest longuser1 1 '*** longuser (np == 1) ***'
rm -f longuser1

#OutTime
#testfiles="$testfiles allredmany.out"
#rm -f allredmany.out
#MakeExe allredmany
#echo '**** allredmany ****'
#echo '*** allredmany ***' >> allredmany.out
#cnt=0
## Run several times to try and catch timing/race conditions in managing
## the flood of one-way messages.
#while [ $cnt -lt 20 ] ; do
#    echo "*** allredmany run $cnt ***" >> allredmany.out
#    $mpirun -np 2 $args allredmany >> allredmany.out 2>&1
#    cnt=`expr $cnt + 1`
#done
#echo '*** allredmany ***' >> allredmany.out
#CleanExe allredmany

RunTest grouptest 4
#uses MPI_Dims_create, MPI_Cart_create ... removed
RunTest allred 4 "*** Allred ***"

RunTest allred2 4 "*** Allred2 ***"
#uses MPI_Dims_create, MPI_Cart_create ... removed
#RunTest scatterv 4 "*** Scatterv ***"

RunTest scattern 4 "*** Scattern ***"

#fails, more debug needed to understand
#RunTest redscat 4 "*** Reduce_scatter ***"

RunTest alltoallv_mpich 4 "*** Alltoallv ***"
echo "runtests: fortran ($have_fortran)"
#
# Run Fortran tests ONLY if Fortran available
if [ $have_fortran -eq "1" ] ; then 
    echo "FORTRAN TESTS"

    RunTest allredf 4 "*** Testing allreduce from Fortran ***"

    RunTest assocf 4 "*** Testing allreduce from Fortran (2) ***"

    RunTest bcastlog 4 "*** Testing logical datatype in BCAST ***"
    #buggy test, not available in original test, removed
    #RunTest allgatherf 2 "*** Testing allgather from Fortran ***"
    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

echo '*** Checking for differences from expected output ***'
CheckAllOutput coll.diff
exit 0
