#! /bin/csh -f
#
#  MPI test script at PPPL -- for TRANSP system test (D. McCune)
#
#  Mod DMC Apr 2010:  support "no_qsub"
#
#  Use qsub to start a PBS (or similar) NxM MPI test job: 
#            pppl_mpi_test_NxM.tcsh --> mpi_portlib_env_test
#
#  or... append argument "no_qsub" to run locally, e.g. after a "use" command.
#
#  This simply exercises the portlib MPI support, and runs in a few seconds...
#
#  Required arguments:
#     $1  -- N = #nodes on which to run (btw 1 and 8)
#     $2  -- M = #procs/node (btw 1 and 16)     total processes: N*M
#
#  Optional arguments ($3 or $3 & $4) -- default, absent
#
#     no_qsub  --  run directly, no qsub
#
#     -q <queue_name>  --  qsub to indicated queue
#
#
    @ ier = 0

    set do_qsub = "TRUE"

    if ( $#argv < 2 ||   $#argv > 16 ) then
        echo " ?mpi_test_qsub: incorrect number of arguments ... "
	@ ier++
    else
        @ nnodes = $1
        if ( $status) then
           echo " ?mpi_test_qsub: 1st argument: not integer: $1 "
           @ ier++
        endif

        @ mnodes = $2
        if ( $status) then
           echo " ?mpi_test_qsub: 2nd argument: not integer: $2 "
           @ ier++
        endif

        if ( $nnodes < 1 || $nnodes > 8 ) then
            @ ier++
            echo " 1st arg. N (# nodes) = $nnodes must be in range [1:8]."
        endif

        if ( $mnodes < 1 || $nnodes > 16 ) then
            @ ier++
            echo " 2nd arg. M (procs/node) = $mnodes must be in range [1:16]."
        endif

        if ( $#argv > 2 ) then
           @ iarg = 2
           while ( $iarg < $#argv )
              @ iarg++
              if ("${argv[$iarg]}" == "-q" ) then
                 @ iarg++
                 set PBSQUE = ${argv[$iarg]}
	         echo pbsque = $PBSQUE
              endif
              if ("${argv[$iarg]}" == "no_qsub" ) then
                 set do_qsub = "FALSE"
              endif
           end
        endif
    endif

    if ( $ier  >0 ) then
        echo "syntax:  mpi_test_qsub <N> <M> [queue options]"
        echo "   <N> -- number of nodes"
        echo "   <M> -- number of processes per node"
        echo "       total number of processes is N*M."
        echo "       (for PPPL PBS tests only the product N*M matters)."
        echo " "
        echo " [queue options] "
        echo "   no_qsub    -- run directly, no qsub "
        echo "   <-q queue> -- if you want to qsub to other than default queue"
        exit 1
    endif

    echo " N -- Number of nodes: $nnodes "
    echo " M -- Number of processes per node: $mnodes "
    echo " "

    # OK...
    if ( "$do_qsub" != "TRUE" ) then

       @ ktot = $nnodes * $mnodes

       echo " "
       echo " mpi_test_qsub: no_qsub option, direct execution... "
       echo " total #cpus: $ktot "
       echo " executing here: $cwd "
       echo " "

       run_mpi_portlib_env_test $ktot $cwd

    else
       if ($?PBSQUE) then
          pbs_mpi_qsub pppl_mpi_test $nnodes $mnodes -q $PBSQUE
       else
          pbs_mpi_qsub pppl_mpi_test $nnodes $mnodes 
       endif
    endif
