| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zavyalov_a_complex_sparse_matrix_mult/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <functional> | ||
| 6 | #include <map> | ||
| 7 | #include <stdexcept> | ||
| 8 | #include <thread> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "util/include/util.hpp" | ||
| 13 | #include "zavyalov_a_complex_sparse_matrix_mult/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace zavyalov_a_compl_sparse_matr_mult { // comm for ci1234 | ||
| 16 | |||
| 17 | 182 | void ZavyalovAComplSparseMatrMultSTL::Worker( | |
| 18 | int tid, std::size_t start, std::size_t end, const SparseMatrix &matr_a, const SparseMatrix &matr_b, | ||
| 19 | std::vector<std::map<std::pair<std::size_t, std::size_t>, Complex>> &local_maps) { | ||
| 20 |
2/2✓ Branch 0 taken 1320 times.
✓ Branch 1 taken 182 times.
|
1502 | for (std::size_t i = start; i < end; ++i) { |
| 21 | 1320 | std::size_t row_a = matr_a.row_ind[i]; | |
| 22 | 1320 | std::size_t col_a = matr_a.col_ind[i]; | |
| 23 | 1320 | Complex val_a = matr_a.val[i]; | |
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 21672 times.
✓ Branch 1 taken 1320 times.
|
22992 | for (std::size_t j = 0; j < matr_b.Count(); ++j) { |
| 26 |
2/2✓ Branch 0 taken 4728 times.
✓ Branch 1 taken 16944 times.
|
21672 | if (col_a == matr_b.row_ind[j]) { |
| 27 | 4728 | local_maps[tid][{row_a, matr_b.col_ind[j]}] += val_a * matr_b.val[j]; | |
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | 182 | } | |
| 32 | |||
| 33 | 80 | SparseMatrix ZavyalovAComplSparseMatrMultSTL::MultiplicateWithStl(const SparseMatrix &matr_a, | |
| 34 | const SparseMatrix &matr_b) { | ||
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (matr_a.width != matr_b.height) { |
| 36 | ✗ | throw std::invalid_argument("Incompatible matrix dimensions for multiplication"); | |
| 37 | } | ||
| 38 | |||
| 39 | 80 | int num_threads = ppc::util::GetNumThreads(); | |
| 40 | 80 | std::size_t total = matr_a.Count(); | |
| 41 | |||
| 42 | 80 | std::vector<std::map<std::pair<std::size_t, std::size_t>, Complex>> local_maps(num_threads); | |
| 43 | |||
| 44 | 80 | std::vector<std::thread> threads; | |
| 45 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | threads.reserve(num_threads); |
| 46 | |||
| 47 | 80 | std::size_t chunk = (total + num_threads - 1) / num_threads; | |
| 48 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 80 times.
|
280 | for (int ti = 0; ti < num_threads; ++ti) { |
| 49 | 200 | std::size_t start = ti * chunk; | |
| 50 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 18 times.
|
200 | std::size_t end = std::min(start + chunk, total); |
| 51 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 18 times.
|
200 | if (start < total) { |
| 52 |
1/2✓ Branch 1 taken 182 times.
✗ Branch 2 not taken.
|
182 | threads.emplace_back(Worker, ti, start, end, std::cref(matr_a), std::cref(matr_b), std::ref(local_maps)); |
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 80 times.
|
262 | for (auto &th : threads) { |
| 57 |
1/2✓ Branch 1 taken 182 times.
✗ Branch 2 not taken.
|
182 | th.join(); |
| 58 | } | ||
| 59 | |||
| 60 | std::map<std::pair<std::size_t, std::size_t>, Complex> mp; | ||
| 61 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 80 times.
|
280 | for (auto &lm : local_maps) { |
| 62 |
2/2✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 200 times.
|
2972 | for (auto &[key, value] : lm) { |
| 63 |
1/2✓ Branch 1 taken 2772 times.
✗ Branch 2 not taken.
|
2772 | mp[key] += value; |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 80 | SparseMatrix res; | |
| 68 | 80 | res.width = matr_b.width; | |
| 69 | 80 | res.height = matr_a.height; | |
| 70 |
2/2✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 80 times.
|
1256 | for (const auto &[key, value] : mp) { |
| 71 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 376 times.
|
1176 | res.val.push_back(value); |
| 72 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 376 times.
|
1176 | res.row_ind.push_back(key.first); |
| 73 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 376 times.
|
1176 | res.col_ind.push_back(key.second); |
| 74 | } | ||
| 75 | |||
| 76 | 80 | return res; | |
| 77 | 80 | } | |
| 78 | |||
| 79 |
1/2✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | ZavyalovAComplSparseMatrMultSTL::ZavyalovAComplSparseMatrMultSTL(const InType &in) { |
| 80 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 81 | GetInput() = in; | ||
| 82 | 80 | } | |
| 83 | |||
| 84 | 80 | bool ZavyalovAComplSparseMatrMultSTL::ValidationImpl() { | |
| 85 | const auto &matr_a = std::get<0>(GetInput()); | ||
| 86 | const auto &matr_b = std::get<1>(GetInput()); | ||
| 87 | 80 | return matr_a.width == matr_b.height; | |
| 88 | } | ||
| 89 | |||
| 90 | 80 | bool ZavyalovAComplSparseMatrMultSTL::PreProcessingImpl() { | |
| 91 | 80 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | 80 | bool ZavyalovAComplSparseMatrMultSTL::RunImpl() { | |
| 95 | const auto &matr_a = std::get<0>(GetInput()); | ||
| 96 | const auto &matr_b = std::get<1>(GetInput()); | ||
| 97 | |||
| 98 | 80 | GetOutput() = MultiplicateWithStl(matr_a, matr_b); | |
| 99 | |||
| 100 | 80 | return true; | |
| 101 | } | ||
| 102 | |||
| 103 | 80 | bool ZavyalovAComplSparseMatrMultSTL::PostProcessingImpl() { | |
| 104 | 80 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace zavyalov_a_compl_sparse_matr_mult | ||
| 108 |