| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozova_s_strassen_multiplication/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "morozova_s_strassen_multiplication/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace morozova_s_strassen_multiplication { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | 88 | Matrix AddMatrixImpl(const Matrix &a, const Matrix &b) { | |
| 15 | 88 | int n = a.size; | |
| 16 | Matrix result(n); | ||
| 17 | |||
| 18 | 360448 | std::ranges::transform(a.data, b.data, result.data.begin(), [](double x, double y) { return x + y; }); | |
| 19 | |||
| 20 | 88 | return result; | |
| 21 | } | ||
| 22 | |||
| 23 | 56 | Matrix SubtractMatrixImpl(const Matrix &a, const Matrix &b) { | |
| 24 | 56 | int n = a.size; | |
| 25 | Matrix result(n); | ||
| 26 | |||
| 27 | 229376 | std::ranges::transform(a.data, b.data, result.data.begin(), [](double x, double y) { return x - y; }); | |
| 28 | |||
| 29 | 56 | return result; | |
| 30 | } | ||
| 31 | |||
| 32 | 120 | Matrix MultiplyStandardImpl(const Matrix &a, const Matrix &b) { | |
| 33 | 120 | int n = a.size; | |
| 34 | Matrix result(n); | ||
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 4624 times.
✓ Branch 1 taken 120 times.
|
4744 | for (int i = 0; i < n; ++i) { |
| 37 |
2/2✓ Branch 0 taken 273136 times.
✓ Branch 1 taken 4624 times.
|
277760 | for (int j = 0; j < n; ++j) { |
| 38 | double sum = 0.0; | ||
| 39 |
2/2✓ Branch 0 taken 17077024 times.
✓ Branch 1 taken 273136 times.
|
17350160 | for (int k = 0; k < n; ++k) { |
| 40 | 17077024 | sum += a(i, k) * b(k, j); | |
| 41 | } | ||
| 42 | 273136 | result(i, j) = sum; | |
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | 120 | return result; | |
| 47 | } | ||
| 48 | |||
| 49 | 16 | void SplitMatrixImpl(const Matrix &m, Matrix &m11, Matrix &m12, Matrix &m21, Matrix &m22) { | |
| 50 | 16 | int n = m.size; | |
| 51 | 16 | int half = n / 2; | |
| 52 | |||
| 53 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 16 times.
|
1040 | for (int i = 0; i < half; ++i) { |
| 54 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 1024 times.
|
66560 | for (int j = 0; j < half; ++j) { |
| 55 | 65536 | m11(i, j) = m(i, j); | |
| 56 | 65536 | m12(i, j) = m(i, j + half); | |
| 57 | 65536 | m21(i, j) = m(i + half, j); | |
| 58 | 65536 | m22(i, j) = m(i + half, j + half); | |
| 59 | } | ||
| 60 | } | ||
| 61 | 16 | } | |
| 62 | |||
| 63 | 8 | Matrix MergeMatricesImpl(const Matrix &m11, const Matrix &m12, const Matrix &m21, const Matrix &m22) { | |
| 64 | 8 | int half = m11.size; | |
| 65 | 8 | int n = 2 * half; | |
| 66 | Matrix result(n); | ||
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
|
520 | for (int i = 0; i < half; ++i) { |
| 69 |
2/2✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 512 times.
|
33280 | for (int j = 0; j < half; ++j) { |
| 70 | 32768 | result(i, j) = m11(i, j); | |
| 71 | 32768 | result(i, j + half) = m12(i, j); | |
| 72 | 32768 | result(i + half, j) = m21(i, j); | |
| 73 | 32768 | result(i + half, j + half) = m22(i, j); | |
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 8 | return result; | |
| 78 | } | ||
| 79 | |||
| 80 | 8 | Matrix MultiplyStrassenIterative(const Matrix &a, const Matrix &b, int leaf_size) { | |
| 81 | 8 | int n = a.size; | |
| 82 | |||
| 83 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (n <= leaf_size || n % 2 != 0) { |
| 84 | ✗ | return MultiplyStandardImpl(a, b); | |
| 85 | } | ||
| 86 | |||
| 87 | 8 | int half = n / 2; | |
| 88 | |||
| 89 | Matrix a11(half); | ||
| 90 | Matrix a12(half); | ||
| 91 | Matrix a21(half); | ||
| 92 | Matrix a22(half); | ||
| 93 | Matrix b11(half); | ||
| 94 | Matrix b12(half); | ||
| 95 | Matrix b21(half); | ||
| 96 | Matrix b22(half); | ||
| 97 | |||
| 98 | 8 | SplitMatrixImpl(a, a11, a12, a21, a22); | |
| 99 | 8 | SplitMatrixImpl(b, b11, b12, b21, b22); | |
| 100 | |||
| 101 |
2/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | Matrix p1 = MultiplyStandardImpl(a11, SubtractMatrixImpl(b12, b22)); |
| 102 |
2/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | Matrix p2 = MultiplyStandardImpl(AddMatrixImpl(a11, a12), b22); |
| 103 |
2/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | Matrix p3 = MultiplyStandardImpl(AddMatrixImpl(a21, a22), b11); |
| 104 |
2/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
8 | Matrix p4 = MultiplyStandardImpl(a22, SubtractMatrixImpl(b21, b11)); |
| 105 |
4/12✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
16 | Matrix p5 = MultiplyStandardImpl(AddMatrixImpl(a11, a22), AddMatrixImpl(b11, b22)); |
| 106 |
4/12✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
16 | Matrix p6 = MultiplyStandardImpl(SubtractMatrixImpl(a12, a22), AddMatrixImpl(b21, b22)); |
| 107 |
4/12✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
16 | Matrix p7 = MultiplyStandardImpl(SubtractMatrixImpl(a11, a21), AddMatrixImpl(b11, b12)); |
| 108 | |||
| 109 |
3/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
8 | Matrix c11 = AddMatrixImpl(SubtractMatrixImpl(AddMatrixImpl(p5, p4), p2), p6); |
| 110 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | Matrix c12 = AddMatrixImpl(p1, p2); |
| 111 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | Matrix c21 = AddMatrixImpl(p3, p4); |
| 112 |
3/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
8 | Matrix c22 = SubtractMatrixImpl(SubtractMatrixImpl(AddMatrixImpl(p5, p1), p3), p7); |
| 113 | |||
| 114 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | return MergeMatricesImpl(c11, c12, c21, c22); |
| 115 | } | ||
| 116 | |||
| 117 | } // namespace | ||
| 118 | |||
| 119 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | MorozovaSStrassenMultiplicationSTL::MorozovaSStrassenMultiplicationSTL(const InType &in) { |
| 120 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 121 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | GetInput() = in; |
| 122 | 88 | GetOutput() = OutType(); | |
| 123 | 88 | } | |
| 124 | |||
| 125 | 88 | bool MorozovaSStrassenMultiplicationSTL::ValidationImpl() { | |
| 126 | 88 | return true; | |
| 127 | } | ||
| 128 | |||
| 129 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 80 times.
|
88 | bool MorozovaSStrassenMultiplicationSTL::PreProcessingImpl() { |
| 130 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 80 times.
|
88 | if (GetInput().empty()) { |
| 131 | 8 | valid_data_ = false; | |
| 132 | 8 | return true; | |
| 133 | } | ||
| 134 | |||
| 135 | 80 | double size_val = GetInput()[0]; | |
| 136 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 72 times.
|
80 | if (size_val <= 0.0) { |
| 137 | 8 | valid_data_ = false; | |
| 138 | 8 | return true; | |
| 139 | } | ||
| 140 | |||
| 141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | int n = static_cast<int>(size_val); |
| 142 | |||
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (GetInput().size() != 1 + (2 * static_cast<size_t>(n) * static_cast<size_t>(n))) { |
| 144 | ✗ | valid_data_ = false; | |
| 145 | ✗ | return true; | |
| 146 | } | ||
| 147 | |||
| 148 | 72 | valid_data_ = true; | |
| 149 | 72 | n_ = n; | |
| 150 | |||
| 151 | 72 | a_ = Matrix(n_); | |
| 152 | 72 | b_ = Matrix(n_); | |
| 153 | |||
| 154 | int idx = 1; | ||
| 155 |
2/2✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 72 times.
|
2136 | for (int i = 0; i < n_; ++i) { |
| 156 |
2/2✓ Branch 0 taken 174832 times.
✓ Branch 1 taken 2064 times.
|
176896 | for (int j = 0; j < n_; ++j) { |
| 157 | 174832 | a_(i, j) = GetInput()[idx++]; | |
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 |
2/2✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 72 times.
|
2136 | for (int i = 0; i < n_; ++i) { |
| 162 |
2/2✓ Branch 0 taken 174832 times.
✓ Branch 1 taken 2064 times.
|
176896 | for (int j = 0; j < n_; ++j) { |
| 163 | 174832 | b_(i, j) = GetInput()[idx++]; | |
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | return true; | ||
| 168 | } | ||
| 169 | |||
| 170 | 88 | bool MorozovaSStrassenMultiplicationSTL::RunImpl() { | |
| 171 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 16 times.
|
88 | if (!valid_data_) { |
| 172 | return true; | ||
| 173 | } | ||
| 174 | |||
| 175 | const int leaf_size = 64; | ||
| 176 | |||
| 177 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 8 times.
|
72 | if (n_ <= leaf_size) { |
| 178 | 128 | c_ = MultiplyStandardImpl(a_, b_); | |
| 179 | } else { | ||
| 180 | 16 | c_ = MultiplyStrassenIterative(a_, b_, leaf_size); | |
| 181 | } | ||
| 182 | |||
| 183 | return true; | ||
| 184 | } | ||
| 185 | |||
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | bool MorozovaSStrassenMultiplicationSTL::PostProcessingImpl() { |
| 187 | OutType &output = GetOutput(); | ||
| 188 | output.clear(); | ||
| 189 | |||
| 190 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 16 times.
|
88 | if (!valid_data_) { |
| 191 | return true; | ||
| 192 | } | ||
| 193 | |||
| 194 | 72 | output.push_back(static_cast<double>(n_)); | |
| 195 | |||
| 196 |
2/2✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 72 times.
|
2136 | for (int i = 0; i < n_; ++i) { |
| 197 |
2/2✓ Branch 0 taken 174832 times.
✓ Branch 1 taken 2064 times.
|
176896 | for (int j = 0; j < n_; ++j) { |
| 198 | output.push_back(c_(i, j)); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | return true; | ||
| 203 | } | ||
| 204 | |||
| 205 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::AddMatrix(const Matrix &a, const Matrix &b) { | |
| 206 | ✗ | return AddMatrixImpl(a, b); | |
| 207 | } | ||
| 208 | |||
| 209 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::SubtractMatrix(const Matrix &a, const Matrix &b) { | |
| 210 | ✗ | return SubtractMatrixImpl(a, b); | |
| 211 | } | ||
| 212 | |||
| 213 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::MultiplyStandard(const Matrix &a, const Matrix &b) { | |
| 214 | ✗ | return MultiplyStandardImpl(a, b); | |
| 215 | } | ||
| 216 | |||
| 217 | ✗ | void MorozovaSStrassenMultiplicationSTL::SplitMatrix(const Matrix &m, Matrix &m11, Matrix &m12, Matrix &m21, | |
| 218 | Matrix &m22) { | ||
| 219 | ✗ | SplitMatrixImpl(m, m11, m12, m21, m22); | |
| 220 | ✗ | } | |
| 221 | |||
| 222 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::MergeMatrices(const Matrix &m11, const Matrix &m12, const Matrix &m21, | |
| 223 | const Matrix &m22) { | ||
| 224 | ✗ | return MergeMatricesImpl(m11, m12, m21, m22); | |
| 225 | } | ||
| 226 | |||
| 227 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::MultiplyStrassen(const Matrix &a, const Matrix &b, int leaf_size) { | |
| 228 | ✗ | return MultiplyStrassenIterative(a, b, leaf_size); | |
| 229 | } | ||
| 230 | |||
| 231 | ✗ | Matrix MorozovaSStrassenMultiplicationSTL::MultiplyStandardParallel(const Matrix &a, const Matrix &b) { | |
| 232 | ✗ | return MultiplyStandardImpl(a, b); | |
| 233 | } | ||
| 234 | |||
| 235 | } // namespace morozova_s_strassen_multiplication | ||
| 236 |