#!/bin/sh
#
# Conv-host for IBM SP:
#  Translates +pN-style charmrun options into 
# POE (Parallel Operating Environment) options.

args=""
pes=1

while [ $# -gt 0 ]
do
	case $1 in
	+p)
		pes=$2
		shift
		;;
	+ppn)
		args=$args"$1 $2"
		shift
		;;
	+p*)
		pes=`echo $1 | awk '{print substr($1,3)}'`
		;;
	*) 
		args=$args"$1 "
		;;
	esac
	shift
done

# Try to guess the number of nodes and tasks per node to use
nodes=$pes
tp=1
for i in 4 2 3
do
  if [ `expr $pes / $i '*' $i` -eq $pes ]
  then
	nodes=`expr $pes / $i`
	tp=$i
	break
  fi
done

extra="-nodes $nodes -tasks_per_node $tp -rmpool 1"

# Prepend path to executable
args=`pwd`/"$args"

echo "Running> poe $args $extra"
poe $args $extra
