| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pikhotskiy_r_multiplication_of_sparse_matrices/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "pikhotskiy_r_multiplication_of_sparse_matrices/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace pikhotskiy_r_multiplication_of_sparse_matrices { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | 1656 | double ComputeRowColProduct(const SparseMatrixCRS &mat_a, const SparseMatrixCRS &mat_bt, int row_a, int row_bt) { | |
| 14 | double sum = 0.0; | ||
| 15 | 1656 | int a_idx = mat_a.row_ptr[row_a]; | |
| 16 | 1656 | int a_end = mat_a.row_ptr[row_a + 1]; | |
| 17 | 1656 | int bt_idx = mat_bt.row_ptr[row_bt]; | |
| 18 | 1656 | int bt_end = mat_bt.row_ptr[row_bt + 1]; | |
| 19 | |||
| 20 |
2/2✓ Branch 0 taken 3672 times.
✓ Branch 1 taken 1656 times.
|
5328 | while (a_idx < a_end && bt_idx < bt_end) { |
| 21 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 2712 times.
|
3672 | int a_col = mat_a.col_indices[a_idx]; |
| 22 | 3672 | int bt_col = mat_bt.col_indices[bt_idx]; | |
| 23 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 2712 times.
|
3672 | if (a_col == bt_col) { |
| 24 | 960 | sum += mat_a.values[a_idx] * mat_bt.values[bt_idx]; | |
| 25 | 960 | ++a_idx; | |
| 26 | 960 | ++bt_idx; | |
| 27 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 1368 times.
|
2712 | } else if (a_col < bt_col) { |
| 28 | 1344 | ++a_idx; | |
| 29 | } else { | ||
| 30 | 1368 | ++bt_idx; | |
| 31 | } | ||
| 32 | } | ||
| 33 | 1656 | return sum; | |
| 34 | } | ||
| 35 | |||
| 36 | } // namespace | ||
| 37 | |||
| 38 | 792 | SparseMatrixCRS DenseToCRS(const std::vector<double> &dense, int rows, int cols) { | |
| 39 | 792 | SparseMatrixCRS result(rows, cols); | |
| 40 |
1/2✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
|
792 | result.row_ptr.resize(rows + 1); |
| 41 | 792 | result.row_ptr[0] = 0; | |
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 2782 times.
✓ Branch 1 taken 792 times.
|
3574 | for (int i = 0; i < rows; ++i) { |
| 44 |
2/2✓ Branch 0 taken 13606 times.
✓ Branch 1 taken 2782 times.
|
16388 | for (int jj = 0; jj < cols; ++jj) { |
| 45 |
2/2✓ Branch 0 taken 4500 times.
✓ Branch 1 taken 9106 times.
|
13606 | double val = dense[(i * cols) + jj]; |
| 46 |
2/2✓ Branch 0 taken 4500 times.
✓ Branch 1 taken 9106 times.
|
13606 | if (std::abs(val) > 1e-12) { |
| 47 |
2/2✓ Branch 0 taken 1898 times.
✓ Branch 1 taken 2602 times.
|
4500 | result.values.push_back(val); |
| 48 |
2/2✓ Branch 0 taken 1898 times.
✓ Branch 1 taken 2602 times.
|
4500 | result.col_indices.push_back(jj); |
| 49 | } | ||
| 50 | } | ||
| 51 | 2782 | result.row_ptr[i + 1] = static_cast<int>(result.values.size()); | |
| 52 | } | ||
| 53 | 792 | return result; | |
| 54 | ✗ | } | |
| 55 | |||
| 56 | 480 | std::vector<double> CRSToDense(const SparseMatrixCRS &sparse) { | |
| 57 | 480 | std::vector<double> dense(static_cast<std::size_t>(sparse.rows) * sparse.cols, 0.0); | |
| 58 |
2/2✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 480 times.
|
2180 | for (int i = 0; i < sparse.rows; ++i) { |
| 59 |
2/2✓ Branch 0 taken 2790 times.
✓ Branch 1 taken 1700 times.
|
4490 | for (int kk = sparse.row_ptr[i]; kk < sparse.row_ptr[i + 1]; ++kk) { |
| 60 | 2790 | dense[(i * sparse.cols) + sparse.col_indices[kk]] = sparse.values[kk]; | |
| 61 | } | ||
| 62 | } | ||
| 63 | 480 | return dense; | |
| 64 | } | ||
| 65 | |||
| 66 | 108 | SparseMatrixCRS TransposeCRS(const SparseMatrixCRS &matrix) { | |
| 67 | 108 | SparseMatrixCRS result(matrix.cols, matrix.rows); | |
| 68 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | result.row_ptr.resize(matrix.cols + 1, 0); |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 567 times.
✓ Branch 1 taken 108 times.
|
675 | for (int col : matrix.col_indices) { |
| 71 | 567 | result.row_ptr[col + 1]++; | |
| 72 | } | ||
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 351 times.
✓ Branch 1 taken 108 times.
|
459 | for (int i = 1; i <= matrix.cols; ++i) { |
| 75 | 351 | result.row_ptr[i] += result.row_ptr[i - 1]; | |
| 76 | } | ||
| 77 | |||
| 78 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | result.values.resize(matrix.values.size()); |
| 79 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | result.col_indices.resize(matrix.col_indices.size()); |
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | std::vector<int> current_pos(result.row_ptr.begin(), result.row_ptr.end() - 1); |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 108 times.
|
477 | for (int i = 0; i < matrix.rows; ++i) { |
| 84 |
2/2✓ Branch 0 taken 567 times.
✓ Branch 1 taken 369 times.
|
936 | for (int kk = matrix.row_ptr[i]; kk < matrix.row_ptr[i + 1]; ++kk) { |
| 85 | 567 | int col = matrix.col_indices[kk]; | |
| 86 | 567 | int pos = current_pos[col]++; | |
| 87 | 567 | result.values[pos] = matrix.values[kk]; | |
| 88 | 567 | result.col_indices[pos] = i; | |
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | 108 | return result; | |
| 93 | ✗ | } | |
| 94 | |||
| 95 | 120 | bool CompareSparseMatrices(const SparseMatrixCRS &aa, const SparseMatrixCRS &bb, double eps) { | |
| 96 |
1/2✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 | if (aa.rows != bb.rows || aa.cols != bb.cols) { |
| 97 | return false; | ||
| 98 | } | ||
| 99 | 120 | auto dense_a = CRSToDense(aa); | |
| 100 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | auto dense_b = CRSToDense(bb); |
| 101 |
2/2✓ Branch 0 taken 2070 times.
✓ Branch 1 taken 120 times.
|
2190 | for (std::size_t i = 0; i < dense_a.size(); ++i) { |
| 102 |
1/2✓ Branch 0 taken 2070 times.
✗ Branch 1 not taken.
|
2070 | if (std::abs(dense_a[i] - dense_b[i]) > eps) { |
| 103 | return false; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 |
1/2✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
|
96 | SparseMatrixMultiplicationSEQ::SparseMatrixMultiplicationSEQ(const InType &in) { |
| 110 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 111 | GetInput() = in; | ||
| 112 | 96 | } | |
| 113 | |||
| 114 | 96 | bool SparseMatrixMultiplicationSEQ::ValidationImpl() { | |
| 115 | const auto &mat_a = std::get<0>(GetInput()); | ||
| 116 | const auto &mat_b = std::get<1>(GetInput()); | ||
| 117 | |||
| 118 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (mat_a.cols != mat_b.rows) { |
| 119 | return false; | ||
| 120 | } | ||
| 121 |
3/6✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
|
96 | if (mat_a.rows <= 0 || mat_a.cols <= 0 || mat_b.rows <= 0 || mat_b.cols <= 0) { |
| 122 | return false; | ||
| 123 | } | ||
| 124 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (mat_a.row_ptr.size() != static_cast<std::size_t>(mat_a.rows) + 1) { |
| 125 | return false; | ||
| 126 | } | ||
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (mat_b.row_ptr.size() != static_cast<std::size_t>(mat_b.rows) + 1) { |
| 128 | ✗ | return false; | |
| 129 | } | ||
| 130 | return true; | ||
| 131 | } | ||
| 132 | |||
| 133 | 96 | bool SparseMatrixMultiplicationSEQ::PreProcessingImpl() { | |
| 134 | 96 | mat_a_ = std::get<0>(GetInput()); | |
| 135 | 96 | mat_b_transposed_ = TransposeCRS(std::get<1>(GetInput())); | |
| 136 | 96 | return true; | |
| 137 | } | ||
| 138 | |||
| 139 | 96 | bool SparseMatrixMultiplicationSEQ::RunImpl() { | |
| 140 | const auto &mat_b = std::get<1>(GetInput()); | ||
| 141 | 96 | SparseMatrixCRS result(mat_a_.rows, mat_b.cols); | |
| 142 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | result.row_ptr.resize(static_cast<std::size_t>(mat_a_.rows) + 1); |
| 143 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | if (!result.row_ptr.empty()) { |
| 144 | 96 | result.row_ptr[0] = 0; | |
| 145 | } | ||
| 146 | |||
| 147 |
2/2✓ Branch 0 taken 344 times.
✓ Branch 1 taken 96 times.
|
440 | for (int i = 0; i < mat_a_.rows; ++i) { |
| 148 |
2/2✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 344 times.
|
2000 | for (int jj = 0; jj < mat_b.cols; ++jj) { |
| 149 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 1080 times.
|
1656 | double sum = ComputeRowColProduct(mat_a_, mat_b_transposed_, i, jj); |
| 150 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 1080 times.
|
1656 | if (std::abs(sum) > 1e-12) { |
| 151 | result.values.push_back(sum); | ||
| 152 | result.col_indices.push_back(jj); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | 344 | result.row_ptr[i + 1] = static_cast<int>(result.values.size()); | |
| 156 | } | ||
| 157 | |||
| 158 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | GetOutput() = result; |
| 159 | 96 | return true; | |
| 160 | 96 | } | |
| 161 | |||
| 162 | 96 | bool SparseMatrixMultiplicationSEQ::PostProcessingImpl() { | |
| 163 | 96 | return true; | |
| 164 | } | ||
| 165 | |||
| 166 | } // namespace pikhotskiy_r_multiplication_of_sparse_matrices | ||
| 167 |