#!/bin/bash -

false=0; true=1

# Usage: arch [ hostname ] returns hostname, cores, startcore
#
#   Define machine architecture based on starting socket, CPUs (cores) per socket, number of
#   sockets, has hyperthreading.

start=0

arch() {
	hostname=${1:-`hostname`}			# return value
	hashyper=${true}					# assume machine has hyperthreads
	if [ "${hostname}" = "plg2" ] ; then
		startsocket=${start}
		cps=16							# coresPerSocket
		sockets=2
		hashyper=${false}				# has no hyperthreads
	elif [ "${hostname}" = "nasus" ] ; then
		startsocket=${start}
		cps=64							# coresPerSocket
		sockets=2
	elif [ "${hostname}" = "pyke" ] ; then
		startsocket=${start}
		cps=24							# coresPerSocket
		sockets=2
	elif [ "${hostname}" = "jax" ] ; then
		startsocket=${start}
		cps=24							# coresPerSocket
		sockets=4
	else
		echo "unsupported host" ${hostname}
		exit 1
	fi
	cores=$(( ${cps} * ${sockets} ))
	startcore=$(( ${startsocket} * ${cps} ))
}

# Usage: affinity (global cps, sockets, startsocket, hashyper, cores, startcore, wrap)
#   returns taskset argument
#
#   This routine assumes hyperthreading has only 2 hyperthreads per core.
#
#   If hyperthread scanning is used: processor units are assigned across the low-number hyperthreads
#   of the socket's cores. When the low-number hyperthreads are filled, the high-number hyperhtreads
#   are assigned across the socket's cores. Then the next socket is assigned.
#
#   If hyperthread wrapping is used: processor units are assigned in low/high-number pairs of
#   hyperthreads across the socket's cores. Then the next socket is assigned.

wrap=${false}							# set to control hyperthread assignment across socket cores

affinity() {
	if [ ${wrap} -eq ${true} -a ${hashyper} -eq ${false} ] ; then
		echo "architecture does not support hyperthreading for wrapping"
		exit 1
	fi
	taskset=""							# return value
	set -- $(( ${1} - 1 ))				# decrement $1
	if [ ${1} -eq 0 ] ; then taskset="${startcore}-${startcore}"; return; fi
	if [ ${1} -ge $(( ${cps} * ( ${sockets} - ${startsocket} ) * ( ${hashyper} + 1 ) )) ] ; then # error
		echo "not enough cores $(( ${cores} * ${sockets} )) for $(( ${1} + 1 )) starting at ${startcore}"
		exit 1
	fi
	if [ ${hashyper} -eq ${false} ] ; then taskset="${startcore}-$(( ${1} + ${startcore} ))"; return; fi # no hyperthreads
	start2=$(( ${startcore} + ${cores} ))
	if [ ${wrap} -eq ${true} ] ; then 	# hyperthread wrapping
		end1=$(( ${1} / 2 + ${startcore} ))
		end2=$(( ${end1} + ${cores} ))
		if [ $(( ${1} % 2 )) -eq 0 ] ; then
			end2=$(( ${end2} - 1 ))
		fi
		taskset="${startcore}-${end1},${start2}-${end2}"
	else								# hyperthread scanning
		if [ ${1} -lt ${cps} ] ; then taskset="${startcore}-$(( ${1} + ${startcore} ))"; return; fi
		filled=$(( ${1} / ( ${cps} * 2 ) * ${cps} ))
		modulus=$(( ${1} % ( ${cps} * 2 ) ))	# leftover cores added to saturated sockets
		if [ ${modulus} -gt ${cps} ] ; then
			taskset="${startcore}-$(( ${startcore} + ${filled} + ${cps} - 1 )),${start2}-$(( ${start2} + ${filled} + ${modulus} % ${cps} ))"
		else
			taskset="${startcore}-$(( ${startcore} + ${filled} + ${modulus} )),${start2}-$(( ${start2} + ${filled} - 1 ))"
		fi
	fi
}

# numtimes=5
numtimes=1

# num_threads='2 4 8 16 24 32'
# side_chan_threads='6 12 18 24 30' # must be mults of 6
num_threads='2'
side_chan_threads='6'

chan_size='10'
future_time='10'

# toggle benchmarks
spin=${true}
contend=${true}
sidechan=${true}
future=${true}
spin=${false}
contend=${false}
sidechan=${false}
# future=${false}

runCFA=${true}
runGO=${true}
runUCPP=${true}
# runCFA=${false}
runGO=${false}
# runUCPP=${false}

cfa=~/cfa-cc/driver/cfa

# Helpers to minimize code duplication

# repeats a command ${numtimes}
preprint=''
repeat_command() {
    t=1
    while [ ${t} -le ${numtimes} ] ; do
        echo -n -e ${preprint}
        "${@}"
        t=`expr ${t} + 1`
    done
}

# prints the leading info for a given run of a variant
print_header() {
    echo ${1}':'
    echo -e "cores\tthroughput (entries)"
}

# runs the current benchmark with provided args
# only works for standard-run benchmarks (not Akka)
# must split into pre and post args to be able to supply val of p
pre_args=''
post_args=''
single_run() {
    affinity ${1}
    preprint="${1}\t"
    repeat_command taskset -c ${taskset} ./a.${hostname} ${pre_args} ${1} ${post_args}
}

# runs the current bench for all processor vals
# works for standard benchs that dont need to set a config file (not Akka or CAF)
run_bench() {
    for p in ${num_threads} ; do
        single_run ${p}
    done
}

run_side_chan() {
    i=1
    for p in ${side_chan_threads} ; do
        affinity ${p}
        preprint="${p}\t"
        repeat_command taskset -c ${taskset} ./a.${hostname} ${pre_args} ${i} ${post_args}
        i=`expr ${i} + 1`
    done
}

run_future() {
    for p in ${num_threads} ; do
        pre_args=$(( ${p} - 1 ))
        affinity ${p}
        preprint="${p}\t"
        repeat_command taskset -c ${taskset} ./a.${hostname} ${pre_args} ${post_args}
    done
}

arch # get hostname

# set up leading info for python script
echo $numtimes
echo $num_threads
echo $side_chan_threads

if [ ${runCFA} -eq ${true} ]; then
    echo -n 'CFA '
fi
if [ ${runGO} -eq ${true} ]; then
    echo -n 'Go '
fi
echo ""

# done printing header info for output

# cfa flags
cfa_flags='-quiet -O3 -nodebug -DNDEBUG'

# UCPP flags
UCPPflags="-quiet -g -Wall -Wextra -O3 -nodebug -DNDEBUG -multi"
UCPP=~/ucpp/u++-7.0.0/bin/u++

# run the benchmarks

run_contend() {
    post_args=${1}

    if [ ${runCFA} -eq ${true} ] ; then
        cd cfa # CFA RUN
        print_header 'CFA'
        ${cfa} ${cfa_flags} '-DNUM_CHANS='${3} ${2}.cfa -o a.${hostname} > /dev/null 2>&1
        run_bench
        rm a.${hostname}
        cd - > /dev/null
    fi # done CFA

    if [ ${runGO} -eq ${true} ] ; then
        cd go/${2}${3} # Go RUN
        print_header 'Go'
        go build -o a.${hostname} > /dev/null 2>&1
        run_bench
        rm a.${hostname}
        cd - > /dev/null
    fi # done Go
}

# /usr/bin/time -f "%Uu %Ss %Er %Mkb"
if [ ${contend} -eq ${true} ] ; then
    echo "contend2: "
    run_contend ${chan_size} 'contend' '2'
    echo "contend4: "
    run_contend ${chan_size} 'contend' '4'
    echo "contend8: "
    run_contend ${chan_size} 'contend' '8'
fi

if [ ${spin} -eq ${true} ] ; then
    echo "spin2: "
    run_contend ${chan_size} 'spin' '2'
    echo "spin4: "
    run_contend ${chan_size} 'spin' '4'
    echo "spin8: "
    run_contend ${chan_size} 'spin' '8'
fi

if [ ${sidechan} -eq ${true} ] ; then
    echo "sidechan: "
    post_args=${chan_size}
    if [ ${runCFA} -eq ${true} ] ; then
        cd cfa # CFA RUN
        print_header 'CFA'
        ${cfa} ${cfa_flags} sidechan.cfa -o a.${hostname} > /dev/null 2>&1
        run_side_chan
        rm a.${hostname}
        cd - > /dev/null
    fi # done CFA

    if [ ${runGO} -eq ${true} ] ; then
        cd go/sidechan
        print_header 'Go'
        go build -o a.${hostname} > /dev/null 2>&1
        run_side_chan
        rm a.${hostname}
        cd - > /dev/null
    fi # done Go
fi

if [ ${future} -eq ${true} ] ; then
    echo "future: "
    post_args=${future_time}
    if [ ${runCFA} -eq ${true} ] ; then
        cd cfa # CFA RUN
        print_header 'CFA'
        ${cfa} ${cfa_flags} future.cfa -o a.${hostname} > /dev/null 2>&1
        run_future
        rm a.${hostname}
        cd - > /dev/null
    fi # done CFA

    if [ ${runUCPP} -eq ${true} ] ; then
        cd ucpp
        print_header 'uC++'
        ${UCPP} ${UCPPflags} future.cc -o a.${hostname} > /dev/null 2>&1
        run_future
        rm a.${hostname}
        cd - > /dev/null
    fi # done Go
fi

