DP-HLS
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
debug.h
Go to the documentation of this file.
1 #ifndef DP_HLS_DEBUG_H
2 #define DP_HLS_DEBUG_H
3 
4 /*
5  * This file contains debug functions of the kernel. Internal development purpose only.
6  */
7 #include "dp_hls_common.h"
8 #include <list>
9 #include <array>
10 #include <string>
11 // #include <experimental/filesystem>
12 #include <map>
13 #include <fstream>
14 #include <unordered_map>
15 #include <hls_vector.h>
16 #include "utils.h"
17 #include <vector>
18 #include <cstdarg>
19 #include <set>
20 
21 // NOTE: This file system thing doesn't seems to present on F1 AWS FPGA AMI.
22 // It uses higher standard, which older HLS's host compiler doesn't support.
23 // namespace fs = std::experimental::filesystem;
24 // using namespace fs;
25 using namespace std;
26 
27 
28 class Container {
29  // A data container to store the debug info (score matrix content, traceback content) of each block.
30 public:
31  std::vector<std::vector<score_vec_t>> scores_kernel;
32  array<array<array<float, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH>, N_LAYERS> scores_cpp;
33 
34  array<array<tbp_t, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH> tbp_mat_kernel;
35  array<array<unsigned int, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH> tbp_mat_cpp; // this need to be translated
36 
37  array<array<tbr_t, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH> tb_mat_kernel;
38  array<array<char, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH> tb_mat_cpp; // this need to be translated
39 
40  std::map<std::pair<uint, uint>, std::array<std::array<std::array<float, N_LAYERS>, PE_NUM+1>, 2>> wf_dp_mem;
41  std::map<std::pair<uint, uint>, std::array<bool, PE_NUM>> wf_predicates;
42 
43  struct score_info {
44  float up[N_LAYERS];
45  float left[N_LAYERS];
46  float diag[N_LAYERS];
47  float write[N_LAYERS];
48  bool pred;
49  bool exiting;
50  bool entering;
53  };
54 
59  std::map<std::pair<int, int>, score_info> scores_infos;
60 
62  : scores_kernel(MAX_QUERY_LENGTH, std::vector<score_vec_t>(MAX_REFERENCE_LENGTH))
63  {};
64 
65  void cast_scores();
66  void cast_tbp();
67  void cast_tb();
68  void cast_all();
69 
70  void set_score(int chunk_row_offset, int chunk_col_offset, int pe_num, int wavefront, score_vec_t vals, bool pred);
71  void set_scores_wf(int chunk_row_offset, int chunk_col_offset, int wavefront, score_vec_t vals[PE_NUM], bool predicates[PE_NUM]);
72 
73  template <typename IDX_T>
74  void set_wf_dp_mem(IDX_T ck_idx, IDX_T wf_idx, dp_mem_block_t dp_mem){
75  array<std::array<std::array<float, N_LAYERS>, PE_NUM+1>, 2> store_dp_mem;
76  for (int i = 0; i < PE_NUM+1; i++){
77  for (int j = 0; j < 2; j++){
78  for (int k = 0; k < N_LAYERS; k++){
79  store_dp_mem[j][i][k] = dp_mem[i][j][k];
80  }
81  }
82  }
83  wf_dp_mem[std::make_pair(ck_idx, wf_idx)] = store_dp_mem;
84  }
85 
86  template <typename IDX_T>
87  void set_score_info_dependency(IDX_T chunk_offset, IDX_T wf_idx, dp_mem_block_t dp_mem){
88  for (int i = 0; i < PE_NUM; i++){
89  score_info curr_info;
90  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)] = curr_info;
91  for (int k = 0; k < N_LAYERS; k++){
92  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].up[k] = dp_mem[i][0][k];
93  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].left[k] = dp_mem[i+1][0][k];
94  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].diag[k] = dp_mem[i][1][k];
95  }
96 
97  }
98  }
99 
100  template <typename IDX_T>
101  void set_score_info_entering_exiting(IDX_T chunk_offset, IDX_T wf_idx, bool entering, bool exiting, int entering_pe, int exiting_pe){
102  for (int i = 0; i < PE_NUM; i++){
103  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].entering = entering;
104  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].exiting = exiting;
105  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].entering_pe = entering_pe;
106  scores_infos[std::make_pair(chunk_offset + i, wf_idx - i)].exiting_pe = exiting_pe;
107  }
108  }
109 
110  // function that dump scores infos to a debug file
111  template <int N_LAYERS_>
112  void dump_scores_infos(ofstream &file){
113  // dump scores, one cell per line
114  file << "Scores: " << endl;
115  for (const auto& kv : this->scores_infos) {
116  const std::pair<int, int>& key = kv.first;
117  const score_info& value = kv.second;
118  for (int i = 0; i < N_LAYERS_; i++){
119  file << "Coordinate: (" << key.first << ", " << key.second << "), Layer: " << i <<
120  ", Up: " << value.up[i] << ", Left: " << value.left[i] << ", Diag: " << value.diag[i] << ", Pred: " << value.pred << ", ";
121  if (value.entering){
122  file << "Entering PE: " << value.entering_pe << ", ";
123  }
124  if (value.exiting){
125  file << "Exiting PE: " << value.exiting_pe << ", ";
126  }
127  file << endl;
128  }
129  }
130 
131 }
132  // set score info predicate
133  template <typename IDX_T>
134  void set_score_info_predicates(IDX_T ck_offset, IDX_T wf_idx, bool preds[PE_NUM]){
135  for (int i = 0; i < PE_NUM; i++){
136  this->scores_infos[std::make_pair(ck_offset + i, wf_idx-i)].pred = preds[i];
137  }
138  }
139 
140  // template <typename IDX_T>
141  // void dump_tb_info(ofstream &file){
142 
143  // }
144 
145  void compare_scores(array<array<array<float, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH>, N_LAYERS> scores_sol,
146  int query_len, int ref_len);
147 
148  void compare_scores(array<array<array<float, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH>, N_LAYERS> scores_sol,
149  int query_len, int ref_len, float threashold);
150 
159  void compare_scores_double(array<array<array<double, MAX_REFERENCE_LENGTH>, MAX_QUERY_LENGTH>, N_LAYERS> scores_sol,
160  int query_len, int ref_len, float threashold);
161 
162 private:
163  std::string debugpath;
164  std::string filepath;
165 
166  // Container(const string debugpath, const string filename, const int query_length, const int reference_length) {
167  // this->query_length = query_length;
168  // this->reference_length = reference_length;
169 
170  // this->debugpath = debugpath; // set the path to the debug folder
171  // fs::path path(debugpath); // get the path to the debug folder
172  // fs::remove_all(path);
173  // fs::create_directories(path);
174  // this->filepath = debugpath + "/" + filename;
175  // std::ofstream createFile(this->filepath);
176  // assert(createFile.is_open() && "Unable to Create File"); // create file to write =
177  // createFile.close();
178  // };
179 
180 };
181 
182 
183 namespace DebugUtils
184 {
185  template <typename T1, typename T2, int LEN>
186  void translate(T1 (&hls_arr)[LEN], T2 (&std_arr)[LEN])
187  {
188  for (int i = 0; i < LEN; i++)
189  {
190  std_arr[i] = (T2)(hls_arr[i]);
191  }
192  }
193 
194  template <typename T1, typename T2, int M, int N>
195  void translate(T1 (&hls_mat)[M][N], T2 (&std_mat)[M][N])
196  {
197  for (int i = 0; i < M; i++)
198  {
199  for (int j = 0; j < N; j++)
200  {
201  std_mat[i][j] = (T2)(hls_mat[i][j]);
202  }
203  }
204  }
205 
206  template <typename T1, typename T2, int M, int N, int K>
207  void translate_multilayer(T1 (&hls_mat)[M][N], T2 (&std_mat)[K][M][N])
208  {
209  for (int i = 0; i < M; i++)
210  {
211  for (int j = 0; j < N; j++)
212  {
213  for (int k = 0; k < K; k++)
214  {
215  std_mat[k][i][j] = (float)(hls_mat[i][j][k]);
216  }
217  }
218  }
219  }
220 
221  std::vector<float> translate_vec(hls::vector<type_t, N_LAYERS>(&arr));
222  std::vector<std::vector<float>> translate_scores(hls::vector<type_t, N_LAYERS> (&arr)[PE_NUM]);
223 
224  namespace Translate
225  {
237  template <typename T, int NL, int M, int N>
238  std::vector<std::vector<std::vector<float>>> translate_3d(
239  hls::vector<T, NL> scores[M][N])
240  {
241  std::vector<std::vector<std::vector<float>>> result;
242 
243  for (int i = 0; i < M; i++)
244  {
245  std::vector<std::vector<float>> pe_row;
246  for (int j = 0; j < N; j++)
247  {
248  std::vector<float> pe_vec;
249  for (int k = 0; k < NL; k++)
250  {
251  pe_vec.push_back(scores[i][j][k].to_float());
252  }
253  pe_row.push_back(pe_vec);
254  }
255  result.push_back(pe_row);
256  }
257  return result;
258  }
259 
270  template <typename T, int NL, int N>
271  std::vector<std::vector<float>> translate_2d(
272  hls::vector<T, NL> scores[N])
273  {
274  std::vector<std::vector<float>> result;
275 
276  for (int i = 0; i < N; i++)
277  {
278  std::vector<float> pe_vec;
279  for (int k = 0; k < NL; k++)
280  {
281  pe_vec.push_back(scores[i][k].to_float());
282  }
283  result.push_back(pe_vec);
284  }
285  return result;
286  }
287 
288  template <typename T, int M, int N>
289  std::vector<std::vector<float>> translate_2d(
290  T scores[M][N])
291  {
292  std::vector<std::vector<float>> result;
293 
294  for (int i = 0; i < M; i++)
295  {
296  std::vector<float> pe_vec;
297  for (int k = 0; k < N; k++)
298  {
299  pe_vec.push_back(scores[i][k].to_float());
300  }
301  result.push_back(pe_vec);
302  }
303  return result;
304  }
305 
315  template <typename T, int NL>
316  std::vector<float> translate_1d(
317  hls::vector<T, NL> scores)
318  {
319  std::vector<float> result;
320 
321  for (int k = 0; k < NL; k++)
322  {
323  result.push_back(scores[k].to_float());
324  }
325  return result;
326  }
327 
328  template <typename T, int N>
329  std::vector<float> translate_1d(
330  T scores[N])
331  {
332  std::vector<float> result;
333 
334  for (int k = 0; k < N; k++)
335  {
336  result.push_back(float(scores[k]));
337  }
338  return result;
339  }
340 
341  void print_3d(const char * name, std::vector<std::vector<std::vector<float>>> scores);
342  void print_2d(const char * name, std::vector<std::vector<float>> scores);
343  void print_1d(const char * name, std::vector<float> scores);
344 
345  }
346 
347 }
348 
349 #endif //DP_HLS_DEBUG_H
float diag[N_LAYERS]
Definition: debug.h:46
float left[N_LAYERS]
Definition: debug.h:45
hls::vector< type_t, N_LAYERS > score_vec_t
Vector of scores, used to store the scores of each layer in the same coordiante in the DP matrix...
Definition: dp_hls_common.h:31
void print_2d(const char *name, std::vector< std::vector< float >> scores)
void dump_scores_infos(ofstream &file)
Definition: debug.h:112
Definition: debug.h:28
Definition: debug.h:43
void translate_multilayer(T1(&hls_mat)[M][N], T2(&std_mat)[K][M][N])
Definition: debug.h:207
void set_score_info_dependency(IDX_T chunk_offset, IDX_T wf_idx, dp_mem_block_t dp_mem)
Definition: debug.h:87
bool entering
Definition: debug.h:50
Declaration of types used by DP-HLS internally, referred from the user defined params.h file.
std::map< std::pair< int, int >, score_info > scores_infos
Record the information of scores, with their coordinate as the index.
Definition: debug.h:59
std::vector< float > translate_1d(hls::vector< T, NL > scores)
Translate a data structure of the shape hls::vector&lt;T, NL&gt; scores[N] to 1d nested float stl vector...
Definition: debug.h:316
array< array< unsigned int, MAX_REFERENCE_LENGTH >, MAX_QUERY_LENGTH > tbp_mat_cpp
Definition: debug.h:35
std::vector< float > translate_vec(hls::vector< type_t, N_LAYERS >(&arr))
Container()
Definition: debug.h:61
int entering_pe
Definition: debug.h:51
void print_3d(const char *name, std::vector< std::vector< std::vector< float >>> scores)
std::map< std::pair< uint, uint >, std::array< std::array< std::array< float, N_LAYERS >, PE_NUM+1 >, 2 > > wf_dp_mem
Definition: debug.h:40
std::map< std::pair< uint, uint >, std::array< bool, PE_NUM > > wf_predicates
Definition: debug.h:41
void print_1d(const char *name, std::vector< float > scores)
void set_wf_dp_mem(IDX_T ck_idx, IDX_T wf_idx, dp_mem_block_t dp_mem)
Definition: debug.h:74
int exiting_pe
Definition: debug.h:52
std::vector< std::vector< float > > translate_scores(hls::vector< type_t, N_LAYERS >(&arr)[PE_NUM])
void set_score_info_predicates(IDX_T ck_offset, IDX_T wf_idx, bool preds[PE_NUM])
Definition: debug.h:134
float up[N_LAYERS]
Definition: debug.h:44
std::vector< std::vector< std::vector< float > > > translate_3d(hls::vector< T, NL > scores[M][N])
Translate a data structure of the shape hls::vector&lt;T, NL&gt; scores[M][N] to 3d nested float stl vector...
Definition: debug.h:238
array< array< tbr_t, MAX_REFERENCE_LENGTH >, MAX_QUERY_LENGTH > tb_mat_kernel
Definition: debug.h:37
array< array< char, MAX_REFERENCE_LENGTH >, MAX_QUERY_LENGTH > tb_mat_cpp
Definition: debug.h:38
bool exiting
Definition: debug.h:49
score_vec_t dp_mem_block_t[PE_NUM+1][2]
Definition: dp_hls_common.h:87
void set_score_info_entering_exiting(IDX_T chunk_offset, IDX_T wf_idx, bool entering, bool exiting, int entering_pe, int exiting_pe)
Definition: debug.h:101
void translate(T1(&hls_arr)[LEN], T2(&std_arr)[LEN])
Definition: debug.h:186
std::vector< std::vector< float > > translate_2d(hls::vector< T, NL > scores[N])
Translate a data structure of the shape hls::vector&lt;T, NL&gt; scores[N] to 2d nested float stl vector...
Definition: debug.h:271
array< array< tbp_t, MAX_REFERENCE_LENGTH >, MAX_QUERY_LENGTH > tbp_mat_kernel
Definition: debug.h:34
bool pred
Definition: debug.h:48
std::vector< std::vector< score_vec_t > > scores_kernel
Definition: debug.h:31
array< array< array< float, MAX_REFERENCE_LENGTH >, MAX_QUERY_LENGTH >, N_LAYERS > scores_cpp
Definition: debug.h:32