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

# bench_cores='1 2 4 8 16 24 32'
bench_cores='8 16 24 32 48'
# bench_cores='32'

# toggle benchmarks
executor=${true}
matrix=${true}
repeat=${true}
balance=${true}
static=${false}
dynamic=${false}

# executor config
batch='1'
nRounds='500'

# matrix config
size='3000'

# repeat config
messages='100000'
n_repeats='100'

# balance config
nOneRounds='2000'
nMultiRounds='800'

# static config
n_static_sends='10000000'

# dynamic config
n_dynamic_sends='10000000'

# names=('NOSTEAL' 'CLOSE' 'SEARCH')
# var_flags=('-D__STEAL=1 -DRAND=1' '-D__STEAL=1 -DCLOSE=1' '-D__STEAL=1 -DSEARCH=1')

names=('SEARCH')
var_flags=('-D__STEAL=1 -DSEARCH=1')


# names=('NOSTEAL' 'SEARCH' 'RAND' 'SEARCHC' 'RANDC')
# var_flags=('' '-D__STEAL=1 -DSEARCH=1' '-D__STEAL=1 -DRAND=1' '-D__STEAL=1 -DSEARCH=1 -DCYCLE' '-D__STEAL=1 -DRAND=1 -DCYCLE')

# names=('NOSTEAL' 'RAND' 'CLOSE' 'SEARCH')
# var_flags=('' '-D__STEAL=1 -DRAND=1' '-D__STEAL=1 -DCLOSE=1' '-D__STEAL=1 -DSEARCH=1')

cfa_flags='-quiet -O3 -nodebug -DNDEBUG'

cfa=~/cfa-cc/driver/cfa

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

arch nasus

echo $numtimes
echo $bench_cores

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

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

if [ ${executor} -eq ${true} ] ; then
    echo "executor"
    for i in ${!names[@]}; do
        echo ${names[$i]}':'
        echo -e "proc\ttime (s)"
        ${cfa} ${cfa_flags} ${var_flags[$i]} executor.cfa > /dev/null 2>&1
        for p in ${bench_cores} ; do
            affinity ${p}
            # echo "taskset -c ${startcore}-`expr ${startcore} + ${p} - 1` ./a.out 40000 100 ${nRounds} ${p} ${batch}"
            preprint="${p}\t"
            repeat_command taskset -c ${taskset} ./a.out 40000 100 ${nRounds} ${p} ${batch}
        done
        rm a.out
    done
    echo ""
fi

if [ ${matrix} -eq ${true} ] ; then
    echo "matrix"
    for i in ${!names[@]}; do
        echo ${names[$i]}
        echo -e "proc\ttime (s)"
        ${cfa} ${cfa_flags} ${var_flags[$i]} matrix.cfa > /dev/null 2>&1
        for p in ${bench_cores} ; do
            affinity ${p}
            preprint="${p}\t"
            repeat_command taskset -c ${taskset} ./a.out ${size} ${size} ${size} ${p}
        done
        rm a.out
    done
    echo ""
fi

if [ ${repeat} -eq ${true} ] ; then
    echo "repeat"
    echo -e "proc\ttime (s)"
    for i in ${!names[@]}; do
        echo ${names[$i]}
        ${cfa} ${cfa_flags} ${var_flags[$i]} repeat.cfa > /dev/null 2>&1
        for p in ${bench_cores} ; do 
            affinity ${p}
            preprint="${p}\t"
            repeat_command taskset -c ${taskset} ./a.out ${messages} ${p} 256 ${n_repeats}
        done
        rm a.out
    done
    echo ""
fi


if [ ${static} -eq ${true} ] ; then
    echo "static"
        for i in ${!names[@]}; do
            echo ${names[$i]}
            ${cfa} ${cfa_flags} ${var_flags[$i]} static.cfa > /dev/null 2>&1
            repeat_command taskset -c ${startcore} ./a.out ${n_static_sends}
            rm a.out
        done
    echo ""
fi

if [ ${dynamic} -eq ${true} ] ; then
    echo "dynamic"
        for i in ${!names[@]}; do
            echo ${names[$i]}
            ${cfa} ${cfa_flags} ${var_flags[$i]} dynamic.cfa > /dev/null 2>&1
            repeat_command taskset -c ${startcore} ./a.out ${n_dynamic_sends}
        done
    echo ""
fi


if [ ${balance} -eq ${true} ] ; then
    echo "balance_one"
    for i in ${!names[@]}; do
        echo ${names[$i]}':'
        echo -e "proc\ttime (s)"
        ${cfa} ${cfa_flags} ${var_flags[$i]} balance.cfa > /dev/null 2>&1
        for p in ${bench_cores} ; do 
            affinity ${p}
            preprint="${p}\t"
            repeat_command taskset -c ${taskset} ./a.out 32 32 ${nOneRounds} ${p}
        done
        rm a.out
    done
    echo ""
    echo "balance_multi"
    for i in ${!names[@]}; do
        echo ${names[$i]}':'
        echo -e "proc\ttime (s)"
        ${cfa} ${cfa_flags} ${var_flags[$i]} -DMULTI balance.cfa > /dev/null 2>&1
        for p in ${bench_cores} ; do 
            affinity ${p}
            preprint="${p}\t"
            repeat_command taskset -c ${taskset} ./a.out 32 32 ${nMultiRounds} ${p}
        done
        rm a.out
    done
    echo ""
fi

