| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "nikitin_a_fox_algorithm/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "nikitin_a_fox_algorithm/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace nikitin_a_fox_algorithm { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
|
240 | NikitinAFoxAlgorithmSEQ::NikitinAFoxAlgorithmSEQ(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | 240 | } | |
| 16 | |||
| 17 |
1/2✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 | bool NikitinAFoxAlgorithmSEQ::ValidationImpl() { |
| 18 | const auto &matrix_a = GetInput().first; | ||
| 19 | const auto &matrix_b = GetInput().second; | ||
| 20 | |||
| 21 |
2/4✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 240 times.
|
240 | if (matrix_a.empty() || matrix_b.empty()) { |
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | 240 | const auto n = static_cast<int>(matrix_a.size()); | |
| 26 |
2/2✓ Branch 0 taken 5176 times.
✓ Branch 1 taken 240 times.
|
5416 | for (int i = 0; i < n; ++i) { |
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5176 times.
|
5176 | if (matrix_a[i].size() != static_cast<std::size_t>(n)) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
|
240 | if (matrix_b.size() != static_cast<std::size_t>(n)) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 5176 times.
✓ Branch 1 taken 240 times.
|
5416 | for (int i = 0; i < n; ++i) { |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5176 times.
|
5176 | if (matrix_b[i].size() != static_cast<std::size_t>(n)) { |
| 38 | return false; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | return true; | ||
| 43 | } | ||
| 44 | |||
| 45 | 240 | bool NikitinAFoxAlgorithmSEQ::PreProcessingImpl() { | |
| 46 | 240 | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | 408 | void NikitinAFoxAlgorithmSEQ::MultiplySingleBlock(int a_row_start, int a_row_end, int a_col_start, int a_col_end, | |
| 50 | int b_col_start, int b_col_end, | ||
| 51 | const std::vector<std::vector<double>> &matrix_a, | ||
| 52 | const std::vector<std::vector<double>> &matrix_b, | ||
| 53 | std::vector<std::vector<double>> &matrix_c) { | ||
| 54 | // Умножаем один блок матриц | ||
| 55 |
2/2✓ Branch 0 taken 13024 times.
✓ Branch 1 taken 408 times.
|
13432 | for (int i = a_row_start; i < a_row_end; ++i) { |
| 56 |
2/2✓ Branch 0 taken 655232 times.
✓ Branch 1 taken 13024 times.
|
668256 | for (int k = a_col_start; k < a_col_end; ++k) { |
| 57 | 655232 | const double a_ik = matrix_a[i][k]; | |
| 58 |
2/2✓ Branch 0 taken 36012712 times.
✓ Branch 1 taken 655232 times.
|
36667944 | for (int j = b_col_start; j < b_col_end; ++j) { |
| 59 | 36012712 | matrix_c[i][j] += a_ik * matrix_b[k][j]; | |
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | 408 | } | |
| 64 | |||
| 65 | 240 | bool NikitinAFoxAlgorithmSEQ::RunImpl() { | |
| 66 | const auto &[matrix_a, matrix_b] = GetInput(); | ||
| 67 | |||
| 68 | 240 | const auto n = static_cast<int>(matrix_a.size()); | |
| 69 | |||
| 70 | // Инициализируем выходную матрицу нулями | ||
| 71 |
1/2✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
480 | std::vector<std::vector<double>> matrix_c(n, std::vector<double>(n, 0.0)); |
| 72 | |||
| 73 | // Определяем размер блока - выбираем оптимальный для кэша | ||
| 74 | int block_size = 64; | ||
| 75 | 240 | block_size = std::min(n, block_size); | |
| 76 | |||
| 77 | // Вычисляем количество блоков | ||
| 78 | 240 | const int grid_size = (n + block_size - 1) / block_size; | |
| 79 | |||
| 80 | // Алгоритм Фокса (последовательная версия) | ||
| 81 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 240 times.
|
504 | for (int iter = 0; iter < grid_size; ++iter) { |
| 82 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 264 times.
|
576 | for (int block_i = 0; block_i < grid_size; ++block_i) { |
| 83 |
2/2✓ Branch 0 taken 408 times.
✓ Branch 1 taken 312 times.
|
720 | for (int block_j = 0; block_j < grid_size; ++block_j) { |
| 84 | // Вычисляем, какой блок матрицы A "активен" на этой итерации | ||
| 85 | 408 | const int a_block_k = (block_i + iter) % grid_size; | |
| 86 | |||
| 87 | // Границы блоков для A | ||
| 88 | 408 | const int a_row_start = block_i * block_size; | |
| 89 | 408 | const int a_row_end = std::min(a_row_start + block_size, n); | |
| 90 | 408 | const int a_col_start = a_block_k * block_size; | |
| 91 | 408 | const int a_col_end = std::min(a_col_start + block_size, n); | |
| 92 | |||
| 93 | // Границы блоков для B и C | ||
| 94 | 408 | const int b_col_start = block_j * block_size; | |
| 95 | 408 | const int b_col_end = std::min(b_col_start + block_size, n); | |
| 96 | |||
| 97 | // Умножаем блоки матриц | ||
| 98 | 408 | MultiplySingleBlock(a_row_start, a_row_end, a_col_start, a_col_end, b_col_start, b_col_end, matrix_a, matrix_b, | |
| 99 | matrix_c); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | // Сохраняем результат | ||
| 105 |
1/2✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
|
240 | GetOutput() = matrix_c; |
| 106 | |||
| 107 | 240 | return true; | |
| 108 | 240 | } | |
| 109 | |||
| 110 | 240 | bool NikitinAFoxAlgorithmSEQ::PostProcessingImpl() { | |
| 111 | const auto &matrix_c = GetOutput(); | ||
| 112 | 240 | return !matrix_c.empty(); | |
| 113 | } | ||
| 114 | |||
| 115 | } // namespace nikitin_a_fox_algorithm | ||
| 116 |