#!/bin/bash 

startTime=$(date +"%T")
echo "Start time : $startTime"

#-Xms   initial java heap size
#-Xmx   maximum java heap size
HEAPSIZE="-Xmx8G"
#Enable verbose garbage collector.
VERBOSE="-verbose:gc"
#-Xbatch option stops the program while the hot spot compiler is recompiling/optimising the code.
COMPILATION="-Xbatch -XX:+PrintCompilation"
#Print out GC detail
DETAIL="-XX:+PrintTenuringDistribution -XX:+PrintGCDetails"
 
#The MaxTenuringThreshold(MTT) flag governs how many times the objects are copied between the Survivor Spaces
#before being moved to the Tenured (Old) 
#Usage: -XX:MaxTenuringThreshold=n
# where n is the number of times the objects are copied. The default value of n is (or actually was) 31.
MaxTenuringThreshold="-XX:MaxTenuringThreshold=1"
 
#Maximum size of new generation (in bytes).
NewSize="-XX:NewSize=4096m"

BIAS="-XX:BiasedLockingStartupDelay=0"
CARDMARK="-XX:+UseCondCardMark"

# Declare an array
declare -a ARGUMENTS
declare -a OUTS
declare -a HEADERS
declare -a LOGS

CASES="seq par"
 
count=0;
ARGS="--benchmark=30"
for CASE in $CASES
do     	
       case $CASE in
       seq)
	for thread in 1 15
	do
		JAR="MatrixMult2-seq-optimized"
		TARGET="jstar.examples.matrixmult2.Matrix3DArrayMain"
		HEADERS[count]="==== Run experiments with $thread threads and $ARGS. ====="
		ARGUMENTS[count]="-cp $JAR.jar $TARGET --threads=$thread $ARGS"
		OUTS[count]=SEQ-Optimized.thread$thread
		count=$count+1;
	done
	;;
       par)
	for thread in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
	do
		JAR="MatrixMult2-par-optimized"
		TARGET="jstar.examples.matrixmult2.Matrix3DArrayMain"			
		HEADERS[count]="==== Run experiments with $thread threads and $ARGS. ====="
		ARGUMENTS[count]="-cp $JAR.jar $TARGET --threads=$thread $ARGS"
		OUTS[count]=PAR-Optimized.thread$thread
		count=$count+1;
	done
	;;
       esac	
done

#JVM option string
OPS="CARDMARK_BIAS DEFAULT" 

for OP in $OPS
do
	if [ -d $OP ]
	then
		echo "Error!!$OP exists!!"
		exit 2			
	fi	
done

for OP in $OPS
do	        
	mkdir $OP
	case $OP in
	DEFAULT) 
		JAVAOPS="$HEAPSIZE $VERBOSE $COMPILATION $DETAIL"
	;;
	CARDMARK_BIAS)
		JAVAOPS="$HEAPSIZE $VERBOSE $COMPILATION $DETAIL $CARDMARK $BIAS"
	;;
	esac

	for(( i=0; i < count; i++));
	do
	    OUT=$OP/${OUTS[i]}".out"

	    echo ${HEADERS[i]}
	    echo ${HEADERS[i]} >>$OUT
	    echo java $JAVAOPS ${ARGUMENTS[i]}
	    echo java $JAVAOPS ${ARGUMENTS[i]} >>$OUT

	    java -version >>$OUT
	    uname -a >>$OUT
	    cat /proc/cpuinfo >>$OUT
	    cat /proc/meminfo >>$OUT
	    cat /proc/meminfo >>$OUT
	    lscpu >>$OUT
	    date >>$OUT	     
	   
	    java $JAVAOPS ${ARGUMENTS[i]} >>$OUT
	done
done

endTime=$(date +"%T")
echo "End time : $endTime"
