Skip to content

Commit

Permalink
Added the option to output scores and remove the traceback pointer st…
Browse files Browse the repository at this point in the history
…eramming in the top level function. Added the kernel configuration for local affine with scores without traceback, targeting the BSW kernel in Darwin-WGA.
  • Loading branch information
Cloud User committed Jun 3, 2024
1 parent 18c714e commit 9cbcb07
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 33 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(EXECUTABLE_TARGETS
test_csim_global_two_piece_affine
test_csim_banding_global_two_piece_affine
test_csim_banding_local_affine

test_csim_banding_local_affine_scored
test_global_affine_real_data
)

Expand Down Expand Up @@ -90,6 +90,13 @@ add_executable(test_csim_global_affine
"kernels/global_affine/kernel_global_affine.cpp"
${COMMON_SRCS})

# BSW in Darwin-WGA
add_executable(test_csim_banding_local_affine_scored
"kernels/banding_local_affine_scored/banding_local_affine_scored.cpp"
"testbench/test_csim_banding_local_affine_scored.cpp"
${COMMON_SRCS})


add_executable(test_csim_local_affine
"testbench/test_csim_local_affine.cpp"
"kernels/local_affine/kernel_local_affine.cpp"
Expand Down Expand Up @@ -192,6 +199,7 @@ target_include_directories(test_csim_global_two_piece_affine PRIVATE "${DP_HLS_H
target_include_directories(test_global_affine_real_data PRIVATE "${DP_HLS_HOME}/kernels/global_affine")
target_include_directories(test_csim_banding_global_two_piece_affine PRIVATE "${DP_HLS_HOME}/kernels/banding_global_two_piece_affine")
target_include_directories(test_csim_banding_local_affine PRIVATE "${DP_HLS_HOME}/kernels/banding_local_affine")
target_include_directories(test_csim_banding_local_affine_scored PRIVATE "${DP_HLS_HOME}/kernels/banding_local_affine_scored")

# Add include to Xilinx Library Files
if (RAPTOR)
Expand Down
24 changes: 16 additions & 8 deletions include/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,15 @@ namespace Align
const idx_t query_length,
const idx_t reference_length,
const Penalties &penalties,
idx_t &tb_i, idx_t &tb_j,
tbr_t (&tb_out)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH]
idx_t &tb_i, idx_t &tb_j
#ifndef NO_TRACEBACK
, tbr_t (&tb_out)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH]
#endif
#ifdef SCORED
, type_t &score
#endif
#ifdef CMAKEDEBUG
,
Container &debugger
, Container &debugger
#endif
);

Expand Down Expand Up @@ -324,11 +328,15 @@ namespace Align
const idx_t query_length,
const idx_t reference_length,
const Penalties &penalties,
idx_t &tb_i, idx_t &tb_j,
tbr_t (&tb_out)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH]
idx_t &tb_i, idx_t &tb_j
#ifndef NO_TRACEBACK
, tbr_t (&tb_out)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH]
#endif
#ifdef SCORED
, type_t &score
#endif
#ifdef CMAKEDEBUG
,
Container &debugger
, Container &debugger
#endif
);

Expand Down
9 changes: 7 additions & 2 deletions include/seq_align_multiple.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ extern "C" {
idx_t (&query_lengths)[N_BLOCKS],
idx_t (&reference_lengths)[N_BLOCKS],
Penalties (&penalties)[N_BLOCKS],
idx_t (&tb_is)[N_BLOCKS], idx_t (&tb_js)[N_BLOCKS],
tbr_t (&tb_streams)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH][N_BLOCKS]
idx_t (&tb_is)[N_BLOCKS], idx_t (&tb_js)[N_BLOCKS]
#ifndef NO_TRACEBACK
, tbr_t (&tb_streams)[MAX_REFERENCE_LENGTH + MAX_QUERY_LENGTH][N_BLOCKS]
#endif
#ifdef SCORED
, type_t (&scores)[N_BLOCKS]
#endif
#ifdef CMAKEDEBUG
, Container (&debugger)[N_BLOCKS]
#endif
Expand Down
189 changes: 189 additions & 0 deletions kernels/banding_local_affine_scored/banding_local_affine_scored.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include "frontend.h"

void BSWAffine::PE::Compute(char_t local_query_val,
char_t local_reference_val,
score_vec_t up_prev,
score_vec_t diag_prev,
score_vec_t left_prev,
const Penalties penalties,
score_vec_t &write_score,
tbp_t &write_traceback)
{

// Define Traceback Pointer Navigation Direction

const type_t insert_open = left_prev[1] + penalties.open + penalties.extend; // Insert open
const type_t insert_extend = left_prev[0] + penalties.open; // insert extend
const type_t delete_open = up_prev[1] + penalties.open + penalties.extend; // delete open
const type_t delete_extend = up_prev[2] + penalties.open; // delete extend

bool insert_open_b = insert_open > insert_extend;
bool delete_open_b = delete_open > delete_extend;
write_score[0] = insert_open_b ? insert_open : insert_extend;
write_score[2] = delete_open_b ? delete_open : delete_extend;
tbp_t insert_tb = insert_open_b ? (tbp_t) 0 : TB_IMAT;
tbp_t delete_tb = delete_open_b ? (tbp_t) 0 : TB_DMAT;


const type_t match = (local_query_val == local_reference_val) ? diag_prev[1] + penalties.match : diag_prev[1] + penalties.mismatch;

type_t max_value = write_score[0] > write_score[2] ? write_score[0] : write_score[2]; // compare between insertion and deletion
max_value = max_value > match ? max_value : match;
max_value = max_value >= 0 ? max_value : (type_t) 0; // compare with match/mismatch
write_score[1] = max_value;

tbp_t dir_tb;

// Set traceback pointer based on the direction of the maximum score.
if (max_value == write_score[0])
{ // Insert Case
dir_tb = TB_LEFT;
}
else if (max_value == write_score[2])
{
dir_tb = TB_UP;
}
else if (max_value == write_score[1])
{
dir_tb = TB_DIAG;
} else if (max_value == 0){
dir_tb = TB_PH;
}
else
{
// Undefined behavior happens if the max score is non of the I, D, or M.
}

write_traceback = dir_tb + insert_tb + delete_tb;
}

void BSWAffine::InitializeScores(
score_vec_t (&init_col_scr)[MAX_QUERY_LENGTH],
score_vec_t (&init_row_scr)[MAX_REFERENCE_LENGTH],
Penalties penalties)
{
// Utils::Init::ArrSet(init_col_scr, {NINF, 0.0, 0.0}); // qry
// Utils::Init::ArrSet(init_row_scr, {0.0, 0.0, NINF}); // reference layer 0

InitializeColumnScores:
for (int i = 0; i < MAX_QUERY_LENGTH; i++)
{
init_col_scr[i][0] = NINF;
init_col_scr[i][1] = 0.0;
init_col_scr[i][2] = 0.0;
}
InitializeRowScores:
for (int i = 0; i < MAX_REFERENCE_LENGTH; i++)
{
init_row_scr[i][0] = 0.0;
init_row_scr[i][1] = 0.0;
init_row_scr[i][2] = NINF;
}
}

void BSWAffine::UpdatePEMaximum(
const wavefront_scores_inf_t scores,
ScorePack (&max)[PE_NUM],
const idx_t chunk_row_offset, const idx_t wavefront,
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 (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 BSWAffine::InitializeMaxScores(ScorePack (&max)[PE_NUM], idx_t qry_len, idx_t ref_len)
{
for (int i = 0; i < PE_NUM; i++)
{
#pragma HLS unroll
max[i].score = NINF;
max[i].p_col = 0;
max[i].ck = 0;
}
}

void BSWAffine::Traceback::StateMapping(tbp_t tbp, TB_STATE &state, tbr_t &navigation)
{

if (state == TB_STATE::MM)
{
if (tbp(1, 0) == TB_DIAG)
{
navigation = AL_MMI;
}
else if (tbp(1, 0) == TB_UP)
{
state = TB_STATE::DEL;
navigation = AL_NULL;
}
else if (tbp(1, 0) == TB_LEFT)
{
state = TB_STATE::INS;
navigation = AL_NULL;
} else if (tbp(1, 0) == TB_PH){
navigation = AL_END;
}
}
else if (state == TB_STATE::DEL)
{
if ((bool)tbp[3])
{ // deletion extending
// states remains the same.
// printf("delete extend");
}
else
{ // deletion closing
state = TB_STATE::MM; // set the state back to MM
}
navigation = AL_DEL;
}
else if (state == TB_STATE::INS)
{
if ((bool)tbp[2])
{ // insertion extending
// states remains the same.
// ("delete extend");
}
else
{ // insertion closing
state = TB_STATE::MM; // set the state back to MM
}
navigation = AL_INS;
}
else
{
// Placeholder for kernel side debug
}
}

void BSWAffine::Traceback::StateInit(tbp_t tbp, TB_STATE &state)
{
if (tbp(1, 0) == TB_DIAG)
{
state = TB_STATE::MM;
}
else if (tbp(1, 0) == TB_UP)
{
state = TB_STATE::DEL;
}
else if (tbp(1, 0) == TB_LEFT)
{
state = TB_STATE::INS;
}
else
{
// Placeholder for kernel side debug
}
}

// <<< Local Affine Implementation <<<
67 changes: 67 additions & 0 deletions kernels/banding_local_affine_scored/params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef PARAMS_H
#define PARAMS_H

#include <ap_int.h>
#include <ap_fixed.h>
#include <hls_vector.h>

#define MAX_QUERY_LENGTH 256
#define MAX_REFERENCE_LENGTH 256

#define BANDING FIXED
#define BANDWIDTH 64

#define SCORED
#define NO_TRACEBACK

#define PE_NUM 32

#define ALIGN_TYPE BSWAffine
#define N_BLOCKS 8
#define N_LAYERS 3
#define LAYER_MAXIMIUM 1 // We need to indicate from which layer (main matrix) is the maximum score stored.

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

// Define Zero Value
#define zero_fp ((type_t)0)
#define ZERO_CHAR (char_t(0))

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

// Legacy Debugger Configuration
#define DEBUG_OUTPUT_PATH "/home/[email protected]/DP-HLS-Debug/local_affine/"
#define DEBUG_FILENAME "debug_kernel"

#define TB_PH (tbp_t) 0b00
#define TB_LEFT (tbp_t) 0b01
#define TB_DIAG (tbp_t) 0b10
#define TB_UP (tbp_t) 0b11

#define TB_IMAT (tbp_t) 0b0100 // Insertion Matrix
#define TB_DMAT (tbp_t) 0b1000 // Deletion Matrix


struct Penalties {
type_t open;
type_t extend;
type_t mismatch;
type_t match;
type_t linear_gap;
};

enum TB_STATE {
MM = 0, // Match/Mismatch
INS = 1, // Insertion
DEL = 2, // Deletion
END = 3 // End
};


#endif
Loading

0 comments on commit 9cbcb07

Please sign in to comment.