forked from funny1dog/cs442-542-f22_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmat_unroll_options.cpp
executable file
·421 lines (371 loc) · 11.8 KB
/
matmat_unroll_options.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
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
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
#include "timer.h"
// To compile with and without vectorization (in gcc):
// gcc -o <executable_name> <file_name> -O1 <--- no vectorization
// Flag to vectorize : -ftree-vectorize
// Flag needed for vectorization of X86 processors : -msse -msse2
// Flag needed for vectorization of PowerPC platforms : -maltivec
// Other optional flags (floating point reductions) : -ffast-math -fassociative-math
//
// To see what the compiler vectorizes : -fopt-info-vec (or -fopt-info-vec-optimized)
// To see what the compiler is not able to vectorize : -fopt-info-vec-missed
// Matrix-Matrix Multiplication of Doubles (Double Pointer)
// Test without the restrict variables
void matmat(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
{
for (int k = 0; k < n; k++)
C[i*n+k] = 0;
for (int j = 0; j < n; j++)
{
val = A[i*n+j];
for (int k = 0; k < n; k++)
{
C[i*n+k] += val * B[j*n+k];
}
}
}
}
}
void matmat_unrolledi_ik(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4, bval;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i += 4)
{
// Initialize entries of C rows [i:i+4) to 0
for (int k = 0; k < n; k++)
{
C[i*n+k] = 0;
C[(i+1)*n+k] = 0;
C[(i+2)*n+k] = 0;
C[(i+3)*n+k] = 0;
}
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = A[i*n+j];
val2 = A[(i+1)*n+j];
val3 = A[(i+2)*n+j];
val4 = A[(i+3)*n+j];
for (int k = 0; k < n; k++)
{
bval = B[j*n+k];
C[i*n+k] += val * bval;
C[(i+1)*n+k] += val2 * bval;
C[(i+2)*n+k] += val3 * bval;
C[(i+3)*n+k] += val4 * bval;
}
}
}
}
}
void matmat_unrolledi_ij(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4, bval;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i += 4)
{
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = 0;
val2 = 0;
val3 = 0;
val4 = 0;
for (int k = 0; k < n; k++)
{
bval = B[k*n+j];
val += A[i*n+k] * bval;
val2 += A[(i+1)*n+k] * bval;
val3 += A[(i+2)*n+k] * bval;
val4 += A[(i+3)*n+k] * bval;
}
C[i*n+j] = val;
C[(i+1)*n+j] = val2;
C[(i+2)*n+j] = val3;
C[(i+3)*n+j] = val4;
}
}
}
}
void matmat_unrolledi_ki(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4, aval;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i += 4)
{
// Initialize entries of C rows [i:i+4) to 0
for (int k = 0; k < n; k++)
{
C[k*n+i] = 0;
C[k*n+i+1] = 0;
C[k*n+i+2] = 0;
C[k*n+i+3] = 0;
}
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = B[j*n+i];
val2 = B[j*n+i+1];
val3 = B[j*n+i+2];
val4 = B[j*n+i+3];
for (int k = 0; k < n; k++)
{
aval = A[k*n+j];
C[k*n+i] += aval * val;
C[k*n+i+1] += aval * val2;
C[k*n+i+2] += aval * val3;
C[k*n+i+3] += aval * val4;
}
}
}
}
}
void matmat_unrolledi_kj(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n*n; i++)
C[i] = 0;
for (int i = 0; i < n; i += 4)
{
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = B[i*n+i];
val2 = B[(i+1)*n+j];
val3 = B[(i+2)*n+j];
val4 = B[(i+3)*n+j];
for (int k = 0; k < n; k++)
{
C[k*n+j] += A[k*n+i] * val;
C[k*n+j] += A[k*n+i+1] * val2;
C[k*n+j] += A[k*n+i+2] * val3;
C[k*n+j] += A[k*n+i+3] * val4;
}
}
}
}
}
void matmat_unrolledi_ji(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4, aval;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i += 4)
{
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = 0;
val2 = 0;
val3 = 0;
val4 = 0;
for (int k = 0; k < n; k++)
{
aval = A[j*n+k];
val += aval*B[k*n+i];
val2 += aval*B[k*n+i+1];
val3 += aval*B[k*n+i+2];
val4 += aval*B[k*n+i+3];
}
C[j*n+i] = val;
C[j*n+i+1] = val2;
C[j*n+i+2] = val3;
C[j*n+i+3] = val4;
}
}
}
}
void matmat_unrolledi_jk(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val2, val3, val4, aval;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n*n; i++)
C[i] = 0;
for (int i = 0; i < n; i += 4)
{
// Multiple four rows [i:i+4) of A by columns of B
for (int j = 0; j < n; j++)
{
val = A[j*n+i];
val2 = A[j*n+i+1];
val3 = A[j*n+i+2];
val4 = A[j*n+i+3];
for (int k = 0; k < n; k++)
{
C[j*n+k] += val*B[i*n+k];
C[j*n+k] += val2*B[(i+1)*n+k];
C[j*n+k] += val3*B[(i+2)*n+k];
C[j*n+k] += val4*B[(i+3)*n+k];
}
}
}
}
}
void matmat_unrolledj_ik(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val1, val2, val3;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
{
for (int k = 0; k < n; k++)
C[i*n+k] = 0;
for (int j = 0; j < n; j += 4)
{
val = A[i*n+j];
val1 = A[i*n+j+1];
val2 = A[i*n+j+2];
val3 = A[i*n+j+3];
for (int k = 0; k < n; k++)
{
C[i*n+k] += val * B[j*n+k];
C[i*n+k] += val1 * B[(j+1)*n+k];
C[i*n+k] += val2 * B[(j+2)*n+k];
C[i*n+k] += val3 * B[(j+3)*n+k];
}
}
}
}
}
void matmat_unrolledj_ij(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val1, val2, val3;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j += 4)
{
C[i*n+j] = 0;
C[i*n+j+1] = 0;
C[i*n+j+2] = 0;
C[i*n+j+3] = 0;
for (int k = 0; k < n; k++)
{
val = A[i*n+k];
C[i*n+j] += val * B[k*n+j];
C[i*n+j+1] += val * B[k*n+j+1];
C[i*n+j+2] += val * B[k*n+j+2];
C[i*n+j+3] += val * B[k*n+j+3];
}
}
}
}
}
void matmat_unrolledj_ji(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val1, val2, val3;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j += 4)
{
C[j*n+i] = 0;
C[(j+1)*n+i] = 0;
C[(j+2)*n+i] = 0;
C[(j+3)*n+i] = 0;
for (int k = 0; k < n; k++)
{
val = B[k*n+i];
C[j*n+i] += A[j*n+k] * val;
C[(j+1)*n+i] += A[(j+1)*n+k] * val;
C[(j+2)*n+i] += A[(j+2)*n+k] * val;
C[(j+3)*n+i] += A[(j+3)*n+k] * val;
}
}
}
}
}
void matmat_unrolled(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
double val, val1, val2, val3;
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j += 4)
{
C[j*n+i] = 0;
C[(j+1)*n+i] = 0;
C[(j+2)*n+i] = 0;
C[(j+3)*n+i] = 0;
for (int k = 0; k < n; k++)
{
val = B[k*n+i];
C[j*n+i] += A[j*n+k] * val;
C[(j+1)*n+i] += A[(j+1)*n+k] * val;
C[(j+2)*n+i] += A[(j+2)*n+k] * val;
C[(j+3)*n+i] += A[(j+3)*n+k] * val;
}
}
}
}
}
// This program runs matrix matrix multiplication with double pointers
// Test vectorization improvements for both doubles and floats
// Try with and without the restrict variables
int main(int argc, char* argv[])
{
double start, end;
int n_access = 1000000000;
if (argc < 2)
{
printf("Need Matrix Dimemsion n and step size k passed as Command Line Arguments (e.g. ./matmat 8 2)\n");
return 0;
}
int n = atoi(argv[1]);
int step = atoi(argv[2]);
int n_iter = (n_access / (n*n*n)) + 1;
double* A = (double*)malloc(n*n*sizeof(double));
double* B = (double*)malloc(n*n*sizeof(double));
double* C = (double*)malloc(n*n*sizeof(double));
double* C_new = (double*)malloc(n*n*sizeof(double));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
A[i*n+j] = 1.0/(i+1);
B[i*n+j] = 1.0;
}
}
// Comparisons
matmat(n, A, B, C, 2);
matmat_unrolled(n, A, B, C_new, 2);
for (int i = 0; i < n*n; i++)
if (fabs(C[i] - C_new[i]) > 1e-10)
{
printf("Different Answers (Unrolled)! idx %d, %e vs %e\n", i, C[i], C_new[i]);
return 0;
}
// Warm-Up
matmat(n, A, B, C, n_iter);
start = get_time();
matmat(n, A, B, C, n_iter);
end = get_time();
printf("N %d, Time Per MatMat %e\n", n, (end - start)/n_iter);
// Warm-Up
matmat_unrolled(n, A, B, C_new, 2);
start = get_time();
matmat_unrolled(n, A, B, C_new, 2);
end = get_time();
printf("N %d, Time Per MatMat Unrolled %e\n", n, (end - start) / n_iter);
free(A);
free(B);
free(C);
return 0;
}