Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:TurakhiaLab/DP-HLS into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
ioeddk committed Jun 17, 2024
2 parents 5bd141d + 7c0615f commit 7885791
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fsanitize=address -fno-inline -D
# "-fsanitize=address" flag was used to check the stack smashing with Google Address Sanitizer. Use this flag with CLang and
# run the program to check.

set(DP_HLS_HOME "/home/centos/workspace/DP-HLS")
set(DP_HLS_HOME "/home/centos/workspace/banding/DP-HLS")

set(EXECUTABLE_TARGETS
baseline_local_linear
Expand Down
9 changes: 5 additions & 4 deletions include/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ namespace Align
idx_t global_query_length, idx_t query_length, idx_t reference_length,
const bool (&col_pred)[PE_NUM],
const Penalties &penalties,
ScorePack (&max)[PE_NUM], // write out so must pass by reference
tbp_t (&chunk_tbp_out)[PE_NUM][TBMEM_SIZE]
ScorePack (&max)[PE_NUM]
#ifndef NO_TRACEBACK
, tbp_t (&chunk_tbp_out)[PE_NUM][TBMEM_SIZE]
#endif
#ifdef CMAKEDEBUG
,
Container &debugger
, Container &debugger
#endif
);

Expand Down
20 changes: 17 additions & 3 deletions kernels/viterbi/kernel_viterbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ void Viterbi::PE::Compute(char_t local_query_val,
score_vec_t &write_score,
tbp_t &write_traceback)
{

// These two pragma needed to be put here to allow multiple access to the transition matrix.
// Remove those two will result in PE unroll failure.
#pragma HLS array_partition variable = penalties.transition dim = 0 type = complete
#pragma HLS array_partition variable = penalties dim = 0 type=complete

/*
* Layer 0: Vi
Expand Down Expand Up @@ -115,7 +118,19 @@ void Viterbi::UpdatePEMaximum(
const idx_t p_cols, const idx_t ck_idx,
const bool (&predicate)[PE_NUM],
const idx_t query_len, const idx_t ref_len){


for (idx_t i = 0; i < PE_NUM; i++)
{
#pragma HLS unroll
if (chunk_row_offset + i == query_len - 1 && wavefront == ref_len - 1){
if (predicate[i] && (scores[i + 1][LAYER_MAXIMIUM] > max[i].score))
{
max[i].score = scores[i + 1][LAYER_MAXIMIUM];
max[i].p_col = p_cols;
max[i].ck = ck_idx;
}
}
}
}

void Viterbi::InitializeMaxScores(ScorePack (&max)[PE_NUM], idx_t qry_len, idx_t ref_len)
Expand All @@ -127,7 +142,6 @@ void Viterbi::InitializeMaxScores(ScorePack (&max)[PE_NUM], idx_t qry_len, idx_t
max[i].p_col = 0;
max[i].ck = 0;
}
Utils::Init::DetermineGlobalTracebackCoordinate(max, qry_len, ref_len);
}


Expand Down
10 changes: 6 additions & 4 deletions kernels/viterbi/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
#define LAYER_MAXIMIUM 1 // We need to indicate from which layer (main matrix) is the maximum score stored.

#define BANDING RECTANGULAR
#define NO_TRACEBACK
#define SCORED

// Primitive Types
typedef ap_fixed<32, 12> type_t; // Scores Type <width, integer_width>
typedef ap_fixed<48, 22> type_t; // Scores Type <width, integer_width>
typedef ap_uint<2> char_t; // Sequence Alphabet
typedef short idx_t; // Indexing Type, could be much less than 32. ap_uint<8>
typedef ap_int<16> idx_t; // Indexing Type, could be much less than 32. ap_uint<8>
typedef ap_uint<4> tbp_t; // Traceback Pointer Type

// Defien upper and lower bound for score type, aka type_t
#define INF 1024
#define NINF -1024
#define INF 1048576
#define NINF -1048576

// Legacy Debugger Configuration
#define DEBUG_OUTPUT_PATH "/home/[email protected]/DP-HLS-Debug/global_affine/"
Expand Down
27 changes: 19 additions & 8 deletions src/align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ void Align::Rectangular::ChunkCompute(
idx_t global_query_length, idx_t query_length, idx_t reference_length,
const bool (&col_pred)[PE_NUM],
const Penalties &penalties,
ScorePack (&max)[PE_NUM],
tbp_t (&chunk_tbp_out)[PE_NUM][TBMEM_SIZE]
ScorePack (&max)[PE_NUM]
#ifndef NO_TRACEBACK
, tbp_t (&chunk_tbp_out)[PE_NUM][TBMEM_SIZE]
#endif
#ifdef CMAKEDEBUG
,
Container &debugger
, Container &debugger
#endif
)
{
Expand Down Expand Up @@ -189,7 +190,9 @@ void Align::Rectangular::ChunkCompute(
score_buff,
tbp_out);

#ifndef NO_TRACEBACK
Align::ArrangeTBP(tbp_out, p_col_offset, predicate, chunk_tbp_out);
#endif

#ifdef CMAKEDEBUG
for (int j = 0; j < PE_NUM; j++)
Expand Down Expand Up @@ -371,13 +374,18 @@ void Align::Rectangular::AlignStatic(
// >>> Initialization >>>
score_vec_t init_col_score[MAX_QUERY_LENGTH];
score_vec_t init_row_score[MAX_REFERENCE_LENGTH];
#ifndef NO_TRACEBACK
tbp_t tbp_matrix[PE_NUM][TBMEM_SIZE];
#endif
bool col_pred[PE_NUM];

#pragma HLS bind_storage variable = init_row_score type = ram_t2p impl = bram
#ifndef NO_TRACEBACK
#pragma HLS array_partition variable = tbp_matrix type = cyclic factor = PRAGMA_PE_NUM dim = 1
#endif

#ifdef CMAKEDEBUG
#ifndef NO_TRACEBACK
// initialize tbp_matrix with TB_PH
for (int i = 0; i < PE_NUM; i++)
{
Expand All @@ -386,6 +394,7 @@ void Align::Rectangular::AlignStatic(
tbp_matrix[i][j] = tbp_t(0);
}
}
#endif
#endif

idx_t p_cols;
Expand Down Expand Up @@ -424,8 +433,10 @@ void Align::Rectangular::AlignStatic(
reference_length,
col_pred,
penalties,
local_max,
tbp_matrix
local_max
#ifndef NO_TRACEBACK
, tbp_matrix
#endif
#ifdef CMAKEDEBUG
, debugger
#endif
Expand Down Expand Up @@ -590,7 +601,7 @@ void Align::Fixed::AlignStatic(
idx_t local_query_length = ((idx_t)PE_NUM < query_length - i) ? (idx_t)PE_NUM : (idx_t)(query_length - i);

p_col = p_cols_internal_offset + p_col_offset;
p_cols_internal_offset = p_cols_internal_offset > (idx_t) PE_NUM ? p_cols_internal_offset - PE_NUM : 0;
p_cols_internal_offset = p_cols_internal_offset > PE_NUM ? (idx_t) ( p_cols_internal_offset - PE_NUM) : (idx_t) 0;

Align::Fixed::PrepareLocals<PE_NUM, MAX_QUERY_LENGTH>(
query,
Expand Down Expand Up @@ -700,7 +711,7 @@ void Align::Fixed::ChunkCompute(
#pragma HLS array_partition variable = score_buff type = complete

const idx_t chunk_start_col = l_lim_reg > 0 ? l_lim_reg : (idx_t) 0;
const idx_t chunk_end_col = u_lim_reg + PE_NUM - 1 <= reference_length - 1 ? u_lim_reg + PE_NUM - 1: reference_length - 1;
const idx_t chunk_end_col = u_lim_reg + PE_NUM - 1 <= reference_length - 1 ? (idx_t) (u_lim_reg + PE_NUM - 1): (idx_t) (reference_length - 1);
idx_t entering_pe = 0;
idx_t exiting_pe = 0;
bool entering = false;
Expand Down
74 changes: 41 additions & 33 deletions testbench/test_csim_viterbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

using namespace std;

#define INPUT_QUERY_LENGTH 240
#define INPUT_REFERENCE_LENGTH 254
#define INPUT_QUERY_LENGTH 256
#define INPUT_REFERENCE_LENGTH 256

struct Penalties_sol {
double log_1_m_2_lambda;
Expand All @@ -27,30 +27,20 @@ struct Penalties_sol {
};


#define MU 0.6321
#define LAMBDA 0.1353
#define MU 0.2
#define LAMBDA 0.05

int main(){
std::string query_string = "CCGTAGACCCGAACTTCGCGGTACACCTTCTGAAACCGTCCCTAATCCGACGAGCGCCTTGAGAACG";
std::string reference_string = "TGAGAACGTAGTCTAGGCGAATCGGCCCTTGTATATCGGGGCCGTAGACCCGAACTTCGCGGTACAC";
char alphabet[4] = {'A', 'T', 'G', 'C'};
// std::string query_string = Random::Sequence<4>(alphabet, INPUT_QUERY_LENGTH);
// std::string reference_string = Random::Sequence<4>(alphabet, INPUT_REFERENCE_LENGTH);

// float transition_[5][5] = {
// {0.8, 0.3, 0.3, 0.3, 0.2},
// {0.3, 0.8, 0.3, 0.3, 0.2},
// {0.3, 0.3, 0.8, 0.3, 0.2},
// {0.3, 0.3, 0.3, 0.8, 0.2},
// {0.2, 0.2, 0.2, 0.2, 0.2}
// };
std::string query_string = Random::Sequence<4>(alphabet, INPUT_QUERY_LENGTH);
std::string reference_string = Random::Sequence<4>(alphabet, INPUT_REFERENCE_LENGTH);

float transition_[5][5] = {
{20.08, 0.1353, 0.1353, 0.1353, 0.3678},
{0.1353, 20.08, 0.1353, 0.1353, 0.3678},
{0.1353, 0.1353, 20.08, 0.1353, 0.3678},
{0.1353, 0.1353, 0.1353, 20.08, 0.3678},
{0.3678, 0.3678, 0.3678, 0.3678, 0.3678}
{0.2, 0.01666, 0.01666, 0.01666, 0.25},
{0.01666, 0.2, 0.01666, 0.01666, 0.25},
{0.01666, 0.01666, 0.2, 0.01666, 0.25},
{0.01666, 0.01666, 0.01666, 0.2, 0.25},
{0.25, 0.25, 0.25, 0.25, 0}
};

float log_transitions_[5][5];
Expand Down Expand Up @@ -140,9 +130,12 @@ int main(){
ref_lengths[b] = reference.size();
}

// Allocate traceback streams
tbr_t tb_streams_d[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH][N_BLOCKS];
tbr_t tb_streams_h[N_BLOCKS][MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH];
type_t scores[N_BLOCKS];

#ifndef NO_TRACEBACK
tbr_t tb_h[N_BLOCKS][MAX_QUERY_LENGTH + MAX_REFERENCE_LENGTH];
tbr_t tb_d[MAX_QUERY_LENGTH + MAX_REFERENCE_LENGTH][N_BLOCKS];
#endif

// initialize traceback starting coordinates
idx_t tb_is[N_BLOCKS];
Expand All @@ -155,8 +148,13 @@ int main(){
qry_lengths,
ref_lengths,
penalties,
tb_is, tb_js,
tb_streams_d
tb_is, tb_js
#ifndef NO_TRACEBACK
, tb_d
#endif
#ifdef SCORED
, scores
#endif
#ifdef CMAKEDEBUG
, debuggers
#endif
Expand Down Expand Up @@ -195,24 +193,34 @@ int main(){
query_string_blocks[i] = query_string;
reference_string_blocks[i] = reference_string;
}
HostUtils::IO::SwitchDimension(tb_streams_d, tb_streams_h);

#ifndef NO_TRACEBACK
HostUtils::IO::SwitchDimension(tb_d, tb_h);

for (int i = 0; i < N_BLOCKS; i++){
cout << "Block " << i << " Traceback Pointers" << endl;
for (int j = 0; j < MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH; j++){
cout << bitset<4>(tb_streams_h[i][j].to_int()) << " ";
cout << bitset<4>(tb_h[i][j].to_int()) << " ";
}
cout << endl;
}

kernel_alignments = HostUtils::Sequence::ReconstructTracebackBlocks<tbr_t, N_BLOCKS, MAX_QUERY_LENGTH, MAX_REFERENCE_LENGTH>(
query_string_blocks, reference_string_blocks,
tb_query_lengths, tb_reference_lengths,
tb_streams_h);
tb_h);

// Print kernel 0 traceback
cout << "Kernel 0 Traceback" << endl;
cout << "Kernel Aligned Query : " << kernel_alignments[0]["query"] << endl;
cout << "Kernel Aligned Reference: " << kernel_alignments[0]["reference"] << endl;
// print out the scores as well
for (int i = 0; i < N_BLOCKS; i++){
cout << "Kernel " << i << "Traceback" << endl;
cout << "Kernel Aligned Query : " << kernel_alignments[0]["query"] << endl;
cout << "Kernel Aligned Reference: " << kernel_alignments[0]["reference"] << endl;
}
#endif

// print the scores for all blocks
for (int i = 0; i < N_BLOCKS; i++) {
// std::exp(scores[i].to_float())
cout << "Block " << i << " Score: " << scores[i].to_float() << endl;
}
}

0 comments on commit 7885791

Please sign in to comment.