Skip to content

Commit

Permalink
Add second CompMath task
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsynSA committed Nov 9, 2020
1 parent 641fe4b commit 4d1489f
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 0 deletions.
52 changes: 52 additions & 0 deletions CompMath2/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/bash
# run.sh <number of repetitions>
compiler="mpic++"
flags="-std=c++11 -Wall -Wextra -Werror"
src="./src/main.cpp"
build="./build"
exe="$build/task"
tests_dir="./tests"

echo "[CLEAR]"
echo " rm $build -r"
rm $build -rf

echo "[BUILD]"
echo " mkdir $build"
mkdir $build
echo " $compiler $src -o $exe $flags"
$compiler $src -o $exe $flags

if [ ! $? -eq 0 ]; then
echo "[ERROR] Can't compile $src"
exit 1
fi

SUCCESS_TESTS=()
FAIL_TESTS=()

for test_dir in $tests_dir/*; do
for proc in {1..4}; do
test="$(basename $test_dir)_p$proc"
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
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
echo "[TEST $test] OK ($DIFF ms)"
SUCCESS_TESTS+=($test)
else
echo "[TEST $test] DIFF FAIL($DIFF ms): vimdiff $build/$test.txt $test_dir/output.txt"
FAIL_TESTS+=($test)
fi
done
done
echo "==========="
echo "SUCCESSFUL: ${SUCCESS_TESTS[@]}"
echo "FAIL: ${FAIL_TESTS[@]}"
148 changes: 148 additions & 0 deletions CompMath2/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <mpi.h>
#include <unistd.h>
#include <cmath>

void calc(double* frame, uint32_t ySize, uint32_t xSize, double delta, int rank, int size)
{
if (rank == 0 && size > 0)
{
double diff = 0;
double* tmpFrame = new double[ySize * xSize];
// Prepare tmpFrame
for (uint32_t y = 0; y < ySize; y++)
{
tmpFrame[y*xSize] = frame[y*xSize];
tmpFrame[y*xSize + xSize - 1] = frame[y*xSize + xSize - 1];
}
for (uint32_t x = 1; x < xSize - 1; x++)
{
tmpFrame[x] = frame[x];
tmpFrame[(ySize - 1)*xSize + x] = frame[(ySize - 1)*xSize + x];
}
// Calculate first iteration
for (uint32_t y = 1; y < ySize - 1; y++)
{
for (uint32_t x = 1; x < xSize - 1; x++)
{
tmpFrame[y*xSize + x] = (frame[(y + 1)*xSize + x] + frame[(y - 1)*xSize + x] +\
frame[y*xSize + x + 1] + frame[y*xSize + x - 1])/4.0;
diff += std::abs(tmpFrame[y*xSize + x] - frame[y*xSize + x]);
}
}

double* currFrame = tmpFrame;
double* nextFrame = frame;
uint32_t iteration = 1;
// Calculate frames
while (diff > delta)
{
diff = 0;
for (uint32_t y = 1; y < ySize - 1; y++)
{
for (uint32_t x = 1; x < xSize - 1; x++)
{
nextFrame[y*xSize + x] = (currFrame[(y + 1)*xSize + x] + currFrame[(y - 1)*xSize + x] +\
currFrame[y*xSize + x + 1] + currFrame[y*xSize + x - 1])/4.0;
diff += std::abs(nextFrame[y*xSize + x] - currFrame[y*xSize + x]);
}
}
std::swap(currFrame, nextFrame);
iteration++;
}

// Copy result from tmp
if (iteration % 2 == 1)
{
for (uint32_t i = 0; i < xSize*ySize; i++)
{
frame[i] = tmpFrame[i];
}
}
delete tmpFrame;
}
}

int main(int argc, char** argv)
{
int rank = 0, size = 0, status = 0;
double delta = 0;
uint32_t ySize = 0, xSize = 0;
double* frame = 0;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (rank == 0)
{
// Check arguments
if (argc != 3)
{
std::cout << "[Error] Usage <inputfile> <output file>\n";
status = 1;
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
}

// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open())
{
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
status = 1;
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
return 1;
}

// Read arguments from input
input >> ySize >> xSize >> delta;
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);

frame = new double[ySize * xSize];

for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
input >> frame[y*xSize + x];
}
}
input.close();
} else {
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (status != 0)
{
return 1;
}
}

calc(frame, ySize, xSize, delta, rank, size);

if (rank == 0)
{
// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open())
{
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
delete frame;
return 1;
}
for (uint32_t y = 0; y < ySize; y++)
{
for (uint32_t x = 0; x < xSize; x++)
{
output << " " << frame[y*xSize + x];
}
output << std::endl;
}
output.close();
delete frame;
}

MPI_Finalize();
return 0;
}
5 changes: 5 additions & 0 deletions CompMath2/tests/00/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 4 0.0001
-1 -1 -1 -1
0 0 0 0
0 0 0 0
1 1 1 1
4 changes: 4 additions & 0 deletions CompMath2/tests/00/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-1 -1 -1 -1
0 -0.25 -0.25 0
0 0.25 0.25 0
1 1 1 1
21 changes: 21 additions & 0 deletions CompMath2/tests/01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
20 20 0.0001
0 0 0 0 0 0 0 0 0 9 9 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9 9 0 0 0 0 0 0 0 0 0
20 changes: 20 additions & 0 deletions CompMath2/tests/01/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
0 0 0 0 0 0 0 0 0 9 9 0 0 0 0 0 0 0 0 0
0 0.0616395 0.123279 0.187812 0.26392 0.371443 0.556654 0.940797 1.88601 4.6084 4.6084 1.88601 0.940797 0.556654 0.371443 0.26392 0.187812 0.123279 0.0616395 0
0 0.123279 0.243664 0.36405 0.496427 0.665196 0.914377 1.32052 1.99485 2.93918 2.93918 1.99485 1.32052 0.914377 0.665196 0.496427 0.36405 0.243664 0.123279 0
0 0.187812 0.36405 0.528296 0.692542 0.878538 1.11514 1.43207 1.8337 2.21428 2.21428 1.8337 1.43207 1.11514 0.878538 0.692542 0.528296 0.36405 0.187812 0
0 0.26392 0.496427 0.692542 0.86691 1.04128 1.23556 1.45892 1.69359 1.86997 1.86997 1.69359 1.45892 1.23556 1.04128 0.86691 0.692542 0.496427 0.26392 0
0 0.371443 0.665196 0.878538 1.04128 1.1841 1.32692 1.47446 1.61177 1.70205 1.70205 1.61177 1.47446 1.32692 1.1841 1.04128 0.878538 0.665196 0.371443 0
0 0.556654 0.914377 1.11514 1.23556 1.32692 1.41357 1.50022 1.57698 1.6244 1.6244 1.57698 1.50022 1.41357 1.32692 1.23556 1.11514 0.914377 0.556654 0
0 0.940797 1.32052 1.43207 1.45892 1.47446 1.50022 1.53587 1.57153 1.59419 1.59419 1.57153 1.53587 1.50022 1.47446 1.45892 1.43207 1.32052 0.940797 0
0 1.88601 1.99485 1.8337 1.69359 1.61177 1.57698 1.57153 1.57908 1.58663 1.58663 1.57908 1.57153 1.57698 1.61177 1.69359 1.8337 1.99485 1.88601 0
9 4.6084 2.93918 2.21428 1.86997 1.70205 1.6244 1.59419 1.58663 1.58663 1.58663 1.58663 1.59419 1.6244 1.70205 1.86997 2.21428 2.93918 4.6084 9
9 4.6084 2.93918 2.21428 1.86997 1.70205 1.6244 1.59419 1.58663 1.58663 1.58663 1.58663 1.59419 1.6244 1.70205 1.86997 2.21428 2.93918 4.6084 9
0 1.88601 1.99485 1.8337 1.69359 1.61177 1.57698 1.57153 1.57908 1.58663 1.58663 1.57908 1.57153 1.57698 1.61177 1.69359 1.8337 1.99485 1.88601 0
0 0.940797 1.32052 1.43207 1.45892 1.47446 1.50022 1.53587 1.57153 1.59419 1.59419 1.57153 1.53587 1.50022 1.47446 1.45892 1.43207 1.32052 0.940797 0
0 0.556654 0.914377 1.11514 1.23556 1.32692 1.41357 1.50022 1.57698 1.6244 1.6244 1.57698 1.50022 1.41357 1.32692 1.23556 1.11514 0.914377 0.556654 0
0 0.371443 0.665196 0.878538 1.04128 1.1841 1.32692 1.47446 1.61177 1.70205 1.70205 1.61177 1.47446 1.32692 1.1841 1.04128 0.878538 0.665196 0.371443 0
0 0.26392 0.496427 0.692542 0.86691 1.04128 1.23556 1.45892 1.69359 1.86997 1.86997 1.69359 1.45892 1.23556 1.04128 0.86691 0.692542 0.496427 0.26392 0
0 0.187812 0.36405 0.528296 0.692542 0.878538 1.11514 1.43207 1.8337 2.21428 2.21428 1.8337 1.43207 1.11514 0.878538 0.692542 0.528296 0.36405 0.187812 0
0 0.123279 0.243664 0.36405 0.496427 0.665196 0.914377 1.32052 1.99485 2.93918 2.93918 1.99485 1.32052 0.914377 0.665196 0.496427 0.36405 0.243664 0.123279 0
0 0.0616395 0.123279 0.187812 0.26392 0.371443 0.556654 0.940797 1.88601 4.6084 4.6084 1.88601 0.940797 0.556654 0.371443 0.26392 0.187812 0.123279 0.0616395 0
0 0 0 0 0 0 0 0 0 9 9 0 0 0 0 0 0 0 0 0
Loading

0 comments on commit 4d1489f

Please sign in to comment.