#!/bin/ksh

TEMP=temp.$$
TEMP2=temp1.$$
TEMP3=temp2.$$
TEMP_WORK=temp3.$$
TEMP_RESULTS=temp4.$$

FILE_PREFIX=$1

CASES="1 2 4 8"

if [ "${FILE_PREFIX}" = "" ]; then
    echo "Error: No file prefix specified"
    exit
fi

# check that output of 1 worker exists

ONE_WORKER="${FILE_PREFIX}.1"

if [ ! -f "${ONE_WORKER}" ]; then
    echo "Error: '${ONE_WORKER}' does not exist."
    exit
fi

# cut out total execs from each worker

j=0
touch ${TEMP}
touch ${TEMP_WORK}
for subcase in ${CASES}; do
    compare_file=${FILE_PREFIX}.${subcase}
    if [ ! -f "${compare_file}" ]; then
        echo "Error: '${compare_file}' does not exist"
        exit
    fi
    i=0
    cut -d" " -f 2 ${compare_file} > ${TEMP2}
    paste ${TEMP} ${TEMP2} > ${TEMP3}
    mv ${TEMP3} ${TEMP}
    (( j = j + 1 ))
    echo ${subcase} >> ${TEMP_WORK} 
done

# calculate speedup

awk '{ print $1/$1 " "  $1/$2 " "  $1/$3 " "  $1/$4}' ${TEMP} > ${TEMP_RESULTS} 
cp ${TEMP_RESULTS}  shava

touch ${FILE_PREFIX}.speedup

cp ${TEMP_WORK} ${FILE_PREFIX}.speedup
cat ${TEMP_RESULTS} |
while read line; do
    cp /dev/null ${TEMP}
    for num in $line; do
        echo ${num} >> ${TEMP}
    done
    paste  ${FILE_PREFIX}.speedup ${TEMP} > ${TEMP3}
    mv ${TEMP3}  ${FILE_PREFIX}.speedup
done

rm ${TEMP} ${TEMP2} ${TEMP3} ${TEMP_WORK} ${TEMP_RESULTS}

# plotit

gnuplot > ${FILE_PREFIX}.speedup.ps <<+
set title 'Speedup on 1024x1024 image'
show title
set xlabel 'number of workers'
show xlabel
set ylabel 'speedup'
show ylabel
set terminal postscript 'eps'
set logscale x
plot '${FILE_PREFIX}.speedup' using 1:2 title '4x4 chunk' with lines,  '${FILE_PREFIX}.speedup' using 1:3 title '16x16 chunk' with lines, '${FILE_PREFIX}.speedup' using 1:4 title '32x32 chunk' with lines, '${FILE_PREFIX}.speedup' using 1:5 title '64x64 chunk' with lines, '${FILE_PREFIX}.speedup' using 1:6 title '256x256 chunk' with lines, '${FILE_PREFIX}.speedup' using 1:7 title '512x512 chunk' with lines
exit
+ 


