Repository navigation
Expand file tree
/
Copy pathkrnl_controller_normal_layout.cpp
More file actions
129 lines (110 loc) · 3.98 KB
/
Copy pathkrnl_controller_normal_layout.cpp
File metadata and controls
129 lines (110 loc) · 3.98 KB
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
#include <stdint.h>
#include "constants.h"
#include "ap_axi_sdata.h"
#include "ap_int.h"
#include "hls_stream.h"
#define NUM_BLOCKS (1 << LOGBLOCKS)
#define MEMORY_DWIDTH 512
#define LOGCORES 3
#define NUM_ROWS (1 << LOGROWS)
#define NUM_CORES (1 << LOGCORES)
typedef ap_axiu<MEMORY_DWIDTH, 1, 0, 0> chunk_t;
/* Suppressing loop flattening below prevents DSP usages. We do pay a constant 1-2 cycles exiting and
* entering the loop, but row_size is 2^12, so this is negligible.
*/
void phase1(
hls::stream<chunk_t>& compute_to_controller,
hls::stream<chunk_t>& controller_to_compute,
ap_uint<MEMORY_DWIDTH>* gmem_in,
ap_uint<MEMORY_DWIDTH>* gmem_out) {
#pragma HLS INLINE off
#pragma HLS dataflow
// dataflow optimization should ensure that phase1_load and phase1_store
// runs in parallel. But this unfortunately means csim will not work.. which
// we don't care about anyway.
//
// flattening is important to ensure that it is sufficiently pipelined.
// TODO(fyquah): Do manual loop flattening to remove DSP usage
// Phase 1: read transposed, writeback transposed.
phase1_load:
for (ap_uint<32> i = 0; i < NUM_ROWS / 8; i += NUM_BLOCKS) {
for (ap_uint<32> j = 0; j < NUM_ROWS; j++) {
for (ap_uint<32> k = 0; k < NUM_BLOCKS; k++) {
#pragma HLS pipeline II=1
chunk_t v;
v.data = gmem_in[i + k + (j * (NUM_ROWS / 8))];
v.user = 0;
controller_to_compute.write(v);
}
}
}
phase1_store:
for (ap_uint<32> i = 0; i < (NUM_ROWS * NUM_ROWS) >> LOGCORES; i++) {
#pragma HLS pipeline II=1
chunk_t v = compute_to_controller.read();
gmem_out[i] = v.data;
}
}
void phase2(
hls::stream<chunk_t>& compute_to_controller,
hls::stream<chunk_t>& controller_to_compute,
ap_uint<MEMORY_DWIDTH>* gmem_in,
ap_uint<MEMORY_DWIDTH>* gmem_out)
{
#pragma HLS INLINE off
#pragma HLS dataflow
phase2_load:
// Phase 2: read linear, writeback transposed
for (ap_uint<32> block_col = 0; block_col < (NUM_ROWS >> LOGCORES); block_col++) {
for (ap_uint<32> block_row = 0; block_row < (NUM_ROWS >> (LOGCORES + LOGBLOCKS)); block_row++) {
for (ap_uint<32> word = 0; word < (NUM_CORES * NUM_BLOCKS); word++) {
#pragma HLS pipeline II=1
chunk_t v;
v.data = gmem_in[
(block_col << (LOGCORES + LOGBLOCKS))
+ (block_row << (LOGROWS + LOGBLOCKS))
+ word
];
v.user = 1;
controller_to_compute.write(v);
}
}
}
phase2_store:
for (ap_uint<32> i = 0; i < NUM_ROWS / 8; i += NUM_BLOCKS) {
for (ap_uint<32> j = 0; j < NUM_ROWS; j++) {
for (ap_uint<32> k = 0; k < NUM_BLOCKS; k++) {
#pragma HLS pipeline II=1
chunk_t v = compute_to_controller.read();
gmem_out[i + k + (j * (NUM_ROWS / 8))] = v.data;
}
}
}
}
void krnl_controller(
hls::stream<chunk_t>& compute_to_controller,
hls::stream<chunk_t>& controller_to_compute_phase_1,
hls::stream<chunk_t>& controller_to_compute_phase_2,
ap_uint<MEMORY_DWIDTH>* gmem_a,
ap_uint<MEMORY_DWIDTH>* gmem_b,
ap_uint<MEMORY_DWIDTH>* gmem_c,
ap_uint<16> row_size,
ap_uint<2> phase) {
#pragma HLS INTERFACE m_axi port = gmem_a offset = slave bundle = gmem_a
#pragma HLS INTERFACE m_axi port = gmem_b offset = slave bundle = gmem_b
#pragma HLS INTERFACE m_axi port = gmem_c offset = slave bundle = gmem_c
#pragma HLS INTERFACE axis port = compute_to_controller
#pragma HLS INTERFACE axis port = controller_to_compute_phase_1
#pragma HLS INTERFACE axis port = controller_to_compute_phase_2
#pragma HLS INTERFACE s_axilite port = gmem_a
#pragma HLS INTERFACE s_axilite port = gmem_b
#pragma HLS INTERFACE s_axilite port = gmem_c
#pragma HLS INTERFACE s_axilite port = row_size
#pragma HLS INTERFACE s_axilite port = return
if (phase(0, 0)) {
phase1(compute_to_controller, controller_to_compute_phase_1, gmem_a, gmem_b);
}
if (phase(1, 1)) {
phase2(compute_to_controller, controller_to_compute_phase_2, gmem_b, gmem_c);
}
}