#!/bin/bash
# Run freqmine benchmark

P=freqmine-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

ITERATIONS=12
TESTLIST=

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--min-threads)
		FREQMINE_MIN_THREADS=$2
		shift 2
		;;
	--max-threads)
		FREQMINE_MAX_THREADS=$2
		shift 2
		;;
	--iterations)
		FREQMINE_ITERATIONS=$2
		shift 2
		;;
	--size)
		FREQMINE_SIZE=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

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

THREADS=
START_THREAD=$FREQMINE_MIN_THREADS
END_THREAD=$FREQMINE_MAX_THREADS
if [ $END_THREAD -gt 32 ]; then
	THREADS=`seq $START_THREAD 3 8`
	THREADS="$THREADS `seq 12 9 32`"
	THREADS="$THREADS `seq 48 31 $END_THREAD`"
elif [ $END_THREAD -gt 8 ]; then
	THREADS=`seq $START_THREAD 2 8`
	THREADS="$THREADS `seq 12 6 $END_THREAD`"
else
	THREADS=`seq $START_THREAD 2 $END_THREAD`
fi
if [ `echo $THREADS | awk '{print $NF}'` -ne $END_THREAD ]; then
	THREADS="$THREADS $END_THREAD"
fi

for NR_THREADS in $THREADS; do
	if [ $NR_THREADS -lt $FREQMINE_MIN_THREADS ]; then
		continue
	fi
	mmtests_activity process $NR_THREADS/$END_THREAD
	for SIZE in $FREQMINE_SIZE; do
		echo "Running $SIZE workload";
		#
		# IN_FILE and MIN_SUP are the benchmark input parameters.
		# Calculate the input data based on size of the run.
		#  - webdocs_[N]: Database with collection of [N] web HTML documents.
		#  - kosarak_[N]: Database with [N] anonymized click streams from an online newspaper.
		#
		#  The actual files are plain text, and quite cryptic (provided by parsec-3.0).
		#
		# The MIN_SUP (minimum-support) parameter acts as an offset for where to start scanning
		# the parsed (in-mem) datafile. These values are based on parsec, yet slightly adjusted
		# to increase runtime on some the smaller workloads. Smaller values will increase runtime.
		#
		if [ $SIZE = "large" ]; then
			IN_FILE=$SHELLPACK_SOURCES/freqmine-${VERSION}-installed/inputs/webdocs_250k.dat
			MIN_SUP=11000
		elif [ $SIZE = "medium" ]; then
			IN_FILE=$SHELLPACK_SOURCES/freqmine-${VERSION}-installed/inputs/kosarak_990k.dat
			MIN_SUP=730
		else # small or default/bogus
			IN_FILE=$SHELLPACK_SOURCES/freqmine-${VERSION}-installed/inputs/kosarak_500k.dat
			MIN_SUP=390
		fi

		monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
for ITERATION in `seq 1 $FREQMINE_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
			echo Starting threads $NR_THREADS/$FREQMINE_MAX_THREADS iteration $ITERATION/$FREQMINE_ITERATIONS
			export OMP_NUM_THREADS=$NR_THREADS
			save_rc $SHELLPACK_SOURCES/freqmine-${VERSION}-installed/freqmine $IN_FILE $MIN_SUP 2>&1 | \
			    tee $LOGDIR_RESULTS/freqmine-${NR_THREADS}-${ITERATION}.log
			recover_rc
			if [ $? -ne 0 ]; then
			    die Failed to run freqmine
			fi
done
		monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
	done
done

exit $SHELLPACK_SUCCESS
#### Description freqmine
#### Details freqmine-bench 7
