| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "barkalova_m_mult_matrix_ccs/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <complex> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <exception> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "barkalova_m_mult_matrix_ccs/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace barkalova_m_mult_matrix_ccs { | ||
| 13 | |||
| 14 |
1/2✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
48 | BarkalovaMMultMatrixCcsSEQ::BarkalovaMMultMatrixCcsSEQ(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 48 | GetOutput() = CCSMatrix{}; | |
| 18 | 48 | } | |
| 19 | |||
| 20 | 48 | bool BarkalovaMMultMatrixCcsSEQ::ValidationImpl() { | |
| 21 | const auto &[A, B] = GetInput(); | ||
| 22 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (A.cols != B.rows) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 |
3/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
|
48 | if (A.rows <= 0 || A.cols <= 0 || B.rows <= 0 || B.cols <= 0) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
48 | if (A.col_ptrs.size() != static_cast<size_t>(A.cols) + 1 || B.col_ptrs.size() != static_cast<size_t>(B.cols) + 1) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 |
4/8✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 48 times.
✗ Branch 7 not taken.
|
48 | if (A.col_ptrs.empty() || A.col_ptrs[0] != 0 || B.col_ptrs.empty() || B.col_ptrs[0] != 0) { |
| 32 | return false; | ||
| 33 | } | ||
| 34 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
96 | if (std::cmp_not_equal(A.nnz, A.values.size()) || std::cmp_not_equal(B.nnz, B.values.size())) { |
| 35 | ✗ | return false; | |
| 36 | } | ||
| 37 | return true; | ||
| 38 | } | ||
| 39 | |||
| 40 | 48 | bool BarkalovaMMultMatrixCcsSEQ::PreProcessingImpl() { | |
| 41 | 48 | return true; | |
| 42 | } | ||
| 43 | namespace { | ||
| 44 | constexpr double kEpsilon = 1e-10; | ||
| 45 | |||
| 46 | 48 | void TransponirMatr(const CCSMatrix &a, CCSMatrix &at) { | |
| 47 | 48 | at.rows = a.cols; | |
| 48 | 48 | at.cols = a.rows; | |
| 49 | 48 | at.nnz = a.nnz; | |
| 50 | |||
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (a.nnz == 0) { |
| 52 | at.values.clear(); | ||
| 53 | at.row_indices.clear(); | ||
| 54 | ✗ | at.col_ptrs.assign(at.cols + 1, 0); | |
| 55 | ✗ | return; | |
| 56 | } | ||
| 57 | |||
| 58 | 48 | std::vector<int> row_count(at.cols, 0); | |
| 59 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 48 times.
|
184 | for (int i = 0; i < a.nnz; i++) { |
| 60 | 136 | row_count[a.row_indices[i]]++; | |
| 61 | } | ||
| 62 | |||
| 63 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | at.col_ptrs.resize(at.cols + 1); |
| 64 | 48 | at.col_ptrs[0] = 0; | |
| 65 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < at.cols; i++) { |
| 66 | 120 | at.col_ptrs[i + 1] = at.col_ptrs[i] + row_count[i]; | |
| 67 | } | ||
| 68 | |||
| 69 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | at.values.resize(a.nnz); |
| 70 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | at.row_indices.resize(a.nnz); |
| 71 | |||
| 72 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<int> current_pos(at.cols, 0); |
| 73 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 48 times.
|
160 | for (int col = 0; col < a.cols; col++) { |
| 74 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 112 times.
|
248 | for (int i = a.col_ptrs[col]; i < a.col_ptrs[col + 1]; i++) { |
| 75 | 136 | int row = a.row_indices[i]; | |
| 76 | 136 | Complex val = a.values[i]; | |
| 77 | |||
| 78 | 136 | int pos = at.col_ptrs[row] + current_pos[row]; | |
| 79 | 136 | at.values[pos] = val; | |
| 80 | 136 | at.row_indices[pos] = col; | |
| 81 | 136 | current_pos[row]++; | |
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | 352 | Complex ComputeScalarProduct(const CCSMatrix &at, const CCSMatrix &b, int row_a, int col_b) { | |
| 87 | Complex sum = Complex(0.0, 0.0); | ||
| 88 | |||
| 89 | 352 | int ks = at.col_ptrs[row_a]; | |
| 90 | 352 | int ls = b.col_ptrs[col_b]; | |
| 91 | 352 | int kf = at.col_ptrs[row_a + 1] - 1; | |
| 92 | 352 | int lf = b.col_ptrs[col_b + 1] - 1; | |
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 472 times.
✓ Branch 1 taken 352 times.
|
824 | while ((ks <= kf) && (ls <= lf)) { |
| 95 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 384 times.
|
472 | if (at.row_indices[ks] < b.row_indices[ls]) { |
| 96 | 88 | ks++; | |
| 97 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 272 times.
|
384 | } else if (at.row_indices[ks] > b.row_indices[ls]) { |
| 98 | 112 | ls++; | |
| 99 | } else { | ||
| 100 | sum += at.values[ks] * b.values[ls]; | ||
| 101 | 272 | ks++; | |
| 102 | 272 | ls++; | |
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | 352 | return sum; | |
| 107 | } | ||
| 108 | 136 | void ProcessColumn(const CCSMatrix &at, const CCSMatrix &b, int col_idx, std::vector<Complex> &values, | |
| 109 | std::vector<int> &rows, int &nz) { | ||
| 110 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 136 times.
|
488 | for (int i = 0; i < at.cols; i++) { |
| 111 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 232 times.
|
352 | Complex sum = ComputeScalarProduct(at, b, i, col_idx); |
| 112 | |||
| 113 |
3/4✓ Branch 0 taken 120 times.
✓ Branch 1 taken 232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 120 times.
|
352 | if (std::abs(sum.real()) > kEpsilon || std::abs(sum.imag()) > kEpsilon) { |
| 114 | values.push_back(sum); | ||
| 115 | rows.push_back(i); | ||
| 116 | 232 | nz++; | |
| 117 | } | ||
| 118 | } | ||
| 119 | 136 | } | |
| 120 | 48 | void MultMatrix(const CCSMatrix &a, const CCSMatrix &b, CCSMatrix &c) { | |
| 121 | 48 | CCSMatrix at; | |
| 122 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | TransponirMatr(a, at); |
| 123 | |||
| 124 | 48 | std::vector<Complex> values; | |
| 125 | 48 | std::vector<int> rows; | |
| 126 | 48 | std::vector<int> col_ptrs; | |
| 127 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | col_ptrs.push_back(0); |
| 128 | 48 | int nz = 0; | |
| 129 | |||
| 130 | 48 | c.rows = a.rows; | |
| 131 | 48 | c.cols = b.cols; | |
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 48 times.
|
184 | for (int j = 0; j < c.cols; j++) { |
| 134 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | ProcessColumn(at, b, j, values, rows, nz); |
| 135 | col_ptrs.push_back(nz); | ||
| 136 | } | ||
| 137 | |||
| 138 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | c.values = values; |
| 139 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | c.row_indices = rows; |
| 140 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | c.col_ptrs = col_ptrs; |
| 141 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | c.nnz = nz; |
| 142 | 48 | } | |
| 143 | |||
| 144 | } // namespace | ||
| 145 | |||
| 146 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | bool BarkalovaMMultMatrixCcsSEQ::RunImpl() { |
| 147 | const auto &[a, b] = GetInput(); | ||
| 148 | |||
| 149 | try { | ||
| 150 | 48 | CCSMatrix c; | |
| 151 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | MultMatrix(a, b, c); |
| 152 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetOutput() = c; |
| 153 | return true; | ||
| 154 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
48 | } catch (const std::exception &) { |
| 155 | return false; | ||
| 156 | ✗ | } | |
| 157 | } | ||
| 158 | |||
| 159 | 48 | bool BarkalovaMMultMatrixCcsSEQ::PostProcessingImpl() { | |
| 160 | const auto &c = GetOutput(); | ||
| 161 |
3/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
|
48 | return c.rows > 0 && c.cols > 0 && c.col_ptrs.size() == static_cast<size_t>(c.cols) + 1; |
| 162 | } | ||
| 163 | |||
| 164 | } // namespace barkalova_m_mult_matrix_ccs | ||
| 165 |