| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "timur_a_cannon/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cstddef> | ||
| 4 | #include <utility> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "timur_a_cannon/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace timur_a_cannon { | ||
| 10 | |||
| 11 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | TimurACannonMatrixMultiplication::TimurACannonMatrixMultiplication(const InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | GetInput() = in; | ||
| 14 | 64 | } | |
| 15 | |||
| 16 | 64 | bool TimurACannonMatrixMultiplication::ValidationImpl() { | |
| 17 | const auto &input = GetInput(); | ||
| 18 | 64 | int b_size = std::get<0>(input); | |
| 19 | const auto &mat_a = std::get<1>(input); | ||
| 20 | const auto &mat_b = std::get<2>(input); | ||
| 21 | |||
| 22 |
3/6✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 64 times.
|
64 | if (b_size <= 0 || mat_a.empty() || mat_b.empty()) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 | size_t n = mat_a.size(); | ||
| 26 |
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 (n == mat_a[0].size() && n == mat_b.size() && n == mat_b[0].size() && (n % static_cast<size_t>(b_size) == 0)); |
| 27 | } | ||
| 28 | |||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | bool TimurACannonMatrixMultiplication::PreProcessingImpl() { |
| 30 | GetOutput().clear(); | ||
| 31 | 64 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 608 | void TimurACannonMatrixMultiplication::BlockMultiplyAccumulate(const std::vector<std::vector<double>> &a, | |
| 35 | const std::vector<std::vector<double>> &b, | ||
| 36 | std::vector<std::vector<double>> &c, int b_size) { | ||
| 37 |
2/2✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 608 times.
|
2176 | for (int i = 0; i < b_size; ++i) { |
| 38 |
2/2✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 1568 times.
|
6016 | for (int k = 0; k < b_size; ++k) { |
| 39 | 4448 | double temp = a[i][k]; | |
| 40 |
2/2✓ Branch 0 taken 13472 times.
✓ Branch 1 taken 4448 times.
|
17920 | for (int j = 0; j < b_size; ++j) { |
| 41 | 13472 | c[i][j] += temp * b[k][j]; | |
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | 608 | } | |
| 46 | |||
| 47 | 64 | void TimurACannonMatrixMultiplication::DistributeData(const std::vector<std::vector<double>> &src_a, | |
| 48 | const std::vector<std::vector<double>> &src_b, | ||
| 49 | std::vector<std::vector<std::vector<std::vector<double>>>> &bl_a, | ||
| 50 | std::vector<std::vector<std::vector<std::vector<double>>>> &bl_b, | ||
| 51 | int b_size, int grid_sz) { | ||
| 52 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int i = 0; i < grid_sz; ++i) { |
| 53 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (int j = 0; j < grid_sz; ++j) { |
| 54 | 272 | int shift = (i + j) % grid_sz; | |
| 55 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 272 times.
|
960 | for (int row = 0; row < b_size; ++row) { |
| 56 |
2/2✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 688 times.
|
2624 | for (int col = 0; col < b_size; ++col) { |
| 57 | 1936 | bl_a[i][j][row][col] = src_a[(i * b_size) + row][(shift * b_size) + col]; | |
| 58 | 1936 | bl_b[i][j][row][col] = src_b[(shift * b_size) + row][(j * b_size) + col]; | |
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | 64 | } | |
| 64 | |||
| 65 | 64 | void TimurACannonMatrixMultiplication::CollectResult( | |
| 66 | const std::vector<std::vector<std::vector<std::vector<double>>>> &bl_c, std::vector<std::vector<double>> &res, | ||
| 67 | int b_size, int grid_sz) { | ||
| 68 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int i = 0; i < grid_sz; ++i) { |
| 69 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (int j = 0; j < grid_sz; ++j) { |
| 70 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 272 times.
|
960 | for (int row = 0; row < b_size; ++row) { |
| 71 |
2/2✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 688 times.
|
2624 | for (int col = 0; col < b_size; ++col) { |
| 72 | 1936 | res[(i * b_size) + row][(j * b_size) + col] = bl_c[i][j][row][col]; | |
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | 64 | } | |
| 78 | |||
| 79 | 64 | void TimurACannonMatrixMultiplication::RotateBlocksA(std::vector<std::vector<std::vector<std::vector<double>>>> &blocks, | |
| 80 | int grid_sz) { | ||
| 81 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 64 times.
|
208 | for (int i = 0; i < grid_sz; ++i) { |
| 82 | 144 | auto first_block = std::move(blocks[i][0]); | |
| 83 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 144 times.
|
336 | for (int j = 0; j < grid_sz - 1; ++j) { |
| 84 | 192 | blocks[i][j] = std::move(blocks[i][j + 1]); | |
| 85 | } | ||
| 86 | 144 | blocks[i][grid_sz - 1] = std::move(first_block); | |
| 87 | 144 | } | |
| 88 | 64 | } | |
| 89 | |||
| 90 | 64 | void TimurACannonMatrixMultiplication::RotateBlocksB(std::vector<std::vector<std::vector<std::vector<double>>>> &blocks, | |
| 91 | int grid_sz) { | ||
| 92 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 64 times.
|
208 | for (int j = 0; j < grid_sz; ++j) { |
| 93 | 144 | auto first_block = std::move(blocks[0][j]); | |
| 94 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 144 times.
|
336 | for (int i = 0; i < grid_sz - 1; ++i) { |
| 95 | 192 | blocks[i][j] = std::move(blocks[i + 1][j]); | |
| 96 | } | ||
| 97 | 144 | blocks[grid_sz - 1][j] = std::move(first_block); | |
| 98 | 144 | } | |
| 99 | 64 | } | |
| 100 | |||
| 101 | 64 | bool TimurACannonMatrixMultiplication::RunImpl() { | |
| 102 | const auto &input = GetInput(); | ||
| 103 | 64 | int b_size = std::get<0>(input); | |
| 104 | 64 | int n = static_cast<int>(std::get<1>(input).size()); | |
| 105 | 64 | int grid_sz = n / b_size; | |
| 106 | |||
| 107 | using Matrix = std::vector<std::vector<double>>; | ||
| 108 | using BlockGrid = std::vector<std::vector<Matrix>>; | ||
| 109 | |||
| 110 |
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.
|
64 | BlockGrid bl_a(grid_sz, std::vector<Matrix>(grid_sz, Matrix(b_size, std::vector<double>(b_size)))); |
| 111 |
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.
|
64 | BlockGrid bl_b(grid_sz, std::vector<Matrix>(grid_sz, Matrix(b_size, std::vector<double>(b_size)))); |
| 112 |
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 | BlockGrid bl_c(grid_sz, std::vector<Matrix>(grid_sz, Matrix(b_size, std::vector<double>(b_size, 0.0)))); |
| 113 | |||
| 114 | 64 | DistributeData(std::get<1>(input), std::get<2>(input), bl_a, bl_b, b_size, grid_sz); | |
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int step = 0; step < grid_sz; ++step) { |
| 117 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 128 times.
|
400 | for (int i = 0; i < grid_sz; ++i) { |
| 118 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 272 times.
|
880 | for (int j = 0; j < grid_sz; ++j) { |
| 119 | 608 | BlockMultiplyAccumulate(bl_a[i][j], bl_b[i][j], bl_c[i][j], b_size); | |
| 120 | } | ||
| 121 | } | ||
| 122 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
|
128 | if (step < grid_sz - 1) { |
| 123 | 64 | RotateBlocksA(bl_a, grid_sz); | |
| 124 | 64 | RotateBlocksB(bl_b, grid_sz); | |
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 |
2/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
|
64 | Matrix res_mat(n, std::vector<double>(n)); |
| 129 | 64 | CollectResult(bl_c, res_mat, b_size, grid_sz); | |
| 130 | |||
| 131 | 64 | GetOutput() = std::move(res_mat); | |
| 132 | 64 | return true; | |
| 133 | 64 | } | |
| 134 | |||
| 135 | 64 | bool TimurACannonMatrixMultiplication::PostProcessingImpl() { | |
| 136 | 64 | return true; | |
| 137 | } | ||
| 138 | |||
| 139 | } // namespace timur_a_cannon | ||
| 140 |