| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "maslova_u_mult_matr_crs/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "maslova_u_mult_matr_crs/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace maslova_u_mult_matr_crs { | ||
| 13 | |||
| 14 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | MaslovaUMultMatrSTL::MaslovaUMultMatrSTL(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 40 | } | |
| 18 | |||
| 19 | 40 | bool MaslovaUMultMatrSTL::ValidationImpl() { | |
| 20 | const auto &input = GetInput(); | ||
| 21 | const auto &matrix_a = std::get<0>(input); | ||
| 22 | const auto &matrix_b = std::get<1>(input); | ||
| 23 | |||
| 24 |
3/6✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
|
40 | if (matrix_a.cols != matrix_b.rows || matrix_a.rows <= 0 || matrix_b.cols <= 0) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (matrix_a.row_ptr.size() != static_cast<size_t>(matrix_a.rows) + 1) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (matrix_b.row_ptr.size() != static_cast<size_t>(matrix_b.rows) + 1) { |
| 31 | ✗ | return false; | |
| 32 | } | ||
| 33 | return true; | ||
| 34 | } | ||
| 35 | |||
| 36 | 40 | bool MaslovaUMultMatrSTL::PreProcessingImpl() { | |
| 37 | const auto &matrix_a = std::get<0>(GetInput()); | ||
| 38 | const auto &matrix_b = std::get<1>(GetInput()); | ||
| 39 | auto &matrix_c = GetOutput(); | ||
| 40 | 40 | matrix_c.rows = matrix_a.rows; | |
| 41 | 40 | matrix_c.cols = matrix_b.cols; | |
| 42 | 40 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 64 | int MaslovaUMultMatrSTL::GetRowNNZ(int i, const CRSMatrix &a, const CRSMatrix &b, std::vector<int> &marker) { | |
| 46 | int row_nnz = 0; | ||
| 47 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 64 times.
|
160 | for (int j = a.row_ptr[i]; j < a.row_ptr[i + 1]; ++j) { |
| 48 | 96 | int col_a = a.col_ind[j]; | |
| 49 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 96 times.
|
240 | for (int k = b.row_ptr[col_a]; k < b.row_ptr[col_a + 1]; ++k) { |
| 50 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 56 times.
|
144 | int col_b = b.col_ind[k]; |
| 51 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 56 times.
|
144 | if (marker[col_b] != i) { |
| 52 | 88 | marker[col_b] = i; | |
| 53 | 88 | row_nnz++; | |
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | 64 | return row_nnz; | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 58 times.
|
64 | void MaslovaUMultMatrSTL::FillRowValues(int i, const CRSMatrix &a, const CRSMatrix &b, CRSMatrix &c, |
| 61 | std::vector<double> &acc, std::vector<int> &marker, std::vector<int> &used) { | ||
| 62 | used.clear(); | ||
| 63 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 64 times.
|
160 | for (int j = a.row_ptr[i]; j < a.row_ptr[i + 1]; ++j) { |
| 64 | 96 | int col_a = a.col_ind[j]; | |
| 65 | 96 | double val_a = a.values[j]; | |
| 66 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 96 times.
|
240 | for (int k = b.row_ptr[col_a]; k < b.row_ptr[col_a + 1]; ++k) { |
| 67 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 56 times.
|
144 | int col_b = b.col_ind[k]; |
| 68 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 56 times.
|
144 | if (marker[col_b] != i) { |
| 69 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | marker[col_b] = i; |
| 70 | used.push_back(col_b); | ||
| 71 | 88 | acc[col_b] = val_a * b.values[k]; | |
| 72 | } else { | ||
| 73 | 56 | acc[col_b] += val_a * b.values[k]; | |
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 8 times.
|
64 | if (!used.empty()) { |
| 79 | std::ranges::sort(used); | ||
| 80 | 56 | int write_pos = c.row_ptr[i]; | |
| 81 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 56 times.
|
144 | for (int col : used) { |
| 82 | 88 | c.values[write_pos] = acc[col]; | |
| 83 | 88 | c.col_ind[write_pos] = col; | |
| 84 | 88 | write_pos++; | |
| 85 | 88 | acc[col] = 0.0; | |
| 86 | } | ||
| 87 | } | ||
| 88 | 64 | } | |
| 89 | |||
| 90 | 40 | void MaslovaUMultMatrSTL::ComputeRowPtrSTL(int rows_a, int cols_b, int num_threads) { | |
| 91 | const auto &matrix_a = std::get<0>(GetInput()); | ||
| 92 | const auto &matrix_b = std::get<1>(GetInput()); | ||
| 93 | auto &matrix_c = GetOutput(); | ||
| 94 | |||
| 95 | 40 | std::vector<std::thread> threads; | |
| 96 | 40 | int chunk = rows_a / num_threads; | |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 40 times.
|
98 | for (int thread_idx = 0; thread_idx < num_threads; ++thread_idx) { |
| 99 | 58 | int start = thread_idx * chunk; | |
| 100 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 40 times.
|
58 | int end = (thread_idx == num_threads - 1) ? rows_a : (thread_idx + 1) * chunk; |
| 101 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | if (start < end) { |
| 102 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | threads.emplace_back([&, start, end, cols_b]() { |
| 103 | 58 | std::vector<int> marker(cols_b, -1); | |
| 104 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 58 times.
|
122 | for (int i = start; i < end; ++i) { |
| 105 | 64 | matrix_c.row_ptr[i + 1] = GetRowNNZ(i, matrix_a, matrix_b, marker); | |
| 106 | } | ||
| 107 | 58 | }); | |
| 108 | } | ||
| 109 | } | ||
| 110 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 40 times.
|
98 | for (auto &thread_item : threads) { |
| 111 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | thread_item.join(); |
| 112 | } | ||
| 113 | 40 | } | |
| 114 | |||
| 115 | 40 | void MaslovaUMultMatrSTL::ComputeValuesSTL(int rows_a, int cols_b, int num_threads) { | |
| 116 | const auto &matrix_a = std::get<0>(GetInput()); | ||
| 117 | const auto &matrix_b = std::get<1>(GetInput()); | ||
| 118 | auto &matrix_c = GetOutput(); | ||
| 119 | |||
| 120 | 40 | std::vector<std::thread> threads; | |
| 121 | 40 | int chunk = rows_a / num_threads; | |
| 122 | |||
| 123 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 40 times.
|
98 | for (int thread_idx = 0; thread_idx < num_threads; ++thread_idx) { |
| 124 | 58 | int start = thread_idx * chunk; | |
| 125 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 40 times.
|
58 | int end = (thread_idx == num_threads - 1) ? rows_a : (thread_idx + 1) * chunk; |
| 126 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | if (start < end) { |
| 127 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | threads.emplace_back([&, start, end, cols_b]() { |
| 128 | 58 | std::vector<double> acc(cols_b, 0.0); | |
| 129 |
1/4✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
58 | std::vector<int> marker(cols_b, -1); |
| 130 | 58 | std::vector<int> used; | |
| 131 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | used.reserve(cols_b); |
| 132 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 58 times.
|
122 | for (int i = start; i < end; ++i) { |
| 133 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | FillRowValues(i, matrix_a, matrix_b, matrix_c, acc, marker, used); |
| 134 | } | ||
| 135 | 58 | }); | |
| 136 | } | ||
| 137 | } | ||
| 138 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 40 times.
|
98 | for (auto &thread_item : threads) { |
| 139 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | thread_item.join(); |
| 140 | } | ||
| 141 | 40 | } | |
| 142 | |||
| 143 | 40 | bool MaslovaUMultMatrSTL::RunImpl() { | |
| 144 | const auto &matrix_a = std::get<0>(GetInput()); | ||
| 145 | const auto &matrix_b = std::get<1>(GetInput()); | ||
| 146 | auto &matrix_c = GetOutput(); | ||
| 147 | |||
| 148 | 40 | int rows_a = matrix_a.rows; | |
| 149 | 40 | int cols_b = matrix_b.cols; | |
| 150 | 40 | matrix_c.row_ptr.assign(static_cast<size_t>(rows_a) + 1, 0); | |
| 151 | |||
| 152 | 40 | int num_threads = std::max(1, ppc::util::GetNumThreads()); | |
| 153 | 40 | num_threads = std::min(num_threads, rows_a); | |
| 154 | |||
| 155 | 40 | ComputeRowPtrSTL(rows_a, cols_b, num_threads); | |
| 156 | |||
| 157 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 40 times.
|
104 | for (int i = 0; i < rows_a; ++i) { |
| 158 | 64 | matrix_c.row_ptr[static_cast<size_t>(i) + 1] += matrix_c.row_ptr[i]; | |
| 159 | } | ||
| 160 | |||
| 161 | 40 | matrix_c.values.resize(matrix_c.row_ptr[rows_a]); | |
| 162 | 40 | matrix_c.col_ind.resize(matrix_c.row_ptr[rows_a]); | |
| 163 | |||
| 164 | 40 | ComputeValuesSTL(rows_a, cols_b, num_threads); | |
| 165 | |||
| 166 | 40 | return true; | |
| 167 | } | ||
| 168 | |||
| 169 | 40 | bool MaslovaUMultMatrSTL::PostProcessingImpl() { | |
| 170 | 40 | return true; | |
| 171 | } | ||
| 172 | |||
| 173 | } // namespace maslova_u_mult_matr_crs | ||
| 174 |