GCC Code Coverage Report


Directory: ./
File: tasks/spichek_d_jacobi/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 35 35 100.0%
Functions: 5 5 100.0%
Branches: 20 32 62.5%

Line Branch Exec Source
1 #include "spichek_d_jacobi/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm> // min, max
6 #include <cmath> // abs
7 #include <cstddef> // size_t
8 #include <utility> // std::move
9 #include <vector> // std::vector
10
11 #include "spichek_d_jacobi/common/include/common.hpp"
12
13 namespace spichek_d_jacobi {
14
15 14 SpichekDJacobiMPI::SpichekDJacobiMPI(InType in) : input_(std::move(in)) {
16 SetTypeOfTask(GetStaticTypeOfTask());
17 14 }
18
19
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 bool SpichekDJacobiMPI::ValidationImpl() {
20 const auto &[A, b, eps, max_iter] = input_;
21
4/8
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 14 times.
14 return !A.empty() && A.size() == b.size() && eps > 0 && max_iter > 0;
22 }
23
24 14 bool SpichekDJacobiMPI::PreProcessingImpl() {
25 GetInput() = input_;
26 14 GetOutput().assign(std::get<1>(input_).size(), 0.0);
27 14 return true;
28 }
29
30 14 bool SpichekDJacobiMPI::RunImpl() {
31 const auto &[A, b, eps, max_iter] = GetInput();
32 14 int rank = 0;
33 14 int size = 0;
34 14 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35 14 MPI_Comm_size(MPI_COMM_WORLD, &size);
36
37 14 size_t n = b.size();
38 14 std::vector<double> x(n, 0.0);
39
1/4
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
14 std::vector<double> x_new(n, 0.0);
40
41 14 size_t chunk = (n + size - 1) / size;
42 14 size_t begin = rank * chunk;
43 14 size_t end = std::min(begin + chunk, n);
44
45
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 8 times.
718 for (int iter = 0; iter < max_iter; ++iter) {
46 710 double local_diff = 0.0;
47
48
2/2
✓ Branch 0 taken 1310 times.
✓ Branch 1 taken 710 times.
2020 for (size_t i = begin; i < end; ++i) {
49 double sum = 0.0;
50
2/2
✓ Branch 0 taken 5224 times.
✓ Branch 1 taken 1310 times.
6534 for (size_t j = 0; j < n; ++j) {
51
2/2
✓ Branch 0 taken 3914 times.
✓ Branch 1 taken 1310 times.
5224 if (j != i) {
52 3914 sum += A[i][j] * x[j];
53 }
54 }
55 1310 x_new[i] = (b[i] - sum) / A[i][i];
56 1310 local_diff = std::max(local_diff, std::abs(x_new[i] - x[i]));
57 }
58
59
1/2
✓ Branch 1 taken 710 times.
✗ Branch 2 not taken.
710 MPI_Allreduce(MPI_IN_PLACE, x_new.data(), static_cast<int>(n), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
60
61 710 double global_diff = 0.0;
62
1/2
✓ Branch 1 taken 710 times.
✗ Branch 2 not taken.
710 MPI_Allreduce(&local_diff, &global_diff, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
63
64
1/2
✓ Branch 1 taken 710 times.
✗ Branch 2 not taken.
710 x = x_new;
65
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 6 times.
710 if (global_diff < eps) {
66 break;
67 }
68 }
69
70
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 GetOutput() = x;
71 14 return true;
72 }
73
74 14 bool SpichekDJacobiMPI::PostProcessingImpl() {
75 14 return true;
76 }
77
78 } // namespace spichek_d_jacobi
79