#!/usr/bin/env bash

set -euo pipefail

function run-measure-rodinia-srad {
	local bench_iterations="5"

	local srad_rows="1024"
	local srad_cols="1024"
	local srad_y1="0"
	local srad_y2="127"
	local srad_x1="0"
	local srad_x2="127"
	local srad_lambda="0.5"
	local srad_iterations="2000"

	local smt="$(cat /sys/devices/system/cpu/smt/active)"

	if [ ! "${smt}" = "0" ]; then
		echo "Hyperthreading / SMT is not disabled! exiting..." 1>&2
		return 1
	fi

	mkdir output/rodinia-srad
	pushd output/rodinia-srad

	local timestamp="$(date +%s)"
	echo "OUTPUT DIR: output/rodinia-srad/${timestamp}"

	mkdir "${timestamp}"
	pushd "${timestamp}"

	local -a programs=(
		"rodinia-srad-c-gcc"
		"rodinia-srad-cpp-gcc"
		"rodinia-srad-fortran-gcc"
		"rodinia-srad-c-clang"
		"rodinia-srad-cpp-clang"
		"rodinia-srad-fortran-llvm-flang"
		"rodinia-srad-julia"
	)

	for program in "${programs[@]}"; do
		mkdir "${program}"
		pushd "${program}"

		for iteration in $(seq 1 "${bench_iterations}"); do
			mkdir "${iteration}"
			pushd "${iteration}"

			sleep 30

			"$(which time)" -f "%M" -o "memory_usage.txt" \
				ema-measure "${program}" "${srad_rows}" "${srad_cols}" "${srad_y1}" "${srad_y2}" \
					"${srad_x1}" "${srad_x2}" "${srad_lambda}" "${srad_iterations}"

			sleep 30

			popd
		done

		popd
	done

	popd
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
	run-measure-rodinia-srad "$@"
fi
