| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "barkalova_m_mult_matrix_ccs/omp/include/ops_omp.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 24 times.
✗ Branch 3 not taken.
|
24 | BarkalovaMMultMatrixCcsOMP::BarkalovaMMultMatrixCcsOMP(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 24 | GetOutput() = CCSMatrix{}; | |
| 18 | 24 | } | |
| 19 | |||
| 20 | 24 | bool BarkalovaMMultMatrixCcsOMP::ValidationImpl() { | |
| 21 | const auto &[A, B] = GetInput(); | ||
| 22 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (A.cols != B.rows) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
24 | if (A.rows <= 0 || A.cols <= 0 || B.rows <= 0 || B.cols <= 0) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | 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 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
|
24 | 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 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
48 | 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 | 24 | bool BarkalovaMMultMatrixCcsOMP::PreProcessingImpl() { | |
| 41 | 24 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | namespace { | ||
| 45 | constexpr double kEpsilon = 1e-10; | ||
| 46 | |||
| 47 | 24 | void TransponirMatr(const CCSMatrix &a, CCSMatrix &at) { | |
| 48 | 24 | at.rows = a.cols; | |
| 49 | 24 | at.cols = a.rows; | |
| 50 | 24 | at.nnz = a.nnz; | |
| 51 | |||
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (a.nnz == 0) { |
| 53 | at.values.clear(); | ||
| 54 | at.row_indices.clear(); | ||
| 55 | ✗ | at.col_ptrs.assign(at.cols + 1, 0); | |
| 56 | ✗ | return; | |
| 57 | } | ||
| 58 | |||
| 59 | 24 | std::vector<int> row_count(at.cols, 0); | |
| 60 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 24 times.
|
92 | for (int i = 0; i < a.nnz; i++) { |
| 61 | 68 | row_count[a.row_indices[i]]++; | |
| 62 | } | ||
| 63 | |||
| 64 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | at.col_ptrs.resize(at.cols + 1); |
| 65 | 24 | at.col_ptrs[0] = 0; | |
| 66 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (int i = 0; i < at.cols; i++) { |
| 67 | 60 | at.col_ptrs[i + 1] = at.col_ptrs[i] + row_count[i]; | |
| 68 | } | ||
| 69 | |||
| 70 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | at.values.resize(a.nnz); |
| 71 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | at.row_indices.resize(a.nnz); |
| 72 | |||
| 73 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<int> current_pos(at.cols, 0); |
| 74 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 24 times.
|
80 | for (int col = 0; col < a.cols; col++) { |
| 75 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 56 times.
|
124 | for (int i = a.col_ptrs[col]; i < a.col_ptrs[col + 1]; i++) { |
| 76 | 68 | int row = a.row_indices[i]; | |
| 77 | 68 | Complex val = a.values[i]; | |
| 78 | |||
| 79 | 68 | int pos = at.col_ptrs[row] + current_pos[row]; | |
| 80 | 68 | at.values[pos] = val; | |
| 81 | 68 | at.row_indices[pos] = col; | |
| 82 | 68 | current_pos[row]++; | |
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | 176 | Complex ComputeScalarProduct(const CCSMatrix &at, const CCSMatrix &b, int row_a, int col_b) { | |
| 88 | Complex sum = Complex(0.0, 0.0); | ||
| 89 | |||
| 90 | 176 | int ks = at.col_ptrs[row_a]; | |
| 91 | 176 | int ls = b.col_ptrs[col_b]; | |
| 92 | 176 | int kf = at.col_ptrs[row_a + 1]; | |
| 93 | 176 | int lf = b.col_ptrs[col_b + 1]; | |
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 176 times.
|
412 | while ((ks < kf) && (ls < lf)) { |
| 96 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 192 times.
|
236 | if (at.row_indices[ks] < b.row_indices[ls]) { |
| 97 | 44 | ks++; | |
| 98 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 136 times.
|
192 | } else if (at.row_indices[ks] > b.row_indices[ls]) { |
| 99 | 56 | ls++; | |
| 100 | } else { | ||
| 101 | sum += at.values[ks] * b.values[ls]; | ||
| 102 | 136 | ks++; | |
| 103 | 136 | ls++; | |
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | 176 | return sum; | |
| 108 | } | ||
| 109 | |||
| 110 | bool IsNonZero(const Complex &val) { | ||
| 111 | return std::abs(val.real()) > kEpsilon || std::abs(val.imag()) > kEpsilon; | ||
| 112 | } | ||
| 113 | |||
| 114 | } // namespace | ||
| 115 | |||
| 116 | 24 | bool BarkalovaMMultMatrixCcsOMP::RunImpl() { | |
| 117 | 24 | const auto &a = GetInput().first; | |
| 118 | 24 | const auto &b = GetInput().second; | |
| 119 | |||
| 120 | try { | ||
| 121 | 24 | CCSMatrix at; | |
| 122 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | TransponirMatr(a, at); |
| 123 | |||
| 124 | 24 | CCSMatrix c; | |
| 125 | 24 | c.rows = a.rows; | |
| 126 | 24 | c.cols = b.cols; | |
| 127 | |||
| 128 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<std::vector<int>> col_rows(c.cols); |
| 129 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<std::vector<Complex>> col_vals(c.cols); |
| 130 | |||
| 131 | 24 | #pragma omp parallel for schedule(static) default(none) shared(at, b, c, col_rows, col_vals) | |
| 132 | for (int j = 0; j < c.cols; j++) { | ||
| 133 | std::vector<int> rows; | ||
| 134 | std::vector<Complex> vals; | ||
| 135 | |||
| 136 | for (int i = 0; i < at.cols; i++) { | ||
| 137 | Complex sum = ComputeScalarProduct(at, b, i, j); | ||
| 138 | if (IsNonZero(sum)) { | ||
| 139 | rows.push_back(i); | ||
| 140 | vals.push_back(sum); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | col_rows[j] = std::move(rows); | ||
| 145 | col_vals[j] = std::move(vals); | ||
| 146 | } | ||
| 147 | |||
| 148 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<int> col_ptrs = {0}; |
| 149 | 24 | std::vector<int> row_indices; | |
| 150 | 24 | std::vector<Complex> values; | |
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 24 times.
|
92 | for (int j = 0; j < c.cols; j++) { |
| 153 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 68 times.
|
184 | for (size_t idx = 0; idx < col_rows[j].size(); idx++) { |
| 154 | row_indices.push_back(col_rows[j][idx]); | ||
| 155 | values.push_back(col_vals[j][idx]); | ||
| 156 | } | ||
| 157 |
1/4✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
68 | col_ptrs.push_back(static_cast<int>(values.size())); |
| 158 | } | ||
| 159 | |||
| 160 | c.values = std::move(values); | ||
| 161 | c.row_indices = std::move(row_indices); | ||
| 162 | c.col_ptrs = std::move(col_ptrs); | ||
| 163 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | c.nnz = static_cast<int>(c.values.size()); |
| 164 | |||
| 165 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput() = c; |
| 166 | return true; | ||
| 167 | |||
| 168 |
0/2✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
24 | } catch (const std::exception &) { |
| 169 | return false; | ||
| 170 | ✗ | } | |
| 171 | } | ||
| 172 | |||
| 173 | 24 | bool BarkalovaMMultMatrixCcsOMP::PostProcessingImpl() { | |
| 174 | const auto &c = GetOutput(); | ||
| 175 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | return c.rows > 0 && c.cols > 0 && c.col_ptrs.size() == static_cast<size_t>(c.cols) + 1; |
| 176 | } | ||
| 177 | |||
| 178 | } // namespace barkalova_m_mult_matrix_ccs | ||
| 179 |