#!/bin/sh

# mpprun - Run a Scyld Beowulf job with a map from the beomap scheduler.
#
# Copyright (C) 2001-2003 Scyld Computing Corporation
#   Scyld Computing Corporation
#   410 Severn Ave, Suite 210
#   Annapolis, MD 21403
#
# Initial implementation 2001 by Rick Niles, niles@scyld.com, based on
# the design from the Scyld Node Parallel Resource (NPR) scheduler.

bpshflags="-nL"
BEOMAP=/usr/bin/beomap

PrintHelp() {
cat <<EOF
mpprun [mpprun_options...] <progname> [options...]
    -p
            Prefix each line of output with the node number
            it is from.
    -np <np>
            specify the number of processors to run on
    -nolocal
            do not run on the local machine (only works for 
            p4 and ch_p4 jobs)
    -all-cpus, -allcpus
            Use all available CPUs on all the nodes.
    -all-local
            Run all processes on the master node.
    -exclude <list>
            Exclude nodes in a colon delimited list.
    -map <list>
            Use the colon delimited list to specify 
            which nodes to run on.
EOF
}


while [ 1 -le $# ] ; do
  arg=$1
  shift
  case $arg in 
    -np | --np)
	export NP="$1"
	shift
	;;
    -nolocal | -no-local | --nolocal | --no-local)
        nolocal=1
        scyld_opt=1
        export NO_LOCAL=1
	;;	
    -all-cpus | -allcpus | --all-cpus | --allcpus)
        scyld_opt=1
        export ALL_CPUS=1
        ;;
    -all-local | --all-local)
        scyld_opt=1
        export ALL_LOCAL=1
        ;;
    -exclude | --exclude)
        scyld_opt=1
        export EXCLUDE="$1"
        shift
        ;;
    -map | --map)
        scyld_opt=1
        export BEOWULF_JOB_MAP="$1"
        shift
        ;;
    -h | --help)
        PrintHelp
      	exit 1
	;;	
    -p | --prefix)
        bpshflags="$bashflags -p"
	;;
    *)
	progname="$arg"
        while [ 1 -le $# ] ; do
          cmdLineArgs="$cmdLineArgs $1"
	  shift
        done
        ;;
  esac
done

# If the program name is not set display the help message
if [ "$progname" = "" ] ; then
 PrintHelp
 exit 1;
fi

# bpsh a copy of the program to each node
for i in `$BEOMAP | sed 's/:/ /g'`
  do
    bpsh $bpshflags $i $progname $cmdLineArgs
  done
