| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dolov_v_crs_mat_mult/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/global_control.h> | ||
| 4 | #include <tbb/parallel_for.h> | ||
| 5 | |||
| 6 | #include <cmath> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "dolov_v_crs_mat_mult/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace dolov_v_crs_mat_mult { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | DolovVCrsMatMultTbb::DolovVCrsMatMultTbb(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | GetInput() = in; |
| 18 | 28 | } | |
| 19 | |||
| 20 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | bool DolovVCrsMatMultTbb::ValidationImpl() { |
| 21 | const auto &input_data = GetInput(); | ||
| 22 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (input_data.size() != 2) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 | const auto &matrix_a = input_data[0]; | ||
| 26 | const auto &matrix_b = input_data[1]; | ||
| 27 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
|
28 | return matrix_a.num_cols == matrix_b.num_rows && matrix_a.num_rows > 0 && matrix_b.num_cols > 0; |
| 28 | } | ||
| 29 | |||
| 30 | 28 | bool DolovVCrsMatMultTbb::PreProcessingImpl() { | |
| 31 | 28 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 28 | SparseMatrix DolovVCrsMatMultTbb::TransposeMatrix(const SparseMatrix &matrix) { | |
| 35 | 28 | SparseMatrix transposed; | |
| 36 | 28 | transposed.num_rows = matrix.num_cols; | |
| 37 | 28 | transposed.num_cols = matrix.num_rows; | |
| 38 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | transposed.row_pointers.assign(transposed.num_rows + 1, 0); |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 28 times.
|
1220 | for (int col_idx : matrix.col_indices) { |
| 41 | 1192 | transposed.row_pointers[col_idx + 1]++; | |
| 42 | } | ||
| 43 |
2/2✓ Branch 0 taken 540 times.
✓ Branch 1 taken 28 times.
|
568 | for (int i = 0; i < transposed.num_rows; ++i) { |
| 44 | 540 | transposed.row_pointers[i + 1] += transposed.row_pointers[i]; | |
| 45 | } | ||
| 46 | |||
| 47 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | transposed.values.resize(matrix.values.size()); |
| 48 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | transposed.col_indices.resize(matrix.col_indices.size()); |
| 49 | |||
| 50 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | std::vector<int> current_pos = transposed.row_pointers; |
| 51 |
2/2✓ Branch 0 taken 916 times.
✓ Branch 1 taken 28 times.
|
944 | for (int i = 0; i < matrix.num_rows; ++i) { |
| 52 |
2/2✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 916 times.
|
2108 | for (int j = matrix.row_pointers[i]; j < matrix.row_pointers[i + 1]; ++j) { |
| 53 | 1192 | int col = matrix.col_indices[j]; | |
| 54 | 1192 | int dest_idx = current_pos[col]++; | |
| 55 | 1192 | transposed.values[dest_idx] = matrix.values[j]; | |
| 56 | 1192 | transposed.col_indices[dest_idx] = i; | |
| 57 | } | ||
| 58 | } | ||
| 59 | 28 | return transposed; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | 41744 | double DolovVCrsMatMultTbb::DotProduct(const SparseMatrix &matrix_a, int row_a, const SparseMatrix &matrix_b_t, | |
| 63 | int row_b) { | ||
| 64 | double sum = 0.0; | ||
| 65 | 41744 | int ptr_a = matrix_a.row_pointers[row_a]; | |
| 66 | 41744 | int ptr_b = matrix_b_t.row_pointers[row_b]; | |
| 67 | 41744 | const int end_a = matrix_a.row_pointers[row_a + 1]; | |
| 68 | 41744 | const int end_b = matrix_b_t.row_pointers[row_b + 1]; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 100876 times.
✓ Branch 1 taken 41744 times.
|
142620 | while (ptr_a < end_a && ptr_b < end_b) { |
| 71 |
2/2✓ Branch 0 taken 2468 times.
✓ Branch 1 taken 98408 times.
|
100876 | if (matrix_a.col_indices[ptr_a] == matrix_b_t.col_indices[ptr_b]) { |
| 72 | 2468 | sum += matrix_a.values[ptr_a] * matrix_b_t.values[ptr_b]; | |
| 73 | 2468 | ptr_a++; | |
| 74 | 2468 | ptr_b++; | |
| 75 |
2/2✓ Branch 0 taken 52700 times.
✓ Branch 1 taken 45708 times.
|
98408 | } else if (matrix_a.col_indices[ptr_a] < matrix_b_t.col_indices[ptr_b]) { |
| 76 | 52700 | ptr_a++; | |
| 77 | } else { | ||
| 78 | 45708 | ptr_b++; | |
| 79 | } | ||
| 80 | } | ||
| 81 | 41744 | return sum; | |
| 82 | } | ||
| 83 | |||
| 84 | 28 | bool DolovVCrsMatMultTbb::RunImpl() { | |
| 85 | const auto &matrix_a = GetInput()[0]; | ||
| 86 | const auto &matrix_b = GetInput()[1]; | ||
| 87 | |||
| 88 | 28 | SparseMatrix matrix_b_t = TransposeMatrix(matrix_b); | |
| 89 | 28 | int rows = matrix_a.num_rows; | |
| 90 | |||
| 91 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | std::vector<std::vector<double>> temp_values(rows); |
| 92 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | std::vector<std::vector<int>> temp_cols(rows); |
| 93 | |||
| 94 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | int num_threads = ppc::util::GetNumThreads(); |
| 95 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | tbb::global_control global_limit(tbb::global_control::max_allowed_parallelism, num_threads); |
| 96 | |||
| 97 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | tbb::parallel_for(0, rows, [&](int i) { |
| 98 | 560 | std::vector<double> local_vals; | |
| 99 | 560 | std::vector<int> local_cols; | |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 41744 times.
✓ Branch 1 taken 560 times.
|
42304 | for (int j = 0; j < matrix_b_t.num_rows; ++j) { |
| 102 | 41744 | double sum = DolovVCrsMatMultTbb::DotProduct(matrix_a, i, matrix_b_t, j); | |
| 103 |
2/2✓ Branch 0 taken 2252 times.
✓ Branch 1 taken 39492 times.
|
41744 | if (std::fabs(sum) > 1e-15) { |
| 104 | local_vals.push_back(sum); | ||
| 105 | local_cols.push_back(j); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | 560 | temp_values[i] = std::move(local_vals); | |
| 109 | 560 | temp_cols[i] = std::move(local_cols); | |
| 110 | 560 | }); | |
| 111 | |||
| 112 | 28 | SparseMatrix res; | |
| 113 | 28 | res.num_rows = rows; | |
| 114 | 28 | res.num_cols = matrix_b.num_cols; | |
| 115 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | res.row_pointers.assign(rows + 1, 0); |
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 28 times.
|
588 | for (int i = 0; i < rows; ++i) { |
| 118 | 560 | res.row_pointers[i + 1] = res.row_pointers[i] + static_cast<int>(temp_values[i].size()); | |
| 119 | } | ||
| 120 | |||
| 121 | 28 | int total_nz = res.row_pointers[rows]; | |
| 122 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | res.values.reserve(total_nz); |
| 123 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | res.col_indices.reserve(total_nz); |
| 124 | |||
| 125 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 28 times.
|
588 | for (int i = 0; i < rows; ++i) { |
| 126 |
2/4✓ Branch 1 taken 560 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 560 times.
✗ Branch 5 not taken.
|
560 | res.values.insert(res.values.end(), temp_values[i].begin(), temp_values[i].end()); |
| 127 | 560 | res.col_indices.insert(res.col_indices.end(), temp_cols[i].begin(), temp_cols[i].end()); | |
| 128 | } | ||
| 129 | |||
| 130 | 28 | GetOutput() = std::move(res); | |
| 131 | 28 | return true; | |
| 132 | 56 | } | |
| 133 | |||
| 134 | 28 | bool DolovVCrsMatMultTbb::PostProcessingImpl() { | |
| 135 | 28 | return true; | |
| 136 | } | ||
| 137 | |||
| 138 | } // namespace dolov_v_crs_mat_mult | ||
| 139 |