| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "luzan_e_double_sparse_matrix_mult/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include "luzan_e_double_sparse_matrix_mult/common/include/common.hpp" | ||
| 4 | // #include "util/include/util.hpp" | ||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <thread> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace luzan_e_double_sparse_matrix_mult { | ||
| 12 | 128 | void LuzanEDoubleSparseMatrixMultSTL::AccumulateColumn(const SparseMatrix &a, const SparseMatrix &b, unsigned b_col, | |
| 13 | std::vector<double> &tmp_col) { | ||
| 14 | 128 | unsigned b_rows_start = b.col_index[b_col]; | |
| 15 | 128 | unsigned b_rows_end = b.col_index[b_col + 1]; | |
| 16 | |||
| 17 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 128 times.
|
288 | for (unsigned b_pos = b_rows_start; b_pos < b_rows_end; b_pos++) { |
| 18 | 160 | double b_val = b.value[b_pos]; | |
| 19 | 160 | unsigned b_row = b.row[b_pos]; | |
| 20 | 160 | unsigned a_rows_start = a.col_index[b_row]; | |
| 21 | 160 | unsigned a_rows_end = a.col_index[b_row + 1]; | |
| 22 | |||
| 23 |
2/2✓ Branch 0 taken 376 times.
✓ Branch 1 taken 160 times.
|
536 | for (unsigned a_pos = a_rows_start; a_pos < a_rows_end; a_pos++) { |
| 24 | 376 | tmp_col[a.row[a_pos]] += a.value[a_pos] * b_val; | |
| 25 | } | ||
| 26 | } | ||
| 27 | 128 | } | |
| 28 | |||
| 29 | 128 | void LuzanEDoubleSparseMatrixMultSTL::CollectNonZeros(const std::vector<double> &tmp_col, unsigned b_col, | |
| 30 | std::vector<std::vector<double>> &values_per_col, | ||
| 31 | std::vector<std::vector<unsigned>> &rows_per_col) { | ||
| 32 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 128 times.
|
608 | for (unsigned i = 0; i < static_cast<unsigned>(tmp_col.size()); i++) { |
| 33 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 240 times.
|
480 | if (fabs(tmp_col[i]) > kEPS) { |
| 34 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 208 times.
|
240 | values_per_col[b_col].push_back(tmp_col[i]); |
| 35 | rows_per_col[b_col].push_back(i); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | 128 | } | |
| 39 | |||
| 40 | 128 | void LuzanEDoubleSparseMatrixMultSTL::ProcessColumn(const SparseMatrix &a, const SparseMatrix &b, unsigned b_col, | |
| 41 | std::vector<std::vector<double>> &values_per_col, | ||
| 42 | std::vector<std::vector<unsigned>> &rows_per_col) { | ||
| 43 | 128 | std::vector<double> tmp_col(a.rows, 0.0); | |
| 44 | 128 | AccumulateColumn(a, b, b_col, tmp_col); | |
| 45 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | CollectNonZeros(tmp_col, b_col, values_per_col, rows_per_col); |
| 46 | 128 | } | |
| 47 | |||
| 48 | 48 | void LuzanEDoubleSparseMatrixMultSTL::AssembleResult(SparseMatrix &c, unsigned cols, | |
| 49 | const std::vector<std::vector<double>> &values_per_col, | ||
| 50 | const std::vector<std::vector<unsigned>> &rows_per_col) { | ||
| 51 | 48 | c.col_index.push_back(0); | |
| 52 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 48 times.
|
176 | for (unsigned j = 0; j < cols; j++) { |
| 53 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 128 times.
|
368 | for (size_t k = 0; k < values_per_col[j].size(); k++) { |
| 54 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | c.value.push_back(values_per_col[j][k]); |
| 55 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 136 times.
|
240 | c.row.push_back(rows_per_col[j][k]); |
| 56 | } | ||
| 57 | 128 | c.col_index.push_back(static_cast<unsigned>(c.value.size())); | |
| 58 | } | ||
| 59 | 48 | } | |
| 60 | |||
| 61 | 48 | SparseMatrix LuzanEDoubleSparseMatrixMultSTL::CalcProdSTL(const SparseMatrix &a, const SparseMatrix &b) { | |
| 62 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | SparseMatrix c(a.rows, b.cols); |
| 63 | |||
| 64 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<std::vector<double>> values_per_col(b.cols); |
| 65 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<std::vector<unsigned>> rows_per_col(b.cols); |
| 66 | |||
| 67 | 48 | const unsigned num_threads = std::thread::hardware_concurrency(); | |
| 68 | 48 | const unsigned chunk = (b.cols + num_threads - 1) / num_threads; | |
| 69 | |||
| 70 | 192 | auto worker = [&](unsigned thread_id) { | |
| 71 | 192 | unsigned start = thread_id * chunk; | |
| 72 | 192 | unsigned end = std::min(start + chunk, static_cast<unsigned>(b.cols)); | |
| 73 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 192 times.
|
320 | for (unsigned b_col = start; b_col < end; b_col++) { |
| 74 | 128 | ProcessColumn(a, b, b_col, values_per_col, rows_per_col); | |
| 75 | } | ||
| 76 | 240 | }; | |
| 77 | |||
| 78 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<std::thread> threads(num_threads); |
| 79 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (unsigned thrd = 0; thrd < num_threads; thrd++) { |
| 80 |
2/4✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 192 times.
|
192 | threads[thrd] = std::thread(worker, thrd); |
| 81 | } | ||
| 82 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (auto &thrd : threads) { |
| 83 |
1/2✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
|
192 | thrd.join(); |
| 84 | } | ||
| 85 | |||
| 86 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | AssembleResult(c, b.cols, values_per_col, rows_per_col); |
| 87 | 48 | return c; | |
| 88 | 48 | } | |
| 89 | |||
| 90 |
1/2✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
48 | LuzanEDoubleSparseMatrixMultSTL::LuzanEDoubleSparseMatrixMultSTL(const InType &in) { |
| 91 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 92 | GetInput() = in; | ||
| 93 | // GetOutput() = 0; | ||
| 94 | 48 | } | |
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool LuzanEDoubleSparseMatrixMultSTL::ValidationImpl() { |
| 97 | const auto &a = std::get<0>(GetInput()); | ||
| 98 | const auto &b = std::get<1>(GetInput()); | ||
| 99 |
4/8✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 48 times.
|
48 | return a.GetCols() == b.GetRows() && a.GetCols() != 0 && a.GetRows() != 0 && b.GetCols() != 0; |
| 100 | } | ||
| 101 | |||
| 102 | 48 | bool LuzanEDoubleSparseMatrixMultSTL::PreProcessingImpl() { | |
| 103 | 48 | return true; | |
| 104 | } | ||
| 105 | |||
| 106 | 48 | bool LuzanEDoubleSparseMatrixMultSTL::RunImpl() { | |
| 107 | const auto &a = std::get<0>(GetInput()); | ||
| 108 | const auto &b = std::get<1>(GetInput()); | ||
| 109 | |||
| 110 | 48 | GetOutput() = CalcProdSTL(a, b); | |
| 111 | 48 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | 48 | bool LuzanEDoubleSparseMatrixMultSTL::PostProcessingImpl() { | |
| 115 | 48 | return true; | |
| 116 | } | ||
| 117 | |||
| 118 | } // namespace luzan_e_double_sparse_matrix_mult | ||
| 119 |