Skip to content

Commit

Permalink
Add first CompMath task
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsynSA committed Nov 8, 2020
1 parent 70a4e91 commit 641fe4b
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
52 changes: 52 additions & 0 deletions CompMath1/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[@]}"
112 changes: 112 additions & 0 deletions CompMath1/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <mpi.h>
#include <unistd.h>
#include <cmath>

double acceleration(double t)
{
return sin(t);
}

void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, double y1, int rank, int size)
{
// 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];
}
}

}

int main(int argc, char** argv)
{
int rank = 0, size = 0, status = 0;
uint32_t traceSize = 0;
double t0 = 0, t1 = 0, dt = 0, y0 = 0, y1 = 0;
double* trace = 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 >> t0 >> t1 >> dt >> y0 >> y1;
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
traceSize = (t1 - t0)/dt;
trace = new double[traceSize];

input.close();
} else {
MPI_Bcast(&status, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (status != 0)
{
return 1;
}
}

calc(trace, traceSize, t0, dt, y0, y1, 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 trace;
return 1;
}

for (uint32_t i = 0; i < traceSize; i++)
{
output << " " << trace[i];
}
output << std::endl;
output.close();
delete trace;
}

MPI_Finalize();
return 0;
}
2 changes: 2 additions & 0 deletions CompMath1/tests/00/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0 9.424778 0.001 0 0
t0 t1=3*PI dt y0 y1
1 change: 1 addition & 0 deletions CompMath1/tests/00/output.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CompMath1/tests/01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0 9.424778 0.000002 0 -2
t0 t1=3*PI dt y0 y1
1 change: 1 addition & 0 deletions CompMath1/tests/01/output.txt

Large diffs are not rendered by default.

0 comments on commit 641fe4b

Please sign in to comment.