/** * @file * * @author jeffrey.daily@gmail.com * * Copyright (c) 2015 Battelle Memorial Institute. */ #include "config.h" #include #include #include "parasail.h" #include "parasail/memory.h" #include "parasail/internal_avx.h" #define SG_STATS #define SG_SUFFIX _diag_avx2_256_64 #include "sg_helper.h" #if HAVE_AVX2_MM256_INSERT_EPI64 #define _mm256_insert_epi64_rpl _mm256_insert_epi64 #else static inline __m256i _mm256_insert_epi64_rpl(__m256i a, int64_t i, int imm) { __m256i_64_t A; A.m = a; A.v[imm] = i; return A.m; } #endif #if HAVE_AVX2_MM256_SET1_EPI64X #define _mm256_set1_epi64x_rpl _mm256_set1_epi64x #else static inline __m256i _mm256_set1_epi64x_rpl(int64_t i) { __m256i_64_t A; A.v[0] = i; A.v[1] = i; A.v[2] = i; A.v[3] = i; return A.m; } #endif static inline __m256i _mm256_max_epi64_rpl(__m256i a, __m256i b) { __m256i_64_t A; __m256i_64_t B; A.m = a; B.m = b; A.v[0] = (A.v[0]>B.v[0]) ? A.v[0] : B.v[0]; A.v[1] = (A.v[1]>B.v[1]) ? A.v[1] : B.v[1]; A.v[2] = (A.v[2]>B.v[2]) ? A.v[2] : B.v[2]; A.v[3] = (A.v[3]>B.v[3]) ? A.v[3] : B.v[3]; return A.m; } #if HAVE_AVX2_MM256_SET_EPI64X #define _mm256_set_epi64x_rpl _mm256_set_epi64x #else static inline __m256i _mm256_set_epi64x_rpl(int64_t e3, int64_t e2, int64_t e1, int64_t e0) { __m256i_64_t A; A.v[0] = e0; A.v[1] = e1; A.v[2] = e2; A.v[3] = e3; return A.m; } #endif #if HAVE_AVX2_MM256_EXTRACT_EPI64 #define _mm256_extract_epi64_rpl _mm256_extract_epi64 #else static inline int64_t _mm256_extract_epi64_rpl(__m256i a, int imm) { __m256i_64_t A; A.m = a; return A.v[imm]; } #endif static inline __m256i _mm256_min_epi64_rpl(__m256i a, __m256i b) { __m256i_64_t A; __m256i_64_t B; A.m = a; B.m = b; A.v[0] = (A.v[0]type == PARASAIL_MATRIX_TYPE_PSSM) { PARASAIL_CHECK_NULL_PSSM_STATS(_s1); } else { PARASAIL_CHECK_NULL(_s1); PARASAIL_CHECK_GT0(_s1Len); } /* initialize stack variables */ N = 4; /* number of values in vector */ PAD = N-1; PAD2 = PAD*2; s1Len = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? _s1Len : matrix->length; s1Len_PAD = s1Len+PAD; s2Len_PAD = s2Len+PAD; i = 0; j = 0; end_query = s1Len-1; end_ref = s2Len-1; NEG_LIMIT = (-open < matrix->min ? INT64_MIN + open : INT64_MIN - matrix->min) + 1; POS_LIMIT = INT64_MAX - matrix->max - 1; score = NEG_LIMIT; matches = NEG_LIMIT; similar = NEG_LIMIT; length = NEG_LIMIT; vNegLimit = _mm256_set1_epi64x_rpl(NEG_LIMIT); vPosLimit = _mm256_set1_epi64x_rpl(POS_LIMIT); vSaturationCheckMin = vPosLimit; vSaturationCheckMax = vNegLimit; vNegInf = _mm256_set1_epi64x_rpl(NEG_LIMIT); vOpen = _mm256_set1_epi64x_rpl(open); vGap = _mm256_set1_epi64x_rpl(gap); vZero = _mm256_set1_epi64x_rpl(0); vOne = _mm256_set1_epi64x_rpl(1); vN = _mm256_set1_epi64x_rpl(N); vGapN = s1_beg ? _mm256_set1_epi64x_rpl(0) : _mm256_set1_epi64x_rpl(gap*N); vNegOne = _mm256_set1_epi64x_rpl(-1); vI = _mm256_set_epi64x_rpl(0,1,2,3); vJreset = _mm256_set_epi64x_rpl(0,-1,-2,-3); vMaxHRow = vNegInf; vMaxMRow = vNegInf; vMaxSRow = vNegInf; vMaxLRow = vNegInf; vMaxHCol = vNegInf; vMaxMCol = vNegInf; vMaxSCol = vNegInf; vMaxLCol = vNegInf; vLastValH = vNegInf; vLastValM = vNegInf; vLastValS = vNegInf; vLastValL = vNegInf; vEndI = vNegInf; vEndJ = vNegInf; vILimit = _mm256_set1_epi64x_rpl(s1Len); vILimit1 = _mm256_sub_epi64(vILimit, vOne); vJLimit = _mm256_set1_epi64x_rpl(s2Len); vJLimit1 = _mm256_sub_epi64(vJLimit, vOne); vIBoundary = s1_beg ? _mm256_set1_epi64x_rpl(0) : _mm256_set_epi64x_rpl( -open-0*gap, -open-1*gap, -open-2*gap, -open-3*gap); /* initialize result */ #ifdef PARASAIL_TABLE result = parasail_result_new_table3(s1Len, s2Len); #else #ifdef PARASAIL_ROWCOL result = parasail_result_new_rowcol3(s1Len, s2Len); #else result = parasail_result_new_stats(); #endif #endif if (!result) return NULL; /* set known flags */ result->flag |= PARASAIL_FLAG_SG | PARASAIL_FLAG_DIAG | PARASAIL_FLAG_STATS | PARASAIL_FLAG_BITS_64 | PARASAIL_FLAG_LANES_4; result->flag |= s1_beg ? PARASAIL_FLAG_SG_S1_BEG : 0; result->flag |= s1_end ? PARASAIL_FLAG_SG_S1_END : 0; result->flag |= s2_beg ? PARASAIL_FLAG_SG_S2_BEG : 0; result->flag |= s2_end ? PARASAIL_FLAG_SG_S2_END : 0; #ifdef PARASAIL_TABLE result->flag |= PARASAIL_FLAG_TABLE; #endif #ifdef PARASAIL_ROWCOL result->flag |= PARASAIL_FLAG_ROWCOL; #endif /* initialize heap variables */ s1 = parasail_memalign_int64_t(32, s1Len+PAD); s2B = parasail_memalign_int64_t(32, s2Len+PAD2); _H_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _HM_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _HS_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _HL_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _F_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _FM_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _FS_pr = parasail_memalign_int64_t(32, s2Len+PAD2); _FL_pr = parasail_memalign_int64_t(32, s2Len+PAD2); s2 = s2B+PAD; /* will allow later for negative indices */ H_pr = _H_pr+PAD; HM_pr = _HM_pr+PAD; HS_pr = _HS_pr+PAD; HL_pr = _HL_pr+PAD; F_pr = _F_pr+PAD; FM_pr = _FM_pr+PAD; FS_pr = _FS_pr+PAD; FL_pr = _FL_pr+PAD; /* validate heap variables */ if (!s1) return NULL; if (!s2B) return NULL; if (!_H_pr) return NULL; if (!_HM_pr) return NULL; if (!_HS_pr) return NULL; if (!_HL_pr) return NULL; if (!_F_pr) return NULL; if (!_FM_pr) return NULL; if (!_FS_pr) return NULL; if (!_FL_pr) return NULL; /* convert _s1 from char to int in range 0-23 */ for (i=0; imapper[(unsigned char)_s1[i]]; } /* pad back of s1 with dummy values */ for (i=s1Len; imapper[(unsigned char)_s2[j]]; } /* pad front of s2 with dummy values */ for (j=-PAD; j<0; ++j) { s2[j] = 0; /* point to first matrix row because we don't care */ } /* pad back of s2 with dummy values */ for (j=s2Len; jmatrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+0] : ((i+0 >= s1Len) ? s1Len-1 : i+0))]; const int * const restrict matrow1 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+1] : ((i+1 >= s1Len) ? s1Len-1 : i+1))]; const int * const restrict matrow2 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+2] : ((i+2 >= s1Len) ? s1Len-1 : i+2))]; const int * const restrict matrow3 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+3] : ((i+3 >= s1Len) ? s1Len-1 : i+3))]; __m256i vIltLimit = _mm256_cmplt_epi64_rpl(vI, vILimit); __m256i vIeqLimit1 = _mm256_cmpeq_epi64(vI, vILimit1); vNH = _mm256_srli_si256_rpl(vNH, 8); vNH = _mm256_insert_epi64_rpl(vNH, H_pr[-1], 3); vWH = _mm256_srli_si256_rpl(vWH, 8); vWH = _mm256_insert_epi64_rpl(vWH, s1_beg ? 0 : (-open - i*gap), 3); H_pr[-1] = -open - (i+N)*gap; /* iterate over database sequence */ for (j=0; j PAD) { vSaturationCheckMin = _mm256_min_epi64_rpl(vSaturationCheckMin, vWH); vSaturationCheckMax = _mm256_max_epi64_rpl(vSaturationCheckMax, vWH); vSaturationCheckMax = _mm256_max_epi64_rpl(vSaturationCheckMax, vWM); vSaturationCheckMax = _mm256_max_epi64_rpl(vSaturationCheckMax, vWS); vSaturationCheckMax = _mm256_max_epi64_rpl(vSaturationCheckMax, vWL); vSaturationCheckMax = _mm256_max_epi64_rpl(vSaturationCheckMax, vWL); } #ifdef PARASAIL_TABLE arr_store_si256(result->stats->tables->score_table, vWH, i, s1Len, j, s2Len); arr_store_si256(result->stats->tables->matches_table, vWM, i, s1Len, j, s2Len); arr_store_si256(result->stats->tables->similar_table, vWS, i, s1Len, j, s2Len); arr_store_si256(result->stats->tables->length_table, vWL, i, s1Len, j, s2Len); #endif #ifdef PARASAIL_ROWCOL arr_store_rowcol(result->stats->rowcols->score_row, result->stats->rowcols->score_col, vWH, i, s1Len, j, s2Len); arr_store_rowcol(result->stats->rowcols->matches_row, result->stats->rowcols->matches_col, vWM, i, s1Len, j, s2Len); arr_store_rowcol(result->stats->rowcols->similar_row, result->stats->rowcols->similar_col, vWS, i, s1Len, j, s2Len); arr_store_rowcol(result->stats->rowcols->length_row, result->stats->rowcols->length_col, vWL, i, s1Len, j, s2Len); #endif H_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vWH,0); HM_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vWM,0); HS_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vWS,0); HL_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vWL,0); F_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vF,0); FM_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vFM,0); FS_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vFS,0); FL_pr[j-3] = (int64_t)_mm256_extract_epi64_rpl(vFL,0); /* as minor diagonal vector passes across the i or j limit * boundary, extract the last value of the column or row */ { __m256i vJeqLimit1 = _mm256_cmpeq_epi64(vJ, vJLimit1); __m256i vJgtNegOne = _mm256_cmpgt_epi64(vJ, vNegOne); __m256i vJltLimit = _mm256_cmplt_epi64_rpl(vJ, vJLimit); __m256i cond_j = _mm256_and_si256(vIltLimit, vJeqLimit1); __m256i cond_i = _mm256_and_si256(vIeqLimit1, _mm256_and_si256(vJgtNegOne, vJltLimit)); __m256i cond_max_row = _mm256_cmpgt_epi64(vWH, vMaxHRow); __m256i cond_max_col = _mm256_cmpgt_epi64(vWH, vMaxHCol); __m256i cond_last_val = _mm256_and_si256(vIeqLimit1, vJeqLimit1); __m256i cond_all_row = _mm256_and_si256(cond_max_row, cond_i); __m256i cond_all_col = _mm256_and_si256(cond_max_col, cond_j); vMaxHRow = _mm256_blendv_epi8(vMaxHRow, vWH, cond_all_row); vMaxMRow = _mm256_blendv_epi8(vMaxMRow, vWM, cond_all_row); vMaxSRow = _mm256_blendv_epi8(vMaxSRow, vWS, cond_all_row); vMaxLRow = _mm256_blendv_epi8(vMaxLRow, vWL, cond_all_row); vMaxHCol = _mm256_blendv_epi8(vMaxHCol, vWH, cond_all_col); vMaxMCol = _mm256_blendv_epi8(vMaxMCol, vWM, cond_all_col); vMaxSCol = _mm256_blendv_epi8(vMaxSCol, vWS, cond_all_col); vMaxLCol = _mm256_blendv_epi8(vMaxLCol, vWL, cond_all_col); vLastValH = _mm256_blendv_epi8(vLastValH, vWH, cond_last_val); vLastValM = _mm256_blendv_epi8(vLastValM, vWM, cond_last_val); vLastValS = _mm256_blendv_epi8(vLastValS, vWS, cond_last_val); vLastValL = _mm256_blendv_epi8(vLastValL, vWL, cond_last_val); vEndI = _mm256_blendv_epi8(vEndI, vI, cond_all_col); vEndJ = _mm256_blendv_epi8(vEndJ, vJ, cond_all_row); } vJ = _mm256_add_epi64(vJ, vOne); } vI = _mm256_add_epi64(vI, vN); vIBoundary = _mm256_sub_epi64(vIBoundary, vGapN); } /* alignment ending position */ { int64_t max_rowh = NEG_LIMIT; int64_t max_rowm = NEG_LIMIT; int64_t max_rows = NEG_LIMIT; int64_t max_rowl = NEG_LIMIT; int64_t max_colh = NEG_LIMIT; int64_t max_colm = NEG_LIMIT; int64_t max_cols = NEG_LIMIT; int64_t max_coll = NEG_LIMIT; int64_t last_valh = NEG_LIMIT; int64_t last_valm = NEG_LIMIT; int64_t last_vals = NEG_LIMIT; int64_t last_vall = NEG_LIMIT; int64_t *rh = (int64_t*)&vMaxHRow; int64_t *rm = (int64_t*)&vMaxMRow; int64_t *rs = (int64_t*)&vMaxSRow; int64_t *rl = (int64_t*)&vMaxLRow; int64_t *ch = (int64_t*)&vMaxHCol; int64_t *cm = (int64_t*)&vMaxMCol; int64_t *cs = (int64_t*)&vMaxSCol; int64_t *cl = (int64_t*)&vMaxLCol; int64_t *lh = (int64_t*)&vLastValH; int64_t *lm = (int64_t*)&vLastValM; int64_t *ls = (int64_t*)&vLastValS; int64_t *ll = (int64_t*)&vLastValL; int64_t *i = (int64_t*)&vEndI; int64_t *j = (int64_t*)&vEndJ; int32_t k; for (k=0; k max_colh || (*ch == max_colh && *i < end_query)) { max_colh = *ch; end_query = *i; max_colm = *cm; max_cols = *cs; max_coll = *cl; } if (*rh > max_rowh) { max_rowh = *rh; end_ref = *j; max_rowm = *rm; max_rows = *rs; max_rowl = *rl; } if (*lh > last_valh) { last_valh = *lh; last_valm = *lm; last_vals = *ls; last_vall = *ll; } } if (s1_end && s2_end) { if (max_colh > max_rowh || (max_colh == max_rowh && end_ref == s2Len-1)) { score = max_colh; end_ref = s2Len-1; matches = max_colm; similar = max_cols; length = max_coll; } else { score = max_rowh; end_query = s1Len-1; matches = max_rowm; similar = max_rows; length = max_rowl; } } else if (s1_end) { score = max_colh; end_ref = s2Len-1; matches = max_colm; similar = max_cols; length = max_coll; } else if (s2_end) { score = max_rowh; end_query = s1Len-1; matches = max_rowm; similar = max_rows; length = max_rowl; } else { score = last_valh; end_query = s1Len-1; end_ref = s2Len-1; matches = last_valm; similar = last_vals; length = last_vall; } } if (_mm256_movemask_epi8(_mm256_or_si256( _mm256_cmplt_epi64_rpl(vSaturationCheckMin, vNegLimit), _mm256_cmpgt_epi64(vSaturationCheckMax, vPosLimit)))) { result->flag |= PARASAIL_FLAG_SATURATED; score = 0; matches = 0; similar = 0; length = 0; end_query = 0; end_ref = 0; } result->score = score; result->end_query = end_query; result->end_ref = end_ref; result->stats->matches = matches; result->stats->similar = similar; result->stats->length = length; parasail_free(_FL_pr); parasail_free(_FS_pr); parasail_free(_FM_pr); parasail_free(_F_pr); parasail_free(_HL_pr); parasail_free(_HS_pr); parasail_free(_HM_pr); parasail_free(_H_pr); parasail_free(s2B); parasail_free(s1); return result; } SG_IMPL_ALL