forked from tgmattso/OpenMP_intro_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul_recur.cpp
executable file
·204 lines (174 loc) · 5.46 KB
/
matmul_recur.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
// Several versions of serial codes for matrix-matrix multiplication
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include "2DArray.h"
// define sizes of matrices to be used
#define MM 1000
#define NN 1000
#define PP 1000
double dabs(double d){return (d<0.0?d:(-d));}
// Default triple-nested loop for matrix-matrix multiplication
void matmult1(int m, int n, int p, double **A, double **B, double **C)
{
int i, j, k;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++){
C[i][j]=0;
for (k = 0; k < p; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
/*
Recursive code for matrix multiplication.
The recursion uses the formula
C00 = A00*B00 + A01*B10
C01 = A00*B01 + B01*B11
C10 = A10*B00 + A11*B10
C11 = A10*B01 + A11*B11
*/
void matmultleaf(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C)
/*
subroutine that uses the simple triple loop to multiply
a submatrix from A with a submatrix from B and store the
result in a submatrix of C.
(We could use a tiled version,for better performance)
*/
// mf, ml; /* first and last+1 i index */
// nf, nl; /* first and last+1 j index */
// pf, pl; /* first and last+1 k index */
{
int i,j,k;
for (i = mf; i < ml; i++)
for (j = nf; j < nl; j++)
for (k = pf; k < pl; k++)
C[i][j] += A[i][k]*B[k][j];
}
#define GRAIN 32768 /* product size below which matmultleaf is used */
void matmultrec(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C)
/*
recursive subroutine to compute the product of two
submatrices of A and B and store the result in C
*/
// mf, ml; /* first and last+1 i index */
// nf, nl; /* first and last+1 j index */
// pf, pl; /* first and last+1 k index */
{
//
// Check sizes of matrices;
// if below threshold then compute product w/o recursion
//
if ((ml-mf)*(nl-nf)*(pl-pf) < GRAIN)
matmultleaf(mf, ml, nf, nl, pf, pl, A, B, C);
else {
//
// Apply OpenMP tasks to the eight recursive calls below
// be sure to not create data races between tasks
//
// C00 += A00 * B00
matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf, pf+(pl-pf)/2, A, B, C);
// C01 += A00 * B01
matmultrec(mf, mf+(ml-mf)/2, nf+(nl-nf)/2, nl, pf, pf+(pl-pf)/2, A, B, C);
// C00 += A01 * B10
matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf+(pl-pf)/2, pl, A, B, C);
// C01 += A01 * B11
matmultrec(mf, mf+(ml-mf)/2, nf+(nl-nf)/2, nl, pf+(pl-pf)/2, pl, A, B, C);
// C10 += A10 * B00
matmultrec(mf+(ml-mf)/2, ml, nf, nf+(nl-nf)/2, pf, pf+(pl-pf)/2, A, B, C);
// C11 += A10 * B01
matmultrec(mf+(ml-mf)/2, ml, nf+(nl-nf)/2, nl, pf, pf+(pl-pf)/2, A, B, C);
// C10 += A11 * B10
matmultrec(mf+(ml-mf)/2, ml, nf, nf+(nl-nf)/2, pf+(pl-pf)/2, pl, A, B, C);
// C11 += A11 * B11
matmultrec(mf+(ml-mf)/2, ml, nf+(nl-nf)/2, nl, pf+(pl-pf)/2, pl, A, B, C);
}
}
//
// "Helper" function to intialize C and start recursive routine
//
void matmultr(int m, int n, int p, double **A, double **B, double **C)
{
int i,j;
for (i = 0; i < m; i++)
for (j=0; j < n; j++)
C[i][j] = 0;
matmultrec(0, m, 0, n, 0, p, A, B, C);
}
int CheckResults(int m, int n, double **C, double **C1)
{
#define ERR_THRESHOLD 0.001
int code = 0;
//
// May need to take into consideration the floating point roundoff error
// due to parallel execution
//
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dabs(C[i][j] - C1[i][j]) > ERR_THRESHOLD ) {
printf("%f %f at [%d][%d]\n", C[i][j], C1[i][j], i, j);
code = 1;
}
}
}
return code;
}
int main(int argc, char* argv[])
{
int i, j;
double start, time1, time2;
int M = MM;
int N = NN;
int P = PP;
//
// If 3 values on command line, use those for matrix sizes
//
if (argc != 4) {
printf("Suggested Usage: %s <M> <N> <P> \n", argv[0]);
printf("Using default values\n");
}
else {
M = atoi(argv[1]);
N = atoi(argv[2]);
P = atoi(argv[3]);
}
double **A = Allocate2DArray< double >(M, P);
double **B = Allocate2DArray< double >(P, N);
double **C1 = Allocate2DArray< double >(M, N);
double **C4 = Allocate2DArray< double >(M, N);
//
// Initialize with random values
//
for (i = 0; i < M; i++) {
for (j = 0; j < P; j++) {
A[i][j] = (double)(rand()%100) / 10.0;
}
}
for (i = 0; i < P; i++) {
for (j = 0; j < N; j++) {
B[i][j] = (double)(rand()%100) / 10.0;
}
}
printf("Matrix Dimensions: M = %d P = %d N = %d\n\n", M, P, N);
printf("Execute matmult1\n");
start = omp_get_wtime();
matmult1(M, N, P, A, B, C1);
time1 = omp_get_wtime() - start;
printf("Time = %f seconds\n\n",time1);
printf("Execute matmultr\n");
start = omp_get_wtime();
matmultr(M, N, P, A, B, C4);
time2 = omp_get_wtime() - start;
printf("Time = %f seconds\n\n",time2);
printf("Checking...");
if (CheckResults(M, N, C1, C4))
printf("Error in Recursive Matrix Multiplication\n\n");
else {
printf("OKAY\n\n");
printf("Speedup = %5.1fX\n", time1/time2);
}
Free2DArray< double >(A);
Free2DArray< double >(B);
Free2DArray< double >(C1);
Free2DArray< double >(C4);
return 0;
}