#!/usr/bin/env bash

BASE="ftp://math.nist.gov/pub/MatrixMarket2/Harwell-Boeing/"
                
PWD=`pwd`
IWD="$PWD"
MATRIX_SOLVERS_BENCHMARK="$IWD/matrix-solvers-benchmark-dbg"
STOP_TIME="1"

source ../tnl-env-variables

export CUDA_PROFILE=0
export CUDA_PROFILE_CONFIG="$TNL_SOURCE_DIR/tests/cuda-profiler.conf"
PROCESS_CUDA_PROFILE="$TNL_SOURCE_DIR/tests/process-cuda-profile.pl"
source $TNL_SOURCE_DIR/tests/matrix-market
source $TNL_SOURCE_DIR/tests/florida-matrix-market

export TNL_SPARSE_MATRIX_CHECK_CFG_DESC_FILE="$TNL_SOURCE_DIR/tests/tnl-sparse-matrix-check.cfg.desc"

#MM_MATRICES=""
#FLORIDA_MM_MATRICES=""


for link in $MM_MATRICES;
do
   echo "###############################################################################################"
   matrix=matrices`echo $link | sed 's/ftp:\/\/math.nist.gov\/pub//'`
   unzipped_matrix=`echo $matrix | sed 's/.gz//'`
   if test ! -e $matrix;
   then
      echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first."
      #echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first." >> sparse-matrix-benchmark.log            
   else
      if test ! -e $unzipped_matrix.double.bin.bz2;
      then
         echo "Missing $unzipped_matrix.double.bin.bz2 !!! Run the script 'convert-matrices'."
         #echo "Missing $unzipped_matrix.double.bin.bz2 !!! Run the script 'convert-matrices'."  >> sparse-matrix-benchmark.log
      else
         gunzip -f $matrix
         echo "Checking with the matrix $unzipped_matrix in double precison ..."
         export CUDA_PROFILE_LOG=$unzipped_matrix.double.log 
         $MATRIX_SOLVERS_BENCHMARK --input-file $unzipped_matrix.double.bin.bz2 --input-mtx-file $unzipped_matrix --log-file sparse-matrix-benchmark-double.log --stop-time $STOP_TIME --verbose 1
         gzip $unzipped_matrix
      fi          
   fi
done

for link in $FLORIDA_MM_MATRICES;
do
   matrix=matrices`echo $link | sed 's/http:\/\/www.cise.ufl.edu\/research\/sparse//'`
   if test ! -e $matrix;
   then      
      echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first."
      #echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first." >> sparse-matrix-benchmark.log
   else
     DIRNAME=`dirname $matrix`
     FILENAME=`basename $matrix`
     cd $DIRNAME
     tar zxvf $FILENAME
     cd $IWD
     SUBDIRNAME=`echo $FILENAME | sed 's/.tar.gz//'`
     rm -f $DIRNAME/$SUBDIRNAME/*_b.mtx # these are usualy in array format
     for file in $DIRNAME/$SUBDIRNAME/*.mtx;
     do
         echo "###############################################################################################"
         if test ! -e $file.double.bin.bz2;
         then
            echo "Missing $file.double.bin.bz2 !!! Run the script 'convert-matrices'."
         else   
            echo "Checking with the matrix $file ..."
            $MATRIX_SOLVERS_BENCHMARK --input-file $file.double.bin.bz2 --input-mtx-file $file --log-file sparse-matrix-benchmark-double.log --stop-time $STOP_TIME --verbose 1                        
         fi                           
         rm $file         
      done
   fi
done