-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatmul.cpp
111 lines (71 loc) · 1.54 KB
/
matmul.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
#include "matmul.hpp"
void axis2Mat(axis_t *src, T A[N]) {
union {
float ival;
T oval;
} converter;
for (int i = 0; i < N; i++) {
converter.ival = src[i].data;
A[i] = converter.oval;
}
}
void Mat2axis(T buff[out_len], axis_t *dst) {
union {
float oval;
T ival;
} converter;
int tmp = 0;
for (int i = 0; i < out_len; i++) {
if (i == out_len-1) {
tmp = 1;
}
dst[i].last = tmp;
converter.ival = buff[i];
dst[i].data = converter.oval;
}
}
void matmul(T a[n][n], T b[n][n], T out[n][n]) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
T sum = 0;
for (int k = 0; k < n; ++k)
sum += a[i][k] * b[k][j];
out[i][j] = sum;
}
return;
}
void smul(axis_t *INPUT, axis_t *OUTPUT,int length){
#pragma HLS INTERFACE s_axilite port=length bundle=CTRL
#pragma HLS INTERFACE axis depth=50 port=OUTPUT
#pragma HLS INTERFACE axis depth=50 port=INPUT
// T A[N];
T buff[out_len];
T AB[N];
T mat_A[n][n];
T mat_B[n][n];
T C[n][n];
axis2Mat(INPUT, AB);
int sA = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
mat_A[i][j]=AB[sA];
sA+=1;
}
}
int sB = out_len;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
mat_B[i][j]=AB[sB];
sB+=1;
}
}
matmul(mat_A,mat_B,C);
int sout = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
buff[sout]=C[i][j];
sout+=1;
}
}
Mat2axis(buff, OUTPUT);
}