forked from funny1dog/cs442-542-f22_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmat.cpp
executable file
·218 lines (178 loc) · 5.72 KB
/
matmat.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
#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_unrolled(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 += 4)
{
C[i*n+k] += val * B[j*n+k];
C[i*n+k+1] += val * B[j*n+k+1];
C[i*n+k+2] += val * B[j*n+k+2];
C[i*n+k+3] += val * B[j*n+k+3];
}
}
}
}
}
void inner_matmat(int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C,
int start_i, int start_j,int start_k, int n_orig)
{
double val;
int end_i = start_i + n;
int end_j = start_j + n;
int end_k = start_k + n;
for (int i = start_i; i < end_i; i++)
{
for (int j = start_j; j < end_j; j++)
{
// This is the correct way to stripe matrix multiplication
val = A[i*n_orig+j];
for (int k = start_k; k < end_k; k++)
{
C[i*n_orig+k] += val * B[j*n_orig+k];
}
// Don't Do This! SLOW : Cannot vectorize
//val = 0;
//for (int k = start_k; k < end_k; k++)
//{
//val += A[i*n_orig+k] * B[k*n_orig+j];
//}
//C[i*n_orig+j] += val;
// Don't Do This! SLOW : Out of order accesses!
/*val = B[j*n_orig+i];
for (int k = start_k; k < end_k; k++)
{
C[k*n_orig+i] += A[k*n_orig+j] * val;
}*/
}
}
}
void matmat_split(int step, int n, double* __restrict__ A, double* __restrict__ B, double* __restrict__ C, int n_iter)
{
for (int iter = 0; iter < n_iter; iter++)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i*n+j] = 0;
for (int i = 0; i < n; i += step)
{
for (int j = 0; j < n; j+= step)
{
for (int k = 0; k < n; k += step)
{
inner_matmat(step, A, B, C, i, j, k, n);
}
}
}
}
}
// 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;
}
matmat_split(step, n, A, B, C_new, 1);
for (int i = 0; i < n*n; i++)
if (fabs(C[i] - C_new[i]) > 1e-10)
{
printf("Different Answers (Split)! 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);
// Warm-Up
matmat_split(step, n, A, B, C, n_iter);
start = get_time();
matmat_split(step, n, A, B, C, n_iter);
end = get_time();
printf("N %d, Time Per MatMat Split %e\n", n, (end - start) / n_iter);
free(A);
free(B);
free(C);
return 0;
}