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