-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadedMatrixMult.h
More file actions
401 lines (335 loc) · 15.8 KB
/
Copy pathThreadedMatrixMult.h
File metadata and controls
401 lines (335 loc) · 15.8 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
/*
* Author: Shepard Berry
* Assignment Title: MatrixMultiplication
* Assignment Description: This program demonstrates 4 different matrix multiplication
* algorithms.
* Due Date: 3/26/2023
* Date Created: 3/3/2023
* Date Last Modified: 3/26/2023
*/
#ifndef MATRIXMULTIPLICATION_THREADEDMATRIXMULT_H
#define MATRIXMULTIPLICATION_THREADEDMATRIXMULT_H
#include <pthread.h>
using namespace std;
struct SquareMatrix{
int dim;
int** data; // points to a [dim x dim] square matrix
/*
* description: Default SquareMatrix constructor
* return: none
* precondition: none
* postcondition: A SquareMatrix is constructed
*/
SquareMatrix() {}
/*
* description: SquareMatrix constructor with dimension parameter.
* return: none
* precondition: Dimensions necessary for SquareMatrix are declared
* postcondition: A SquareMatrix is constructed
*/
SquareMatrix(int dim) {
this->dim = dim;
data = new int*[dim];
for(int i = 0; i < dim; i++) {
data[i] = new int[dim];
for(int j = 0; j < dim; j++) {
data[i][j] = 0;
}
}
}
};
struct MatrixSection {
// refers to a complete matrix, and knows its bounds
SquareMatrix* M;
int rowStart, colStart, dim;
/*
* description: Constructs a matrix section with an underlying matrix
* and bounded dimensions
* return: none
* precondition: A matrix in memory is declared
* postcondition: A MatrixSection is constructed
*/
MatrixSection(SquareMatrix* M, int rowStart, int colStart, int dim) {
this->M = M;
this->rowStart = rowStart;
this->colStart = colStart;
this->dim = dim;
}
};
struct MatrixThreadParams {
const MatrixSection* A;
const MatrixSection* B;
MatrixSection* R;
/*
* description: Constructs a MatrixThreadParams object
* return: none
* precondition: 3 matrices are declared
* postcondition: A MatrixThreadParams object is constructed
*/
MatrixThreadParams(const MatrixSection* A, const MatrixSection* B, MatrixSection* R) {
this->A = A;
this->B = B;
this->R = R;
}
};
/*
* description: adds two matrix sections and stores the result in a
* resulting matrix section
* return: MatrixSection*
* precondition: 3 Matrix Sections are declared
* postcondition: result of A + B stored in C
*/
MatrixSection* matrixAddition(const MatrixSection& A, const MatrixSection& B, MatrixSection& C) {
for(int i = 0; i < C.dim; i++) {
for(int j = 0; j < C.dim; j++) {
C.M->data[i + C.rowStart][j + C.colStart] = A.M->data[i + A.rowStart][j + A.colStart] + B.M->data[i + B.rowStart][j + B.colStart];
}
}
return (MatrixSection *)&C;
}
/*
* description: subtracts two matrix sections and stores the result in a
* resulting matrix section
* return: MatrixSection*
* precondition: 3 Matrix Sections are declared
* postcondition: result of A - B stored in C
*/
MatrixSection* matrixSubtraction(const MatrixSection& A, const MatrixSection& B, MatrixSection& C) {
for(int i = 0; i < C.dim; i++) {
for(int j = 0; j < C.dim; j++) {
C.M->data[i + C.rowStart][j + C.colStart] = A.M->data[i + A.rowStart][j + A.colStart] - B.M->data[i + B.rowStart][j + B.colStart];
}
}
return (MatrixSection *)&C;
}
/*
* description: Multiplies two matrix sections together
* return: void *
* precondition: A MatrixThreadParam* is declared
* postcondition: results of A * B stored in R
*/
void* BruteForceSquareMatrixMultiplication(void* params) {
MatrixThreadParams* matrixParams = (MatrixThreadParams*)params;
const MatrixSection* A = matrixParams->A;
const MatrixSection* B = matrixParams->B;
MatrixSection* R = matrixParams->R;
int mid = R->M->dim/2;
for(int i = 0; i < A->dim; i++) {
for(int j = 0; j < A->dim; j++) {
int sum = 0;
for(int k = 0; k < R->M->dim; k++) {
sum += A->M->data[i + A->rowStart][k + A->colStart] * B->M->data[k + B->rowStart][j + B->colStart];
}
R->M->data[i + R->rowStart][j + R->colStart] = sum;
}
}
}
/*
* description: Helper function for Strassen that performs Threaded Multiplication
* return: void
* precondition: 3 matrix sections are declared
* postcondition: results of A * B stored in C
*/
void StrassenHelper(const MatrixSection& A, const MatrixSection& B, MatrixSection& C) {
// uses custom input from strassen to then call threaded divide and conquer
// Going to perform A * B and store it in C
if(A.dim == 1 && B.dim == 1) {
C.M->data[0][0] = A.M->data[A.rowStart][A.colStart] * B.M->data[B.rowStart][B.colStart];
} else {
int mid = A.dim/2;
// split matrix into 4 sections
MatrixThreadParams* R11Param = new MatrixThreadParams(new MatrixSection(A.M, A.rowStart, A.colStart, mid), new MatrixSection(B.M, B.rowStart, B.colStart, mid), new MatrixSection(C.M, C.rowStart, C.colStart, mid));
MatrixThreadParams* R12Param = new MatrixThreadParams(new MatrixSection(A.M, A.rowStart, A.colStart, mid), new MatrixSection(B.M, B.rowStart, B.colStart + mid, mid), new MatrixSection(C.M, C.rowStart, C.colStart + mid, mid));
MatrixThreadParams* R21Param = new MatrixThreadParams(new MatrixSection(A.M, A.rowStart + mid, A.colStart, mid), new MatrixSection(B.M, B.rowStart, B.colStart, mid), new MatrixSection(C.M, C.rowStart + mid, C.colStart, mid));
MatrixThreadParams* R22Param = new MatrixThreadParams(new MatrixSection(A.M, A.rowStart + mid, A.colStart, mid),new MatrixSection(B.M, B.rowStart, B.colStart + mid, mid), new MatrixSection(C.M, C.rowStart + mid, C.colStart + mid, mid));
pthread_t ids[4];
// spawn 4 threads
pthread_create(&ids[0], NULL, &BruteForceSquareMatrixMultiplication, (void *)R11Param);
pthread_create(&ids[1], NULL, &BruteForceSquareMatrixMultiplication, (void *)R12Param);
pthread_create(&ids[2], NULL, &BruteForceSquareMatrixMultiplication, (void *)R21Param);
pthread_create(&ids[3], NULL, &BruteForceSquareMatrixMultiplication, (void *)R22Param);
// join all threads
pthread_join(ids[0], NULL);
pthread_join(ids[1], NULL);
pthread_join(ids[2], NULL);
pthread_join(ids[3], NULL);
}
}
/*
* description: Performs brute force matrix multiplication
* return: SquareMatrix*
* precondition: 2 SquareMatrix objects with equal dimensions are declared
* postcondition: result of A * B returned
*/
SquareMatrix* BruteForce(const SquareMatrix& A, const SquareMatrix& B) {
SquareMatrix* m = new SquareMatrix(A.dim);
int sum;
for(int i = 0; i < m->dim; i++) {
for(int j = 0; j < m->dim; j++) {
sum = 0;
for(int k = 0; k < m->dim; k++) {
sum += A.data[i][k] * B.data[k][j];
}
m->data[i][j] = sum;
}
}
return m;
}
/*
* description: Performs threaded brute force multiplication
* return: SquareMatrix*
* precondition: 2 SquareMatrix objects with equal dimensions are declared
* postcondition: result of A * B returned
*/
SquareMatrix* ThreadedDivideAndConquer(const SquareMatrix& A, const SquareMatrix& B) {
SquareMatrix* resM = new SquareMatrix(A.dim);
if(A.dim == 1 && B.dim == 1) {
resM->data[0][0] = A.data[0][0] * B.data[0][0];
} else {
int mid = A.dim / 2;
MatrixThreadParams R11Param = MatrixThreadParams(new MatrixSection((SquareMatrix *) &A, 0, 0, mid), new MatrixSection((SquareMatrix *)&B, 0, 0, mid), new MatrixSection(resM, 0, 0, mid));
MatrixThreadParams R12Param = MatrixThreadParams(new MatrixSection((SquareMatrix *) &A, 0, 0, mid), new MatrixSection((SquareMatrix*) &B, 0, mid,mid), new MatrixSection(resM, 0, mid, mid));
MatrixThreadParams R21Param = MatrixThreadParams(new MatrixSection((SquareMatrix *) &A, mid, 0, mid), new MatrixSection((SquareMatrix*) &B, 0, 0, mid), new MatrixSection(resM, mid, 0, mid));
MatrixThreadParams R22Param = MatrixThreadParams(new MatrixSection((SquareMatrix *) &A, mid, 0, mid), new MatrixSection((SquareMatrix *)&B, 0, mid, mid), new MatrixSection(resM, mid, mid, mid));
pthread_t ids[4];
pthread_create(&ids[0], NULL, &BruteForceSquareMatrixMultiplication, (void *)&R11Param);
pthread_create( &ids[1], NULL, &BruteForceSquareMatrixMultiplication, (void *)&R12Param);
pthread_create(&ids[2], NULL, &BruteForceSquareMatrixMultiplication, (void *)&R21Param);
pthread_create(&ids[3], NULL, &BruteForceSquareMatrixMultiplication, (void *)&R22Param);
pthread_join(ids[0], NULL);
pthread_join(ids[1], NULL);
pthread_join(ids[2], NULL);
pthread_join(ids[3], NULL);
}
return resM;
}
/*
* description: Performs Strassen matrix multiplication on two matrices
* return: SquareMatrix*
* precondition: 2 SquareMatrix objects with equal dimensions are declared
* postcondition: result of A * B returned
*/
SquareMatrix* Strassen(const SquareMatrix& A, const SquareMatrix& B) {
int mid = A.dim/2;
// no new matrices being built, just sections being assigned
MatrixSection A11 = MatrixSection((SquareMatrix *)&A, 0, 0, mid);
MatrixSection A12 = MatrixSection((SquareMatrix *)&A, 0, mid, mid);
MatrixSection A21 = MatrixSection((SquareMatrix *)&A, mid, 0, mid);
MatrixSection A22 = MatrixSection((SquareMatrix *)&A, mid, mid, mid);
MatrixSection B11 = MatrixSection((SquareMatrix *)&B, 0, 0, mid);
MatrixSection B12 = MatrixSection((SquareMatrix *)&B, 0, mid, mid);
MatrixSection B21 = MatrixSection((SquareMatrix *)&B, mid, 0, mid);
MatrixSection B22 = MatrixSection((SquareMatrix *)&B, mid, mid, mid);
MatrixSection* sMatrices[7];
// only time that this function will change with input size. No way around it
for(int i = 0; i < 7; i++) {
sMatrices[i] = new MatrixSection(new SquareMatrix(mid), 0, 0, mid); // death
}
SquareMatrix* resM = new SquareMatrix(A.dim);
// storing results of operations
MatrixSection storeRes1 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes2 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
// s1
StrassenHelper(A11, *matrixSubtraction(B12, B22, storeRes1), *sMatrices[0]);
// s2
StrassenHelper(*matrixAddition(A11, A12, storeRes1), B22, *sMatrices[1]);
// s3
StrassenHelper(*matrixAddition(A21, A22, storeRes1), B11, *sMatrices[2]);
// s4
StrassenHelper(A22, *matrixSubtraction(B21, B11, storeRes1), *sMatrices[3]);
// s5
StrassenHelper(*matrixAddition(A11, A22, storeRes1), *matrixAddition(B11, B22, storeRes2), *sMatrices[4]);
// s6
StrassenHelper(*matrixSubtraction(A12, A22, storeRes1), *matrixAddition(B21, B22, storeRes2), *sMatrices[5]);
// s7
StrassenHelper(*matrixSubtraction(A11, A21, storeRes1), *matrixAddition(B11, B12, storeRes2), *sMatrices[6]);
MatrixSection I = MatrixSection(resM, 0, 0, mid);
MatrixSection J = MatrixSection(resM, 0, mid, mid);
MatrixSection K = MatrixSection(resM, mid, 0, mid);
MatrixSection L = MatrixSection(resM, mid, mid, mid);
// I
matrixAddition(*sMatrices[4], *sMatrices[3], storeRes1);
matrixSubtraction(storeRes1, *sMatrices[1], storeRes2);
matrixAddition(storeRes2, *sMatrices[5], I);
// J
matrixAddition(*sMatrices[0], *sMatrices[1], J);
// K
matrixAddition(*sMatrices[2], *sMatrices[3], K);
// L
matrixAddition(*sMatrices[0], *sMatrices[4], storeRes1);
matrixSubtraction(storeRes1, *sMatrices[2], storeRes2);
matrixSubtraction(storeRes2, *sMatrices[6], L);
return resM;
}
/*
* description: Performs threaded Strassen matrix multiplication on two matrices
* return: SquareMatrix*
* precondition: 2 SquareMatrix objects with equal dimensions are declared
* postcondition: result of A * B returned
*/
SquareMatrix* ThreadedStrassen(const SquareMatrix& A, const SquareMatrix& B) {
int mid = A.dim/2;
// no new matrices being built, just sections being assigned
MatrixSection A11 = MatrixSection((SquareMatrix *)&A, 0, 0, mid);
MatrixSection A12 = MatrixSection((SquareMatrix *)&A, 0, mid, mid);
MatrixSection A21 = MatrixSection((SquareMatrix *)&A, mid, 0, mid);
MatrixSection A22 = MatrixSection((SquareMatrix *)&A, mid, mid, mid);
MatrixSection B11 = MatrixSection((SquareMatrix *)&B, 0, 0, mid);
MatrixSection B12 = MatrixSection((SquareMatrix *)&B, 0, mid, mid);
MatrixSection B21 = MatrixSection((SquareMatrix *)&B, mid, 0, mid);
MatrixSection B22 = MatrixSection((SquareMatrix *)&B, mid, mid, mid);
MatrixSection* sMatrices[7];
// only time that this function will change with input size. No way around it
for(int i = 0; i < 7; i++) {
sMatrices[i] = new MatrixSection(new SquareMatrix(mid), 0, 0, mid); // death
}
SquareMatrix* resM = new SquareMatrix(A.dim);
// storing results of operations
MatrixSection storeRes1 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes2 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes3 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes4 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes5 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
MatrixSection storeRes6 = MatrixSection(new SquareMatrix(mid), 0, 0, mid);
pthread_t ids[4];
MatrixThreadParams s1 = MatrixThreadParams(&A11, matrixSubtraction(B12, B22, storeRes1), sMatrices[0]);
MatrixThreadParams s2 = MatrixThreadParams(matrixAddition(A11, A12, storeRes2), &B22, sMatrices[1]);
MatrixThreadParams s3 = MatrixThreadParams(matrixAddition(A21, A22, storeRes3), &B11, sMatrices[2]);
MatrixThreadParams s4 = MatrixThreadParams(&A22, matrixSubtraction(B21, B11, storeRes4), sMatrices[3]);
pthread_create(&ids[0] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s1);
pthread_create(&ids[1] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s2);
pthread_create(&ids[2] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s3);
pthread_create(&ids[3] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s4);
pthread_join(ids[0], NULL);
pthread_join(ids[1], NULL);
pthread_join(ids[2], NULL);
pthread_join(ids[3], NULL);
MatrixThreadParams s5 = MatrixThreadParams(matrixAddition(A11, A22, storeRes1), matrixAddition(B11, B22, storeRes2), sMatrices[4]);
MatrixThreadParams s6 = MatrixThreadParams(matrixSubtraction(A12, A22, storeRes3), matrixAddition(B21, B22, storeRes4), sMatrices[5]);
MatrixThreadParams s7 = MatrixThreadParams(matrixSubtraction(A11, A21, storeRes5), matrixAddition(B11, B12, storeRes6), sMatrices[6]);
pthread_create(&ids[0] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s5);
pthread_create(&ids[1] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s6);
pthread_create(&ids[2] , NULL, &BruteForceSquareMatrixMultiplication, (void *)&s7);
pthread_join(ids[0], NULL);
pthread_join(ids[1], NULL);
pthread_join(ids[2], NULL);
MatrixSection *I, *J, *K, *L;
I = new MatrixSection(resM, 0, 0, mid);
J = new MatrixSection(resM, 0, mid, mid);
K = new MatrixSection(resM, mid, 0, mid);
L = new MatrixSection(resM, mid, mid, mid);
// I
matrixAddition(*sMatrices[4], *sMatrices[3], storeRes1);
matrixSubtraction(storeRes1, *sMatrices[1], storeRes2);
matrixAddition(storeRes2, *sMatrices[5], *I);
// J
matrixAddition(*sMatrices[0], *sMatrices[1], *J);
// K
matrixAddition(*sMatrices[2], *sMatrices[3], *K);
// L
matrixAddition(*sMatrices[0], *sMatrices[4], storeRes1);
matrixSubtraction(storeRes1, *sMatrices[2], storeRes2);
matrixSubtraction(storeRes2, *sMatrices[6], *L);
return resM;
}
#endif//MATRIXMULTIPLICATION_THREADEDMATRIXMULT_H