forked from drmackay/samplematrixcode
/
mm.c
264 lines (211 loc) · 6.7 KB
/
mm.c
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
// A simple matrix multiply code to show affect of ordering and blocking
// to compile this use gcc -O2 mm.c -lrt or icc -O2 mm.c -lrt
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <cblas.h>
#define MATSIZE 8000
#define BLOCKSIZE 8
void setmat() ;
void fillmat() ;
void abasicmm() ;
void abettermm() ;
void ablockmm() ;
void checkmatmult() ;
void current_utc_time(struct timespec *ts);
int main(int argc, char *argv[])
{
double *a, *b, *c, *aa ;
unsigned int n ;
unsigned i, j, k, iInner, jInner, kInner, blockSize ;
struct timespec ts1, ts2, ts3, ts4, ts5, ts6, ts7 ;
printf("hello code beginning\n") ;
n = MATSIZE ; // default settings
blockSize = BLOCKSIZE ;
if (argc != 3)
{
printf("input matrix size and blocksize\n") ;
exit(0);
}
n = atoi(argv[1]) ;
blockSize = atoi(argv[2]) ;
printf("matrix size %d blocksize %d\n", n,blockSize) ;
if (n%blockSize)
{
printf("for this simple example matrix size must be a multiple of the block size.\n Please re-start \n") ;
exit(0);
}
// allocate matrices
a = (double *)calloc((n+blockSize)*(n+blockSize), sizeof(double)) ;
b = (double *)calloc((n+blockSize)*(n+blockSize), sizeof(double)) ;
c = (double *)calloc((n+blockSize)*(n+blockSize), sizeof(double)) ;
aa = (double *)calloc((n+blockSize)*(n+blockSize), sizeof(double)) ;
if (aa == NULL) // cheap check only the last allocation checked.
{
printf("insufficient memory \n") ;
exit(0) ;
}
// fill matrices
setmat(n, n, a) ;
setmat(n, n, aa) ;
srand(1) ; // set random seed (change to go off time stamp to make it better
fillmat(n,n,b) ;
fillmat(n,n,c) ;
current_utc_time(&ts1) ;
// multiply matrices
abasicmm (n,n,a,b,c) ;
current_utc_time(&ts2) ;
setmat(n, n, a) ;
current_utc_time(&ts3) ;
abettermm (n,n,a,b,c) ;
current_utc_time(&ts4) ;
ablockmm (n, n, aa, b, c, blockSize) ;
current_utc_time(&ts5) ;
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
n, n, n, 1.0, b, n, c, n, 0.0, a, n);
current_utc_time(&ts6) ;
printf("matrix multplies complete \n") ; fflush(stdout) ;
/**/
checkmatmult(n,n,a,aa) ;
{
double t1, t2, t3, t4, tmp ;
t1 = ts2.tv_sec-ts1.tv_sec;
tmp = ts2.tv_nsec-ts1.tv_nsec;
tmp /= 1.0e+09 ;
t1 += tmp ;
printf("ijk ordering basic time %lf\n",t1) ;
t2 = ts4.tv_sec-ts3.tv_sec;
tmp = ts4.tv_nsec-ts3.tv_nsec;
tmp /= 1.0e+09 ;
t2 += tmp ;
printf("ikj ordering bette time %lf\n",t2) ;
t3 = ts5.tv_sec-ts4.tv_sec;
tmp = ts5.tv_nsec-ts4.tv_nsec;
tmp /= 1.0e+09 ;
t3 += tmp ;
printf("ikj blocked time %lf\n",t3) ;
t4 = ts6.tv_sec-ts5.tv_sec;
tmp = ts6.tv_nsec-ts5.tv_nsec;
tmp /= 1.0e+09 ;
t4 += tmp ;
printf("cblas_dgemm %lf\n",t4) ;
}
}
void setmat(int n, int m, double a[n][m])
{
int i, j ;
for (i=0;i<n; i++)
for (j = 0 ; j<m; j++)
{
a[i][j] = 0.0 ;
}
}
void fillmat(int n, int m, double a[n][m])
{
int i, j ;
for (i = 0; i<n; i++)
for (j = 0 ; j < m; j++)
{
a[i][j] = (double)rand() / 3.1e09 ;
}
}
void abasicmm(int n, int m, double a[n][m], double b[n][m], double c[n][m])
{
int i, j, k ;
for (i=0;i<n; i++)
for (j = 0; j<n; j++)
for (k=0;k<n; k++)
a[i][j] += b[i][k]* c[k][j] ;
}
void abettermm(int n, int m, double a[n][m], double b[n][m], double c[n][m])
{
int i, j, k ;
for (i=0;i<n; i++)
for (k=0;k<n; k++)
for (j = 0; j<n; j++)
a[i][j] += b[i][k]* c[k][j] ;
}
void ablockmm(int n,int m,double a[n][m],double b[n][m],double c[n][m], int blockSize)
{
int i, j, k, iInner, jInner, kInner ;
for (i = 0; i < n; i+=blockSize)
for (k = 0 ; k < n; k+=blockSize)
for (j=0; j<n ; j+= blockSize)
for (iInner = i; iInner<i+blockSize; iInner++)
for (kInner = k ; kInner<k+blockSize ; kInner++)
for (jInner = j ; jInner<j+blockSize; jInner++)
a[iInner][jInner] += b[iInner][kInner] * c[kInner][jInner] ;
}
void checkmatmult(int n,int m, double a[n][m], double aa[n][m])
{
// crude check. Never compare floating point or double with ==.
// most floating point results are too sensitive to order of operations.
// this worked(sizes up to about 4600) it was quick, in general this is not safe
int i, j ;
for (i=0;i<n;i++) {
for (j=0;j<m;j++) {
double diff = fabs(a[i][j]-aa[i][j]);
if (diff > 1e-6) printf("i %d j %d diff %lf\n", i, j, diff) ;
}
}
printf("check OK\n") ;
}
/**/
// use thsi main instead of the one above to measure impact of unalinged matrix
// this version expects intel compiler
/*
int main(int argc, char *argv[])
{
double *a, *b, *c, *aa ;
unsigned int n ;
unsigned i, j, k, iInner, jInner, kInner, blockSize ;
struct timespec ts1, ts2, ts3, ts4, ts5, ts6, ts7 ;
printf("hello code beginning\n") ;
n = MATSIZE ; // default settings
blockSize = BLOCKSIZE ;
if (argc != 3) {printf("input matrix size and blocksize\n") ; exit(0);}
n = atoi(argv[1]) ;
blockSize = atoi(argv[2]) ;
printf("matrix size %d blocksize %d\n", n,blockSize) ;
if (n%blockSize) {printf("for this simple example matrix size must be a multiple of the block size.\n Please re-start \n") ; exit(0); }
// allocate matrices
a = (double *)_mm_malloc((n+blockSize)*(n+blockSize)* sizeof(double),64) ;
b = (double *)_mm_malloc((n+blockSize)*(n+blockSize)* sizeof(double),64) ;
c = (double *)_mm_malloc((n+blockSize)*(n+blockSize)* sizeof(double),64) ;
aa = (double *)_mm_malloc((n+blockSize)*(n+blockSize)* sizeof(double),64) ;
if (aa == NULL)
{
printf("insufficient memory \n") ;
exit(0) ;
}
// fill matrices
aa += 3 ; //offset aa. aa is intentionally allocated larger than required.
setmat(n, n, a) ;
setmat(n, n, aa) ;
srand(4.16) ; // set random seed (change to go off time stamp to make it better
fillmat(n,n,b) ;
fillmat(n,n,c) ;
current_utc_time(&ts1) ;
// multiply matrices
ablockmm (n, n, a, b, c, blockSize) ;
current_utc_time(&ts2) ;
ablockmm (n, n, aa, b, c, blockSize) ;
current_utc_time(&ts3) ;
printf("matrix multplies complete \n") ; fflush(stdout) ;
checkmatmult(n,n,a,aa) ;
{
double t1, t2, t3, tmp ;
t1 = ts2.tv_sec-ts1.tv_sec;
tmp = ts2.tv_nsec-ts1.tv_nsec;
tmp /= 1.0e+09 ;
t1 += tmp ;
printf("aligned block time %lf\n",t1) ;
t2 = ts3.tv_sec-ts2.tv_sec;
tmp = ts3.tv_nsec-ts2.tv_nsec;
tmp /= 1.0e+09 ;
t2 += tmp ;
printf("unaligned block time %lf\n",t2) ;
}
}
*/