#!/bin/sh
#
# run MPI on selected machines that ruptime reports as "up"
#
# usage:
#   mpirun-on-upmachines [-v] machine-prefix mpirun-arguments
#   -v for verbose output
#   machine-prefix is a prefix for machine names (e.g., xena or janus)
#   mpirun-arguments are command-line arguments for mpirun
# example usage:  mpirun-on-upmachines xena -np 2 hello
#
if [ ".$1" = ".-v" ]
then
    verbose=y
    shift 1
else
    verbose=n
fi
if [ -z "$2" ]
then
    echo "usage:  `basename $0` [-v] machine-prefix mpirun-arguments"
    echo "  -v for verbose output"
    echo "  machine-prefix is a prefix for machine names (e.g., xena or janus)"
    echo "  mpirun-arguments are command-line arguments for mpirun"
    echo "example usage:  `basename $0` xena -np 2 hello"
    exit 1
fi
prefix=$1
shift 1
hosts=`ruptime | grep -i $prefix | grep up | \
    awk '{print $1}' | tr "\n" "," | sed 's/,$//'`
if [ $verbose = y ]
then
    echo executing mpirun with parameters $* 
    echo on hosts $hosts
fi
mpirun --host $hosts $*
