-
Notifications
You must be signed in to change notification settings - Fork 5
/
2mm.cc
263 lines (224 loc) · 3.99 KB
/
2mm.cc
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
#define NI 16
#define NJ 18
#define NK 24
//#define NK 22
#define NL 24
void buffer_func1_D(
int D_input[NI][NL],
int D_mid[NI][NL]
)
{
#pragma HLS inline off
int i,j,k;
for (i = 0; i < NI; i++)
for (j = 0; j < NL; j++)
{
#pragma HLS pipeline
D_mid[i][j] = D_input[i][j];
}
}
void buffer_func1_C(
int C_input[NJ][NL],
int C_mid[NJ][NL]
)
{
#pragma HLS inline off
int i,j,k;
for (j = 0; j < NL; j++)
{
for (k = 0; k < NJ; ++k)
{
#pragma HLS unroll factor=8
C_mid[k][j] = C_input[k][j];
}
}
}
void func1_execute(
int tmp_mid_execute[NI][NJ],
int A[NI][NK],
int B[NK][NJ]
)
{
#pragma HLS inline off
int i,j,k;
for (i = 0; i < NI; i++)
for (j = 0; j < NJ; j++)
{
tmp_mid_execute[i][j] = 0;
for (k = 0; k < NK; ++k)
{
#pragma HLS unroll factor=2
#pragma HLS pipeline II=4
tmp_mid_execute[i][j] += 123 * A[i][k] * B[k][j];
}
}
}
void func1(
int A[NI][NK],
int B[NK][NJ],
int C[NJ][NL],
int D[NI][NL],
int tmp[NI][NJ],
int C_mid[NJ][NL],
int D_mid[NI][NL]
)
{
#pragma HLS inline off
int i,j,k;
buffer_func1_D(
D,
D_mid
);
buffer_func1_C(
C,
C_mid
);
func1_execute(
tmp,
A,
B
);
}
void func2(
int C[NJ][NL],
int D[NI][NL],
int tmp[NI][NJ],
int D_output[NI][NL]
)
{
#pragma HLS inline off
int i,j,k;
for (i = 0; i < NI; i++)
for (j = 0; j < NL; j++)
{
int sum = D[i][j] * 321;
for (k = 0; k < NJ; ++k)
{
#pragma HLS unroll factor=8
sum+= tmp[i][k] * C[k][j];
}
D_output[i][j] = sum;
}
}
void kernel_2mm(
int A[NI][NK],
int B[NK][NJ],
int C[NJ][NL],
int D[NI][NL],
int D_output[NI][NL])
{
#pragma HLS dataflow
int D_mid[NI][NL];
int C_mid[NJ][NL];
int tmp_mid[NI][NJ];
#pragma HLS array_partition variable=tmp_mid dim=2 factor=8 cyclic
#pragma HLS array_partition variable=C_mid dim=1 factor=8 cyclic
func1(
A,
B,
C,
D,
tmp_mid,
C_mid,
D_mid
);
func2(
C_mid,
D_mid,
tmp_mid,
D_output
);
}
void readData(
int A_AXI[NI][NK],
int B_AXI[NK][NJ],
int C_AXI[NJ][NL],
int D_input_AXI[NI][NL],
int A[NI][NK],
int B[NK][NJ],
int C[NJ][NL],
int D[NI][NL]
)
{
int i,j,k;
for (i = 0; i < NI; i++)
{
for (k = 0; k < NK; ++k)
{
#pragma HLS pipeline
A[i][k] = A_AXI[i][k];
}
}
for (k = 0; k < NK; ++k)
for (j = 0; j < NJ; j++)
{
#pragma HLS pipeline
B[k][j] = B_AXI[k][j];
}
for (j = 0; j < NJ; j++)
for (k = 0; k < NL; ++k)
{
#pragma HLS pipeline
C[j][k] = C_AXI[j][k];
}
for (i = 0; i < NI; i++)
for (k = 0; k < NL; ++k)
{
#pragma HLS pipeline
D[i][k] = D_input_AXI[i][k];
}
}
void writeData(
int D_output_AXI[NI][NL],
int D_output[NI][NL])
{
int i,j,k;
for (i = 0; i < NI; i++)
for (j = 0; j < NL; j++)
{
#pragma HLS pipeline
D_output_AXI[i][j] = D_output[i][j];
}
}
void kernel_2mm_wrapper(
int A_AXI[NI][NK],
int B_AXI[NK][NJ],
int C_AXI[NJ][NL],
int D_input_AXI[NI][NL],
int D_output_AXI[NI][NL])
{
#pragma HLS INTERFACE s_axilite port=return bundle=for_control
#pragma HLS INTERFACE m_axi port=A_AXI offset=slave bundle=gmem0
#pragma HLS INTERFACE m_axi port=B_AXI offset=slave bundle=gmem1
#pragma HLS INTERFACE m_axi port=C_AXI offset=slave bundle=gmem2
#pragma HLS INTERFACE m_axi port=D_input_AXI offset=slave bundle=gmem3
#pragma HLS INTERFACE m_axi port=D_output_AXI offset=slave bundle=gmem4
int A[NI][NK];
int B[NK][NJ];
int C[NJ][NL];
int D_input[NI][NL];
int D[NI][NL];
int D_output[NI][NL];
#pragma HLS array_partition variable=A dim=2 factor=2 cyclic
#pragma HLS array_partition variable=B dim=1 factor=2 cyclic
#pragma HLS array_partition variable=C dim=1 factor=8 cyclic
#pragma HLS dataflow
readData(A_AXI,
B_AXI,
C_AXI,
D_input_AXI,
A,
B,
C,
D
);
kernel_2mm(
A,
B,
C,
D,
D_output);
writeData(
D_output_AXI,
D_output);
}