#!/bin/bash

ACCOUNT=nprg042s
PARTITION=mpi-homo-short
DATA_DIR='/home/_teaching/para/labtest/data'
INPUT_FILE="$DATA_DIR/$1"
if [ ! -f "$INPUT_FILE" ]; then
	echo "Input file $INPUT_FILE does not exist in $DATA_DIR directory"
	exit 1
fi

SERIAL_OUT="./$1.serial"
PARALLEL32_OUT="./$1.parallel32"
PARALLEL64_OUT="./$1.parallel64"
EXEC='./cpu-knapsack'

# remove the condition and possibly add `purge all` as make args if you want to rebuild every time
if [ ! -f "$EXEC" ]; then
	echo "Making the executable..."
	srun -p "$PARTITION" -A "$ACCOUNT" -n 1 make
fi

echo "Running serial ..."
ST=`srun -p "$PARTITION" -A "$ACCOUNT" -c 1 -n 1 "$EXEC" --algorithm serial "$INPUT_FILE" "$SERIAL_OUT"`
echo "Serial time: $ST"

echo "Running parallel (32 cores) ..."
PT32=`srun -p "$PARTITION" -A "$ACCOUNT" -c 32 -n 1 "$EXEC" --algorithm parallel "$INPUT_FILE" "$PARALLEL32_OUT"`
echo "Parallel time: $PT32"

echo "Verifying results using diff..."
diff "$SERIAL_OUT" "$PARALLEL32_OUT"
if [[ $? == 0 ]]; then
	echo "Verification OK"
fi

echo "Running parallel (64 cores) ..."
PT64=`srun -p "$PARTITION" -A "$ACCOUNT" -c 64 -n 1 "$EXEC" --algorithm parallel "$INPUT_FILE" "$PARALLEL64_OUT"`
echo "Parallel time: $PT64"

echo "Verifying results using diff..."
diff "$SERIAL_OUT" "$PARALLEL64_OUT"
if [[ $? == 0 ]]; then
	echo "Verification OK"
fi

echo -n "Speedup (32 cores): $ST/$PT32 = "
echo "$ST/$PT32" | bc -l
echo -n "Speedup (64 cores): $ST/$PT64 = "
echo "$ST/$PT64" | bc -l
