#!/bin/bash

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

#-Xms   initial java heap size
#-Xmx   maximum java heap size
HEAPSIZE="-Xmx64G"
#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"
 
CASES="par seq" 

# Declare an array
declare -a ARGUMENTS
declare -a OUTS
declare -a HEADERS
declare -a LOGS
task=120;
count=0;

for CASE in $CASES
do     
    case $CASE in
    seq)
        thread=1
		######Sequential program###### 
		#JAR="Median-seq"
		#TARGET="jstar.examples.median.ParallelMain"
		#HEADERS[count]="==== Run experiments with CHMDataTable, $thread threads and $task tasks. ====="
		#ARGUMENTS[count]="-cp $JAR.jar $TARGET --threads=1 --benchmark=30 --tasks=$task"
		#OUTS[count]=SEQ.CHMDataTable.thread1.task$task
		#count=$count+1;
		
		######Sequential optimized program######
		Gammas="CHMDataTable"
		for gamma in $Gammas
		do
			JAR="Median-seq-optimized"
			TARGET="jstar.examples.median.ParallelMain"
			HEADERS[count]="==== Run experiments with $gamma, $thread threads and $task tasks. ====="
			ARGUMENTS[count]="-cp $JAR.jar $TARGET --benchmark=30 --table,Data,gamma=$gamma --tasks=$task"
			OUTS[count]=SEQ-Optimized.$gamma.thread$thread.task$task
			count=$count+1;
		done
		;;
    par)
        ######Parallel programs#######	
		for thread in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
		do
			Gammas="CHMDataTable"
			for gamma in $Gammas
			do
				JAR="Median-par-optimized"
				TARGET="jstar.examples.median.ParallelMain"
				HEADERS[count]="==== Run experiments with $gamma, $thread threads and $task tasks. ====="
				ARGUMENTS[count]="-cp $JAR.jar $TARGET --table,Data,gamma=$gamma --threads=$thread --benchmark=30 --tasks=$task"
				OUTS[count]=PAR-Optimized.$gamma.thread$thread.task$task
				count=$count+1;
			done 
		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                 
	    lscpu >>$OUT
	    date >>$OUT
	    
	    java $JAVAOPS ${ARGUMENTS[i]} >>$OUT
	done
done

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