| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "safronov_m_multiplication_matrix_blockscheme_cannon/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <functional> | ||
| 4 | #include <thread> | ||
| 5 | #include <utility> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "safronov_m_multiplication_matrix_blockscheme_cannon/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace safronov_m_multiplication_matrix_blocksscheme_cannon { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | SafronovMMultiplicationMatrixBlockSchemeCannonSTL::SafronovMMultiplicationMatrixBlockSchemeCannonSTL(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | 64 | } | |
| 16 | |||
| 17 | 64 | bool SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ValidationImpl() { | |
| 18 | const auto &in = GetInput(); | ||
| 19 | 64 | int size_block = std::get<0>(in); | |
| 20 | const auto &matrix_a = std::get<1>(in); | ||
| 21 | const auto &matrix_b = std::get<2>(in); | ||
| 22 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 64 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 64 times.
|
64 | return (size_block > 0) && (!matrix_a.empty() && !matrix_b.empty()) && (matrix_a.size() == matrix_a[0].size()) && |
| 23 |
2/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
|
128 | (matrix_b.size() == matrix_b[0].size()) && (matrix_a.size() == matrix_b.size()) && |
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | (matrix_a.size() % size_block == 0); |
| 25 | } | ||
| 26 | |||
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | bool SafronovMMultiplicationMatrixBlockSchemeCannonSTL::PreProcessingImpl() { |
| 28 | GetOutput().clear(); | ||
| 29 | 64 | return true; | |
| 30 | } | ||
| 31 | |||
| 32 | 608 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::MultiplyingBlocks(std::vector<std::vector<double>> &block_a, | |
| 33 | std::vector<std::vector<double>> &block_b, | ||
| 34 | std::vector<std::vector<double>> &block_c, | ||
| 35 | int size_block) { | ||
| 36 |
2/2✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 608 times.
|
2176 | for (int i = 0; i < size_block; i++) { |
| 37 |
2/2✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 1568 times.
|
6016 | for (int j = 0; j < size_block; j++) { |
| 38 |
2/2✓ Branch 0 taken 13472 times.
✓ Branch 1 taken 4448 times.
|
17920 | for (int k = 0; k < size_block; k++) { |
| 39 | 13472 | block_c[i][j] += block_a[i][k] * block_b[k][j]; | |
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | 608 | } | |
| 44 | |||
| 45 | 144 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftALeftTask( | |
| 46 | int start, int end, int columns, std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_a) { | ||
| 47 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 144 times.
|
288 | for (int i = start; i < end; i++) { |
| 48 | 144 | std::vector<std::vector<double>> tmp = std::move(matrix_a[i][0]); | |
| 49 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 144 times.
|
336 | for (int j = 1; j < columns; j++) { |
| 50 | 192 | matrix_a[i][j - 1] = std::move(matrix_a[i][j]); | |
| 51 | } | ||
| 52 | 144 | matrix_a[i][columns - 1] = std::move(tmp); | |
| 53 | 144 | } | |
| 54 | 144 | } | |
| 55 | |||
| 56 | 144 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftBUpTask( | |
| 57 | int start, int end, int columns, std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_b) { | ||
| 58 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 144 times.
|
288 | for (int i = start; i < end; i++) { |
| 59 | 144 | std::vector<std::vector<double>> tmp = std::move(matrix_b[0][i]); | |
| 60 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 144 times.
|
336 | for (int j = 1; j < columns; j++) { |
| 61 | 192 | matrix_b[j - 1][i] = std::move(matrix_b[j][i]); | |
| 62 | } | ||
| 63 | 144 | matrix_b[columns - 1][i] = std::move(tmp); | |
| 64 | 144 | } | |
| 65 | 144 | } | |
| 66 | |||
| 67 | 272 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::CannonTask( | |
| 68 | int start, int end, int columns_blocks, int size_block, | ||
| 69 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_a, | ||
| 70 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_b, | ||
| 71 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_c) { | ||
| 72 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 272 times.
|
544 | for (int j = start; j < end; j++) { |
| 73 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 272 times.
|
880 | for (int k = 0; k < columns_blocks; k++) { |
| 74 | 608 | MultiplyingBlocks(matrix_a[j][k], matrix_b[j][k], matrix_c[j][k], size_block); | |
| 75 | } | ||
| 76 | } | ||
| 77 | 272 | } | |
| 78 | |||
| 79 | 128 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::FillingTask( | |
| 80 | int start, int end, int columns_blocks, int size_block, | ||
| 81 | const std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_c, | ||
| 82 | std::vector<std::vector<double>> &matrix_c) { | ||
| 83 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | for (int i = start; i < end; i++) { |
| 84 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (int j = 0; j < columns_blocks; j++) { |
| 85 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 272 times.
|
960 | for (int k = 0; k < size_block; k++) { |
| 86 |
2/2✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 688 times.
|
2624 | for (int col = 0; col < size_block; col++) { |
| 87 | 1936 | matrix_c[(i * size_block) + k][(j * size_block) + col] = matrix_blocks_c[i][j][k][col]; | |
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | 128 | } | |
| 93 | |||
| 94 | 128 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::FillBlocks( | |
| 95 | int start, int end, int columns_blocks, int size_block, const std::vector<std::vector<double>> &matrix_a, | ||
| 96 | const std::vector<std::vector<double>> &matrix_b, | ||
| 97 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_a, | ||
| 98 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_b) { | ||
| 99 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | for (int i = start; i < end; i++) { |
| 100 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (int j = 0; j < columns_blocks; j++) { |
| 101 | 272 | int shift = (i + j) % columns_blocks; | |
| 102 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 272 times.
|
960 | for (int k = 0; k < size_block; k++) { |
| 103 |
2/2✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 688 times.
|
2624 | for (int col = 0; col < size_block; col++) { |
| 104 | 1936 | matrix_blocks_a[i][j][k][col] = matrix_a[(i * size_block) + k][(shift * size_block) + col]; | |
| 105 | 1936 | matrix_blocks_b[i][j][k][col] = matrix_b[(shift * size_block) + k][(j * size_block) + col]; | |
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | 128 | } | |
| 111 | |||
| 112 | 64 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftingBlocksMatrixALeft( | |
| 113 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_a, int columns) { | ||
| 114 | 64 | unsigned int hardware_threads = std::thread::hardware_concurrency(); | |
| 115 | 64 | int num_threads = (hardware_threads == 0) ? 1 : static_cast<int>(hardware_threads); | |
| 116 | 64 | std::vector<std::thread> threads; | |
| 117 | 64 | int base_size = columns / num_threads; | |
| 118 | 64 | int rem = columns % num_threads; | |
| 119 | 64 | int start = 0; | |
| 120 | |||
| 121 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (int thd_id = 0; thd_id < num_threads; thd_id++) { |
| 122 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 144 times.
|
256 | int end = start + base_size + (thd_id < rem ? 1 : 0); |
| 123 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 112 times.
|
256 | if (start < end) { |
| 124 | 288 | threads.emplace_back(&SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftALeftTask, start, end, columns, | |
| 125 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | std::ref(matrix_blocks_a)); |
| 126 | } | ||
| 127 | 256 | start = end; | |
| 128 | } | ||
| 129 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 64 times.
|
208 | for (auto &th : threads) { |
| 130 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | th.join(); |
| 131 | } | ||
| 132 | 64 | } | |
| 133 | |||
| 134 | 64 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftingBlocksMatrixBUp( | |
| 135 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_b, int columns) { | ||
| 136 | 64 | unsigned int hardware_threads = std::thread::hardware_concurrency(); | |
| 137 | 64 | int num_threads = (hardware_threads == 0) ? 1 : static_cast<int>(hardware_threads); | |
| 138 | 64 | std::vector<std::thread> threads; | |
| 139 | 64 | int base_size = columns / num_threads; | |
| 140 | 64 | int rem = columns % num_threads; | |
| 141 | 64 | int start = 0; | |
| 142 | |||
| 143 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (int thd_id = 0; thd_id < num_threads; thd_id++) { |
| 144 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 144 times.
|
256 | int end = start + base_size + (thd_id < rem ? 1 : 0); |
| 145 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 112 times.
|
256 | if (start < end) { |
| 146 | 288 | threads.emplace_back(&SafronovMMultiplicationMatrixBlockSchemeCannonSTL::ShiftBUpTask, start, end, columns, | |
| 147 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | std::ref(matrix_blocks_b)); |
| 148 | } | ||
| 149 | 256 | start = end; | |
| 150 | } | ||
| 151 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 64 times.
|
208 | for (auto &th : threads) { |
| 152 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | th.join(); |
| 153 | } | ||
| 154 | 64 | } | |
| 155 | |||
| 156 | 64 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::AlgorithmCannon( | |
| 157 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_a, | ||
| 158 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_b, | ||
| 159 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_c, int size_block, int columns_blocks) { | ||
| 160 | 64 | unsigned int hardware_threads = std::thread::hardware_concurrency(); | |
| 161 | 64 | int num_threads = (hardware_threads == 0) ? 1 : static_cast<int>(hardware_threads); | |
| 162 | |||
| 163 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int i = 0; i < columns_blocks; i++) { |
| 164 | 128 | std::vector<std::thread> threads; | |
| 165 | 128 | int base_size = columns_blocks / num_threads; | |
| 166 | 128 | int rem = columns_blocks % num_threads; | |
| 167 | 128 | int start = 0; | |
| 168 | |||
| 169 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 128 times.
|
640 | for (int thd_id = 0; thd_id < num_threads; thd_id++) { |
| 170 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 272 times.
|
512 | int end = start + base_size + (thd_id < rem ? 1 : 0); |
| 171 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 240 times.
|
512 | if (start < end) { |
| 172 | 544 | threads.emplace_back(&SafronovMMultiplicationMatrixBlockSchemeCannonSTL::CannonTask, start, end, columns_blocks, | |
| 173 | 544 | size_block, std::ref(matrix_blocks_a), std::ref(matrix_blocks_b), | |
| 174 |
1/2✓ Branch 1 taken 272 times.
✗ Branch 2 not taken.
|
272 | std::ref(matrix_blocks_c)); |
| 175 | } | ||
| 176 | 512 | start = end; | |
| 177 | } | ||
| 178 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (auto &th : threads) { |
| 179 |
1/2✓ Branch 1 taken 272 times.
✗ Branch 2 not taken.
|
272 | th.join(); |
| 180 | } | ||
| 181 | |||
| 182 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
|
128 | if (i < columns_blocks - 1) { |
| 183 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | ShiftingBlocksMatrixALeft(matrix_blocks_a, columns_blocks); |
| 184 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | ShiftingBlocksMatrixBUp(matrix_blocks_b, columns_blocks); |
| 185 | } | ||
| 186 | 128 | } | |
| 187 | 64 | } | |
| 188 | |||
| 189 | 64 | void SafronovMMultiplicationMatrixBlockSchemeCannonSTL::FillingResultingMatrix( | |
| 190 | std::vector<std::vector<std::vector<std::vector<double>>>> &matrix_blocks_c, | ||
| 191 | std::vector<std::vector<double>> &matrix_c, int size_block, int columns_blocks) { | ||
| 192 | 64 | unsigned int hardware_threads = std::thread::hardware_concurrency(); | |
| 193 | 64 | int num_threads = (hardware_threads == 0) ? 1 : static_cast<int>(hardware_threads); | |
| 194 | 64 | std::vector<std::thread> threads; | |
| 195 | 64 | int base_size = columns_blocks / num_threads; | |
| 196 | 64 | int rem = columns_blocks % num_threads; | |
| 197 | 64 | int start = 0; | |
| 198 | |||
| 199 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (int thd_id = 0; thd_id < num_threads; thd_id++) { |
| 200 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | int end = start + base_size + (thd_id < rem ? 1 : 0); |
| 201 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | if (start < end) { |
| 202 | 256 | threads.emplace_back(&SafronovMMultiplicationMatrixBlockSchemeCannonSTL::FillingTask, start, end, columns_blocks, | |
| 203 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | size_block, std::ref(matrix_blocks_c), std::ref(matrix_c)); |
| 204 | } | ||
| 205 | 256 | start = end; | |
| 206 | } | ||
| 207 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (auto &th : threads) { |
| 208 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | th.join(); |
| 209 | } | ||
| 210 | 64 | } | |
| 211 | |||
| 212 | 64 | bool SafronovMMultiplicationMatrixBlockSchemeCannonSTL::RunImpl() { | |
| 213 | const auto &in = GetInput(); | ||
| 214 | 64 | int size_block = std::get<0>(in); | |
| 215 | const auto &matrix_a = std::get<1>(in); | ||
| 216 | const auto &matrix_b = std::get<2>(in); | ||
| 217 | 64 | int n = static_cast<int>(matrix_a.size()); | |
| 218 | 64 | int columns_blocks = n / size_block; | |
| 219 | |||
| 220 | std::vector<std::vector<std::vector<std::vector<double>>>> matrix_blocks_a( | ||
| 221 | columns_blocks, | ||
| 222 | 64 | std::vector<std::vector<std::vector<double>>>( | |
| 223 |
3/6✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 64 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 64 times.
✗ Branch 9 not taken.
|
128 | columns_blocks, std::vector<std::vector<double>>(size_block, std::vector<double>(size_block)))); |
| 224 | std::vector<std::vector<std::vector<std::vector<double>>>> matrix_blocks_b( | ||
| 225 | columns_blocks, | ||
| 226 | 64 | std::vector<std::vector<std::vector<double>>>( | |
| 227 |
4/8✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 64 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 64 times.
✗ Branch 11 not taken.
|
128 | columns_blocks, std::vector<std::vector<double>>(size_block, std::vector<double>(size_block)))); |
| 228 | std::vector<std::vector<std::vector<std::vector<double>>>> matrix_blocks_c( | ||
| 229 | columns_blocks, | ||
| 230 | 64 | std::vector<std::vector<std::vector<double>>>( | |
| 231 |
4/8✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 64 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 64 times.
✗ Branch 11 not taken.
|
128 | columns_blocks, std::vector<std::vector<double>>(size_block, std::vector<double>(size_block, 0.0)))); |
| 232 | |||
| 233 | 64 | unsigned int hardware_threads = std::thread::hardware_concurrency(); | |
| 234 | 64 | int num_threads = (hardware_threads == 0) ? 1 : static_cast<int>(hardware_threads); | |
| 235 | 64 | std::vector<std::thread> threads; | |
| 236 | 64 | int base_size = columns_blocks / num_threads; | |
| 237 | 64 | int rem = columns_blocks % num_threads; | |
| 238 | 64 | int start = 0; | |
| 239 | |||
| 240 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (int thd_id = 0; thd_id < num_threads; thd_id++) { |
| 241 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | int end = start + base_size + (thd_id < rem ? 1 : 0); |
| 242 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
|
256 | if (start < end) { |
| 243 | 256 | threads.emplace_back(&SafronovMMultiplicationMatrixBlockSchemeCannonSTL::FillBlocks, start, end, columns_blocks, | |
| 244 | 256 | size_block, std::ref(matrix_a), std::ref(matrix_b), std::ref(matrix_blocks_a), | |
| 245 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | std::ref(matrix_blocks_b)); |
| 246 | } | ||
| 247 | 256 | start = end; | |
| 248 | } | ||
| 249 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (auto &th : threads) { |
| 250 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | th.join(); |
| 251 | } | ||
| 252 | |||
| 253 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | AlgorithmCannon(matrix_blocks_a, matrix_blocks_b, matrix_blocks_c, size_block, columns_blocks); |
| 254 | |||
| 255 |
2/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
|
64 | std::vector<std::vector<double>> matrix_c(n, std::vector<double>(n)); |
| 256 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | FillingResultingMatrix(matrix_blocks_c, matrix_c, size_block, columns_blocks); |
| 257 | 64 | GetOutput() = std::move(matrix_c); | |
| 258 | 64 | return true; | |
| 259 | 64 | } | |
| 260 | |||
| 261 | 64 | bool SafronovMMultiplicationMatrixBlockSchemeCannonSTL::PostProcessingImpl() { | |
| 262 | 64 | return true; | |
| 263 | } | ||
| 264 | |||
| 265 | } // namespace safronov_m_multiplication_matrix_blocksscheme_cannon | ||
| 266 |