DP-HLS
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
utils.h
Go to the documentation of this file.
1 #ifndef UTILS_H
2 #define UTILS_H
3 
4 #include "params.h"
5 #include <string>
6 
7 namespace Utils
8 {
9 
10  namespace Array
11  {
12  // template <typename T, int N>
13  // void WriteStreamBlock(T arr[N], hls::stream_of_blocks<T[N]> &stream_block)
14  // {
15  // hls::write_lock<T[N]> wr(stream_block);
16 
17  // #pragma HLS array_partition variable = arr type = complete
18  // #pragma HLS array_partition variable = wr type = complete
19 
20  // for (int i = 0; i < N; i++)
21  // {
22  // #pragma HLS unroll
23  // wr[i] = arr[i];
24  // }
25  // }
26  template <typename T, int N>
27  void ShiftLeft(T (&arr)[N], T new_data)
28  {
29  for (int i = 0; i < N; i++)
30  {
31 #pragma HLS unroll
32  arr[i] = arr[i + 1];
33  }
34  arr[N - 1] = new_data;
35  }
36  template <typename T, int N>
37  void ShiftRight(T (&arr)[N], T new_data)
38  {
39 #pragma HLS array_partition variable = arr dim = 1 type = complete
40  for (int i = N - 1; i > 0; i--)
41  {
42 #pragma HLS unroll
43  arr[i] = arr[i - 1];
44  }
45  arr[0] = new_data;
46  }
47 
60  template <typename T, int M, int N, int LEN>
61  void Copy(T (&src)[M], T (&dst)[N], idx_t len, T default_num)
62  {
63  for (int i = 0; i < LEN; i++)
64  {
65 #pragma HLS unroll
66  dst[i] = i < len ? src[i] : default_num;
67  }
68  }
69 
70  template <typename T, int N>
71  void Copy(T (&src)[N], T (&dst)[N])
72  {
73  for (int i = 0; i < N; i++)
74  {
75  dst[i] = src[i];
76  }
77  }
78 
79 
80  // template <typename T, int M, int N>
81  // void Copy(T(&dst)[M], idx_t dst_idx, T (&src)[N], idx_t src_idx, idx_t len, T default_num)
82  // {
83  // for (int i = 0; i < len; i++)
84  // {
85  // #pragma HLS unroll
86  // dst[i] = i < len ? src[i] : default_num;
87  // }
88  // }
89 
90  template <typename T>
91  void Switch(T **arr1, T **arr2)
92  {
93  T *tmp = *arr1;
94  *arr1 = *arr2;
95  *arr2 = tmp;
96  }
97 
98  template <typename T, int LEN>
99  void Linspace(T (&arr)[LEN], T start, T step)
100  {
101  T cnt = start;
102  for (int i = 0; i < LEN; i++)
103  {
104  arr[i] = cnt;
105  cnt += step;
106  }
107  }
108  }
109 
110  namespace Init
111  {
112 
113  template <typename T, int N>
114  void ArrSet(T (&arr)[N], T val)
115  {
116 
117  for (int i = 0; i < N; i++)
118  {
119  arr[i] = val;
120  }
121  }
122 
123  template <typename T, int N>
124  void ArrSet(T (&arr)[N], idx_t layer, T val)
125  {
126 
127  for (int i = 0; i < N; i++)
128  {
129  arr[i][layer] = val[layer];
130  }
131  }
132 
133  template <typename T, int LAY, int N>
134  void Linspace(hls::vector<T, LAY> (&arr)[N], idx_t starting_idx, idx_t layer, type_t start, type_t step)
135  {
136  type_t cnt = start;
137  for (int i = starting_idx; i < N; i++)
138  {
139  cnt += step;
140  arr[i][layer] = cnt;
141  }
142  }
143 
144  template <typename T>
145  void Linspace(T &arr, idx_t starting_idx, idx_t layer, type_t start, type_t step, int len)
146  {
147  type_t cnt = start;
148  for (int i = starting_idx; i < len; i++)
149  {
150  cnt += step;
151  arr[i][layer] = cnt;
152  }
153  }
154 
167  template <typename PACK, typename IDX_T, int LEN>
168  void DetermineGlobalTracebackCoordinate(PACK (&local_maximum)[LEN], IDX_T qry_len, IDX_T ref_len){
169  IDX_T max_pe = (qry_len - 1) % PE_NUM;
170  IDX_T max_ck = (qry_len - 1) / PE_NUM;
171  local_maximum[max_pe].score = INF;
172  local_maximum[max_pe].p_col = (max_ck) * (MAX_REFERENCE_LENGTH + PE_NUM - 1) + max_pe + ref_len - 1;
173  local_maximum[max_pe].ck = max_ck;
174  }
175 
176  template <typename PACK, typename IDX_T, int PE_NUM_, int BANDWIDTH_, int TB_CHUNK_WIDTH_>
177  void DetermineFixedBandingGlobalTracebackCoordinate(PACK (&max)[PE_NUM_], IDX_T qry_len, IDX_T ref_len){
178  IDX_T max_pe = (qry_len - 1) % PE_NUM;
179  IDX_T max_ck = (qry_len - 1) / PE_NUM;
180  max[max_pe].score = INF;
181  max[max_pe].p_col = max_ck * (TB_CHUNK_WIDTH_) + max_pe + ref_len - ( max_ck * PE_NUM - BANDWIDTH_ ) - 1; // FIXME: Problemmatic, the problem is that the last chunk doesn't have length ref_len.
182  max[max_pe].ck = max_ck;
183  }
184 
185  }
186 
187  namespace Matrix
188  {
189 
190  // template <typename T, int M, int N>
191  // void WriteStreamBlock(T arr[M][N], hls::stream_of_blocks<T[M][N]> &stream_block)
192  // {
193  // hls::write_lock<T[M][N]> wr(stream_block);
194 
195  // for (int i = 0; i < M; i++)
196  // {
197  // #pragma HLS unroll
198  // for (int j = 0; j < N; j++)
199  // {
200  // #pragma HLS unroll
201  // wr[i][j] = arr[i][j];
202  // }
203  // }
204  // }
205 
206  // template <typename T, int M, int N>
207  // void ReadStreamBlock(T (&arr)[M][N], hls::stream_of_blocks<T[M][N]> &stream_block)
208  // {
209  // hls::read_lock<T[M][N]> rd(stream_block);
210 
211  // for (int i = 0; i < M; i++)
212  // {
213  // #pragma HLS unroll
214  // for (int j = 0; j < N; j++)
215  // {
216  // #pragma HLS unroll
217  // arr[i][j] = rd[i][j];
218  // }
219  // }
220  // }
221 
222  // /**
223  // * @brief This function is for alias typname for steram of blocks
224  // *
225  // * @tparam T: For example, typedef int T[16]
226  // * @param arr: array form of redefined type
227  // * @param stream_block: stream of the block type
228  // */
229  // template <typename T, int M, int N>
230  // void WriteStreamBlock(T arr, hls::stream_of_blocks<T> &stream_block)
231  // {
232  // hls::write_lock<T> wr(stream_block);
233 
234  // for (int i = 0; i < M; i++)
235  // {
236  // #pragma HLS unroll
237  // for (int j = 0; j < N; j++)
238  // {
239  // #pragma HLS unroll
240  // wr[i][j] = arr[i][j];
241  // }
242  // }
243  // }
244 
245  // template <typename T, int M, int N>
246  // void ReadStreamBlock(T &arr, hls::stream_of_blocks<T> &stream_block)
247  // {
248  // hls::read_lock<T> rd(stream_block);
249 
250  // for (int i = 0; i < M; i++)
251  // {
252  // #pragma HLS unroll
253  // for (int j = 0; j < N; j++)
254  // {
255  // #pragma HLS unroll
256  // arr[i][j] = rd[i][j];
257  // }
258  // }
259  // }
260 
261  }
262 
263  namespace Kernel {
264  template <typename T, int LEN, int BLK>
265  void top_level_radin_line(T src[BLK], T dst[BLK][LEN], idx_t i){
266  for (idx_t j = 0; j < BLK; j++){
267  dst[j][i] = src[j];
268  }
269  }
270 
271  template <typename T, int LEN, int BLK>
272  void top_level_readin(T ddr_in[LEN][BLK], T internal[BLK][LEN]){
273  // char_t burst_buffer[BLK];
274  for (idx_t i = 0; i < LEN; i ++){
275 #pragma HLS PIPELINE II=1
276  // top_level_radin_line<T, LEN, BLK>(ddr_in[i], internal, i);
277  for (idx_t j = 0; j < BLK; j++){
278  internal[j][i] = ddr_in[i][j];
279  }
280  // top_level_radin_line<T, LEN, BLK>(burst_buffer, internal, i);
281  // for (idx_t j = 0; j < BLK; j++){
282  // internal[j][k] = ddr_in[i + j];
283  // }
284  }
285  }
286 
287  template <typename T, int LEN, int BLK>
288  void top_level_writeout(T internal[BLK][LEN], T ddr_out[LEN][BLK]){
289  for (idx_t i = 0; i < LEN; i++){
290 #pragma HLS PIPELINE II=1
291  for (idx_t j = 0; j < BLK; j++){
292  ddr_out[i][j] = internal[j][i];
293  }
294  }
295  }
296  }
297 }
298 
299 #endif // UTILS_H
void Copy(T(&src)[M], T(&dst)[N], idx_t len, T default_num)
Copy length LEN of data from src to dst. If more than len, fill the rest with default_num. We need to use fixed length array rather than a pointer is because HLS is not pointer friendly.
Definition: utils.h:61
void top_level_readin(T ddr_in[LEN][BLK], T internal[BLK][LEN])
Definition: utils.h:272
void DetermineGlobalTracebackCoordinate(PACK(&local_maximum)[LEN], IDX_T qry_len, IDX_T ref_len)
ONLY FOR RECTANGULAR KERNEL Global alignment start the traceback at the bottom right corner of the sc...
Definition: utils.h:168
void Switch(T **arr1, T **arr2)
Definition: utils.h:91
void Linspace(T(&arr)[LEN], T start, T step)
Definition: utils.h:99
void DetermineFixedBandingGlobalTracebackCoordinate(PACK(&max)[PE_NUM_], IDX_T qry_len, IDX_T ref_len)
Definition: utils.h:177
void top_level_radin_line(T src[BLK], T dst[BLK][LEN], idx_t i)
Definition: utils.h:265
void Linspace(hls::vector< T, LAY >(&arr)[N], idx_t starting_idx, idx_t layer, type_t start, type_t step)
Definition: utils.h:134
void ArrSet(T(&arr)[N], T val)
Definition: utils.h:114
void top_level_writeout(T internal[BLK][LEN], T ddr_out[LEN][BLK])
Definition: utils.h:288
void ShiftRight(T(&arr)[N], T new_data)
Definition: utils.h:37
void ShiftLeft(T(&arr)[N], T new_data)
Definition: utils.h:27