#!/bin/csh

set p_cpus = ($1 $2)
set p_plat = ($3 $4)

set p_two  = (1 0)
set p_four = (2 0)
set p_six  = (1 1)
set p_eight  = (3 0)

.pwr1_le_pwr2 $p_cpus $p_six
if( $status ) then
  set p_thrd = ($p_two)
else
  set p_thrd = ($p_four)
endif

set lim = `.pwr2num $p_eight`
set nthrd = `.pwr2num $p_thrd`

echo "initial nthrd, lim = $nthrd $lim"

while( $nthrd <= $lim )
  set p_procs = `.div_pwrs $p_cpus $p_thrd`
  .valid_pwr $p_procs
  if( $status ) then
    set p_wrk   = `.div_pwrs $p_plat $p_procs`
    .valid_pwr $p_wrk
    if( $status ) then
      .pwr1_ge_pwr2 $p_wrk $p_four
      if( $status ) then
        break
      endif
    endif
  endif

  if( $p_cpus[2] == 0 ) then
    @ p_thrd[1]++
  else
    set p_thrd = `.inc_pwr $p_thrd`
  endif
  set nthrd = `.pwr2num $p_thrd`
end

.pwr1_le_pwr2 $p_thrd $p_eight
if( $status ) then
  set p_wrk = `.div_pwrs $p_plat $p_procs`
  set rc = `.pwr2num $p_wrk`
  set nprocs = `.pwr2num $p_procs`
  set nthreads = $nthrd
  echo "HYB model will use $nprocs procs and $nthreads threads with $rc lats per proc"
else
  set nprocs = -1
  set nthreads = -1
  echo "can't decompose spatial domain on hardware"
endif

exit 0
