-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrassen.cpp
384 lines (332 loc) · 8.97 KB
/
strassen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using std::atoi;
#define DEBUG false
/**
* @brief Print a matrix
*
* @param n The size of the matrix
* @param matrix The matrix to be printed
*/
void print_matrix(size_t n, int** const& matrix) {
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j)
printf("%d\t", matrix[i][j]);
printf("\n");
}
}
/**
* @brief Allocate memory for a matrix
*
* @param n The size of the matrix
* @return int** The allocated matrix
*/
int** allocate_matrix(size_t n) {
int** matrix = new int* [n] {};
for (size_t i = 0; i < n; ++i)
matrix[i] = new int[n] {};
return matrix;
}
/**
* @brief Deallocate memory for a matrix
*
* @param n The size of the matrix
* @param matrix The matrix to be deallocated
*/
void deallocate_matrix(size_t n, int** matrix) {
for (size_t i = 0; i < n; ++i)
delete[] matrix[i];
delete[] matrix;
}
/**
* @brief Generate a random matrix
*
* @param n The size of the matrix
* @param matrix The matrix to be generated
*/
void generate_matrix(size_t n, int** matrix) {
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < n; j++)
matrix[i][j] = rand() % 10;
}
/**
* @brief Add two matrices. C = A + B
*
* @param n The size of the matrices
* @param A The first matrix
* @param B The second matrix
*
* @return int** The result matrix
*/
int** add_matrix(size_t n, int** A, int** B) {
int** result = allocate_matrix(n);
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
result[i][j] = A[i][j] + B[i][j];
return result;
}
/**
* @brief Subtract two matrices. C = A - B
*
* @param n The size of the matrices
* @param A The first matrix
* @param B The second matrix
*
* @return int** The result matrix
*/
int** sub_matrix(size_t n, int** A, int** B) {
int** result = allocate_matrix(n);
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
result[i][j] = A[i][j] - B[i][j];
return result;
}
/**
* @brief Extract a quadrant of the matrix
*
* @param n size of the matrix
* @param matrix The matrix to be extracted
* @param row row offset
* @param col column offset
* @return int** The extracted matrix
*/
int** seperate_matrix(size_t n, int** matrix, size_t row, size_t col) {
size_t m = n >> 1;
int** slice = allocate_matrix(m);
for (size_t i = 0; i < m; ++i)
for (size_t j = 0; j < m; ++j)
slice[i][j] = matrix[i + row][j + col];
return slice;
}
/**
* @brief Combines 4 matrices into a single matrix
*
* @param m The size of the matrices
* @param C11 The top-left matrix
* @param C12 The top-right matrix
* @param C21 The bottom-left matrix
* @param C22 The bottom-right matrix
*
* @return int** The combined matrix
*/
int** combine_matrix(size_t m, int** C11, int** C12, int** C21, int** C22) {
int n = m << 1;
int** C = allocate_matrix(n);
for (size_t i = 0; i < m; ++i)
for (size_t j = 0; j < m; ++j) {
C[i][j] = C11[i][j];
C[i][j + m] = C12[i][j];
C[i + m][j] = C21[i][j];
C[i + m][j + m] = C22[i][j];
}
return C;
}
/**
* @brief Multiplies two matrixes together with the traditional method.
*
* @param n The size of the matrices
* @param A The first matrix
* @param B The second matrix
*
* @return int** The result matrix
*/
int** naive(size_t n, int** A, int** B) {
int** prod = allocate_matrix(n);
size_t i{}, j{};
#pragma omp parallel for collapse(2)
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
prod[i][j] = 0;
for (size_t k = 0; k < n; k++)
prod[i][j] += A[i][k] * B[k][j];
}
}
return prod;
}
/**
* @brief Multiply two matrices using the Strassen algorithm. C = A * B
*
* @param n The size of the matrices
* @param threshold The threshold value
* @param A The first matrix
* @param B The second matrix
*
* @return int** The result matrix
*/
int** strassen(size_t n, size_t threshold, int** A, int** B) {
if (n <= threshold)
return naive(n, A, B);
size_t m = n >> 1;
// Split matrices into 4 submatrices
int** A11 = seperate_matrix(n, A, 0, 0);
int** A12 = seperate_matrix(n, A, 0, m);
int** A21 = seperate_matrix(n, A, m, 0);
int** A22 = seperate_matrix(n, A, m, m);
int** B11 = seperate_matrix(n, B, 0, 0);
int** B12 = seperate_matrix(n, B, 0, m);
int** B21 = seperate_matrix(n, B, m, 0);
int** B22 = seperate_matrix(n, B, m, m);
// M1 = (A11 + A22)(B11 + B22)
int** M1{};
#pragma omp task shared(M1)
{
int** temp1 = add_matrix(m, A11, A22);
int** temp2 = add_matrix(m, B11, B22);
M1 = strassen(m, threshold, temp1, temp2);
deallocate_matrix(m, temp1);
deallocate_matrix(m, temp2);
}
// M2 = (A21 + A22) B11
int** M2{};
#pragma omp task shared(M2)
{
int** temp = add_matrix(m, A21, A22);
M2 = strassen(m, threshold, temp, B11);
deallocate_matrix(m, temp);
}
// M3 = A11 (B12 - B22)
int** M3{};
#pragma omp task shared(M3)
{
int** temp = sub_matrix(m, B12, B22);
M3 = strassen(m, threshold, A11, temp);
deallocate_matrix(m, temp);
}
// M4 = A22 (B21 - B11)
int** M4{};
#pragma omp task shared(M4)
{
int** temp = sub_matrix(m, B21, B11);
M4 = strassen(m, threshold, A22, temp);
deallocate_matrix(m, temp);
}
// M5 = (A11 + A12) B22
int** M5{};
#pragma omp task shared(M5)
{
int** temp = add_matrix(m, A11, A12);
M5 = strassen(m, threshold, temp, B22);
deallocate_matrix(m, temp);
}
// M6 = (A21 - A11) (B11 + B12)
int** M6{};
#pragma omp task shared(M6)
{
int** temp1 = sub_matrix(m, A21, A11);
int** temp2 = add_matrix(m, B11, B12);
M6 = strassen(m, threshold, temp1, temp2);
deallocate_matrix(m, temp1);
deallocate_matrix(m, temp2);
}
// M7 = (A12 - A22) (B21 + B22)
int** M7{};
#pragma omp task shared(M7)
{
int** temp1 = sub_matrix(m, A12, A22);
int** temp2 = add_matrix(m, B21, B22);
M7 = strassen(m, threshold, temp1, temp2);
deallocate_matrix(m, temp1);
deallocate_matrix(m, temp2);
}
#pragma omp taskwait
deallocate_matrix(m, A11);
deallocate_matrix(m, A12);
deallocate_matrix(m, A21);
deallocate_matrix(m, A22);
deallocate_matrix(m, B11);
deallocate_matrix(m, B12);
deallocate_matrix(m, B21);
deallocate_matrix(m, B22);
// C11 = M1 + M4 - M5 + M7
int** c11{};
#pragma omp task shared(c11)
{
int** temp1 = add_matrix(m, M1, M4);
int** temp2 = sub_matrix(m, M7, M5);
c11 = add_matrix(m, temp1, temp2);
deallocate_matrix(m, temp1);
deallocate_matrix(m, temp2);
}
// C12 = M3 + M5
int** c12{};
#pragma omp task shared(c12)
{
c12 = add_matrix(m, M4, M5);
}
// C21 = M2 + M4
int** c21{};
#pragma omp task shared(c21)
{
c21 = add_matrix(m, M6, M7);
}
// C22 = M1 - M2 + M3 + M6
int** c22{};
#pragma omp task shared(c22)
{
int** temp1 = sub_matrix(m, M1, M2);
int** temp2 = add_matrix(m, M3, M6);
c22 = add_matrix(m, temp1, temp2);
deallocate_matrix(m, temp1);
deallocate_matrix(m, temp2);
}
#pragma omp taskwait
deallocate_matrix(m, M1);
deallocate_matrix(m, M2);
deallocate_matrix(m, M3);
deallocate_matrix(m, M4);
deallocate_matrix(m, M5);
deallocate_matrix(m, M6);
deallocate_matrix(m, M7);
int** prod = combine_matrix(m, c11, c12, c21, c22);
deallocate_matrix(m, c11);
deallocate_matrix(m, c12);
deallocate_matrix(m, c21);
deallocate_matrix(m, c22);
return prod;
}
int main(int argc, char* argv[]) {
struct timespec start, stop;
double total_time;
if (argc != 4) {
printf("Usage: %s <k> <k'> <num_threads>\n", argv[0]);
return EXIT_FAILURE;
}
int k = atoi(argv[1]);
int k_prime = atoi(argv[2]);
int num_threads = atoi(argv[3]);
size_t n = 1 << k;
int** A{};
int** B{};
int** C{};
srand(0);
A = allocate_matrix(n);
B = allocate_matrix(n);
generate_matrix(n, A);
generate_matrix(n, B);
if (DEBUG) {
print_matrix(n, A);
printf("-----------------------\n");
print_matrix(n, B);
printf("-----------------------\n");
}
clock_gettime(CLOCK_REALTIME, &start);
omp_set_num_threads(num_threads);
#pragma omp parallel
{
#pragma omp single
{
C = strassen(n, k_prime, A, B);
}
}
clock_gettime(CLOCK_REALTIME, &stop);
total_time = (stop.tv_sec - start.tv_sec) + 0.000000001 * (stop.tv_nsec - start.tv_nsec);
if (DEBUG)
print_matrix(n, C);
printf("Time: %f\n", total_time);
deallocate_matrix(n, A);
deallocate_matrix(n, B);
deallocate_matrix(n, C);
return EXIT_SUCCESS;
}