DP-HLS
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
traceback.h
Go to the documentation of this file.
1 #ifndef TRACEBACK_H
2 #define TRACEBACK_H
3 
4 #include "dp_hls_common.h"
5 #include "frontend.h"
6 #include <hls_stream.h>
7 #include <hls_vector.h>
8 
9 #ifdef CMAKEDEBUG
10 #include "debug.h"
11 #endif // DEBUG
12 
13 using namespace hls;
14 
15 namespace Traceback
16 {
23  void Traceback(
24  tbp_t (&tbmat)[MAX_QUERY_LENGTH][MAX_REFERENCE_LENGTH],
25  tbr_t (&traceback_out)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH],
26  const int max_row, const int max_col); // starting index to traceback
27 
34  void TracebackOptimized(
35  tbp_t (&tbmat)[PE_NUM][TBMEM_SIZE],
36  traceback_buf_t(&traceback_out),
37  idx_t (&ck_start_col)[MAX_QUERY_LENGTH / PE_NUM], // chunk start index
38  idx_t (&ck_end_col)[MAX_QUERY_LENGTH / PE_NUM], // chunk end index
39  int ck_idx, int pe_idx, int col_idx, int v_row, int v_col); // starting index to traceback
40 
41  template <int VIRTUAL_CHUNK_WIDTH>
43  tbr_t &nav,
44  int &chunk, int &pe, int &col, int &v_row, int &v_col)
45  {
46 #ifdef CMAKEDEBUG
47  int nav_int = nav.to_int();
48  // cout << "col: " << col << ", pe: " << pe << ", chunk: " << chunk << ", v_row: " << v_row << ", v_col " << v_col << ", nav_int: " << nav_int << endl;
49 #endif
50 
51  // Check the condition based on the virtual row and column
52  if (v_row <= 0 || v_col <= 0)
53  {
54  nav = AL_END;
55  }
56  else if (nav == AL_INS)
57  { // Moving left
58  col--;
59  v_col--;
60  }
61  else if (nav == AL_DEL)
62  { // Moving up
63  if (pe == 0)
64  {
65  pe = PE_NUM - 1;
66  col -= VIRTUAL_CHUNK_WIDTH;
67  chunk--;
68  }
69  else
70  {
71  col--;
72  pe--;
73  }
74  v_row--;
75  }
76  else if (nav == AL_MMI)
77  {
78  // Moving Diagonal
79  // Moving diagonal is a combination of moving left and moving up
80 
81  col--;
82 
83  if (pe == 0)
84  {
85  pe = PE_NUM - 1;
86  col -= VIRTUAL_CHUNK_WIDTH;
87  chunk--;
88  }
89  else
90  {
91  col--;
92  pe--;
93  }
94  v_row--;
95  v_col--;
96  }
97  else if (nav == AL_NULL)
98  {
99  // Skip a cycle and do nothing
100  }
101  else
102  {
103  // Stop Condition Set by User
104  // In this case the Stopping Flag is manually set by the user
105  // that want to early stop the traceback. For example, in the
106  // local alignment, the traceback will stop if the scores reaches 0.
107  nav = AL_END; // Just repeat the last write
108  }
109  }
110 
111  template <int CHUNK_WIDTH>
113  tbp_t (&tbmat)[PE_NUM][TBMEM_SIZE],
114  traceback_buf_t(&traceback_out),
115  int ck_idx, int pe_idx, int col_idx, int v_row, int v_col)
116  {
117 
118  int pe = pe_idx; // row index, but in tbmat
119  int col = col_idx;
120  int chunk = ck_idx;
121 
122  int w_id = 0; // write idx
123  tbr_t navigation = AL_MMI; // current write value
124 
125  TB_STATE state;
126  ALIGN_TYPE::Traceback::StateInit(tbmat[pe][col], state);
127 
128  TracebackLoop:
129  while (navigation != AL_END) // Now solely this flag determines whether to stop the traceback.
130  {
131 #pragma HLS PIPELINE II = 1
132 
133  tbp_t tbptr = tbmat[pe][col]; // Want to represented by the symbol rather than pointer
134 
135  // User define mapping from a pointer and current state to
136  // one of the Del, Ins, Match/Mismatch, or End to the next state.
137  ALIGN_TYPE::Traceback::StateMapping(tbptr, state, navigation);
138  traceback_out[w_id++] = navigation;
139  Traceback::NextAddressFixedSize<CHUNK_WIDTH>(navigation, chunk, pe, col, v_row, v_col);
140  }
141  traceback_out[w_id] = AL_END;
142  }
143 
156  void pointer_to_coordinate(tbp_t tbp, TB_STATE &state, int &row, int &column);
157 
164  void state_initial(tbp_t tbp, TB_STATE &state);
165 
171  tbp_t extract_direction(tbp_t tbp);
172 
177  tbr_t state_to_path(TB_STATE state);
178 
179  void NextAddress(tbr_t &nagivation,
180  idx_t (&ck_start_idx)[CK_NUM],
181  idx_t (&ck_end_idx)[CK_NUM],
182  int &chunk, int &pe, int &col, int &v_row, int &v_col);
183 }
184 
185 #endif
void TracebackOptimized(tbp_t(&tbmat)[PE_NUM][TBMEM_SIZE], traceback_buf_t(&traceback_out), idx_t(&ck_start_col)[MAX_QUERY_LENGTH/PE_NUM], idx_t(&ck_end_col)[MAX_QUERY_LENGTH/PE_NUM], int ck_idx, int pe_idx, int col_idx, int v_row, int v_col)
Traceback function. It extract the traceback path from a matrix of traceback pointers.
#define CK_NUM
Defines the number of chunks (CK) based on the maximum query length and the number of processing elem...
Definition: dp_hls_common.h:58
#define AL_INS
Definition: dp_hls_common.h:96
void NextAddress(tbr_t &nagivation, idx_t(&ck_start_idx)[CK_NUM], idx_t(&ck_end_idx)[CK_NUM], int &chunk, int &pe, int &col, int &v_row, int &v_col)
void state_initial(tbp_t tbp, TB_STATE &state)
Function used to set the initial state of the traceback, according to the ruls of the traceback point...
#define AL_DEL
Definition: dp_hls_common.h:98
#define AL_NULL
Definition: dp_hls_common.h:99
Declaration of types used by DP-HLS internally, referred from the user defined params.h file.
void TracebackFixedSize(tbp_t(&tbmat)[PE_NUM][TBMEM_SIZE], traceback_buf_t(&traceback_out), int ck_idx, int pe_idx, int col_idx, int v_row, int v_col)
Definition: traceback.h:112
tbr_t state_to_path(TB_STATE state)
tbp_t extract_direction(tbp_t tbp)
Private helper function that extract the direction bit of the traceback pointer.
void pointer_to_coordinate(tbp_t tbp, TB_STATE &state, int &row, int &column)
A traceback pointer to coordinate mapping function. The traceback function is implemeted as a state m...
void StateMapping(tbp_t tbp, TB_STATE &state, tbr_t &navigation)
Map the current step&#39;s state and the traceback pointer to the next state and one of the Insertion...
#define AL_MMI
Definition: dp_hls_common.h:97
tbr_t traceback_buf_t[MAX_QUERY_LENGTH+MAX_REFERENCE_LENGTH]
Definition: dp_hls_common.h:101
void NextAddressFixedSize(tbr_t &nav, int &chunk, int &pe, int &col, int &v_row, int &v_col)
Definition: traceback.h:42
void StateInit(tbp_t tbp, TB_STATE &state)
Initialize the initial traceback state based on the initial traceback pointer.
#define AL_END
Definition: dp_hls_common.h:95
ap_uint< 3 > tbr_t
Definition: dp_hls_common.h:94
void Traceback(tbp_t(&tbmat)[MAX_QUERY_LENGTH][MAX_REFERENCE_LENGTH], tbr_t(&traceback_out)[MAX_REFERENCE_LENGTH+MAX_QUERY_LENGTH], const int max_row, const int max_col)
Traceback function. It extract the traceback path from a matrix of traceback pointers.