/** * @file * * @author jeffrey.daily@gmail.com * * Copyright (c) 2015 Battelle Memorial Institute. */ #include "config.h" #include #include #define SG_STATS #define SG_SUFFIX _striped_altivec_128_16 #define SG_SUFFIX_PROF _striped_profile_altivec_128_16 #include "sg_helper.h" #include "parasail.h" #include "parasail/memory.h" #include "parasail/internal_altivec.h" #define SWAP(A,B) { vec128i* tmp = A; A = B; B = tmp; } #ifdef PARASAIL_TABLE static inline void arr_store_si128( int *array, vec128i vH, int32_t t, int32_t seglen, int32_t d, int32_t dlen) { array[1LL*(0*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 0); array[1LL*(1*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 1); array[1LL*(2*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 2); array[1LL*(3*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 3); array[1LL*(4*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 4); array[1LL*(5*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 5); array[1LL*(6*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 6); array[1LL*(7*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 7); } #endif #ifdef PARASAIL_ROWCOL static inline void arr_store_col( int *col, vec128i vH, int32_t t, int32_t seglen) { col[0*seglen+t] = (int16_t)_mm_extract_epi16(vH, 0); col[1*seglen+t] = (int16_t)_mm_extract_epi16(vH, 1); col[2*seglen+t] = (int16_t)_mm_extract_epi16(vH, 2); col[3*seglen+t] = (int16_t)_mm_extract_epi16(vH, 3); col[4*seglen+t] = (int16_t)_mm_extract_epi16(vH, 4); col[5*seglen+t] = (int16_t)_mm_extract_epi16(vH, 5); col[6*seglen+t] = (int16_t)_mm_extract_epi16(vH, 6); col[7*seglen+t] = (int16_t)_mm_extract_epi16(vH, 7); } #endif #ifdef PARASAIL_TABLE #define FNAME parasail_sg_flags_stats_table_striped_altivec_128_16 #define PNAME parasail_sg_flags_stats_table_striped_profile_altivec_128_16 #else #ifdef PARASAIL_ROWCOL #define FNAME parasail_sg_flags_stats_rowcol_striped_altivec_128_16 #define PNAME parasail_sg_flags_stats_rowcol_striped_profile_altivec_128_16 #else #define FNAME parasail_sg_flags_stats_striped_altivec_128_16 #define PNAME parasail_sg_flags_stats_striped_profile_altivec_128_16 #endif #endif parasail_result_t* FNAME( const char * const restrict s1, const int s1Len, const char * const restrict s2, const int s2Len, const int open, const int gap, const parasail_matrix_t *matrix, int s1_beg, int s1_end, int s2_beg, int s2_end) { /* declare local variables */ parasail_profile_t *profile = NULL; parasail_result_t *result = NULL; /* validate inputs */ PARASAIL_CHECK_NULL(s2); PARASAIL_CHECK_GT0(s2Len); PARASAIL_CHECK_GE0(open); PARASAIL_CHECK_GE0(gap); PARASAIL_CHECK_NULL(matrix); if (matrix->type == PARASAIL_MATRIX_TYPE_PSSM) { PARASAIL_CHECK_NULL_PSSM_STATS(s1); } else { PARASAIL_CHECK_NULL(s1); PARASAIL_CHECK_GT0(s1Len); } /* initialize local variables */ profile = parasail_profile_create_stats_altivec_128_16(s1, s1Len, matrix); if (!profile) return NULL; result = PNAME(profile, s2, s2Len, open, gap, s1_beg, s1_end, s2_beg, s2_end); parasail_profile_free(profile); return result; } parasail_result_t* PNAME( const parasail_profile_t * const restrict profile, const char * const restrict s2, const int s2Len, const int open, const int gap, int s1_beg, int s1_end, int s2_beg, int s2_end) { /* declare local variables */ int32_t i = 0; int32_t j = 0; int32_t k = 0; int32_t s1Len = 0; int32_t end_query = 0; int32_t end_ref = 0; const parasail_matrix_t *matrix = NULL; int32_t segWidth = 0; int32_t segLen = 0; int32_t offset = 0; int32_t position = 0; vec128i* restrict vProfile = NULL; vec128i* restrict vProfileM = NULL; vec128i* restrict vProfileS = NULL; vec128i* restrict pvHStore = NULL; vec128i* restrict pvHLoad = NULL; vec128i* restrict pvHMStore = NULL; vec128i* restrict pvHMLoad = NULL; vec128i* restrict pvHSStore = NULL; vec128i* restrict pvHSLoad = NULL; vec128i* restrict pvHLStore = NULL; vec128i* restrict pvHLLoad = NULL; vec128i* restrict pvE = NULL; vec128i* restrict pvEM = NULL; vec128i* restrict pvES = NULL; vec128i* restrict pvEL = NULL; int16_t* restrict boundary = NULL; vec128i vGapO; vec128i vGapE; int16_t NEG_LIMIT = 0; int16_t POS_LIMIT = 0; vec128i vZero; vec128i vOne; int16_t score = 0; int16_t matches = 0; int16_t similar = 0; int16_t length = 0; vec128i vNegLimit; vec128i vPosLimit; vec128i vSaturationCheckMin; vec128i vSaturationCheckMax; vec128i vMaxH; vec128i vMaxHM; vec128i vMaxHS; vec128i vMaxHL; vec128i vPosMask; parasail_result_t *result = NULL; /* validate inputs */ PARASAIL_CHECK_NULL(profile); PARASAIL_CHECK_NULL(profile->profile16.score); PARASAIL_CHECK_NULL(profile->matrix); PARASAIL_CHECK_GT0(profile->s1Len); PARASAIL_CHECK_NULL(s2); PARASAIL_CHECK_GT0(s2Len); PARASAIL_CHECK_GE0(open); PARASAIL_CHECK_GE0(gap); /* initialize stack variables */ i = 0; j = 0; k = 0; s1Len = profile->s1Len; end_query = s1Len-1; end_ref = s2Len-1; matrix = profile->matrix; segWidth = 8; /* number of values in vector unit */ segLen = (s1Len + segWidth - 1) / segWidth; offset = (s1Len - 1) % segLen; position = (segWidth - 1) - (s1Len - 1) / segLen; vProfile = (vec128i*)profile->profile16.score; vProfileM = (vec128i*)profile->profile16.matches; vProfileS = (vec128i*)profile->profile16.similar; vGapO = _mm_set1_epi16(open); vGapE = _mm_set1_epi16(gap); NEG_LIMIT = (-open < matrix->min ? INT16_MIN + open : INT16_MIN - matrix->min) + 1; POS_LIMIT = INT16_MAX - matrix->max - 1; vZero = _mm_setzero_si128(); vOne = _mm_set1_epi16(1); score = NEG_LIMIT; matches = 0; similar = 0; length = 0; vNegLimit = _mm_set1_epi16(NEG_LIMIT); vPosLimit = _mm_set1_epi16(POS_LIMIT); vSaturationCheckMin = vPosLimit; vSaturationCheckMax = vNegLimit; vMaxH = vNegLimit; vMaxHM = vNegLimit; vMaxHS = vNegLimit; vMaxHL = vNegLimit; vPosMask = _mm_cmpeq_epi16(_mm_set1_epi16(position), _mm_set_epi16(0,1,2,3,4,5,6,7)); /* initialize result */ #ifdef PARASAIL_TABLE result = parasail_result_new_table3(segLen*segWidth, s2Len); #else #ifdef PARASAIL_ROWCOL result = parasail_result_new_rowcol3(segLen*segWidth, s2Len); #else result = parasail_result_new_stats(); #endif #endif if (!result) return NULL; /* set known flags */ result->flag |= PARASAIL_FLAG_SG | PARASAIL_FLAG_STRIPED | PARASAIL_FLAG_STATS | PARASAIL_FLAG_BITS_16 | PARASAIL_FLAG_LANES_8; 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 */ pvHStore = parasail_memalign_vec128i(16, segLen); pvHLoad = parasail_memalign_vec128i(16, segLen); pvHMStore = parasail_memalign_vec128i(16, segLen); pvHMLoad = parasail_memalign_vec128i(16, segLen); pvHSStore = parasail_memalign_vec128i(16, segLen); pvHSLoad = parasail_memalign_vec128i(16, segLen); pvHLStore = parasail_memalign_vec128i(16, segLen); pvHLLoad = parasail_memalign_vec128i(16, segLen); pvE = parasail_memalign_vec128i(16, segLen); pvEM = parasail_memalign_vec128i(16, segLen); pvES = parasail_memalign_vec128i(16, segLen); pvEL = parasail_memalign_vec128i(16, segLen); boundary = parasail_memalign_int16_t(16, s2Len+1); /* validate heap variables */ if (!pvHStore) return NULL; if (!pvHLoad) return NULL; if (!pvHMStore) return NULL; if (!pvHMLoad) return NULL; if (!pvHSStore) return NULL; if (!pvHSLoad) return NULL; if (!pvHLStore) return NULL; if (!pvHLLoad) return NULL; if (!pvE) return NULL; if (!pvEM) return NULL; if (!pvES) return NULL; if (!pvEL) return NULL; if (!boundary) return NULL; parasail_memset_vec128i(pvHMStore, vZero, segLen); parasail_memset_vec128i(pvHSStore, vZero, segLen); parasail_memset_vec128i(pvHLStore, vZero, segLen); parasail_memset_vec128i(pvEM, vZero, segLen); parasail_memset_vec128i(pvES, vZero, segLen); parasail_memset_vec128i(pvEL, vOne, segLen); /* initialize H and E */ { int32_t index = 0; for (i=0; imapper[(unsigned char)s2[j]] * segLen; vPM = vProfileM + matrix->mapper[(unsigned char)s2[j]] * segLen; vPS = vProfileS + matrix->mapper[(unsigned char)s2[j]] * segLen; /* Swap the 2 H buffers. */ SWAP(pvHLoad, pvHStore) SWAP(pvHMLoad, pvHMStore) SWAP(pvHSLoad, pvHSStore) SWAP(pvHLLoad, pvHLStore) /* inner loop to process the query sequence */ for (i=0; istats->tables->matches_table, vHM, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->similar_table, vHS, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->length_table, vHL, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->score_table, vH, i, segLen, j, s2Len); #endif vEF_opn = _mm_subs_epi16(vH, vGapO); /* Update vE value. */ vE_ext = _mm_subs_epi16(vE, vGapE); vE = _mm_max_epi16(vEF_opn, vE_ext); case1 = _mm_cmpgt_epi16(vEF_opn, vE_ext); vEM = _mm_blendv_epi8(vEM, vHM, case1); vES = _mm_blendv_epi8(vES, vHS, case1); vEL = _mm_blendv_epi8( _mm_adds_epi16(vEL, vOne), _mm_adds_epi16(vHL, vOne), case1); _mm_store_si128(pvE + i, vE); _mm_store_si128(pvEM + i, vEM); _mm_store_si128(pvES + i, vES); _mm_store_si128(pvEL + i, vEL); /* Update vF value. */ vF_ext = _mm_subs_epi16(vF, vGapE); vF = _mm_max_epi16(vEF_opn, vF_ext); case1 = _mm_cmpgt_epi16(vEF_opn, vF_ext); vFM = _mm_blendv_epi8(vFM, vHM, case1); vFS = _mm_blendv_epi8(vFS, vHS, case1); vFL = _mm_blendv_epi8( _mm_adds_epi16(vFL, vOne), _mm_adds_epi16(vHL, vOne), case1); /* Load the next vH. */ vH = _mm_load_si128(pvHLoad + i); vHM = _mm_load_si128(pvHMLoad + i); vHS = _mm_load_si128(pvHSLoad + i); vHL = _mm_load_si128(pvHLLoad + i); } /* Lazy_F loop: has been revised to disallow adjecent insertion and * then deletion, so don't update E(i, i), learn from SWPS3 */ for (k=0; kstats->tables->matches_table, vHM, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->similar_table, vHS, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->length_table, vHL, i, segLen, j, s2Len); arr_store_si128(result->stats->tables->score_table, vH, i, segLen, j, s2Len); #endif /* Update vF value. */ vEF_opn = _mm_subs_epi16(vH, vGapO); vF_ext = _mm_subs_epi16(vF, vGapE); if (! _mm_movemask_epi8( _mm_or_si128( _mm_cmpgt_epi16(vF_ext, vEF_opn), _mm_cmpeq_epi16(vF_ext, vEF_opn)))) goto end; /*vF = _mm_max_epi16(vEF_opn, vF_ext);*/ vF = vF_ext; cond = _mm_cmpgt_epi16(vEF_opn, vF_ext); vFM = _mm_blendv_epi8(vFM, vHM, cond); vFS = _mm_blendv_epi8(vFS, vHS, cond); vFL = _mm_blendv_epi8( _mm_adds_epi16(vFL, vOne), _mm_adds_epi16(vHL, vOne), cond); vHp = _mm_load_si128(pvHLoad + i); } } end: { } /* extract vector containing last value from the column */ { vec128i cond_max; vH = _mm_load_si128(pvHStore + offset); vHM = _mm_load_si128(pvHMStore + offset); vHS = _mm_load_si128(pvHSStore + offset); vHL = _mm_load_si128(pvHLStore + offset); cond_max = _mm_cmpgt_epi16(vH, vMaxH); vMaxH = _mm_blendv_epi8(vMaxH, vH, cond_max); vMaxHM = _mm_blendv_epi8(vMaxHM, vHM, cond_max); vMaxHS = _mm_blendv_epi8(vMaxHS, vHS, cond_max); vMaxHL = _mm_blendv_epi8(vMaxHL, vHL, cond_max); if (_mm_movemask_epi8(_mm_and_si128(vPosMask, cond_max))) { end_ref = j; } } #ifdef PARASAIL_ROWCOL for (k=0; kstats->rowcols->score_row[j] = (int16_t) _mm_extract_epi16 (vH, 7); result->stats->rowcols->matches_row[j] = (int16_t) _mm_extract_epi16 (vHM, 7); result->stats->rowcols->similar_row[j] = (int16_t) _mm_extract_epi16 (vHS, 7); result->stats->rowcols->length_row[j] = (int16_t) _mm_extract_epi16 (vHL, 7); #endif } #ifdef PARASAIL_ROWCOL for (i=0; istats->rowcols->score_col, vH, i, segLen); arr_store_col(result->stats->rowcols->matches_col, vHM, i, segLen); arr_store_col(result->stats->rowcols->similar_col, vHS, i, segLen); arr_store_col(result->stats->rowcols->length_col, vHL, i, segLen); } #endif /* extract last value from the column maximums */ if (s2_end) { for (k=0; k= s1Len) continue; if (*t > score) { score = *t; end_query = temp; end_ref = s2Len-1; matches = *m; similar = *s; length = *l; } else if (*t == score && end_ref == s2Len-1 && temp < end_query) { end_query = temp; matches = *m; similar = *s; length = *l; } } } /* extract last value from the last column */ if (!s1_end && !s2_end) { vec128i vH = _mm_load_si128(pvHStore + offset); vec128i vHM = _mm_load_si128(pvHMStore + offset); vec128i vHS = _mm_load_si128(pvHSStore + offset); vec128i vHL = _mm_load_si128(pvHLStore + offset); for (k=0; kflag |= 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(boundary); parasail_free(pvEL); parasail_free(pvES); parasail_free(pvEM); parasail_free(pvE); parasail_free(pvHLLoad); parasail_free(pvHLStore); parasail_free(pvHSLoad); parasail_free(pvHSStore); parasail_free(pvHMLoad); parasail_free(pvHMStore); parasail_free(pvHLoad); parasail_free(pvHStore); return result; } SG_IMPL_ALL SG_IMPL_PROF_ALL