-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
239 lines (199 loc) · 6.44 KB
/
matrix.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
typedef struct {
int depth;
int rows;
int cols;
int length;
float *data;
} Matrix;
void allocate_matrix_zeros(Matrix *m, int depth, int rows, int cols) {
m->depth = depth;
m->rows = rows;
m->cols = cols;
m->length = depth * rows * cols;
m->data = (float *)calloc(depth * rows * cols, sizeof(float));
if (m->data == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1); // Or handle the error appropriately
}
}
void allocate_matrix_random(Matrix *m, int depth, int rows, int cols) {
m->depth = depth;
m->rows = rows;
m->cols = cols;
m->length = depth * rows * cols;
m->data = (float *)calloc(depth * rows * cols, sizeof(float));
if (m->data == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1); // Or handle the error appropriately
}
srand(time(NULL));
for (int i = 0; i < depth * rows * cols; i++) {
m->data[i] = (float)rand() / RAND_MAX;
}
}
void allocate_matrix_consecutive(Matrix *m, int depth, int rows, int cols) {
m->depth = depth;
m->rows = rows;
m->cols = cols;
m->length = depth * rows * cols;
m->data = (float *)calloc(depth * rows * cols, sizeof(float));
if (m->data == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1); // Or handle the error appropriately
}
for (int i = 0; i < m->length; i++) {
m->data[i] = i;
}
}
void free_matrix(Matrix *m) {
free(m->data);
}
int strided_index(Matrix *m, int d, int r, int c) {
return d * m->rows * m->cols + r * m->cols + c;
}
float get(Matrix *m, int d, int r, int c) {
return m->data[strided_index(m, d, r, c)];
}
void set(Matrix *m, int d, int r, int c, float value) {
m->data[strided_index(m, d, r, c)] = value;
}
// avoiding get and set due to significant function overhead
void transpose(Matrix *m, Matrix *dst) {
for (int d = 0; d < m->depth; d++) {
for (int r = 0; r < m->rows; r++) {
for (int c = 0; c < m->cols; c++) {
dst->data[d * dst->rows * dst->cols + r * dst->cols + c] = m->data[d * m->rows * m->cols + r * m->cols + c];
}
}
}
}
void transpose_inplace(Matrix *m) {
for (int d = 0; d < m->depth; d++) {
for (int r = 0; r < m->rows; r++) {
for (int c = 0; c < r; c++) {
float temp = m->data[d * m->rows * m->cols + r * m->cols + c];
m->data[d * m->rows * m->cols + r * m->cols + c] = m->data[d * m->rows * m->cols + c * m->cols + r];
m->data[d * m->rows * m->cols + c * m->cols + r] = temp;
}
}
}
}
void print_matrix(Matrix *m) {
for (int d = 0; d < m->depth; d++) {
for (int r = 0; r < m->rows; r++) {
for (int c = 0; c < m->cols; c++) {
printf("%f ", get(m, d, r, c));
}
printf("\n");
}
printf("\n");
}
}
void matmul(Matrix *a, Matrix *b, Matrix *res) {
for (int d = 0; d < a->depth; d++) {
for (int r = 0; r < a->rows; r++) {
for (int c = 0; c < b->cols; c++) {
float temp = 0.0f;
for (int i = 0; i < a->cols; i++) {
temp += get(a, d, r, i) * get(b, d, i, c);
}
// printf("temp: %f\n", temp);
set(res, d, r, c, temp);
}
}
}
}
void matmul_transpose(Matrix *a, Matrix *b, Matrix *res) {
transpose_inplace(b);
for (int d = 0; d < a->depth; d++) {
for (int r = 0; r < a->rows; r++) {
for (int c = 0; c < b->cols; c++) {
float temp = 0.0f;
for (int i = 0; i < a->cols; i++) {
temp += a->data[d * a->rows * a->cols + r * a->cols + i] * b->data[d * b->rows * b->cols + c * b->cols + i];
}
res -> data[d * res->rows * res->cols + r * res->cols + c] = temp;
}
}
}
}
void zero_matrix(Matrix *m) {
for (int i = 0; i < m->length; i++) {
m->data[i] = 0.0f;
}
}
// setting the tille size to 32, as Ryzen 3600 could hold ~52x52, but we need a power of 2
// and the closest power of 2 is 32
void matmul_transpose_tiled(Matrix *a, Matrix *b, Matrix *res, int tile_size) {
transpose_inplace(b);
// zero_matrix(res);
if (tile_size > a->rows || tile_size > b->cols) {
tile_size = a->rows;
}
for (int d = 0; d < a->depth; d++) {
for (int r = 0; r < a->rows; r += tile_size) {
for (int c = 0; c < b->cols; c += tile_size) {
for (int rr = r; rr < r + tile_size; rr++) {
for (int cc = c; cc < c + tile_size; cc++) {
float sum = 0.0f;
for (int ii = 0; ii < tile_size; ii++) {
sum += a->data[d * a->rows * a->cols + rr * a->cols + ii] *
b->data[d * b->rows * b->cols + cc * b->cols + ii];
}
res->data[d * res->rows * res->cols + rr * res->cols + cc] += sum;
}
}
}
}
}
}
int main() {
Matrix m;
allocate_matrix_random(&m, 1, 2, 2);
print_matrix(&m);
set(&m, 0, 0, 0, 1.0);
print_matrix(&m);
Matrix n;
Matrix res;
allocate_matrix_consecutive(&n, 1, 2, 2);
allocate_matrix_zeros(&res, 1, 2, 2);
matmul(&n, &n, &res);
print_matrix(&n);
print_matrix(&res);
Matrix o;
Matrix res2;
allocate_matrix_consecutive(&o, 1, 2, 1);
allocate_matrix_zeros(&res2, 1, 2, 3);
matmul(&n, &o, &res2);
print_matrix(&n);
print_matrix(&o);
print_matrix(&res2);
Matrix p;
Matrix res3;
allocate_matrix_consecutive(&p, 1, 1, 2);
allocate_matrix_zeros(&res3, 1, 3, 3);
matmul(&p, &o, &res3);
print_matrix(&p);
print_matrix(&o);
print_matrix(&res3);
printf("Im testing:\n");
Matrix a, b;
allocate_matrix_consecutive(&a, 1, 2, 2);
allocate_matrix_consecutive(&b, 1, 2, 2);
matmul_transpose_tiled(&a, &b, &res, 32);
print_matrix(&a);
print_matrix(&b);
print_matrix(&res);
free_matrix(&a);
free_matrix(&b);
free_matrix(&res3);
free_matrix(&p);
free_matrix(&m);
free_matrix(&n);
free_matrix(&o);
free_matrix(&res);
free_matrix(&res2);
}