| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "yakimov_i_mult_of_dense_matrices_fox_algorithm/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <fstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "util/include/util.hpp" | ||
| 10 | #include "yakimov_i_mult_of_dense_matrices_fox_algorithm/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace yakimov_i_mult_of_dense_matrices_fox_algorithm { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | 128 | bool ReadDimensions(std::ifstream &file, DenseMatrix &matrix) { | |
| 17 | 128 | file >> matrix.rows; | |
| 18 | 128 | file >> matrix.cols; | |
| 19 |
2/4✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
|
128 | return matrix.rows > 0 && matrix.cols > 0; |
| 20 | } | ||
| 21 | |||
| 22 | 128 | bool ReadMatrixData(std::ifstream &file, DenseMatrix &matrix) { | |
| 23 | 128 | auto total_elements = static_cast<std::size_t>(matrix.rows) * static_cast<std::size_t>(matrix.cols); | |
| 24 | 128 | matrix.data.resize(total_elements, 0.0); | |
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 128 times.
|
640 | for (int i = 0; i < matrix.rows; ++i) { |
| 27 |
2/2✓ Branch 0 taken 4336 times.
✓ Branch 1 taken 512 times.
|
4848 | for (int j = 0; j < matrix.cols; ++j) { |
| 28 |
1/2✓ Branch 1 taken 4336 times.
✗ Branch 2 not taken.
|
4336 | if (!(file >> matrix(i, j))) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | return true; | ||
| 34 | } | ||
| 35 | |||
| 36 | 128 | bool ReadMatrixFromFileImpl(const std::string &filename, DenseMatrix &matrix) { | |
| 37 | 128 | std::ifstream file(filename); | |
| 38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (!file.is_open()) { |
| 39 | return false; | ||
| 40 | } | ||
| 41 | |||
| 42 |
2/4✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 128 times.
|
128 | if (!ReadDimensions(file, matrix)) { |
| 43 | return false; | ||
| 44 | } | ||
| 45 |
2/4✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 128 times.
|
128 | if (!ReadMatrixData(file, matrix)) { |
| 46 | return false; | ||
| 47 | } | ||
| 48 | |||
| 49 |
1/2✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
|
128 | file.close(); |
| 50 | return true; | ||
| 51 | 128 | } | |
| 52 | |||
| 53 | 16 | void SimpleMultiply(const DenseMatrix &a, const DenseMatrix &b, DenseMatrix &result) { | |
| 54 | 16 | result.rows = a.rows; | |
| 55 | 16 | result.cols = b.cols; | |
| 56 | 16 | result.data.assign(static_cast<std::size_t>(result.rows) * result.cols, 0.0); | |
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
|
48 | for (int i = 0; i < a.rows; ++i) { |
| 59 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
|
96 | for (int j = 0; j < b.cols; ++j) { |
| 60 | double sum = 0.0; | ||
| 61 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 64 times.
|
256 | for (int k = 0; k < a.cols; ++k) { |
| 62 | 192 | sum += a(i, k) * b(k, j); | |
| 63 | } | ||
| 64 | 64 | result(i, j) = sum; | |
| 65 | } | ||
| 66 | } | ||
| 67 | 16 | } | |
| 68 | |||
| 69 | 48 | void MultiplyBlock(const DenseMatrix &a, const DenseMatrix &b, DenseMatrix &result, int row_start, int col_start, | |
| 70 | int block_size, int a_row_offset, int b_col_offset) { | ||
| 71 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 48 times.
|
264 | for (int i = 0; i < block_size; ++i) { |
| 72 |
2/2✓ Branch 0 taken 2072 times.
✓ Branch 1 taken 216 times.
|
2288 | for (int j = 0; j < block_size; ++j) { |
| 73 | double sum = 0.0; | ||
| 74 |
2/2✓ Branch 0 taken 27864 times.
✓ Branch 1 taken 2072 times.
|
29936 | for (int k = 0; k < block_size; ++k) { |
| 75 | 27864 | sum += a(row_start + i, a_row_offset + k) * b(b_col_offset + k, col_start + j); | |
| 76 | } | ||
| 77 | 2072 | result(row_start + i, col_start + j) += sum; | |
| 78 | } | ||
| 79 | } | ||
| 80 | 48 | } | |
| 81 | |||
| 82 | 64 | void FoxAlgorithmImpl(const DenseMatrix &a, const DenseMatrix &b, DenseMatrix &result, int block_size) { | |
| 83 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 48 times.
|
64 | if (block_size <= 0) { |
| 84 | 16 | SimpleMultiply(a, b, result); | |
| 85 | 16 | return; | |
| 86 | } | ||
| 87 | |||
| 88 |
3/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
|
48 | if (a.rows != a.cols || b.rows != b.cols || a.rows != b.rows) { |
| 89 | ✗ | SimpleMultiply(a, b, result); | |
| 90 | ✗ | return; | |
| 91 | } | ||
| 92 | |||
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (a.rows % block_size != 0) { |
| 94 | ✗ | SimpleMultiply(a, b, result); | |
| 95 | ✗ | return; | |
| 96 | } | ||
| 97 | |||
| 98 | int n = a.rows; | ||
| 99 | 48 | int num_blocks = n / block_size; | |
| 100 | 48 | result.rows = n; | |
| 101 | 48 | result.cols = n; | |
| 102 | 48 | result.data.assign(static_cast<std::size_t>(n) * n, 0.0); | |
| 103 | |||
| 104 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | for (int stage = 0; stage < num_blocks; ++stage) { |
| 105 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | for (int i = 0; i < num_blocks; ++i) { |
| 106 | 48 | int broadcast_block = (i + stage) % num_blocks; | |
| 107 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | for (int j = 0; j < num_blocks; ++j) { |
| 108 | 48 | MultiplyBlock(a, b, result, i * block_size, j * block_size, block_size, broadcast_block * block_size, | |
| 109 | j * block_size); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | } // namespace | ||
| 116 | |||
| 117 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::YakimovIMultOfDenseMatricesFoxAlgorithmSEQ(const InType &in) { |
| 118 | this->SetTypeOfTask(YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::GetStaticTypeOfTask()); | ||
| 119 | 64 | this->GetInput() = in; | |
| 120 | this->GetOutput() = 0.0; | ||
| 121 | |||
| 122 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | std::string task_name = "yakimov_i_mult_of_dense_matrices_fox_algorithm"; |
| 123 |
1/2✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
|
128 | this->matrix_a_filename_ = ppc::util::GetAbsoluteTaskPath(task_name, "A_" + std::to_string(in) + ".txt"); |
| 124 |
1/2✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
|
192 | this->matrix_b_filename_ = ppc::util::GetAbsoluteTaskPath(task_name, "B_" + std::to_string(in) + ".txt"); |
| 125 | 64 | } | |
| 126 | |||
| 127 | 64 | bool YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::ValidationImpl() { | |
| 128 |
2/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
|
64 | return (this->GetInput() > 0) && (this->GetOutput() == 0.0); |
| 129 | } | ||
| 130 | |||
| 131 | 64 | bool YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::PreProcessingImpl() { | |
| 132 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
|
64 | if (!ReadMatrixFromFileImpl(this->matrix_a_filename_, this->matrix_a_)) { |
| 133 | return false; | ||
| 134 | } | ||
| 135 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
|
64 | if (!ReadMatrixFromFileImpl(this->matrix_b_filename_, this->matrix_b_)) { |
| 136 | return false; | ||
| 137 | } | ||
| 138 |
4/6✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
|
64 | if (this->matrix_a_.rows != this->matrix_a_.cols || this->matrix_b_.rows != this->matrix_b_.cols || |
| 139 | this->matrix_a_.rows != this->matrix_b_.rows) { | ||
| 140 | 16 | this->block_size_ = 0; | |
| 141 | 16 | return true; | |
| 142 | } | ||
| 143 | int n = this->matrix_a_.rows; | ||
| 144 | 48 | this->block_size_ = 64; | |
| 145 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
48 | while (this->block_size_ * 2 <= n && this->block_size_ < 256) { |
| 146 | ✗ | this->block_size_ *= 2; | |
| 147 | } | ||
| 148 | 48 | this->block_size_ = std::min(this->block_size_, n); | |
| 149 | |||
| 150 | 48 | return this->block_size_ > 0; | |
| 151 | } | ||
| 152 | |||
| 153 | 64 | bool YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::RunImpl() { | |
| 154 | 64 | FoxAlgorithmImpl(this->matrix_a_, this->matrix_b_, this->result_matrix_, this->block_size_); | |
| 155 | 64 | return true; | |
| 156 | } | ||
| 157 | |||
| 158 | 64 | bool YakimovIMultOfDenseMatricesFoxAlgorithmSEQ::PostProcessingImpl() { | |
| 159 | double sum = 0.0; | ||
| 160 |
2/2✓ Branch 0 taken 2136 times.
✓ Branch 1 taken 64 times.
|
2200 | for (double val : this->result_matrix_.data) { |
| 161 | 2136 | sum += val; | |
| 162 | } | ||
| 163 | 64 | this->GetOutput() = sum; | |
| 164 | 64 | return true; | |
| 165 | } | ||
| 166 | |||
| 167 | } // namespace yakimov_i_mult_of_dense_matrices_fox_algorithm | ||
| 168 |