GCC Code Coverage Report


Directory: ./
File: tasks/samoylenko_i_conj_grad_method/seq/src/ops_seq.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 66 66 100.0%
Functions: 8 8 100.0%
Branches: 47 70 67.1%

Line Branch Exec Source
1 #include "samoylenko_i_conj_grad_method/seq/include/ops_seq.hpp"
2
3 #include <cmath>
4 #include <cstddef>
5 #include <vector>
6
7 #include "samoylenko_i_conj_grad_method/common/include/common.hpp"
8
9 namespace samoylenko_i_conj_grad_method {
10
11 96 SamoylenkoIConjGradMethodSEQ::SamoylenkoIConjGradMethodSEQ(const InType &in) {
12 SetTypeOfTask(GetStaticTypeOfTask());
13 GetInput() = in;
14 GetOutput().clear();
15 96 }
16
17 96 bool SamoylenkoIConjGradMethodSEQ::ValidationImpl() {
18
4/8
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 96 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 96 times.
96 return (GetInput().first > 0) && (GetInput().second >= 0 && GetInput().second <= 2) && GetOutput().empty();
19 }
20
21
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 bool SamoylenkoIConjGradMethodSEQ::PreProcessingImpl() {
22 GetOutput().clear();
23 96 return true;
24 }
25
26 namespace {
27
28 96 std::vector<double> BuildMatrix(size_t size, int variant) {
29 96 std::vector<double> matrix(size * size, 0.0);
30
31
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 96 times.
1632 for (size_t i = 0; i < size; ++i) {
32
3/4
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 512 times.
✗ Branch 3 not taken.
1536 switch (variant) {
33 512 case 0: {
34
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 32 times.
512 matrix[(i * size) + i] = 4.0;
35
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 32 times.
512 if (i > 0) {
36 480 matrix[(i * size) + (i - 1)] = 1.0;
37 }
38
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 32 times.
512 if (i + 1 < size) {
39 480 matrix[(i * size) + (i + 1)] = 1.0;
40 }
41 break;
42 }
43
44 512 case 1: {
45 512 matrix[(i * size) + i] = 5.0;
46 512 break;
47 }
48
49 512 case 2: {
50
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 16 times.
512 matrix[(i * size) + i] = 3.0;
51 512 size_t j = size - 1 - i;
52
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 16 times.
512 if (i != j) {
53 496 matrix[(i * size) + j] = -1.0;
54 496 matrix[(j * size) + i] = -1.0;
55 }
56 break;
57 }
58
59 default:
60 break;
61 }
62 }
63 96 return matrix;
64 }
65
66 336 void MatrixVectorMult(size_t size, const std::vector<double> &matrix, const std::vector<double> &vec,
67 std::vector<double> &result) {
68
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 336 times.
8576 for (size_t i = 0; i < size; ++i) {
69 double sum = 0.0;
70
2/2
✓ Branch 0 taken 368624 times.
✓ Branch 1 taken 8240 times.
376864 for (size_t j = 0; j < size; ++j) {
71 368624 sum += matrix[(i * size) + j] * vec[j];
72 }
73
74 8240 result[i] = sum;
75 }
76 336 }
77
78 double DotProduct(const std::vector<double> &first, const std::vector<double> &second) {
79 double sum = 0.0;
80
6/6
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 6704 times.
✓ Branch 3 taken 240 times.
✓ Branch 4 taken 6704 times.
✓ Branch 5 taken 240 times.
15520 for (size_t i = 0; i < first.size(); ++i) {
81 14944 sum += first[i] * second[i];
82 }
83
84 return sum;
85 }
86
87 96 void ConjugateGradient(size_t size, const std::vector<double> &matrix, const std::vector<double> &vector,
88 std::vector<double> &x) {
89 const double eps = 1e-7;
90 const int iters = 2000;
91
92 96 std::vector<double> res(size);
93
1/4
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
96 std::vector<double> dir(size);
94
1/4
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
96 std::vector<double> matdir(size);
95
96 96 MatrixVectorMult(size, matrix, x, res);
97
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 96 times.
1632 for (size_t i = 0; i < size; ++i) {
98 1536 res[i] = vector[i] - res[i];
99 1536 dir[i] = res[i];
100 }
101
102 double res_dot = DotProduct(res, res);
103
104
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 for (int it = 0; it < iters; ++it) {
105
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 96 times.
336 if (std::sqrt(res_dot) < eps) {
106 break;
107 }
108
109 240 MatrixVectorMult(size, matrix, dir, matdir);
110
111 double matdir_dot = DotProduct(dir, matdir);
112
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
240 if (std::fabs(matdir_dot) < 1e-15) {
113 break; // so we dont divide by 0
114 }
115
116 240 double step = res_dot / matdir_dot;
117
118
2/2
✓ Branch 0 taken 6704 times.
✓ Branch 1 taken 240 times.
6944 for (size_t i = 0; i < size; ++i) {
119 6704 x[i] += step * dir[i];
120 6704 res[i] -= step * matdir[i];
121 }
122
123 double res_dot_new = DotProduct(res, res);
124 240 double conj_coef = res_dot_new / res_dot;
125
126
2/2
✓ Branch 0 taken 6704 times.
✓ Branch 1 taken 240 times.
6944 for (size_t i = 0; i < size; ++i) {
127 6704 dir[i] = res[i] + (conj_coef * dir[i]);
128 }
129
130 res_dot = res_dot_new;
131 }
132 96 }
133
134 } // namespace
135
136 96 bool SamoylenkoIConjGradMethodSEQ::RunImpl() {
137 96 const int n = GetInput().first;
138 96 const int variant = GetInput().second;
139
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (n <= 0) {
140 return false;
141 }
142
143 96 auto size = static_cast<size_t>(n);
144
145 96 std::vector<double> matrix = BuildMatrix(size, variant);
146
1/4
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
96 std::vector<double> vector(size, 1.0);
147
148
1/4
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
96 std::vector<double> x(size, 0.0);
149
150
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 ConjugateGradient(size, matrix, vector, x);
151
152
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 GetOutput() = x;
153 return true;
154 }
155
156 96 bool SamoylenkoIConjGradMethodSEQ::PostProcessingImpl() {
157 96 return !GetOutput().empty();
158 }
159
160 } // namespace samoylenko_i_conj_grad_method
161