-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamutils.hpp
More file actions
266 lines (249 loc) · 8.02 KB
/
streamutils.hpp
File metadata and controls
266 lines (249 loc) · 8.02 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
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
264
265
266
#ifndef __STANN_HLS_STREAM_HPP__
#define __STANN_HLS_STREAM_HPP__
#include <stdio.h>
#include "stann.hpp"
#include "utils.hpp"
/**
* This namespace contains some useful utility functions to handle streams.
*/
namespace StreamUtil {
template<int DIM, typename T = DEFAULT_DATATYPE>
void sink(hls::stream<T> &input) {
for (int i = 0; i < DIM; i++) {
input.read();
}
}
template<int INPUT_DIM>
void print_stream_float(hls::stream<float> &input, hls::stream<float> &output, int reps) {
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
float tmp = input.read();
printf("%f ", tmp);
output.write(tmp);
}
}
}
/**
* Takes an array and writes each element to a stream.
*
* @tparam INPUT_DIM dimension of input array
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param[in] input input array
* @param[out] output output stream
*/
template<int INPUT_DIM, typename T = DEFAULT_DATATYPE>
void tostream(T *input, hls::stream<T> &output) {
#pragma HLS inline
for (int i = 0; i < INPUT_DIM; i++) {
output.write(input[i]);
}
}
/**
* Takes an array and writes each element to a stream. Multiple repetitions.
*
* @tparam INPUT_DIM dimension of input array
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param[in] input input array
* @param[out] output output stream
* @param[in] reps number of repetitions
*/
template<int INPUT_DIM, typename T = DEFAULT_DATATYPE>
void tostream(T *input, hls::stream<T> &output, int reps) {
#pragma HLS inline
tostream_loop:
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
#pragma HLS pipeline II=20
//output.write(input[r * INPUT_DIM + i]);
output.write(input[i * reps + r]);
}
}
}
/**
* Takes an array and writes each element to a stream.
* Converts between two data types while doing this.
* Multiple repetitions.
*
* @tparam INPUT_DIM dimension of input array
* @tparam T_IN input data type
* @tparam T_OUT output data type
*
* @param[in] input input array
* @param[out] output output stream
* @param[in] reps number of repetitions
*/
template<int INPUT_DIM, typename T_IN, typename T_OUT >
void tostream_convert(T_IN *input, hls::stream<T_OUT> &output, int reps) {
#pragma HLS inline
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
T_OUT tmp = *reinterpret_cast<T_OUT*>(&input[i * reps + r]);
output.write(tmp);
}
}
}
/**
* Takes a stream, copies the content into an array, and also forwards the
* content into a next stream. Mostly useful for debugging.
*
* @tparam INPUT_DIM dimension of input array
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param[in] input input stream
* @param[out] copy copy array
* @param[out] output output stream
* @param[in] reps number of repetitions
*/
template<int INPUT_DIM, typename T>
void getcopy(hls::stream<T> &input, T *copy, hls::stream<T> &output, int reps) {
#pragma HLS inline
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
T tmp = input.read();
copy[r * INPUT_DIM + i] = tmp;
output.write(tmp);
}
}
}
/**
* Takes data from a stream and writes to an array.
*
* @tparam INPUT_DIM amount of data to read from the stream
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param input input stream
* @param output output array
*/
template<int INPUT_DIM, typename T = DEFAULT_DATATYPE>
void toarray(hls::stream<T> &input, T *output) {
#pragma HLS inline
for (int i = 0; i < INPUT_DIM; i++) {
T data = input.read();
output[i] = data;
}
}
/**
* Takes data from a stream and writes to an array. Multiple repetitions.
*
* @tparam INPUT_DIM amount of data to read from the stream
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param input input stream
* @param output output array
*/
template<int INPUT_DIM, typename T = DEFAULT_DATATYPE>
void toarray(hls::stream<T> &input, T *output, int reps) {
#pragma HLS inline
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
T data = input.read();
output[i * reps + r] = data;
//output[r * INPUT_DIM + i] = data;
}
}
}
/**
* Takes data from a stream and writes to an array. Multiple repetitions.
* Stores the data in transposed order.
*
* @tparam INPUT_DIM amount of data to read from the stream
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param input input stream
* @param output output array
*/
template<int INPUT_DIM, typename T = DEFAULT_DATATYPE>
void toarray_transposed(hls::stream<T> &input, T *output, int reps) {
#pragma HLS inline
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
T data = input.read();
output[r * INPUT_DIM + i] = data;
}
}
}
/**
* Takes data from a stream and writes to an array. Multiple repetitions.
* Converts between two data types while doing this.
*
* @tparam INPUT_DIM amount of data to read from the stream
* @tparam T_IN input data type
* @tparam T_OUT output data type
*
* @param input input stream
* @param output output array
*/
template<int INPUT_DIM, typename T_IN, typename T_OUT>
void toarray_convert(hls::stream<T_IN> &input, T_OUT *output, int reps) {
#pragma HLS inline
for (int r = 0; r < reps; r++) {
for (int i = 0; i < INPUT_DIM; i++) {
T_IN data = input.read();
output[i] = *reinterpret_cast<T_OUT*>(&data);
}
}
}
/**
* Takes a batch of data and converts it to a stream.
*
* @tparam INPUT_DIM input dimension
* @tparam BATCH_SIZE number of inputs
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param input input array
* @param output output stream
*/
template<int INPUT_DIM, int BATCH_SIZE, typename T = DEFAULT_DATATYPE>
void batchtostream(T *input, hls::stream<T> &output) {
#pragma HLS inline
for (int i = 0; i < INPUT_DIM; i++) {
for (int b = 0; b < BATCH_SIZE; b++) {
output.write(input[b * INPUT_DIM + i]);
}
}
}
/**
* Takes data from a source stream and writes it to two different streams.
*
* @tparam SIZE how much data to read from the source
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param in_stream input stream
* @param out_stream1 first output stream
* @param out_stream2 second output stream
*/
template<int SIZE, typename T = DEFAULT_DATATYPE>
void duplicate(hls::stream<T> &in_stream, hls::stream<T> &out_stream1, hls::stream<T> &out_stream2) {
#pragma HLS inline
duplicate_loop: for (int i = 0; i < SIZE; i++) {
#pragma HLS pipeline II=20
T d = in_stream.read();
out_stream1.write(d);
out_stream2.write(d);
}
}
/**
* Takes data from a source stream and writes it to three different streams.
*
* @tparam SIZE how much data to read from the source
* @tparam T datatype to work with (should usually be float or fixed_t)
*
* @param in_stream input stream
* @param out_stream1 first output stream
* @param out_stream2 second output stream
* @param out_stream3 third output stream
*/
template<int SIZE, typename T = DEFAULT_DATATYPE>
void triplicate(hls::stream<T> &in_stream, hls::stream<T> &out_stream1, hls::stream<T> &out_stream2, hls::stream<T> &out_stream3) {
#pragma HLS inline
for (int i = 0; i < SIZE; i++) {
T d = in_stream.read();
out_stream1.write(d);
out_stream2.write(d);
out_stream3.write(d);
}
}
};
#endif