Repository navigation
Expand file tree
/
Copy pathOpenMP7.cpp
More file actions
543 lines (476 loc) · 22 KB
/
Copy pathOpenMP7.cpp
File metadata and controls
543 lines (476 loc) · 22 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*******************************************************************************************
This algorithm is a parallel implementation of Strassen's matrix multiplication algorithm
using OpenMP.
The implementation will use 7 threads if needed. A single thread will be used for all
recursive calls.
To compile/run in linux:
mpicxx -std=c++11 -g -Wall -fopenmp -o openmp7 OpenMP7.cpp
./openmp7 [dimension] [max integer]
*******************************************************************************************/
#include <iostream>
#include <time.h>
#include <iomanip>
#include <string>
#include <math.h>
#include <omp.h>
using namespace std;
void FillMatrix(double matrix[], int dimension, int maxInt);
void StrassenMult(double matrix1[], double matrix2[], double matrix3[], int dim);
void StrassenMultOpenMP(double matrix1[], double matrix2[], double matrix3[], int dim);
void FillSubmatrices(double matrix[], int dim, double sub1[],
double sub2[], double sub3[], double sub4[],
int subDim);
void AddMatrices(double matrix1[], double matrix2[], double matrix3[], int dim);
void SubtractMatrices(double matrix1[], double matrix2[], double matrix3[], int dim);
void DisplayMatrix(double matrix[], int dim);
void FillWithQuads(double quad1[], double quad2[], double quad3[],
double quad4[], int subDim, double matrix[], int dim);
int main(int argc, char* argv[])
{
int dim; // row/col dim of input matrices
int maxInt; // number of possible element values
double* firstMatrix = NULL; // first input matrix
double* secondMatrix = NULL; // second input matrix
double* resultMatrix = NULL; // matrix mult result
double startTime; // start of matrix mult
double endTime; // end of matrix mult
// Check for correct argument count
if (argc != 3)
{
cerr << "Incorrect number of args!\n";
return 1;
}
// Set matrix dimension and max element size
dim = stoi(argv[1]);
maxInt = stoi(argv[2]);
// Check for valid dimension
if ((log(dim) / log(2)) != (int)(log(dim) / log(2))) // dim must be 2^x
{
cerr << "Invalid dimension!\n";
return 1;
}
// Check for valid maxInt
if (maxInt < 0)
{
cerr << "Invalid max integer!\n";
return 1;
}
// Initialize matrices
firstMatrix = new double[dim * dim];
secondMatrix = new double[dim * dim];
resultMatrix = new double[dim * dim];
// Initialize random number generator
srand(time(NULL));
// Fill two matrices
FillMatrix(firstMatrix, dim, maxInt);
FillMatrix(secondMatrix, dim, maxInt);
// Multiply the two matrices using Strassen's algorithm
startTime = omp_get_wtime();
StrassenMultOpenMP(firstMatrix, secondMatrix, resultMatrix, dim);
endTime = omp_get_wtime();
// Display results
printf("\nMultiplication of dimension %d matrices took %f"
" seconds\n\n", dim, endTime - startTime);
/*printf("first matrix:\n");
DisplayMatrix(firstMatrix, dim);
printf("second matrix:\n");
DisplayMatrix(secondMatrix, dim);
printf("result:\n");
DisplayMatrix(resultMatrix, dim);*/
// Deallocate matrices
delete [] firstMatrix;
delete [] secondMatrix;
delete [] resultMatrix;
return 0;
}
// Fill a matrix of a specified dimension with random integers
void FillMatrix(double matrix[], int dim, int maxInt)
{
if (maxInt == 0)
for (int i=0; i<(dim*dim); i++)
matrix[i] = 1;
else
for (int i=0; i<(dim*dim); i++)
matrix[i] = rand() % maxInt; // 0 <= matrix element < maxInt
}
// Multiply two matrices using Strassen's algorithm
void StrassenMult(double matrix1[], double matrix2[], double matrix3[], int dim)
{
// Check for matrices with 1 element
if (dim == 1)
{
matrix3[0] = matrix1[0] * matrix2[0]; // only int multipication needed
return;
}
if (dim == 2)
{
double p1, p2, p3, p4, p5, p6, p7; // hold results of Strassen's 7 equations
// Find 7 equation results for 2 x 2 matrices
p1 = matrix1[0] * (matrix2[1] - matrix2[3]); // a(f-h)
p2 = (matrix1[0] + matrix1[1]) * matrix2[3]; // (a+b)h
p3 = (matrix1[2] + matrix1[3]) * matrix2[0]; // (c+d)e
p4 = matrix1[3] * (matrix2[2] - matrix2[0]); // d(g-e)
p5 = (matrix1[0] + matrix1[3]) *
(matrix2[0] + matrix2[3]); // (a+d)(e+h)
p6 = (matrix1[1] - matrix1[3]) *
(matrix2[2] + matrix2[3]); // (b-d)(g+h)
p7 = (matrix1[0] - matrix1[2]) *
(matrix2[0] + matrix2[1]); // (a-c)(e+f)
// Fill result matrix3 based on p1-p7
matrix3[0] = p5 + p4 - p2 + p6;
matrix3[1] = p1 + p2;
matrix3[2] = p3 + p4;
matrix3[3] = p1 + p5 - p3 - p7;
}
else
{
int subDim = dim / 2; // dim for each quadrant of the matrices
int numElements = subDim * subDim; // number of elements in sub-matrices
// Initialize sub-matrices
double* a = new double[numElements]; // top, left quadrant of matrix1
double* b = new double[numElements]; // top, right quadrant of matrix1
double* c = new double[numElements]; // bottom, left quadrant of matrix1
double* d = new double[numElements]; // bottom, right quadrant of matrix1
double* e = new double[numElements]; // top, left quadrant of matrix2
double* f = new double[numElements]; // top, right quadrant of matrix2
double* g = new double[numElements]; // bottom, left quadrant of matrix2
double* h = new double[numElements]; // bottom, right quadrant of matrix2
double* result1 = new double[numElements]; // the result of a sub-matrix operation
double* result2 = new double[numElements]; // the result of a sub-matrix operation
double* m1 = new double[numElements]; // matrix with result of Strassen's eq. 1
double* m2 = new double[numElements]; // matrix with result of Strassen's eq. 2
double* m3 = new double[numElements]; // matrix with result of Strassen's eq. 3
double* m4 = new double[numElements]; // matrix with result of Strassen's eq. 4
double* m5 = new double[numElements]; // matrix with result of Strassen's eq. 5
double* m6 = new double[numElements]; // matrix with result of Strassen's eq. 6
double* m7 = new double[numElements]; // matrix with result of Strassen's eq. 7
double* quad1 = new double[numElements]; // top, left quadrant of matrix3
double* quad2 = new double[numElements]; // top, right quadrant of matrix3
double* quad3 = new double[numElements]; // bottom, left quadrant of matrix3
double* quad4 = new double[numElements]; // bottom, right quadrant of matrix3
// Fill sub-matrices a-h from matrix1 and matrix2
FillSubmatrices(matrix1, dim, a, b, c, d, subDim);
FillSubmatrices(matrix2, dim, e, f, g, h, subDim);
// Find matrices m1-m7 with results for equations 1-7
SubtractMatrices(f, h, result1, subDim); // f-h
StrassenMult(a, result1, m1, subDim); // a(f-h)
AddMatrices(a, b, result1, subDim); // a+b
StrassenMult(result1, h, m2, subDim); // (a+b)h
AddMatrices(c, d, result1, subDim); // c+d
StrassenMult(result1, e, m3, subDim); // (c+d)e
SubtractMatrices(g, e, result1, subDim); // g-e
StrassenMult(d, result1, m4, subDim); // d(g-e)
AddMatrices(a, d, result1, subDim); // a+d
AddMatrices(e, h, result2, subDim); // e+h
StrassenMult(result1, result2, m5, subDim); // (a+d)(e+h)
SubtractMatrices(b, d, result1, subDim); // b-d
AddMatrices(g, h, result2, subDim); // g+h
StrassenMult(result1, result2, m6, subDim); // (b-d)(g+h)
SubtractMatrices(a, c, result1, subDim); // a-c
AddMatrices(e, f, result2, subDim); // e+f
StrassenMult(result1, result2, m7, subDim); // (a-c)(e+f)
// Determine quadrants of matrix3 based on m1-m7
AddMatrices(m5, m4, result1, subDim); // m5+m4
SubtractMatrices(result1, m2, result2, subDim); // m5+m4-m2
AddMatrices(result2, m6, quad1, subDim); // m5+m4-m2+m6
AddMatrices(m1, m2, quad2, subDim); // m1+m2
AddMatrices(m3, m4, quad3, subDim); // m3+m4
AddMatrices(m1, m5, result1, subDim); // m1+m5
SubtractMatrices(result1, m3, result2, subDim); // m1+m5-m3
SubtractMatrices(result2, m7, quad4, subDim); // m1+m5-m3-m7
// Fill matrix3 from quadrants
FillWithQuads(quad1, quad2, quad3, quad4, subDim, matrix3, dim);
// Deallocate sub-matrices
delete [] a;
delete [] b;
delete [] c;
delete [] d;
delete [] e;
delete [] f;
delete [] g;
delete [] h;
delete [] result1;
delete [] result2;
delete [] m1;
delete [] m2;
delete [] m3;
delete [] m4;
delete [] m5;
delete [] m6;
delete [] m7;
delete [] quad1;
delete [] quad2;
delete [] quad3;
delete [] quad4;
}
}
// Multiply two matrices using Strassen's algorithm and OpenMP
void StrassenMultOpenMP(double matrix1[], double matrix2[], double matrix3[], int dim)
{
// Check for matrices with 1 element
if (dim == 1)
{
matrix3[0] = matrix1[0] * matrix2[0]; // only int multipication needed
}
if (dim == 2)
{
double p1, p2, p3, p4, p5, p6, p7; // hold results of Strassen's 7 equations
// Find 7 equation results for 2 x 2 matrices
p1 = matrix1[0] * (matrix2[1] - matrix2[3]); // a(f-h)
p2 = (matrix1[0] + matrix1[1]) * matrix2[3]; // (a+b)h
p3 = (matrix1[2] + matrix1[3]) * matrix2[0]; // (c+d)e
p4 = matrix1[3] * (matrix2[2] - matrix2[0]); // d(g-e)
p5 = (matrix1[0] + matrix1[3]) *
(matrix2[0] + matrix2[3]); // (a+d)(e+h)
p6 = (matrix1[1] - matrix1[3]) *
(matrix2[2] + matrix2[3]); // (b-d)(g+h)
p7 = (matrix1[0] - matrix1[2]) *
(matrix2[0] + matrix2[1]); // (a-c)(e+f)
// Fill result matrix3 based on p1-p7
matrix3[0] = p5 + p4 - p2 + p6;
matrix3[1] = p1 + p2;
matrix3[2] = p3 + p4;
matrix3[3] = p1 + p5 - p3 - p7;
}
if (dim > 2)
{
int subDim = dim / 2; // dim for each quadrant of the matrices
int numElements = subDim * subDim; // number of elements in sub-matrices
// Initialize sub-matrices
double* a = new double[numElements]; // top, left quadrant of matrix1
double* b = new double[numElements]; // top, right quadrant of matrix1
double* c = new double[numElements]; // bottom, left quadrant of matrix1
double* d = new double[numElements]; // bottom, right quadrant of matrix1
double* e = new double[numElements]; // top, left quadrant of matrix2
double* f = new double[numElements]; // top, right quadrant of matrix2
double* g = new double[numElements]; // bottom, left quadrant of matrix2
double* h = new double[numElements]; // bottom, right quadrant of matrix2
double* m1 = new double[numElements]; // matrix with result of Strassen's eq. 1
double* m2 = new double[numElements]; // matrix with result of Strassen's eq. 2
double* m3 = new double[numElements]; // matrix with result of Strassen's eq. 3
double* m4 = new double[numElements]; // matrix with result of Strassen's eq. 4
double* m5 = new double[numElements]; // matrix with result of Strassen's eq. 5
double* m6 = new double[numElements]; // matrix with result of Strassen's eq. 6
double* m7 = new double[numElements]; // matrix with result of Strassen's eq. 7
double* temp1 = new double[numElements]; // matrix with intermediate results
double* temp2 = new double[numElements]; // matrix with intermediate results
double* quad1 = new double[numElements]; // top, left quadrant of matrix3
double* quad2 = new double[numElements]; // top, right quadrant of matrix3
double* quad3 = new double[numElements]; // bottom, left quadrant of matrix3
double* quad4 = new double[numElements]; // bottom, right quadrant of matrix3
// Fill sub-matrices a-d from matrix1
FillSubmatrices(matrix1, dim, a, b, c, d, subDim);
// Fill sub-matrices e-f from matrix2
FillSubmatrices(matrix2, dim, e, f, g, h, subDim);
// Find matrices m1-m7 with results for equations 1-7
# pragma omp parallel sections num_threads(7) // fork 7 threads
{
# pragma omp section // allow only one thread per section
{
double* result1 = new double[numElements];
SubtractMatrices(f, h, result1, subDim); // f-h
StrassenMult(a, result1, m1, subDim); // a(f-h)
delete [] result1;
}
# pragma omp section
{
double* result1 = new double[numElements];
AddMatrices(a, b, result1, subDim); // a+b
StrassenMult(result1, h, m2, subDim); // (a+b)h
delete [] result1;
}
# pragma omp section
{
double* result1 = new double[numElements];
AddMatrices(c, d, result1, subDim); // c+d
StrassenMult(result1, e, m3, subDim); // (c+d)e
delete [] result1;
}
# pragma omp section
{
double* result1 = new double[numElements];
SubtractMatrices(g, e, result1, subDim); // g-e
StrassenMult(d, result1, m4, subDim); // d(g-e)
delete [] result1;
}
# pragma omp section
{
double* result1 = new double[numElements];
double* result2 = new double[numElements];
AddMatrices(a, d, result1, subDim); // a+d
AddMatrices(e, h, result2, subDim); // e+h
StrassenMult(result1, result2, m5, subDim); // (a+d)(e+h)
delete [] result1;
delete [] result2;
}
# pragma omp section
{
double* result1 = new double[numElements];
double* result2 = new double[numElements];
SubtractMatrices(b, d, result1, subDim); // b-d
AddMatrices(g, h, result2, subDim); // g+h
StrassenMult(result1, result2, m6, subDim); // (b-d)(g+h)
delete [] result1;
delete [] result2;
}
# pragma omp section
{
double* result1 = new double[numElements];
double* result2 = new double[numElements];
SubtractMatrices(a, c, result1, subDim); // a-c
AddMatrices(e, f, result2, subDim); // e+f
StrassenMult(result1, result2, m7, subDim); // (a-c)(e+f)
delete [] result1;
delete [] result2;
}
}
// Determine quadrants of matrix3 based on m1-m7
AddMatrices(m5, m4, temp1, subDim); // m5+m4
SubtractMatrices(temp1, m2, temp2, subDim); // m5+m4-m2
AddMatrices(temp2, m6, quad1, subDim); // m5+m4-m2+m6
AddMatrices(m1, m2, quad2, subDim); // m1+m2
AddMatrices(m3, m4, quad3, subDim); // m3+m4
AddMatrices(m1, m5, temp1, subDim); // m1+m5
SubtractMatrices(temp1, m3, temp2, subDim); // m1+m5-m3
SubtractMatrices(temp2, m7, quad4, subDim); // m1+m5-m3-m7
// Fill matrix3 from quadrants
FillWithQuads(quad1, quad2, quad3, quad4, subDim, matrix3, dim);
// Deallocate sub-matrices
delete [] a;
delete [] b;
delete [] c;
delete [] d;
delete [] e;
delete [] f;
delete [] g;
delete [] h;
delete [] m1;
delete [] m2;
delete [] m3;
delete [] m4;
delete [] m5;
delete [] m6;
delete [] m7;
delete [] temp1;
delete [] temp2;
delete [] quad1;
delete [] quad2;
delete [] quad3;
delete [] quad4;
}
}
// Divide a matrix up by quadrant into 4 sub-matrices. They are
// sub1, sub2, sub3, and sub4 starting in the top, left quadrant
// of the matrix, going left to right, and top to bottom.
void FillSubmatrices(double matrix[], int dim, double sub1[],
double sub2[], double sub3[], double sub4[],
int subDim)
{
int index1 = 0; // index of a sub-matrix 1 element
int index2 = 0; // index of a sub-matrix 2 element
int index3 = 0; // index of a sub-matrix 3 element
int index4 = 0; // index of a sub-matrix 4 element
int matrixElement = 0; // index of a matrix element
for (int row=0; row<subDim; row++)
for (int col=0; col<dim; col++)
{
if (col < subDim)
{
// Set sub1 element from matrix quadrant 1
sub1[index1] = matrix[matrixElement];
index1++;
}
else
{
// Set sub2 element from matrix quadrant 2
sub2[index2] = matrix[matrixElement];
index2++;
}
matrixElement++;
}
for (int row=subDim; row<dim; row++)
for (int col=0; col<dim; col++)
{
if (col < subDim)
{
// Set sub3 element from matrix quadrant 3
sub3[index3] = matrix[matrixElement];
index3++;
}
else
{
// Set sub4 element from matrix quadrant 4
sub4[index4] = matrix[matrixElement];
index4++;
}
matrixElement++;
}
}
// Add two matrices together into a result matrix
void AddMatrices(double matrix1[], double matrix2[], double matrix3[], int dim)
{
for (int i=0; i<(dim*dim); i++)
matrix3[i] = matrix1[i] + matrix2[i];
}
// Subtract two matrices. Subtract matrix2 from matrix1 and put the difference
// into a result matrix.
void SubtractMatrices(double matrix1[], double matrix2[], double matrix3[], int dim)
{
for (int i=0; i<(dim*dim); i++)
matrix3[i] = matrix1[i] - matrix2[i];
}
// Display a matrix
void DisplayMatrix(double matrix[], int dim)
{
for (int i=0; i<(dim*dim); i++)
{
if (i % dim == 0) // return at end of matrix line
cout << endl;
cout << " " << matrix[i];
}
cout << endl << endl;
}
// Fill a matrix with elements from four separate quadrant sub-matrices
void FillWithQuads(double quad1[], double quad2[], double quad3[],
double quad4[], int subDim, double matrix[], int dim)
{
int index1 = 0; // index of a quad1 element
int index2 = 0; // index of a quad2 element
int index3 = 0; // index of a quad3 element
int index4 = 0; // index of a quad4 element
int matrixElement = 0; // index of a matrix element
for (int row=0; row<subDim; row++)
for (int col=0; col<dim; col++)
{
if (col < subDim)
{
// Set matrix element from quad1
matrix[matrixElement] = quad1[index1];
index1++;
}
else
{
// Set matrix element from quad2
matrix[matrixElement] = quad2[index2];
index2++;
}
matrixElement++;
}
for (int row=subDim; row<dim; row++)
for (int col=0; col<dim; col++)
{
if (col < subDim)
{
// Set matrix element from quad3
matrix[matrixElement] = quad3[index3];
index3++;
}
else
{
// Set matrix element from quad4
matrix[matrixElement] = quad4[index4];
index4++;
}
matrixElement++;
}
}