| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kichanova_k_lin_system_by_conjug_grad/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "kichanova_k_lin_system_by_conjug_grad/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace kichanova_k_lin_system_by_conjug_grad { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | double ComputeDotProduct(const std::vector<double> &a, const std::vector<double> &b, int n) { | ||
| 13 | double result = 0.0; | ||
| 14 |
6/6✓ Branch 0 taken 472 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 2804 times.
✓ Branch 3 taken 435 times.
✓ Branch 4 taken 2804 times.
✓ Branch 5 taken 435 times.
|
7046 | for (int i = 0; i < n; ++i) { |
| 15 | 6080 | result += a[i] * b[i]; | |
| 16 | } | ||
| 17 | return result; | ||
| 18 | } | ||
| 19 | |||
| 20 | 435 | void ComputeMatrixVectorProduct(const std::vector<double> &a, const std::vector<double> &v, std::vector<double> &result, | |
| 21 | int n) { | ||
| 22 | 435 | const auto stride = static_cast<size_t>(n); | |
| 23 |
2/2✓ Branch 0 taken 2804 times.
✓ Branch 1 taken 435 times.
|
3239 | for (int i = 0; i < n; ++i) { |
| 24 | double sum = 0.0; | ||
| 25 | 2804 | const double *a_row = &a[static_cast<size_t>(i) * stride]; | |
| 26 |
2/2✓ Branch 0 taken 20752 times.
✓ Branch 1 taken 2804 times.
|
23556 | for (int j = 0; j < n; ++j) { |
| 27 | 20752 | sum += a_row[j] * v[j]; | |
| 28 | } | ||
| 29 | 2804 | result[i] = sum; | |
| 30 | } | ||
| 31 | 435 | } | |
| 32 | |||
| 33 | void UpdateSolution(std::vector<double> &x, const std::vector<double> &p, double alpha, int n) { | ||
| 34 |
2/2✓ Branch 0 taken 2804 times.
✓ Branch 1 taken 435 times.
|
3239 | for (int i = 0; i < n; ++i) { |
| 35 | 2804 | x[i] += alpha * p[i]; | |
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | void UpdateResidual(std::vector<double> &r, const std::vector<double> &ap, double alpha, int n) { | ||
| 40 |
2/2✓ Branch 0 taken 2804 times.
✓ Branch 1 taken 435 times.
|
3239 | for (int i = 0; i < n; ++i) { |
| 41 | 2804 | r[i] -= alpha * ap[i]; | |
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | void UpdateSearchDirection(std::vector<double> &p, const std::vector<double> &r, double beta, int n) { | ||
| 46 |
2/2✓ Branch 0 taken 2332 times.
✓ Branch 1 taken 339 times.
|
2671 | for (int i = 0; i < n; ++i) { |
| 47 | 2332 | p[i] = r[i] + (beta * p[i]); | |
| 48 | } | ||
| 49 | } | ||
| 50 | } // namespace | ||
| 51 | |||
| 52 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | KichanovaKLinSystemByConjugGradSEQ::KichanovaKLinSystemByConjugGradSEQ(const InType &in) { |
| 53 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 54 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | GetInput() = in; |
| 55 | 96 | GetOutput() = OutType(); | |
| 56 | 96 | } | |
| 57 | |||
| 58 | 96 | bool KichanovaKLinSystemByConjugGradSEQ::ValidationImpl() { | |
| 59 | const InType &input_data = GetInput(); | ||
| 60 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (input_data.n <= 0) { |
| 61 | return false; | ||
| 62 | } | ||
| 63 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (input_data.A.size() != static_cast<size_t>(input_data.n) * static_cast<size_t>(input_data.n)) { |
| 64 | return false; | ||
| 65 | } | ||
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (input_data.b.size() != static_cast<size_t>(input_data.n)) { |
| 67 | ✗ | return false; | |
| 68 | } | ||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | 96 | bool KichanovaKLinSystemByConjugGradSEQ::PreProcessingImpl() { | |
| 73 | 96 | GetOutput().assign(GetInput().n, 0.0); | |
| 74 | 96 | return true; | |
| 75 | } | ||
| 76 | |||
| 77 | 96 | bool KichanovaKLinSystemByConjugGradSEQ::RunImpl() { | |
| 78 | const InType &input_data = GetInput(); | ||
| 79 | OutType &x = GetOutput(); | ||
| 80 | |||
| 81 | 96 | int n = input_data.n; | |
| 82 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (n == 0) { |
| 83 | return false; | ||
| 84 | } | ||
| 85 | |||
| 86 | 96 | const std::vector<double> &a = input_data.A; | |
| 87 | const std::vector<double> &b = input_data.b; | ||
| 88 | 96 | double epsilon = input_data.epsilon; | |
| 89 | |||
| 90 | 96 | std::vector<double> r(n); | |
| 91 |
1/4✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
96 | std::vector<double> p(n); |
| 92 |
1/4✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
96 | std::vector<double> ap(n); |
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 472 times.
✓ Branch 1 taken 96 times.
|
568 | for (int i = 0; i < n; i++) { |
| 95 | 472 | r[i] = b[i]; | |
| 96 | 472 | p[i] = r[i]; | |
| 97 | } | ||
| 98 | |||
| 99 | double rr_old = ComputeDotProduct(r, r, n); | ||
| 100 | 96 | double residual_norm = std::sqrt(rr_old); | |
| 101 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (residual_norm < epsilon) { |
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | 96 | int max_iter = n * 1000; | |
| 106 |
1/2✓ Branch 0 taken 435 times.
✗ Branch 1 not taken.
|
435 | for (int iter = 0; iter < max_iter; iter++) { |
| 107 | 435 | ComputeMatrixVectorProduct(a, p, ap, n); | |
| 108 | |||
| 109 | double p_ap = ComputeDotProduct(p, ap, n); | ||
| 110 |
1/2✓ Branch 0 taken 435 times.
✗ Branch 1 not taken.
|
435 | if (std::abs(p_ap) < 1e-30) { |
| 111 | break; | ||
| 112 | } | ||
| 113 | |||
| 114 | 435 | double alpha = rr_old / p_ap; | |
| 115 | UpdateSolution(x, p, alpha, n); | ||
| 116 | UpdateResidual(r, ap, alpha, n); | ||
| 117 | |||
| 118 | double rr_new = ComputeDotProduct(r, r, n); | ||
| 119 | 435 | residual_norm = std::sqrt(rr_new); | |
| 120 |
2/2✓ Branch 0 taken 339 times.
✓ Branch 1 taken 96 times.
|
435 | if (residual_norm < epsilon) { |
| 121 | break; | ||
| 122 | } | ||
| 123 | |||
| 124 | 339 | double beta = rr_new / rr_old; | |
| 125 | UpdateSearchDirection(p, r, beta, n); | ||
| 126 | |||
| 127 | rr_old = rr_new; | ||
| 128 | } | ||
| 129 | |||
| 130 | return true; | ||
| 131 | } | ||
| 132 | |||
| 133 | 96 | bool KichanovaKLinSystemByConjugGradSEQ::PostProcessingImpl() { | |
| 134 | 96 | return true; | |
| 135 | } | ||
| 136 | |||
| 137 | } // namespace kichanova_k_lin_system_by_conjug_grad | ||
| 138 |