-
Notifications
You must be signed in to change notification settings - Fork 13
/
matrix.c
68 lines (51 loc) · 1.58 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
#include "matrix.h"
#include <stdlib.h>
matrix* new_matrix(size_t rows, size_t cols) {
matrix *mat = malloc(sizeof(matrix));
if (!mat)
return NULL;
mat->rows = rows;
mat->cols = cols;
size_t size = rows * cols * sizeof(((matrix*)NULL)->p_data[0]);
mat->size = size;
mat->p_data = aligned_alloc(1024, size);
if (!mat->p_data) {
free(mat);
return NULL;
}
return mat;
}
void free_matrix(matrix *mat) {
if (mat)
free(mat->p_data);
free(mat);
}
void randomize_matrix(matrix *mat) {
if (!mat || !mat->p_data)
return;
size_t elements = mat->rows * mat->cols;
for (size_t i = 0; i < elements; i++)
mat->p_data[i] = (float)((double)rand() / RAND_MAX * 10.0);
}
matrix* matmul_plain(const matrix *lhs, const matrix *rhs) {
if (lhs == NULL || lhs->p_data == NULL || rhs == NULL || rhs->p_data == NULL)
return NULL;
if (lhs->cols != rhs->rows)
return NULL;
matrix *dst = new_matrix(lhs->rows, rhs->cols);
if (dst == NULL)
return NULL;
plain_gemm(dst->rows, dst->cols, lhs->cols, lhs->p_data, rhs->p_data, dst->p_data);
return dst;
}
matrix* matmul_improved(const matrix *lhs, const matrix *rhs) {
if (lhs == NULL || lhs->p_data == NULL || rhs == NULL || rhs->p_data == NULL)
return NULL;
if (lhs->cols != rhs->rows)
return NULL;
matrix *dst = new_matrix(lhs->rows, rhs->cols);
if (dst == NULL)
return NULL;
gepb_aligned_gemm(dst->rows, dst->cols, lhs->cols, lhs->p_data, rhs->p_data, dst->p_data);
return dst;
}