#!/bin/bash
# This script installs thp memscale
P=thpmemscale-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--max-threads)
		THPMEMSCALE_MAX_THREADS=$2
		shift 2
		;;
	--memory)
		THPMEMSCALE_MEMORY=$2
		shift 2
		;;

	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/thpmemscale-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/thpmemscale-${VERSION}-installed ]; then
	$SHELLPACK_INCLUDE/shellpack-install-thpmemscale -v ${VERSION}  || die thpmemscale install script returned error
fi
cd $SHELLPACK_SOURCES/thpmemscale-${VERSION}-installed || die Failed to cd to thpmemscale install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo thpmemscale installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

cd $SHELLPACK_SOURCES/thpmemscale-${VERSION}-installed || die Failed to cd to thp memscale directory
# -C 0 -m 0 -c <cores> -b <memory>
THREADS=
START_THREAD=1
END_THREAD=$THPMEMSCALE_MAX_THREADS
if [ $END_THREAD -gt 8 ]; then
        THREADS=`seq $START_THREAD 8`
        THREADS="$THREADS `seq 12 4 $END_THREAD`"
else
        THREADS=`seq $START_THREAD $END_THREAD`
fi
for NR_THREADS in $THREADS; do
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	( ./thp_memscale -C 0 -m 0 -c $NR_THREADS -b $THPMEMSCALE_MEMORY \
		2>&1 | tee -a $LOGDIR_RESULTS/thpmemscale-$NR_THREADS ) \
			|| die Failed to run thp memscale
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
done

exit $SHELLPACK_SUCCESS
#### Description THP memory scaling benchmark from SGI
#### Details thpmemscale-bench 6
