#!/bin/sh

LOG=data_$(date +%F_%T).log
PERF=./perforate.sh
INFO=./build/info2

get_scop() {
    $PERF -e "$1"
}

get_info() {
    IFS=";"
    $INFO "$1"| \
    while read a x y; do
        printf "%s %s\n" $x $y
    done
}

perforate() {
    if [ "$1" = "--nopluto" ]; then
        shift
        $PERF -e -p -g -o "$2" -s "$3" -l "$4" "$1"
    elif [ "$1" = "--noperf" ]; then
        shift
        $PERF  -e -g -P -o "$2" "$1"
    else
        $PERF -a -o "$2" -s "$3" -l "$4" "$1"
    fi
}

compile() {
    gcc -s -O3 -I../polybench-c-4.2.1-beta/utilities -I. -DPOLYBENCH_TIME  -o "$2" ../polybench-c-4.2.1-beta/utilities/polybench.c "$1" -lm "$3"
}

correlation() {
    target=codes/correlation/correlation

    # accurate baseline
    # FIXME: baseline is currently without pluto, we need also accurate+pluto
baseline() {
    output="$(dirname ${target})/$(basename ${target})"
    type=baseline
    if [ "$1" = "--pluto" ]; then
        perforate --noperf "${target}.c" "${output}_baseline.c"
        type=plutobaseline
    else
        cat "${target}.c" > "${output}_baseline.c"
    fi
    compile "${output}_baseline.c" "${output}" "codes/${code}/data.o"
    printf "${code},${type},,," >> ${LOG}
    ./${output} >> ${LOG}
}

permute() {
    get_scop "${target}.c" "${target}_perf.c"
    get_info "${target}.c.openscop" | \
    while read x y; do
        # pluto
        output="$(dirname ${target})/${x}_${y}_$(basename ${target})"
        perforate "${target}.c" "${output}.c" "$x" "$y"
        echo compile "${output}.c" "${output}"
        compile "${output}.c" "${output}" "codes/${code}/data.o"
        printf "${code},perfpluto,%s,%s," "${x}" "${y}" >> ${LOG}
        ./${output} >> ${LOG}
        # no pluto
        perforate --nopluto "${target}.c" "${output}.c" "$x" "$y"
        echo compile "${output}.c" "${output}"
        compile "${output}.c" "${output}" "codes/${code}/data.o"
        printf "${code},perf,%s,%s," "${x}" "${y}" >> ${LOG}
        ./${output} >> ${LOG}
    done
}

correlation() {
    code=correlation
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

jacobi_2d() {
    code=jacobi-2d
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

deriche() {
    code=deriche
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

floyd_warshall() {
    code=floyd-warshall
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

doitgen() {
    code=doitgen
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

seidel_2d() {
    code=seidel-2d
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

fdtd_2d() {
    code=fdtd-2d
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

adi() {
    code=adi
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

heat_3d() {
    code=heat-3d
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}

jacobi_1d() {
    code=jacobi-1d
    target="codes/${code}/${code}"

    baseline
    baseline --pluto

    permute
}


hotspot() {
    code=hotspot
    target=codes/hotspot/hotspot_openmp

    # accurate
    output="$(dirname ${target})/$(basename ${target})"
    g++ -fopenmp -o "${output}" "${target}.cpp" codes/hotspot/data.o
    printf "hotspot,,," >> ${LOG}
    ./${output} 1024 1024 100 1 ~/rodinia_3.1/data/hotspot/temp_1024 ~/rodinia_3.1/data/hotspot/power_1024 /dev/null >> ${LOG}

    get_scop "${target}.cpp" "${target}_perf.cpp"
    get_info "${target}.cpp.openscop" | \
    while read x y; do
        output="$(dirname ${target})/${x}_${y}_$(basename ${target})"
        perforate "${target}.cpp" "${output}.cpp" "$x" "$y"
        g++ -fopenmp -o "${output}" "${output}.cpp" codes/hotspot/data.o
        printf "hotspot,%s,%s," "${x}" "${y}" >> ${LOG}
        ./${output} 1024 1024 100 1 ~/rodinia_3.1/data/hotspot/temp_1024 ~/rodinia_3.1/data/hotspot/power_1024 /dev/null >> ${LOG}
    done
}

correlation
deriche
floyd_warshall
doitgen
seidel_2d
fdtd_2d
adi
heat_3d
jacobi_2d
jacobi_1d
#hotspot
