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