#!/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=11

# locks=('-DLOCKS=L1' '-DLOCKS=L2' '-DLOCKS=L3' '-DLOCKS=L4' '-DLOCKS=L5' '-DLOCKS=L6' '-DLOCKS=L7' '-DLOCKS=L8')
# locks='1 2 3 4 5 6 7 8'
lock_flags=('-DLOCKS=L2' '-DLOCKS=L4' '-DLOCKS=L8')
locks=('2' '4' '8')

num_threads='2 4 8 16 24 32'
# num_threads='2 4 8'

# toggle benchmarks
order=${true}
rand=${true}
baseline=${true}

runCFA=${true}
runCPP=${true}
# runCFA=${false}
# runCPP=${false}

cfa=~/cfa-cc/driver/cfa
cpp=g++

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

arch # get hostname

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

for i in ${!locks[@]}; do
        echo -n ${locks[$i]}' '
done
echo ""

if [ ${runCFA} -eq ${true} ] && [ ${order} -eq ${true} ]; then
    echo -n 'CFA-order '
fi
if [ ${runCPP} -eq ${true} ] && [ ${order} -eq ${true} ]; then
    echo -n 'CPP-order '
fi
if [ ${runCFA} -eq ${true} ] && [ ${baseline} -eq ${true} ]; then
    echo -n 'CFA-baseline '
fi
if [ ${runCPP} -eq ${true} ] && [ ${baseline} -eq ${true} ]; then
    echo -n 'CPP-baseline '
fi
if [ ${runCFA} -eq ${true} ] && [ ${rand} -eq ${true} ]; then
    echo -n 'CFA-rand '
fi
if [ ${runCPP} -eq ${true} ] && [ ${rand} -eq ${true} ]; then
    echo -n 'CPP-rand '
fi
echo ""

# done printing header info for output

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

# cpp flagse
cpp_flags='-O3 -std=c++17 -lpthread -pthread -DNDEBUG'

# run the benchmarks

run_order() {
    post_args=${1}

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

    if [ ${runCPP} -eq ${true} ] ; then
        cd cpp # CPP RUN
        print_header 'CPP-'${3}
        ${cpp} ${cpp_flags} ${2} ${3}.cc -o a.${hostname} > /dev/null 2>&1
        run_bench
        rm a.${hostname}
        cd - > /dev/null
    fi # done CPP
}

# /usr/bin/time -f "%Uu %Ss %Er %Mkb"

for i in ${!locks[@]}; do
    echo "locks: "${locks[$i]}
    if [ ${order} -eq ${true} ] ; then
        run_order ${locks[$i]} ${lock_flags[$i]} 'order'
    fi
    if [ ${baseline} -eq ${true} ] ; then
        run_order ${locks[$i]} ${lock_flags[$i]} 'baseline'
    fi
    if [ ${rand} -eq ${true} ] ; then
        run_order ${locks[$i]} '-DLOCKS=L8' 'rand'
    fi
done


