#! /bin/sh
#
# This is a simple heterogeneous test which exploits the mpicc command and
# mpirun.  This is an example of how heterogeneous programs may be built and
# run
#
#
# Parameters for all programs and systems
set -x
device=ch_p4
mpihome=../../..
rshcmd=rsh
programs="sendrecv sendrecv2 sendrecv3 sendrecv4 getelm"
# Extra files needed for each program.
sendrecvfiles="test.c"
sendrecvargs="-nolongdouble"
sendrecv2files="dtypes.c gcomm.c"
sendrecv3files="dtypes.c gcomm.c"
sendrecv4files="dtypes.c gcomm.c"
getelmfiles=""
#
#
# arch1 is local, arch2 is remote
arch1=sun4
arch2=freebsd
name2=dogbert
#
debug_args=""
fail_hard=1
rebuild=0
mpirun_args=""
for arg in "$@" ; do
    case "$arg" in 
	-echo) set -x ;;
	-noclean) noclean=1 ;;
	-debug) debug_args="-p4dbg 99 -p4rdbg 99" ;;
	-mpichdebug) debug_args="$debug_args -mpichdebug" ;;
	-xxgdb) mpirun_args="-xxgdb" ;;
        -soft) fail_hard=0 ;;
	-force | -rebuild) rebuild=1 ;;
	-alpha) arch2=alpha ; name2=ptera ;;
	-help) 
	echo "Test heterogeneous operation of MPICH with ch_p4 using"
	echo "the versions of MPICH built in the current tree."
	echo "Should be run on a sun4; it rsh's to other machines as"
	echo "necessary."
	exit 1
	;;
	*) echo "Unrecognized argument $arg"
	exit 1
	;;
    esac
done
#
arches="$arch1 $arch2"
#
mypwd=`pwd`
# Fixup for brain-dead automounters
mypwd=`echo $mypwd | sed s%/tmp_mnt%%g`
#
# Build local versions
if [ 1 = 1 ] ; then
    for pgm in $programs ; do
        eval extrafiles=$"${pgm}files"
        $mpihome/lib/$arch1/$device/mpicc -o $pgm.$arch1 $pgm.c $extrafiles
    done
fi
#
# Build remote versions
for pgm in $programs ; do 
    eval extrafiles=$"${pgm}files"
    $rshcmd -n $name2 "(cd $mypwd ; $mpihome/lib/$arch2/$device/mpicc \
	-o $pgm.$arch2 $pgm.c $extrafiles)"
done
#
# Run the programs
for pgm in $programs ; do
    echo "Running $pgm..."
    eval extraargs=$"${pgm}args"
    $mpihome/lib/$arch1/$device/mpirun $mpirun_args \
		-arch $arch1 -np 1 -arch $arch2 -np 1 $pgm.%a $debug_args \
		$extraargs
done
#
# Remove the executables
if [ -z "$noclean" ] ; then 
    for arch in $arches ; do
        for pgm in $programs ; do
	    rm -f $pgm.$arch
        done
    done
fi
