| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kruglova_a_conjugate_gradient_sle/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "kruglova_a_conjugate_gradient_sle/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace kruglova_a_conjugate_gradient_sle { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | 264 | void MatrixVectorMultiply(const std::vector<double> &a, const std::vector<double> &p, std::vector<double> &ap, int n) { | |
| 13 |
2/2✓ Branch 0 taken 11016 times.
✓ Branch 1 taken 264 times.
|
11280 | for (int i = 0; i < n; ++i) { |
| 14 | 11016 | ap[i] = 0.0; | |
| 15 |
2/2✓ Branch 0 taken 841144 times.
✓ Branch 1 taken 11016 times.
|
852160 | for (int j = 0; j < n; ++j) { |
| 16 | 841144 | ap[i] += a[(i * n) + j] * p[j]; | |
| 17 | } | ||
| 18 | } | ||
| 19 | 264 | } | |
| 20 | } // namespace | ||
| 21 | |||
| 22 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | KruglovaAConjGradSleSEQ::KruglovaAConjGradSleSEQ(const InType &in) { |
| 23 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 24 | GetInput() = in; | ||
| 25 | 48 | } | |
| 26 | |||
| 27 | 48 | bool KruglovaAConjGradSleSEQ::ValidationImpl() { | |
| 28 | const auto &in = GetInput(); | ||
| 29 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (in.size <= 0) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 | |||
| 33 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (in.A.size() != static_cast<size_t>(in.size) * static_cast<size_t>(in.size)) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (in.b.size() != static_cast<size_t>(in.size)) { |
| 37 | ✗ | return false; | |
| 38 | } | ||
| 39 | return true; | ||
| 40 | } | ||
| 41 | |||
| 42 | 48 | bool KruglovaAConjGradSleSEQ::PreProcessingImpl() { | |
| 43 | 48 | GetOutput().assign(GetInput().size, 0.0); | |
| 44 | 48 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 | 48 | bool KruglovaAConjGradSleSEQ::RunImpl() { | |
| 48 | 48 | const auto &a = GetInput().A; | |
| 49 | 48 | const auto &b = GetInput().b; | |
| 50 | 48 | int n = GetInput().size; | |
| 51 | auto &x = GetOutput(); | ||
| 52 | |||
| 53 | 48 | std::vector<double> r = b; | |
| 54 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<double> p = r; |
| 55 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<double> ap(n, 0.0); |
| 56 | |||
| 57 | double rsold = 0.0; | ||
| 58 |
2/2✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 48 times.
|
1408 | for (int i = 0; i < n; ++i) { |
| 59 | 1360 | rsold += r[i] * r[i]; | |
| 60 | } | ||
| 61 | |||
| 62 | const double tolerance = 1e-8; | ||
| 63 | |||
| 64 |
1/2✓ Branch 0 taken 264 times.
✗ Branch 1 not taken.
|
264 | for (int iter = 0; iter < n * 2; ++iter) { |
| 65 | 264 | MatrixVectorMultiply(a, p, ap, n); | |
| 66 | |||
| 67 | double p_ap = 0.0; | ||
| 68 |
2/2✓ Branch 0 taken 11016 times.
✓ Branch 1 taken 264 times.
|
11280 | for (int i = 0; i < n; ++i) { |
| 69 | 11016 | p_ap += p[i] * ap[i]; | |
| 70 | } | ||
| 71 | |||
| 72 | if (std::abs(p_ap) < 1e-15) { | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | |||
| 76 | 264 | double alpha = rsold / p_ap; | |
| 77 |
2/2✓ Branch 0 taken 11016 times.
✓ Branch 1 taken 264 times.
|
11280 | for (int i = 0; i < n; ++i) { |
| 78 | 11016 | x[i] += alpha * p[i]; | |
| 79 | 11016 | r[i] -= alpha * ap[i]; | |
| 80 | } | ||
| 81 | |||
| 82 | double rsnew = 0.0; | ||
| 83 |
2/2✓ Branch 0 taken 11016 times.
✓ Branch 1 taken 264 times.
|
11280 | for (int i = 0; i < n; ++i) { |
| 84 | 11016 | rsnew += r[i] * r[i]; | |
| 85 | } | ||
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 48 times.
|
264 | if (std::sqrt(rsnew) < tolerance) { |
| 88 | break; | ||
| 89 | } | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 9656 times.
✓ Branch 1 taken 216 times.
|
9872 | for (int i = 0; i < n; ++i) { |
| 92 | 9656 | p[i] = r[i] + ((rsnew / rsold) * p[i]); | |
| 93 | } | ||
| 94 | rsold = rsnew; | ||
| 95 | } | ||
| 96 | 48 | return true; | |
| 97 | } | ||
| 98 | |||
| 99 | 48 | bool KruglovaAConjGradSleSEQ::PostProcessingImpl() { | |
| 100 | 48 | return true; | |
| 101 | } | ||
| 102 | |||
| 103 | } // namespace kruglova_a_conjugate_gradient_sle | ||
| 104 |