-
Notifications
You must be signed in to change notification settings - Fork 3
/
kernel_matmul.cpp
73 lines (62 loc) · 1.97 KB
/
kernel_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
#ifndef USE_CPU_ONLY
#include <hls_stream.h>
#include <stdint.h>
#define MAX_DATA_SIZE 1024
static void load_vec(float* i_vec, hls::stream<float>& inStream, int vec_size) {
mem_rd:
for (int i = 0; i < vec_size; i++) {
inStream << i_vec[i];
}
}
static void load_mat(float* i_mat, hls::stream<float>& inStream, int vec_size,
int col_size) {
mem_rd:
for (int i = 0; i < col_size; i++) {
for (int j = 0; j < vec_size; j++) {
inStream << i_mat[vec_size * i + j];
}
}
}
static void compute_matmul(hls::stream<float>& in1_stream,
hls::stream<float>& in2_stream,
hls::stream<float>& out_stream, int vec_size,
int col_size) {
float vec_local[MAX_DATA_SIZE];
float sum_local = 0;
for (int i = 0; i < vec_size; i++) {
vec_local[i] = in1_stream.read();
}
execute:
for (int i = 0; i < col_size; i++) {
for (int j = 0; j < vec_size; j++) {
#pragma HLS UNROLL
sum_local += vec_local[j] * in2_stream.read();
}
out_stream << sum_local;
sum_local = 0;
}
}
static void store_result(float* out, hls::stream<float>& out_stream,
int col_size) {
mem_wr:
for (int i = 0; i < col_size; i++) {
out[i] = out_stream.read();
}
}
extern "C" {
void kernel_matmul(float* i_vec, float* i_mat, float* o_vec, int vec_size,
int col_size) {
#pragma HLS INTERFACE m_axi port = i_vec bundle = gmem0
#pragma HLS INTERFACE m_axi port = i_mat bundle = gmem1
#pragma HLS INTERFACE m_axi port = o_vec bundle = gmem0
static hls::stream<float> vec_stream("vec_stream");
static hls::stream<float> mat_stream("mat_stream");
static hls::stream<float> out_stream("out_stream");
#pragma HLS dataflow
load_vec(i_vec, vec_stream, vec_size);
load_mat(i_mat, mat_stream, vec_size, col_size);
compute_matmul(vec_stream, mat_stream, out_stream, vec_size, col_size);
store_result(o_vec, out_stream, col_size);
}
}
#endif // USE_CPU_ONLY