GCC Code Coverage Report


Directory: ./
File: tasks/sinev_a_mult_matrix_fox_algorithm/omp/src/ops_omp.cpp
Date: 2026-05-11 08:26:31
Exec Total Coverage
Lines: 28 37 75.7%
Functions: 5 9 55.6%
Branches: 16 28 57.1%

Line Branch Exec Source
1 #include "sinev_a_mult_matrix_fox_algorithm/omp/include/ops_omp.hpp"
2
3 #include <omp.h>
4
5 #include <cmath>
6 #include <cstddef>
7 #include <vector>
8
9 #include "sinev_a_mult_matrix_fox_algorithm/common/include/common.hpp"
10
11 namespace sinev_a_mult_matrix_fox_algorithm {
12
13
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 SinevAMultMatrixFoxAlgorithmOMP::SinevAMultMatrixFoxAlgorithmOMP(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 GetOutput() = {};
17 52 }
18
19 52 bool SinevAMultMatrixFoxAlgorithmOMP::ValidationImpl() {
20 const auto &[matrix_size, matrix_a, matrix_b] = GetInput();
21
22
3/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 52 times.
52 return matrix_size > 0 && matrix_a.size() == matrix_size * matrix_size &&
23 52 matrix_b.size() == matrix_size * matrix_size;
24 }
25
26 52 bool SinevAMultMatrixFoxAlgorithmOMP::PreProcessingImpl() {
27 const auto &[matrix_size, matrix_a, matrix_b] = GetInput();
28 52 GetOutput() = std::vector<double>(matrix_size * matrix_size, 0.0);
29 52 return true;
30 }
31
32 void SinevAMultMatrixFoxAlgorithmOMP::SimpleMultiply(size_t n, const std::vector<double> &a,
33 const std::vector<double> &b, std::vector<double> &c) {
34 20 #pragma omp parallel for default(none) shared(n, a, b, c) collapse(2)
35 for (size_t i = 0; i < n; ++i) {
36 for (size_t j = 0; j < n; ++j) {
37 double sum = 0.0;
38 for (size_t k = 0; k < n; ++k) {
39 sum += a[(i * n) + k] * b[(k * n) + j];
40 }
41 c[(i * n) + j] = sum;
42 }
43 }
44 }
45
46 void SinevAMultMatrixFoxAlgorithmOMP::DecomposeToBlocks(const std::vector<double> &src, std::vector<double> &dst,
47 size_t n, size_t bs, int q) {
48 32 #pragma omp parallel for default(none) shared(src, dst, n, bs, q) collapse(2)
49 for (int bi = 0; bi < q; ++bi) {
50 for (int bj = 0; bj < q; ++bj) {
51 const size_t block_off = (static_cast<size_t>((bi * q) + bj)) * (bs * bs);
52 for (size_t i = 0; i < bs; ++i) {
53 for (size_t j = 0; j < bs; ++j) {
54 const size_t src_idx = ((static_cast<size_t>(bi) * bs + i) * n) + (static_cast<size_t>(bj) * bs + j);
55 const size_t dst_idx = block_off + (i * bs) + j;
56 dst[dst_idx] = src[src_idx];
57 }
58 }
59 }
60 }
61 }
62
63 void SinevAMultMatrixFoxAlgorithmOMP::AssembleFromBlocks(const std::vector<double> &src, std::vector<double> &dst,
64 size_t n, size_t bs, int q) {
65
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 #pragma omp parallel for default(none) shared(src, dst, n, bs, q) collapse(2)
66 for (int bi = 0; bi < q; ++bi) {
67 for (int bj = 0; bj < q; ++bj) {
68 const size_t block_off = (static_cast<size_t>((bi * q) + bj)) * (bs * bs);
69 for (size_t i = 0; i < bs; ++i) {
70 for (size_t j = 0; j < bs; ++j) {
71 const size_t src_idx = block_off + (i * bs) + j;
72 const size_t dst_idx = ((static_cast<size_t>(bi) * bs + i) * n) + (static_cast<size_t>(bj) * bs + j);
73 dst[dst_idx] = src[src_idx];
74 }
75 }
76 }
77 }
78 }
79
80 void SinevAMultMatrixFoxAlgorithmOMP::FoxStep(const std::vector<double> &blocks_a, const std::vector<double> &blocks_b,
81 std::vector<double> &blocks_c, size_t bs, int q, int step) {
82 const size_t block_size_bytes = bs * bs;
83 232 #pragma omp parallel for default(none) shared(blocks_a, blocks_b, blocks_c, bs, q, step, block_size_bytes) collapse(2)
84 for (int i = 0; i < q; ++i) {
85 for (int j = 0; j < q; ++j) {
86 const int k = (i + step) % q;
87
88 const size_t a_off = (static_cast<size_t>((i * q) + k)) * block_size_bytes;
89 const size_t b_off = (static_cast<size_t>((k * q) + j)) * block_size_bytes;
90 const size_t c_off = (static_cast<size_t>((i * q) + j)) * block_size_bytes;
91
92 for (size_t ii = 0; ii < bs; ++ii) {
93 for (size_t kk = 0; kk < bs; ++kk) {
94 const double val = blocks_a[a_off + (ii * bs) + kk];
95 for (size_t jj = 0; jj < bs; ++jj) {
96 blocks_c[c_off + (ii * bs) + jj] += val * blocks_b[b_off + (kk * bs) + jj];
97 }
98 }
99 }
100 }
101 }
102 }
103
104 52 bool SinevAMultMatrixFoxAlgorithmOMP::RunImpl() {
105 const auto &input = GetInput();
106
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 32 times.
52 const size_t n = std::get<0>(input);
107 const auto &a = std::get<1>(input);
108 const auto &b = std::get<2>(input);
109 auto &c = GetOutput();
110
111 // Для маленьких матриц используем простое умножение
112
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 32 times.
52 if (n <= 8) {
113 SimpleMultiply(n, a, b, c);
114 20 return true;
115 }
116
117 size_t bs = 1;
118 32 auto sqrt_n = static_cast<size_t>(std::sqrt(static_cast<double>(n)));
119
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 for (size_t div = sqrt_n; div >= 1; --div) {
120
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 32 times.
60 if (n % div == 0) {
121 bs = div;
122 break;
123 }
124 }
125
126 32 const int actual_q = static_cast<int>(n / bs);
127
128 32 auto total_blocks = static_cast<size_t>(actual_q) * static_cast<size_t>(actual_q);
129 32 auto block_elements = bs * bs;
130
131 32 std::vector<double> blocks_a(total_blocks * block_elements);
132
1/4
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
32 std::vector<double> blocks_b(total_blocks * block_elements);
133
1/4
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
32 std::vector<double> blocks_c(total_blocks * block_elements, 0.0);
134
135 DecomposeToBlocks(a, blocks_a, n, bs, actual_q);
136 DecomposeToBlocks(b, blocks_b, n, bs, actual_q);
137
138
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 32 times.
264 for (int step = 0; step < actual_q; ++step) {
139 FoxStep(blocks_a, blocks_b, blocks_c, bs, actual_q, step);
140 }
141
142 AssembleFromBlocks(blocks_c, c, n, bs, actual_q);
143
144 return true;
145 }
146
147 52 bool SinevAMultMatrixFoxAlgorithmOMP::PostProcessingImpl() {
148 52 return true;
149 }
150
151 } // namespace sinev_a_mult_matrix_fox_algorithm
152