Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 127 additions & 3 deletions CompMath1/run.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,134 @@
#! /bin/bash
# run.sh <number of repetitions>
compiler="mpic++"
flags="-std=c++11 -Wall -Wextra -Werror"
flags="-std=c++11 -Wall -Werror"
src="./src/main.cpp"
build="./build"
exe="$build/task"
tests_dir="./tests"
delta=0.000001
procnum=4
mpiexecflags=

function compare_curves {
filein1=$1
filein2=$2

if ! command -v bc &> /dev/null; then
echo "bc is required, run 'sudo apt --yes install bc'," \
"'sudo yum -y install bc' or something similar"
exit 1
fi

if [ ! -f "$filein1" ]; then
echo "File '$filein1' does not exist"
return 1
fi
if [ ! -f "$filein2" ]; then
echo "File '$filein2' does not exist"
return 1
fi

# Count and compare number of lines in input files
# wc also prints file num, so cut only prints linenum
linenum1=$(wc -l $filein1 | cut -d " " -f1)
linenum2=$(wc -l $filein2 | cut -d " " -f1)
if [ $linenum1 -ne $linenum2 ]; then
echo "Different number of points($linenum1 vs. $linenum2)"
echo "$filein1, $filein2"
return 1
fi
if [ $linenum1 -eq 0 ]; then
echo "Zero points found in both files"
return 1
fi

# Create file that will have two values in each line to compare
filedata=$(mktemp)
paste $filein1 $filein2 > $filedata

# Create file with actual bc code, which
# itself is pretty much obvious
filebccode=$(mktemp)
cat >$filebccode<<EOF
define abs(i) {
if (i < 0) return (-i);
return (i);
}

off = read();
len = read();
for (p = off; p < off+len; p++) {
v1 = read();
v2 = read();
if (abs(v1-v2) >= $delta) {
print p, "\n";
halt;
}
}
print -1, "\n";
EOF

# dry run of mktemp, get the name to create fifo
tmppipe=$(mktemp -u)
mkfifo $tmppipe
# Attach pipe to 3rd fd
exec 3<>$tmppipe

# This will divide filedata in smaller files with prefix "$filedata."
split -n l/$procnum $filedata "$filedata."

TIME_START=$(date +%s)
# Split created $procnum small files, launch bc for each of them
chunkoffset=0
for file in $(ls $filedata\.*); do
chunksize=$(wc -l $file | cut -d " " -f1)
echo $chunkoffset $chunksize $(cat $file) | bc $filebccode >&3 &
chunkoffset=$(($chunkoffset+$chunksize))
done

# Collect execution results
failed=0
for ((i=0; i < $procnum; i++)); do
read pointnum <&3
if [ $pointnum -ne -1 ]; then
echo "Failed point is $pointnum(numeration starts from 0)"
failed=1
fi
done
echo "bc took $(($(date +%s)-$TIME_START)) sec on $procnum CPUs"

# cleanup, remove temporary files
rm -f $filebccode $tmppipe $filedata $filedata.*
# close pipe
exec 3>&-

return $failed
}

# Create 01 test output file if it does not exist
testoutfilechecksum="8dbe67f912e0ccbc53411adfeaa42b9b"
testoutfilename="tests/01/output.txt"
testinfilename="tests/01/input.txt"
if [ ! -f $testoutfilename ]; then
echo "Creating $testoutfilename file"
mkdir $build 2> /dev/null
$compiler $src -o $exe -DREFERENCE $flags
if [ ! $? -eq 0 ]; then
echo "[ERROR] Failed to compile $src with -DREFERENCE"
exit 1
fi
mpiexec $mpiexecflags -np 1 $exe $testinfilename $testoutfilename
fi

# Assuming that md5sum is available out of the box
curchecksum=$(md5sum $testoutfilename | cut -d " " -f1)
if [ $curchecksum != $testoutfilechecksum ]; then
echo "[WARNING] Weird, $testoutfilename checksum does not match." \
"Did you edit test output/code inside of REFERENCE conditional" \
"compilation block and then created test output? If you want" \
"to fix it, try deleting $testoutfilename and running run.sh again"
fi

echo "[CLEAR]"
echo " rm $build -r"
Expand All @@ -31,14 +154,15 @@ for test_dir in $tests_dir/*; do
printf "\n[TEST $test]\n"
echo "mpiexec -np $proc $exe $test_dir/input.txt $build/$test.txt"
START=$(date +%s%N)
mpiexec -np $proc $exe $test_dir/input.txt $build/$test.txt
mpiexec $mpiexecflags -np $proc $exe $test_dir/input.txt $build/$test.txt
END=$(date +%s%N)
DIFF=$((($END - $START)/1000000))
if [ ! $? -eq 0 ]; then
echo "[TEST $test] RUNTIME FAIL"
continue;
fi
if cmp -s $build/$test.txt $test_dir/output.txt; then
compare_curves $build/$test.txt $test_dir/output.txt
if (($? == 0)); then
echo "[TEST $test] OK ($DIFF ms)"
SUCCESS_TESTS+=($test)
else
Expand Down
33 changes: 31 additions & 2 deletions CompMath1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ double acceleration(double t)
return sin(t);
}

#ifdef REFERENCE /* don't edit reference implementation, it is needed for tests */
void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, double y1, int rank, int size)
{
// Sighting shot
Expand All @@ -35,8 +36,37 @@ void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, do
trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2];
}
}
}
#else /* REFERENCE */
void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, double y1, int rank, int size)
{
/* Feel free to edit code in this function */

// Sighting shot
double v0 = 0;
if (rank == 0 && size > 0)
{
trace[0] = y0;
trace[1] = y0 + dt*v0;
for (uint32_t i = 2; i < traceSize; i++)
{
trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2];
}
}

// The final shot
if (rank == 0 && size > 0)
{
v0 = (y1 - trace[traceSize - 1])/(dt*traceSize);
trace[0] = y0;
trace[1] = y0 + dt*v0;
for (uint32_t i = 2; i < traceSize; i++)
{
trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2];
}
}
}
#endif

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -100,9 +130,8 @@ int main(int argc, char** argv)

for (uint32_t i = 0; i < traceSize; i++)
{
output << " " << trace[i];
output << std::fixed << std::setprecision(13) << trace[i] << std::endl;
}
output << std::endl;
output.close();
delete trace;
}
Expand Down
9,425 changes: 9,424 additions & 1 deletion CompMath1/tests/00/output.txt

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion CompMath1/tests/01/output.txt

This file was deleted.