GCC Code Coverage Report


Directory: ./
File: tasks/potashnik_m_matrix_mult_complex/stl/src/ops_stl.cpp
Date: 2026-05-11 08:26:31
Exec Total Coverage
Lines: 51 51 100.0%
Functions: 7 7 100.0%
Branches: 29 42 69.0%

Line Branch Exec Source
1 #include "potashnik_m_matrix_mult_complex/stl/include/ops_stl.hpp"
2
3 #include <algorithm>
4 #include <cstddef>
5 #include <map>
6 #include <thread>
7 #include <utility>
8 #include <vector>
9
10 #include "potashnik_m_matrix_mult_complex/common/include/common.hpp"
11
12 namespace potashnik_m_matrix_mult_complex {
13
14
1/2
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
80 PotashnikMMatrixMultComplexSTL::PotashnikMMatrixMultComplexSTL(const InType &in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16 GetInput() = in;
17 80 }
18
19 80 bool PotashnikMMatrixMultComplexSTL::ValidationImpl() {
20 const auto &matrix_left = std::get<0>(GetInput());
21 const auto &matrix_right = std::get<1>(GetInput());
22 80 return matrix_left.width == matrix_right.height;
23 }
24
25 80 bool PotashnikMMatrixMultComplexSTL::PreProcessingImpl() {
26 80 return true;
27 }
28
29 namespace {
30
31 using Key = std::pair<size_t, size_t>;
32 using LocalMap = std::map<Key, Complex>;
33
34 320 void ProcessChunk(size_t begin, size_t end, const CCSMatrix &matrix_right, const std::vector<Complex> &val_left,
35 const std::vector<size_t> &row_ind_left, const std::vector<size_t> &col_ptr_left,
36 LocalMap &local_buffer) {
37
2/2
✓ Branch 0 taken 3368 times.
✓ Branch 1 taken 320 times.
3688 for (size_t i = begin; i < end; ++i) {
38 3368 size_t row_left = row_ind_left[i];
39 3368 size_t col_left = col_ptr_left[i];
40 3368 Complex left_val = val_left[i];
41
42
2/2
✓ Branch 0 taken 192704 times.
✓ Branch 1 taken 3368 times.
196072 for (size_t j = 0; j < matrix_right.Count(); ++j) {
43
2/2
✓ Branch 0 taken 23872 times.
✓ Branch 1 taken 168832 times.
192704 if (col_left == matrix_right.row_ind[j]) {
44 23872 local_buffer[{row_left, matrix_right.col_ptr[j]}] += left_val * matrix_right.val[j];
45 }
46 }
47 }
48 320 }
49
50 } // namespace
51
52 80 bool PotashnikMMatrixMultComplexSTL::RunImpl() {
53 const auto &matrix_left = std::get<0>(GetInput());
54 const auto &matrix_right = std::get<1>(GetInput());
55
56 80 const auto &val_left = matrix_left.val;
57 80 const auto &row_ind_left = matrix_left.row_ind;
58 80 const auto &col_ptr_left = matrix_left.col_ptr;
59 80 size_t height_left = matrix_left.height;
60 80 size_t width_right = matrix_right.width;
61
62 80 size_t left_count = matrix_left.Count();
63 80 size_t num_threads = std::thread::hardware_concurrency();
64 if (num_threads == 0) {
65 num_threads = 1;
66 }
67
68 80 std::vector<LocalMap> local_buffers(num_threads);
69
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 std::vector<std::thread> threads(num_threads);
70
71 80 size_t chunk = (left_count + num_threads - 1) / num_threads;
72
73
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 80 times.
400 for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
74 320 size_t begin = thread_idx * chunk;
75
1/2
✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
320 size_t end = std::min(begin + chunk, left_count);
76
77 320 threads[thread_idx] = std::thread([&, thread_idx, begin, end]() {
78 320 ProcessChunk(begin, end, matrix_right, val_left, row_ind_left, col_ptr_left, local_buffers[thread_idx]);
79
1/2
✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
320 });
80 }
81
82
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 80 times.
400 for (auto &th : threads) {
83
1/2
✓ Branch 1 taken 320 times.
✗ Branch 2 not taken.
320 th.join();
84 }
85
86 std::map<Key, Complex> buffer;
87
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 80 times.
400 for (const auto &local : local_buffers) {
88
2/2
✓ Branch 0 taken 13256 times.
✓ Branch 1 taken 320 times.
13576 for (const auto &[key, value] : local) {
89
1/2
✓ Branch 1 taken 13256 times.
✗ Branch 2 not taken.
13256 buffer[key] += value;
90 }
91 }
92
93 80 CCSMatrix matrix_res;
94 80 matrix_res.width = width_right;
95
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 matrix_res.height = height_left;
96
97
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 matrix_res.val.reserve(buffer.size());
98
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 matrix_res.row_ind.reserve(buffer.size());
99
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 matrix_res.col_ptr.reserve(buffer.size());
100
101
2/2
✓ Branch 0 taken 3432 times.
✓ Branch 1 taken 80 times.
3512 for (const auto &[key, value] : buffer) {
102 matrix_res.val.push_back(value);
103
1/2
✓ Branch 0 taken 3432 times.
✗ Branch 1 not taken.
3432 matrix_res.row_ind.push_back(key.first);
104
1/2
✓ Branch 0 taken 3432 times.
✗ Branch 1 not taken.
3432 matrix_res.col_ptr.push_back(key.second);
105 }
106
107
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 GetOutput() = matrix_res;
108 80 return true;
109 160 }
110
111 80 bool PotashnikMMatrixMultComplexSTL::PostProcessingImpl() {
112 80 return true;
113 }
114
115 } // namespace potashnik_m_matrix_mult_complex
116