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