| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sosnina_a_sparse_matrix_mult_crs_double/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <ranges> | ||
| 7 | #include <tuple> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "sosnina_a_sparse_matrix_mult_crs_double/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace sosnina_a_sparse_matrix_mult_crs_double { | ||
| 14 | |||
| 15 | 848 | SosninaAMatrixMultCRSSEQ::SosninaAMatrixMultCRSSEQ(InType in) | |
| 16 | : input_(std::move(in)), | ||
| 17 | 848 | n_rows_A_(std::get<6>(input_)), | |
| 18 | 848 | n_cols_A_(std::get<7>(input_)), | |
| 19 | 848 | n_cols_B_(std::get<8>(input_)) { | |
| 20 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 21 | 848 | GetOutput() = OutType(); | |
| 22 | 848 | } | |
| 23 | |||
| 24 | 848 | bool SosninaAMatrixMultCRSSEQ::ValidationImpl() { | |
| 25 | 848 | values_A_ = std::get<0>(input_); | |
| 26 | 848 | col_indices_A_ = std::get<1>(input_); | |
| 27 | 848 | row_ptr_A_ = std::get<2>(input_); | |
| 28 | 848 | values_B_ = std::get<3>(input_); | |
| 29 | 848 | col_indices_B_ = std::get<4>(input_); | |
| 30 | 848 | row_ptr_B_ = std::get<5>(input_); | |
| 31 | 848 | n_rows_A_ = std::get<6>(input_); | |
| 32 | 848 | n_cols_A_ = std::get<7>(input_); | |
| 33 | 848 | n_cols_B_ = std::get<8>(input_); | |
| 34 | |||
| 35 |
3/6✓ Branch 0 taken 848 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 848 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 848 times.
✗ Branch 5 not taken.
|
848 | if (n_rows_A_ <= 0 || n_cols_A_ <= 0 || n_cols_B_ <= 0) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 |
1/2✓ Branch 0 taken 848 times.
✗ Branch 1 not taken.
|
848 | if (!ValidateMatrixA()) { |
| 40 | return false; | ||
| 41 | } | ||
| 42 | |||
| 43 | 848 | if (!ValidateMatrixB()) { | |
| 44 | return false; | ||
| 45 | } | ||
| 46 | |||
| 47 | return true; | ||
| 48 | } | ||
| 49 | |||
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | bool SosninaAMatrixMultCRSSEQ::ValidateMatrixA() const { |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (row_ptr_A_.size() != static_cast<size_t>(n_rows_A_) + 1U) { |
| 52 | return false; | ||
| 53 | } | ||
| 54 | |||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (row_ptr_A_[0] != 0) { |
| 56 | return false; | ||
| 57 | } | ||
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 848 times.
|
2688 | for (size_t i = 0; i < row_ptr_A_.size() - 1; i++) { |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1840 times.
|
1840 | if (row_ptr_A_[i] > row_ptr_A_[i + 1]) { |
| 61 | return false; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (values_A_.size() != col_indices_A_.size()) { |
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 | static_cast<void>(std::ranges::begin(col_indices_A_)); | ||
| 70 | return std::ranges::all_of(col_indices_A_, | ||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3008 times.
|
3856 | [n_cols_a = n_cols_A_](int col_idx) { return col_idx >= 0 && col_idx < n_cols_a; }); |
| 72 | } | ||
| 73 | |||
| 74 | 848 | bool SosninaAMatrixMultCRSSEQ::ValidateMatrixB() const { | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | int n_rows_b = n_cols_A_; |
| 76 | |||
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (row_ptr_B_.size() != static_cast<size_t>(n_rows_b) + 1U) { |
| 78 | return false; | ||
| 79 | } | ||
| 80 | |||
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (row_ptr_B_[0] != 0) { |
| 82 | return false; | ||
| 83 | } | ||
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 1856 times.
✓ Branch 1 taken 848 times.
|
2704 | for (size_t i = 0; i < row_ptr_B_.size() - 1; i++) { |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1856 times.
|
1856 | if (row_ptr_B_[i] > row_ptr_B_[i + 1]) { |
| 87 | return false; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (values_B_.size() != col_indices_B_.size()) { |
| 92 | return false; | ||
| 93 | } | ||
| 94 | static_cast<void>(std::ranges::begin(col_indices_B_)); | ||
| 95 | return std::ranges::all_of(col_indices_B_, | ||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
|
4432 | [n_cols_b = n_cols_B_](int col_idx) { return col_idx >= 0 && col_idx < n_cols_b; }); |
| 97 | } | ||
| 98 | |||
| 99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | bool SosninaAMatrixMultCRSSEQ::PreProcessingImpl() { |
| 100 | values_C_.clear(); | ||
| 101 | col_indices_C_.clear(); | ||
| 102 | row_ptr_C_.clear(); | ||
| 103 | |||
| 104 | 848 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | 848 | bool SosninaAMatrixMultCRSSEQ::RunImpl() { | |
| 108 | 848 | row_ptr_C_.resize(n_rows_A_ + 1, 0); | |
| 109 | 848 | row_ptr_C_[0] = 0; | |
| 110 | |||
| 111 | 848 | std::vector<std::vector<double>> row_values(n_rows_A_); | |
| 112 |
1/2✓ Branch 1 taken 848 times.
✗ Branch 2 not taken.
|
848 | std::vector<std::vector<int>> row_cols(n_rows_A_); |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 848 times.
|
2688 | for (int i = 0; i < n_rows_A_; i++) { |
| 115 |
1/2✓ Branch 1 taken 1840 times.
✗ Branch 2 not taken.
|
1840 | ProcessRow(i, row_values[i], row_cols[i]); |
| 116 | 1840 | row_ptr_C_[i + 1] = row_ptr_C_[i] + static_cast<int>(row_cols[i].size()); | |
| 117 | } | ||
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 848 times.
|
2688 | for (int i = 0; i < n_rows_A_; i++) { |
| 120 |
1/2✓ Branch 1 taken 1840 times.
✗ Branch 2 not taken.
|
1840 | values_C_.insert(values_C_.end(), row_values[i].begin(), row_values[i].end()); |
| 121 |
1/2✓ Branch 1 taken 1840 times.
✗ Branch 2 not taken.
|
1840 | col_indices_C_.insert(col_indices_C_.end(), row_cols[i].begin(), row_cols[i].end()); |
| 122 | } | ||
| 123 | |||
| 124 | 848 | return true; | |
| 125 | 848 | } | |
| 126 | |||
| 127 | 1840 | void SosninaAMatrixMultCRSSEQ::ProcessRow(int row_idx, std::vector<double> &row_values, std::vector<int> &row_cols) { | |
| 128 | 1840 | int row_start_a = row_ptr_A_[row_idx]; | |
| 129 | 1840 | int row_end_a = row_ptr_A_[row_idx + 1]; | |
| 130 | |||
| 131 | 1840 | std::vector<double> temp_row(n_cols_B_, 0.0); | |
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 3008 times.
✓ Branch 1 taken 1840 times.
|
4848 | for (int k_idx = row_start_a; k_idx < row_end_a; k_idx++) { |
| 134 | 3008 | double a_val = values_A_[k_idx]; | |
| 135 | 3008 | int k = col_indices_A_[k_idx]; | |
| 136 | |||
| 137 | 3008 | int row_start_b = row_ptr_B_[k]; | |
| 138 | 3008 | int row_end_b = row_ptr_B_[k + 1]; | |
| 139 | |||
| 140 |
2/2✓ Branch 0 taken 6080 times.
✓ Branch 1 taken 3008 times.
|
9088 | for (int j_idx = row_start_b; j_idx < row_end_b; j_idx++) { |
| 141 | 6080 | double b_val = values_B_[j_idx]; | |
| 142 | 6080 | int j = col_indices_B_[j_idx]; | |
| 143 | |||
| 144 | 6080 | temp_row[j] += a_val * b_val; | |
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 |
2/2✓ Branch 0 taken 4112 times.
✓ Branch 1 taken 1840 times.
|
5952 | for (int j = 0; j < n_cols_B_; j++) { |
| 149 |
2/2✓ Branch 0 taken 3824 times.
✓ Branch 1 taken 288 times.
|
4112 | if (std::abs(temp_row[j]) > 1e-12) { |
| 150 | row_values.push_back(temp_row[j]); | ||
| 151 | row_cols.push_back(j); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 |
2/2✓ Branch 0 taken 1712 times.
✓ Branch 1 taken 128 times.
|
1840 | if (!row_cols.empty()) { |
| 156 |
1/2✓ Branch 1 taken 1712 times.
✗ Branch 2 not taken.
|
1712 | std::vector<std::pair<int, double>> pairs; |
| 157 |
1/2✓ Branch 1 taken 1712 times.
✗ Branch 2 not taken.
|
1712 | pairs.reserve(row_cols.size()); |
| 158 |
2/2✓ Branch 0 taken 3824 times.
✓ Branch 1 taken 1712 times.
|
5536 | for (size_t idx = 0; idx < row_cols.size(); idx++) { |
| 159 |
1/2✓ Branch 1 taken 3824 times.
✗ Branch 2 not taken.
|
3824 | pairs.emplace_back(row_cols[idx], row_values[idx]); |
| 160 | } | ||
| 161 | |||
| 162 | std::ranges::sort(pairs); | ||
| 163 | static_cast<void>(std::ranges::begin(pairs)); | ||
| 164 | |||
| 165 |
2/2✓ Branch 0 taken 3824 times.
✓ Branch 1 taken 1712 times.
|
5536 | for (size_t idx = 0; idx < pairs.size(); idx++) { |
| 166 | 3824 | row_cols[idx] = pairs[idx].first; | |
| 167 | 3824 | row_values[idx] = pairs[idx].second; | |
| 168 | } | ||
| 169 | } | ||
| 170 | 1840 | } | |
| 171 | |||
| 172 | 848 | bool SosninaAMatrixMultCRSSEQ::PostProcessingImpl() { | |
| 173 | 848 | OutType result = std::make_tuple(values_C_, col_indices_C_, row_ptr_C_); | |
| 174 | GetOutput() = result; | ||
| 175 | |||
| 176 | 848 | return true; | |
| 177 | } | ||
| 178 | |||
| 179 | } // namespace sosnina_a_sparse_matrix_mult_crs_double | ||
| 180 |