#*************************************************************************
#                          (c) Copyright 1993
#                       University of Tennessee
#                         All Rights Reserved
#*************************************************************************
#!/bin/csh -f 
#svdrun
#######################################################################
#      This script is used to run SVDPACKC codes.  The script takes as
# input the parameters described in the sample input file "svdin".  You
# should invoke the runs specified by svdin via "svdrun svdin".
# The output file from each execution of a SVDPACKC code, will be named
# DATA.outN, where
#
#            DATA= name of file containing sparse matrix data structure,
#            N   = id of runs with this dataset in svdrun.
#
# This script will compile, link, and execute any SVDPACKC code found
# in the subdirectories specified by the following variables:
# 
#         las1files   bls1files   sis1files   tms1files
#         las2files   bls1files   sis2files   tms2files
#
# Any changes in the paths of the SVDPACKC source will require
# updating these variables.  The user must also specify the "datadir"
# path to the compressed Harwell-Boeing input matrices which are
# used to test the library.
#
# This current script is designed to compile and run SVDPACKC on
# the following architectures:
#
#   rios (IBM RS/6000),  sun4 (Sun SPARC), hp9000 (HP 9000 Series),
#   CRAY-YMP (UNICOS)
#
# The script "svdsum" can be used to produce summary tables from the 
# output files generated by "svdrun".  You may need to modify the
# SVDPACKC makefile for compilation on your machine.  This makefile
# is invoked by this script.
#######################################################################
# Author: G. O'Brien, Dept. of Computer Science, UTK (May 1993)
#######################################################################
#
#-----------------------------------------------------------------------
# Create environment --------------------------------------------------
#-----------------------------------------------------------------------
unalias rm
unalias cp
unalias mv
unset noclobber
set userdirectory = $cwd
#-----------------------------------------------------------------------
# Set up paths (Should be modified for all paths to data and executables)
#-----------------------------------------------------------------------
set datadir   = $cwd
set las1files = $cwd
set las2files = $cwd 
set bls1files = $cwd
set bls2files = $cwd
set sis1files = $cwd
set sis2files = $cwd
set tms1files = $cwd
set tms2files = $cwd
#-----------------------------------------------------------------------
# Check the list of possible hosts ------------------------------------
#-----------------------------------------------------------------------
set archtype = `arch`
if ($archtype =~ rios || $archtype =~ sun4 || $archtype =~ hp9000) then
	echo "Running on $archtype"

else if ($archtype =~ CRAY-YMP) then
	echo "Running on $archtype"
else
   echo "${0}: Compilation for rios, sun4, Y-MP, or hp9000 only."
   exit
endif
#-----------------------------------------------------------------------
# Check command line arguments ----------------------------------------
#-----------------------------------------------------------------------
if ($#argv == 0) then
	echo "Usage: svdrun <filename>"
	exit 1
endif

    	if ($#argv == 1) then
			set inputfile = $argv[1]
			if (! -r $inputfile) then
				echo "$inputfile unreadable."
				exit 1
			endif

			set id = (`awk '$0 !~ /^#/ {print $1} ' $inputfile`)
			set program = (`awk '$0 !~ /^#/ {print $2 } ' $inputfile`)
			set dataset = (`awk '$0 !~ /^#/ {print $3 } ' $inputfile`)
        else
		   echo "Usage: svdrun <filename>"
		endif

@ c = 1

foreach file ($dataset)

#-----------------------------------------------------------------------
#       Get parameters -------------------------------------------------
#-----------------------------------------------------------------------
	switch ($program[$c])
      
	  case las1:
	     set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 breaksw

	  case las2:
	     set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 breaksw

      case bls1:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set blocksize = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $9}' `
         breaksw

      case bls2:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set blocksize = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $9}' `
         breaksw

      case sis1:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
         breaksw

	  case sis2:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
         breaksw

	  case tms1:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set residual = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set ritz = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $9}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $10}' `
         breaksw

	  case tms2:
		 set iterations = `grep "^ *$id[$c] " $inputfile | awk '{print $4}' `
		 set triplets = `grep "^ *$id[$c] " $inputfile | awk '{print $5}' `
		 set subspace = `grep "^ *$id[$c] " $inputfile | awk '{print $6}' `
		 set residual = `grep "^ *$id[$c] " $inputfile | awk '{print $7}' `
		 set ritz = `grep "^ *$id[$c] " $inputfile | awk '{print $8}' `
		 set accuracy = `grep "^ *$id[$c] " $inputfile | awk '{print $9}' `
		 set vectors = `grep "^ *$id[$c] " $inputfile | awk '{print $10}' `
         breaksw

    endsw

#-----------------------------------------------------------------------
#       Check parameters -----------------------------------------------
#-----------------------------------------------------------------------

	if ($iterations < $triplets) then
       echo "Invalid input in $inputfile."
	   echo "   Iterations must be greater than or equal to triplets."
    endif

	if ($accuracy !~ [-0-9]) then
	   set accuracy = 1.0e-6
    endif

	if ($vectors !~ [T,F,t,f]) then
	   set vectors = TRUE
    else
	   echo "Invalid input in $inputfile."
	   echo "   Vectors is either TRUE or FALSE."
    endif

	if ($program[$c] =~ tms1 || $program[$c] =~ tms2) then
	   if ($residual !~ [0-9]*E[0-9]*) then
	      echo "Bad residual value in $inputfile."
		  if ($residual !~ [0-9*D[0-9]*) then
             echo "Unable to fix residual."
			 exit
		  endif
		  echo $residual
		  echo $residual > temp
		  set residual = `sed 's/D/E/g' temp`
		  echo $residual
       endif
    endif


#-----------------------------------------------------------------------
#     Get Data files ---------------------------------------------------
#-----------------------------------------------------------------------

	echo " "
	echo "Search for $file (if necessary) ..."
	echo " "
	if (! -e $file) then
		if (! -e $file.Z) then
		   if (! -e $datadir/$file.Z) then
		   	  echo "File does not exist."
		      exit 1
                   endif
                   if( $datadir !~ $cwd) then
	   	      ln -s $datadir/$file.Z
                   endif
		   uncompress $file.Z
		endif
	endif
        set fname = $file

#-----------------------------------------------------------------------
#         parameters ---------------------------------------------------
#-----------------------------------------------------------------------

	echo "Reading $file ..."
	echo " "

	set nrow = `awk '/[Rr][Rr][Aa]/ {print$2}' $file`
	set ncol = `awk '/[Rr][Rr][Aa]/ {print$3}' $file`
	set nnzero = `awk '/[Rr][Rr][Aa]/ {print$4}' $file`

	@ LMTN =  $nrow + $ncol
	@ NZMAX = $nnzero + 1
	@ NSIG = $triplets
	@ LDV = $ncol
	@ LDU = $nrow
	@ MNCMAX = $nnzero
	@ NRMAX = $nrow
	@ NCMAX = $ncol + 1
	@ IK2 = $triplets
	@ N2 = $ncol
	@ M2 = $nrow
	@ LDT2 = $nrow
	@ NCG = $nrow + $ncol
	@ LDY = $nrow + $ncol
	if ($program[$c] !~ las1 && $program[$c] !~ las2) then
	   @ ISMAX = $subspace
    endif
    if ($program[$c] !~ bls1 && $program[$c] !~ bls2) then
	   @ lsizea = ($nrow + $ncol) * ($iterations + 2)
       @ LMTNW = 6 * ($nrow + $ncol) + $iterations * 4 + 1 + $iterations * $iterations
       @ MAXIT = $iterations
    endif
    if ($program[$c] =~ bls1 || $program[$c] =~ bls2) then
       set IB2 = $blocksize
	   set IC2 = $subspace
	   @ NMAX = $triplets
	endif
	if ($program[$c] =~ sis1 || $program[$c] =~ sis2) then
	   @ extravectors = $subspace - $triplets
    endif


	echo "+---------------------------------------------+"
	echo "|  Parameters:                                |"
	echo "+---------------------------------------------+"
	echo "|  nrow, LDU, NRMAX, M2, LDT2  = $nrow"
	echo "|  ncol, LDV , N2              = $ncol"
	echo "|  nnzero, MNCMAX              = $nnzero"
	echo "|  triplets, NSIG, IK2         = $triplets"
	echo "|  LMTN, NCG, LDY              = $LMTN"
	echo "|  NZMAX                       = $NZMAX"
	echo "|  NCMAX                       = $NCMAX"
	if ($program[$c] !~ las1 && $program[$c] !~ las2) then
	   echo "|  ISMAX                       = $ISMAX"
	endif
	if ($program[$c] !~ bls1 && $program[$c] !~ bls2) then
	   echo "|  lsizea                      = $lsizea"
       echo "|  LMTNW                       = $LMTNW"
       echo "|  MAXIT                       = $MAXIT"
	endif
	if ($program[$c] =~ sis1 || $program[$c] =~ sis2) then
	   echo "|  Extravectors                = $extravectors"
    endif
	echo "+---------------------------------------------+"
	echo "   "

#-----------------------------------------------------------------------
#	  Create parameters input file ---------------------------------
#-----------------------------------------------------------------------

    if ($program[$c] =~ las1 || $program[$c] =~ las2) then
       echo "'$file' $iterations $triplets -1.0e-30 1.0e-30 $vectors $accuracy 0" > parameters$c
	endif

	if ($program[$c] =~ bls1 || $program[$c] =~ bls2) then
	   echo "'$file' $iterations $subspace $blocksize $triplets $accuracy $vectors" > parameters$c
    endif

	if ($program[$c] =~ sis1 || $program[$c] =~ sis2) then
	   echo "'$file' $triplets $extravectors $iterations $accuracy $vectors" > parameters$c
    endif

	if ($program[$c] =~ tms1 || $program[$c] =~ tms2) then
	   echo "'$file' $triplets $subspace $ritz $accuracy $residual $vectors $iterations" > parameters$c
    endif

#-----------------------------------------------------------------------
#     Compile & Link C code --------------------------------------------
#-----------------------------------------------------------------------

	echo "Compiling/Linking C Code ..."

    if ($archtype =~ rios || $archtype =~ CRAY-YMP) then
	   switch ($program[$c])
		  case las1:
			 cd $las1files
			 make las1 CC=cc
			 breaksw

                  case las2:
		     cd $las2files
			 make las2 CC=cc
			 breaksw

                  case bls1:
			 cd $bls1files
			 make bls1 CC=cc
			 breaksw

                  case bls2:
			 cd $bls2files
			 make bls2 CC=cc
			 breaksw

                  case sis1:
			 cd $sis1files
			 make sis1 CC=cc
                         breaksw

		  case sis2:
			 cd $sis2files
			 make sis2 CC=cc
                         breaksw

		  case tms1:
			 cd $tms1files
			 make tms1 CC=cc
			 breaksw

		  case tms2:
       	 	         cd $tms2files
             	 	 make tms2 CC=cc
			 breaksw

        endsw
    endif

	if ($archtype =~ sun4 || $archtype =~ hp9000) then
	   switch ($program[$c])
		  case las1:
			 cd $las1files
			 make las1 CC=gcc
			 breaksw

                  case las2:
		        cd $las2files
			 make las2 CC=gcc
			 breaksw

                  case bls1:
			 cd $bls1files
			 make bls1 CC=gcc
			 breaksw

                  case bls2:
			 cd $bls2files
			 make bls2 CC=gcc
			 breaksw

           	  case sis1:
			 cd $sis1files
			 make sis1 CC=gcc
                         breaksw

		  case sis2:
			 cd $sis2files
			 make sis2 CC=gcc
                         breaksw

		  case tms1:
			 cd $tms1files
			 make tms1 CC=gcc
			 breaksw

		  case tms2:
                         cd $tms2files
                         make tms2 CC=gcc
			 breaksw

        endsw

    endif
	cd $userdirectory

#-----------------------------------------------------------------------
#       Run C Code -----------------------------------------------------
#-----------------------------------------------------------------------

    echo "Executing C Code..."
        mv -f $file matrix

	if ($program[$c] =~ las1) then
        mv -f parameters$c lap1
       $las1files/$program[$c] 
       mv -f lao1 out1 
    endif

	if ($program[$c] =~ las2) then
        mv -f parameters$c lap2
       $las2files/$program[$c] 
       mv -f lao2 out1 
    endif

	if ($program[$c] =~ bls1) then
       mv -f parameters$c blp1
       $bls1files/$program[$c]
       mv -f blo1 out1 
    endif

	if ($program[$c] =~ bls2) then
       mv -f parameters$c blp2
       $bls2files/$program[$c]
       mv -f blo2 out1 
    endif

	if ($program[$c] =~ sis1) then
	   mv -f parameters$c sip1
	   $sis1files/$program[$c]
	   mv -f sio1 out1
	endif

	if ($program[$c] =~ sis2) then
	   mv -f parameters$c sip2
	   $sis2files/$program[$c]
	   mv -f sio2 out1
	endif
		
	if ($program[$c] =~ tms1) then
	   mv -f parameters$c tmp1
	   $tms1files/$program[$c]
	   mv -f tmo1 out1
	endif

	if ($program[$c] =~ tms2) then
	   mv -f parameters$c tmp2
	   $tms2files/$program[$c]
	   mv -f tmo2 out1
	endif

    mv -f matrix  $fname
    date '+ ... DATE: %a %h %d 19%y %T EST' >> temp
    cat out1 >> temp
	sed 's/E-/-/g' temp  > temp2
	sed 's/E+/+/g' temp2 > $file.out$id[$c]
     

	rm -f temp
	rm -f temp2


#-----------------------------------------------------------------------
#       Cleaning up ----------------------------------------------------
#-----------------------------------------------------------------------

 	rm -f parameters$c
 	rm -f out1
	rm -f outw
	rm -f matrix


    @ c++
	echo "__________________________"
end
 
if ($datadir !~ $cwd) then
   echo  "...removing links to remote directory"
   foreach file ($dataset)
      rm -f $file
   end
else if ($datadir =~ $cwd) then
   echo  "...compressing files in current directory"
   foreach file ($dataset)
	if (! -e $file.Z) then
          compress -f $file
        endif
   end
endif


echo " "
echo  "...svdrun completed"
#---------------------------------------------------------------------------
# End of svdrun ------------------------------------------------------------
#---------------------------------------------------------------------------
