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