#!/bin/bash
rm $1 > /dev/null 2>&1
rm $1_optimized > /dev/null 2>&1

gcc -O3 -march=native -I ../utilities  ../utilities/polybench.c $1.c -DPOLYBENCH_DUMP_ARRAYS -D$2_DATASET -o $1 -lm

gcc -O3 -march=native -fopenmp -I ../utilities  ../utilities/polybench.c $1_optimized.c -DPOLYBENCH_DUMP_ARRAYS -D$2_DATASET -o $1_optimized -lm

./$1 > $1.log 2>&1

./$1_optimized > $1_optimized.log 2>&1

if [[ $3 == "yes" ]];then
   if cmp -s $1.log $1_optimized.log
   then
      echo "The files match"
      rm $1.log $1_optimized.log
   else
      echo "The files are different"
      exit 1
   fi
else
   if cmp -s $1.log $1_optimized.log
   then
      echo -n ""
      rm $1.log $1_optimized.log
   else
      exit 1
   fi
fi


rm $1 > /dev/null 2>&1
rm $1_optimized > /dev/null 2>&1

gcc -O3 -march=native -I ../utilities  ../utilities/polybench.c $1.c -DPOLYBENCH_TIME -D$2_DATASET -o $1 -lm

gcc -O3 -march=native -fopenmp -I ../utilities  ../utilities/polybench.c $1_optimized.c -DPOLYBENCH_TIME -D$2_DATASET -o $1_optimized -lm

MIN1=9999
MIN2=9999
for i in {0..10}
do
   time_exec1=`./$1 2>&1`

   time_exec2=`OMP_PROC_BIND=true OMP_NUM_THREADS=12 ./$1_optimized 2>&1`

    var=$(awk 'BEGIN{ print "'${time_exec1}'"<"'${MIN1}'" }')
    if [ "$var" -eq 1 ];then
        MIN1="$time_exec1"
    fi
    var=$(awk 'BEGIN{ print "'${time_exec2}'"<"'${MIN2}'" }')
    if [ "$var" -eq 1 ];then
        MIN2="$time_exec2"
    fi
done
var=$(awk 'BEGIN{ print "'${MIN1}'"<"'${MIN2}'" }')
if [[ $3 == "yes" ]];then
   if [ "$var" -eq 1 ];then   
      echo "Not modified is faster !"
      echo "Not modified : $MIN1"
      echo "    Modified : $MIN2"
      difference=`echo "$MIN2 - $MIN1" | bc -l`
      echo "  Difference : 0$difference"
      echo "Not modified is faster !"
      exit 1
   else
      echo "Modified is faster !"
      echo "Not modified : $MIN1"
      echo "    Modified : $MIN2"
      difference=`echo "$MIN1 - $MIN2" | bc -l`
      echo "  Difference : 0$difference"
      echo "Modified is faster !"
   fi
else
   if [ "$var" -eq 1 ];then
      difference=`echo "$MIN2 - $MIN1" | bc -l`
      echo "-0$difference"
      exit 1
   else
      difference=`echo "$MIN1 - $MIN2" | bc -l`
      echo "0$difference"
      exit 0
   fi
fi