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