| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tsyplakov_k_mul_double_crs_matrix/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/blocked_range.h> | ||
| 4 | #include <tbb/parallel_for.h> | ||
| 5 | |||
| 6 | #include <cmath> | ||
| 7 | #include <unordered_map> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "tsyplakov_k_mul_double_crs_matrix/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace tsyplakov_k_mul_double_crs_matrix { | ||
| 14 | |||
| 15 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | TsyplakovKTestTaskTBB::TsyplakovKTestTaskTBB(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 16 | } | |
| 19 | |||
| 20 | 16 | bool TsyplakovKTestTaskTBB::ValidationImpl() { | |
| 21 | const auto &input = GetInput(); | ||
| 22 | 16 | return input.a.cols == input.b.rows; | |
| 23 | } | ||
| 24 | |||
| 25 | 16 | bool TsyplakovKTestTaskTBB::PreProcessingImpl() { | |
| 26 | 16 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | namespace { | ||
| 30 | |||
| 31 | 36 | void ComputeRow(const SparseMatrixCRS &a, const SparseMatrixCRS &b, int row, std::vector<double> &values, | |
| 32 | std::vector<int> &cols) { | ||
| 33 | std::unordered_map<int, double> acc; | ||
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 36 times.
|
80 | for (int idx_a = a.row_ptr[row]; idx_a < a.row_ptr[row + 1]; ++idx_a) { |
| 36 | 44 | const int k = a.col_index[idx_a]; | |
| 37 | 44 | const double val_a = a.values[idx_a]; | |
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
|
88 | for (int idx_b = b.row_ptr[k]; idx_b < b.row_ptr[k + 1]; ++idx_b) { |
| 40 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | const int j = b.col_index[idx_b]; |
| 41 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | acc[j] += val_a * b.values[idx_b]; |
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | values.reserve(acc.size()); |
| 46 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | cols.reserve(acc.size()); |
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | for (const auto &[col, val] : acc) { |
| 49 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (std::fabs(val) > 1e-12) { |
| 50 | cols.push_back(col); | ||
| 51 | values.push_back(val); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | 36 | } | |
| 55 | } // anonymous namespace | ||
| 56 | |||
| 57 | 16 | bool TsyplakovKTestTaskTBB::RunImpl() { | |
| 58 | const auto &input = GetInput(); | ||
| 59 | 16 | const auto &a = input.a; | |
| 60 | 16 | const auto &b = input.b; | |
| 61 | |||
| 62 | 16 | const int rows = a.rows; | |
| 63 | |||
| 64 | 16 | std::vector<std::vector<double>> row_values(rows); | |
| 65 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | std::vector<std::vector<int>> row_cols(rows); |
| 66 | |||
| 67 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
52 | tbb::parallel_for(tbb::blocked_range<int>(0, rows), [&](const tbb::blocked_range<int> &range) { |
| 68 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | for (int i = range.begin(); i < range.end(); ++i) { |
| 69 | 36 | ComputeRow(a, b, i, row_values[i], row_cols[i]); | |
| 70 | } | ||
| 71 | 36 | }); | |
| 72 | |||
| 73 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | SparseMatrixCRS c(a.rows, b.cols); |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 16 times.
|
52 | for (int i = 0; i < c.rows; ++i) { |
| 76 | 36 | c.row_ptr[i + 1] = c.row_ptr[i] + static_cast<int>(row_values[i].size()); | |
| 77 | } | ||
| 78 | |||
| 79 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | const int nnz = c.row_ptr[c.rows]; |
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | c.values.reserve(nnz); |
| 82 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | c.col_index.reserve(nnz); |
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 16 times.
|
52 | for (int i = 0; i < c.rows; ++i) { |
| 85 |
2/4✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
|
36 | c.values.insert(c.values.end(), row_values[i].begin(), row_values[i].end()); |
| 86 | |||
| 87 | 36 | c.col_index.insert(c.col_index.end(), row_cols[i].begin(), row_cols[i].end()); | |
| 88 | } | ||
| 89 | |||
| 90 | 16 | GetOutput() = std::move(c); | |
| 91 | |||
| 92 | 16 | return true; | |
| 93 | 16 | } | |
| 94 | |||
| 95 | 16 | bool TsyplakovKTestTaskTBB::PostProcessingImpl() { | |
| 96 | 16 | return true; | |
| 97 | } | ||
| 98 | |||
| 99 | } // namespace tsyplakov_k_mul_double_crs_matrix | ||
| 100 |