#!/bin/bash

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

count=0;
ARGS="--table,SumMonth,group=1 --benchmark=30 sorted1000X.csv"
thread=1;

CASES="optimisation"
for CASE in $CASES
do     
    case $CASE in
	seq)
	    gamma="TreeSet"
	    JAR="PvWatts-seq"
	    TARGET="jstar.examples.pvwatts.Main"
	    HEADERS[count]="==== Run sequential experiments with $gamma and $ARGS. ====="
	    ARGUMENTS[count]="-cp $JAR.jar $TARGET $ARGS"
	    OUTS[count]=SEQ.$gamma.thread1.reader1.capacity0
	    count=$count+1;

	    gamma="TreeSet"
	    JAR="PvWatts-seq-optimized"
	    TARGET="jstar.examples.pvwatts.Main"
	    HEADERS[count]="==== Run sequential and optimized experiments with $gamma and $ARGS. ====="
	    ARGUMENTS[count]="-cp $JAR.jar $TARGET $ARGS"
	    OUTS[count]=SEQ-Optimized.$gamma.thread1.reader1.capacity0
	    count=$count+1;	    
	    ;;
	optimisation)
		for Size in 1 3 10 300 1000
		do
		    
		    ARGS="--benchmark=30 ${Size}X.csv"
		    JAR="PvWatts-seq"
		    TARGET="jstar.examples.pvwatts.Main"
		    HEADERS[count]="==== Run sequential experiments with $ARGS.====="
		    ARGUMENTS[count]="-cp $JAR.jar $TARGET $ARGS"
		    OUTS[count]=SEQ.NaN.NaN.${Size}X
		    count=$count+1;


		    JAR="PvWatts-seq-noDelta"
		    TARGET="jstar.examples.pvwatts.Main"
		    HEADERS[count]="==== Run sequential and noDelta experiments with $ARGS.====="
		    ARGUMENTS[count]="-cp $JAR.jar $TARGET $ARGS"
		    OUTS[count]=SEQ.noDelta.PvWatts.${Size}X
		    count=$count+1;
		    
		    JAR="PvWatts-seq-noGamma"
		    TARGET="jstar.examples.pvwatts.Main"
		    HEADERS[count]="==== Run sequential and noGamma experiments with $ARGS.====="
		    ARGUMENTS[count]="-cp $JAR.jar $TARGET $ARGS"
		    OUTS[count]=SEQ.noGamma.SumMonth.${Size}X
		    count=$count+1;
		done
	;;
	par)
	    readers=48;	    
	    for thread in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
	    do		  
		    gamma="ConcurrentSkipListSet"
		    JAR="PvWatts-par"
		    TARGET="jstar.examples.pvwatts.Main"		    
		    HEADERS[count]="==== Run experiments with ConcurrentSkipListSet, $thread threads and $ARGS. ======"
		    ARGUMENTS[count]="-cp $JAR.jar $TARGET --threads=$thread --readers=$readers $ARGS"
		    OUTS[count]=PAR.$gamma.thread$thread.reader$readers.capacity0
		    count=$count+1; 		    
	    done
	    ;;
	
	par-optimized)
	    readers=48;	    
	    for thread in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
	    do
	    	Gammas="ConcurrentHashMapV8 ConcurrentHashMap ConcurrentSkipListSet"
		for gamma in $Gammas
		do
			JAR="PvWatts-par-optimized"
			TARGET="jstar.examples.pvwatts.ParallelMain"
			TABLE="--table,PvWatts,estimated=2000000,gamma=$gamma"
			HEADERS[count]="==== Run experiments with ConcurrentHashMapV8, $thread threads and $ARGS. ======"
			ARGUMENTS[count]="-cp $JAR.jar $TARGET $TABLE --threads=$thread --readers=$readers $ARGS"
			OUTS[count]=PAR-Optimized.$gamma.thread$thread.reader$readers.capacity2000000
			count=$count+1;
		done	
	    done
	    ;;
	disruptor)
	    for producer in 1 2 3
	    do			   
			JAR="SolarRadiation"
			TARGET="jstar.examples.solarradiation.JStarDisruptorMain"
			consumer=12
			strategy="Blocking"
			gamma="HashSet"
			TABLE="--table,PvWatts,gamma=$gamma"
			HEADERS[count]="==== Run experiments with disruptor. ======"
			ARGUMENTS[count]="-cp $JAR.jar $TARGET $TABLE --consumers=$consumer --producers=$producer --strategy=$strategy $ARGS"
			OUTS[count]=Disruptor.consumer$consumer.producer$producer.Strategy$strategy.$gamma
			count=$count+1;
	    done
	    ;;
	phases)	       
	    JAR="PvWatts-seq"
	    TARGET="jstar.examples.pvwatts.ReadingSpeedMain"
	    Gammas="HashMap TreeSet"
	    Deltas="DeltaNodeIntRange DeltaNodeInt"
	    for phase in 0 1 2 3 10
	    do
			for gamma in $Gammas
			do
			    for delta in $Deltas
			    do
					HEADERS[count]="==== Run experiments ======"
					ARGUMENTS[count]="-cp $JAR.jar $TARGET --phases=$phase --table,PvWatts,gamma=$gamma --table,SumMonth,delta=$delta --benchmark=30 sorted1000X.csv"
					OUTS[count]=PvWatts-Phase.$gamma.$delta.phase$phase
					count=$count+1;
			    done 
			done
	    done  
	    ;;
    esac    
done
       
#JVM option string
#-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"
PROFILE="-Xprof"
#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 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"
 
OPS="DEFAULT"
#OPS="DEFAULT CARDMARK_BIAS" 
for OP in $OPS
do
	if [ -d $OP ]
	then
		echo "Error!!$OP exists!!"
		exit 2			
	fi	
done

START=$(date +%s)
echo "START:$START"
for OP in $OPS
do	        
	mkdir $OP
	case $OP in
	DEFAULT) 
		JAVAOPS="$HEAPSIZE $VERBOSE $COMPILATION $DETAIL $NewSize"
		;;
	CARDMARK_BIAS)
		JAVAOPS="$HEAPSIZE $VERBOSE $COMPILATION $DETAIL $NewSize $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    
	     
	    java $JAVAOPS ${ARGUMENTS[i]} >>$OUT
	done
done
END=$(date +%s)
echo "END:$END"
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds" 