#!/bin/bash
 
# cd Dijkstra
#-Xms   initial java heap size
#-Xmx   maximum java heap size
HEAPSIZE="-Xmx4G"
#Enable verbose garbage collector.
VERBOSE="-verbose:gc -XX:+PrintGCDetails -XX:+PrintTenuringDistribution"
#-Xbatch option stops the program while the hot spot compiler is recompiling/optimising the code.
COMPILATION="-Xbatch -XX:+PrintCompilation"

#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"

CASES="parallel" #sequential"

# include the JStar runtime .jar files in the generated lib folder.
export RUNJARS="../lib/*"

# Declare an array
declare -a ARGUMENTS
declare -a OUTS
declare -a HEADERS
declare -a LOGS
 
count=0;
ARGS="--benchmark=20 --table,Done,gamma=CHMDoneTable,delta=ParallelDeltaNodeIntRange --table,Edge,gamma=ConcurrentHashMapV8  --graph=1000000,1000000"
for CASE in $CASES
do 
    case $CASE in
    sequential)
        ######Sequential program###### 
        thread=1
        JAR="Dijkstra-seq-optimized"
        TARGET="jstar.examples.dijkstra.Main"
        HEADERS[count]="==== Run experiments with TreeSet, $thread threads and $ARGS. ====="
        ARGUMENTS[count]="-cp $JAR.jar:$RUNJARS $TARGET --threads=$thread $ARGS"
        OUTS[count]=$JAR.thread$thread.task$task
	count=$count+1;
        ;;
    parallel)
	for task in 6 4 2 1 3 5 7 8
	do
            ######Parallel programs#######
            thread=$task
            JAR="Dijkstra-par-optimized"
            TARGET="jstar.examples.dijkstra.ParallelMain"
            HEADERS[count]="==== Run experiments with CHMDoneTable, EdgeHashTable, $thread threads and $ARGS. ====="
            ARGUMENTS[count]="-cp $JAR.jar:$RUNJARS $TARGET --threads=$thread $ARGS"
            OUTS[count]=$JAR.thread$thread.task$task
	    count=$count+1;
	done
        ;;
    esac
done

#JVM option string         
JAVAOPS="$HEAPSIZE $VERBOSE"

for(( i=0; i < count; i++));
do
    OUT=${OUTS[i]}".out"
 
    echo ${HEADERS[i]}
    echo ${HEADERS[i]} >$OUT    # zeroes the file first.
    echo java $JAVAOPS ${ARGUMENTS[i]}
    echo java $JAVAOPS ${ARGUMENTS[i]} >>$OUT
    java -version 2>>$OUT
    uname -a >>$OUT
    cat /proc/cpuinfo >>$OUT
    cat /proc/meminfo >>$OUT                 
    java $JAVAOPS ${ARGUMENTS[i]} >>$OUT
done
