diff --git a/.gitignore b/.gitignore index 3090a33..e8b8ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.[oa] +*.txt +*.fa sw_perf sw_perf_discrete test diff --git a/Makefile b/Makefile index 5044fac..c9b3a6f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC= gcc -#CFLAGS= -g -Wall -Wno-unused-function -CFLAGS= -Wall -Wno-unused-function -O2 -mavx2 +CFLAGS= -g -Wall -Wno-unused-function -mavx2 +#CFLAGS= -Wall -Wno-unused-function -O2 -mavx2 DFLAGS= -DSHOW_PERF OBJS= ksw_normal.o ksw_avx2.o ksw_cuda.o ksw_avx2_u8.o PROG= sw_perf diff --git a/bsw_avx2.c b/bsw_avx2.c new file mode 100644 index 0000000..1054f7c --- /dev/null +++ b/bsw_avx2.c @@ -0,0 +1,425 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +#define LIKELY(x) __builtin_expect((x), 1) +#define UNLIKELY(x) __builtin_expect((x), 0) +#else +#define LIKELY(x) (x) +#define UNLIKELY(x) (x) +#endif + +#undef MAX +#undef MIN +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define SIMD_WIDTH 16 + +static const uint16_t h_vec_int_mask[SIMD_WIDTH][SIMD_WIDTH] = { + {0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0}, + {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}}; + +// const int permute_mask = _MM_SHUFFLE(0, 1, 2, 3); +#define permute_mask _MM_SHUFFLE(0, 1, 2, 3) +// 初始化变量 +#define SIMD_INIT \ + int oe_del = o_del + e_del, oe_ins = o_ins + e_ins; \ + __m256i zero_vec; \ + __m256i max_vec; \ + __m256i oe_del_vec; \ + __m256i oe_ins_vec; \ + __m256i e_del_vec; \ + __m256i e_ins_vec; \ + __m256i h_vec_mask[SIMD_WIDTH]; \ + zero_vec = _mm256_setzero_si256(); \ + oe_del_vec = _mm256_set1_epi16(-oe_del); \ + oe_ins_vec = _mm256_set1_epi16(-oe_ins); \ + e_del_vec = _mm256_set1_epi16(-e_del); \ + e_ins_vec = _mm256_set1_epi16(-e_ins); \ + __m256i match_sc_vec = _mm256_set1_epi16(a); \ + __m256i mis_sc_vec = _mm256_set1_epi16(-b); \ + __m256i amb_sc_vec = _mm256_set1_epi16(-1); \ + __m256i amb_vec = _mm256_set1_epi16(4); \ + for (i = 0; i < SIMD_WIDTH; ++i) \ + h_vec_mask[i] = _mm256_loadu_si256((__m256i *)(&h_vec_int_mask[i])); + +/* + * e 表示当前ref的碱基被删除 + * f 表示当前seq的碱基插入 + * m 表示当前碱基匹配(可以相等,也可以不想等) + * h 表示最大值 + */ +// load向量化数据 +#define SIMD_LOAD \ + __m256i m1 = _mm256_loadu_si256((__m256i *)(&mA1[j])); \ + __m256i e1 = _mm256_loadu_si256((__m256i *)(&eA1[j])); \ + __m256i m1j1 = _mm256_loadu_si256((__m256i *)(&mA1[j - 1])); \ + __m256i f1j1 = _mm256_loadu_si256((__m256i *)(&fA1[j - 1])); \ + __m256i h0j1 = _mm256_loadu_si256((__m256i *)(&hA0[j - 1])); \ + __m256i qs_vec = _mm256_loadu_si256((__m256i *)(&seq[j - 1])); \ + __m256i ts_vec = _mm256_loadu_si256((__m256i *)(&ref[i])); + +// 比对ref和seq的序列,计算罚分 +#define SIMD_CMP_SEQ \ + ts_vec = _mm256_permute4x64_epi64(ts_vec, permute_mask); \ + ts_vec = _mm256_shufflelo_epi16(ts_vec, permute_mask); \ + ts_vec = _mm256_shufflehi_epi16(ts_vec, permute_mask); \ + __m256i match_mask_vec = _mm256_cmpeq_epi16(qs_vec, ts_vec); \ + __m256i mis_score_vec = _mm256_andnot_si256(match_mask_vec, mis_sc_vec); \ + __m256i score_vec = _mm256_and_si256(match_sc_vec, match_mask_vec); \ + score_vec = _mm256_or_si256(score_vec, mis_score_vec); \ + __m256i q_amb_mask_vec = _mm256_cmpeq_epi16(qs_vec, amb_vec); \ + __m256i t_amb_mask_vec = _mm256_cmpeq_epi16(ts_vec, amb_vec); \ + __m256i amb_mask_vec = _mm256_or_si256(q_amb_mask_vec, t_amb_mask_vec); \ + score_vec = _mm256_andnot_si256(amb_mask_vec, score_vec); \ + __m256i amb_score_vec = _mm256_and_si256(amb_mask_vec, amb_sc_vec); \ + score_vec = _mm256_or_si256(score_vec, amb_score_vec); + +// 向量化计算h, e, f, m +#define SIMD_COMPUTE \ + __m256i en_vec0 = _mm256_add_epi16(m1, oe_del_vec); \ + __m256i en_vec1 = _mm256_add_epi16(e1, e_del_vec); \ + __m256i en_vec = _mm256_max_epi16(en_vec0, en_vec1); \ + __m256i fn_vec0 = _mm256_add_epi16(m1j1, oe_ins_vec); \ + __m256i fn_vec1 = _mm256_add_epi16(f1j1, e_ins_vec); \ + __m256i fn_vec = _mm256_max_epi16(fn_vec0, fn_vec1); \ + __m256i mn_vec0 = _mm256_add_epi16(h0j1, score_vec); \ + __m256i mn_mask = _mm256_cmpgt_epi16(h0j1, zero_vec); \ + __m256i mn_vec = _mm256_and_si256(mn_vec0, mn_mask); \ + __m256i hn_vec0 = _mm256_max_epi16(en_vec, fn_vec); \ + __m256i hn_vec = _mm256_max_epi16(hn_vec0, mn_vec); \ + en_vec = _mm256_max_epi16(en_vec, zero_vec); \ + fn_vec = _mm256_max_epi16(fn_vec, zero_vec); \ + mn_vec = _mm256_max_epi16(mn_vec, zero_vec); \ + hn_vec = _mm256_max_epi16(hn_vec, zero_vec); + +// 存储向量化结果 +#define SIMD_STORE \ + max_vec = _mm256_max_epi16(max_vec, hn_vec); \ + _mm256_storeu_si256((__m256i *)&eA2[j], en_vec); \ + _mm256_storeu_si256((__m256i *)&fA2[j], fn_vec); \ + _mm256_storeu_si256((__m256i *)&mA2[j], mn_vec); \ + _mm256_storeu_si256((__m256i *)&hA2[j], hn_vec); + +// 去除多余的部分 +#define SIMD_REMOVE_EXTRA \ + en_vec = _mm256_and_si256(en_vec, h_vec_mask[end - j]); \ + fn_vec = _mm256_and_si256(fn_vec, h_vec_mask[end - j]); \ + mn_vec = _mm256_and_si256(mn_vec, h_vec_mask[end - j]); \ + hn_vec = _mm256_and_si256(hn_vec, h_vec_mask[end - j]); + +// 找最大值和位置 +#define SIMD_FIND_MAX \ + max_vec = _mm256_max_epu16(max_vec, _mm256_alignr_epi8(max_vec, max_vec, 2)); \ + max_vec = _mm256_max_epu16(max_vec, _mm256_alignr_epi8(max_vec, max_vec, 4)); \ + max_vec = _mm256_max_epu16(max_vec, _mm256_alignr_epi8(max_vec, max_vec, 6)); \ + max_vec = _mm256_max_epu16(max_vec, _mm256_alignr_epi8(max_vec, max_vec, 8)); \ + max_vec = _mm256_max_epu16(max_vec, _mm256_permute2x128_si256(max_vec, max_vec, 0x01)); \ + int16_t *maxVal = (int16_t *)&max_vec; \ + m = maxVal[0]; \ + if (m > 0) \ + { \ + for (j = beg, i = iend; j <= end; j += SIMD_WIDTH, i -= SIMD_WIDTH) \ + { \ + __m256i h2_vec = _mm256_loadu_si256((__m256i *)(&hA2[j])); \ + __m256i vcmp = _mm256_cmpeq_epi16(h2_vec, max_vec); \ + uint32_t mask = _mm256_movemask_epi8(vcmp); \ + if (mask > 0) \ + { \ + int pos = SIMD_WIDTH - 1 - ((__builtin_clz(mask)) >> 1); \ + mj = j - 1 + pos; \ + mi = i - 1 - pos; \ + } \ + } \ + } + +// 每轮迭代后,交换数组 +#define SWAP_DATA_POINTER \ + int16_t *tmp = hA0; \ + hA0 = hA1; \ + hA1 = hA2; \ + hA2 = tmp; \ + tmp = eA1; \ + eA1 = eA2; \ + eA2 = tmp; \ + tmp = fA1; \ + fA1 = fA2; \ + fA2 = tmp; \ + tmp = mA1; \ + mA1 = mA2; \ + mA2 = tmp; + +int bsw_avx2(int qlen, // query length 待匹配段碱基的query长度 + const uint8_t *query, // read碱基序列 + int tlen, // target length reference的长度 + const uint8_t *target, // reference序列 + int is_left, // 是不是向左扩展 + int m, // 碱基种类 (5) + const int8_t *mat, // 每个位置的query和target的匹配得分 m*m + int o_del, // deletion 错配开始的惩罚系数 + int e_del, // deletion extension的惩罚系数 + int o_ins, // insertion 错配开始的惩罚系数 + int e_ins, // insertion extension的惩罚系数SIMD_BTYES + int a, // 碱基match时的分数 + int b, // 碱基mismatch时的惩罚分数(正数) + int w, // 提前剪枝系数,w =100 匹配位置和beg的最大距离 + int end_bonus, // 如果query比对到了最后一个字符,额外奖励分值 + int zdrop, // 没匹配上的太多(max-(m+ins or del score)),退出后续匹配 + int h0, // 该seed的初始得分(完全匹配query的碱基数) + int *_qle, // 匹配得到全局最大得分的碱基在query的位置 + int *_tle, // 匹配得到全局最大得分的碱基在reference的位置 + int *_gtle, // query全部匹配上的target的长度 + int *_gscore, // query的端到端匹配得分 + int *_max_off) // 取得最大得分时在query和reference上位置差的 最大值 +{ + int16_t *mA, *hA, *eA, *fA, *mA1, *mA2, *hA0, *hA1, *eA1, *fA1, *hA2, *eA2, *fA2; // hA0保存上上个col的H,其他的保存上个H E F M + int16_t *seq, *ref; + uint8_t *mem; + int16_t *qtmem, *vmem; + int seq_size = qlen + SIMD_WIDTH, ref_size = tlen + SIMD_WIDTH; + int i, iStart, D, j, k, beg, end, max, max_i, max_j, max_ins, max_del, max_ie, gscore, max_off; + int Dloop = tlen + qlen; // 循环跳出条件 + int span, beg1, end1; // 边界条件计算 + int col_size = qlen + 2 + SIMD_WIDTH; + int val_mem_size = (col_size * 9 * 2 + 31) >> 5 << 5; // 32字节的整数倍 + int mem_size = (seq_size + ref_size) * 2 + val_mem_size; + + SIMD_INIT; // 初始化simd用的数据 + + assert(h0 > 0); + + // allocate memory + mem = malloc(mem_size); + qtmem = (int16_t *)&mem[0]; + seq = &qtmem[0]; + ref = &qtmem[seq_size]; + if (is_left) + { + for (i = 0; i < qlen; ++i) + seq[i] = query[qlen - 1 - i]; + for (i = 0; i < tlen; ++i) + ref[i + SIMD_WIDTH] = target[tlen - 1 - i]; + } + else + { + for (i = 0; i < qlen; ++i) + seq[i] = query[i]; + for (i = 0; i < tlen; ++i) + ref[i + SIMD_WIDTH] = target[i]; + } + + vmem = &ref[ref_size]; + for (i = 0; i < (val_mem_size >> 1); i += SIMD_WIDTH) + { + _mm256_storeu_si256((__m256i *)&vmem[i], zero_vec); + } + hA = &vmem[0]; + mA = &vmem[col_size * 3]; + eA = &vmem[col_size * 5]; + fA = &vmem[col_size * 7]; + + hA0 = &hA[0]; + hA1 = &hA[col_size]; + hA2 = &hA1[col_size]; + mA1 = &mA[0]; + mA2 = &mA[col_size]; + eA1 = &eA[0]; + eA2 = &eA[col_size]; + fA1 = &fA[0]; + fA2 = &fA[col_size]; + + // adjust $w if it is too large + k = m * m; + // get the max score + for (i = 0, max = 0; i < k; ++i) + max = max > mat[i] ? max : mat[i]; + max_ins = (int)((double)(qlen * max + end_bonus - o_ins) / e_ins + 1.); + max_ins = max_ins > 1 ? max_ins : 1; + w = w < max_ins ? w : max_ins; + max_del = (int)((double)(qlen * max + end_bonus - o_del) / e_del + 1.); + max_del = max_del > 1 ? max_del : 1; + w = w < max_del ? w : max_del; // TODO: is this necessary? + if (tlen < qlen) + w = MIN(tlen - 1, w); + + // DP loop + max = h0, max_i = max_j = -1; + max_ie = -1, gscore = -1; + ; + max_off = 0; + beg = 1; + end = qlen; + // init h0 + hA0[0] = h0; // 左上角 + + if (qlen == 0 || tlen == 0) + Dloop = 0; // 防止意外情况 + if (w >= qlen) + { + max_ie = 0; + gscore = 0; + } + + int m_last = 0; + int iend; + + for (D = 1; LIKELY(D < Dloop); ++D) + { + // 边界条件一定要注意! tlen 大于,等于,小于 qlen时的情况 + if (D > tlen) + { + span = MIN(Dloop - D, w); + beg1 = MAX(D - tlen + 1, ((D - w) / 2) + 1); + } + else + { + span = MIN(D - 1, w); + beg1 = MAX(1, ((D - w) / 2) + 1); + } + end1 = MIN(qlen, beg1 + span); + + if (beg < beg1) + beg = beg1; + if (end > end1) + end = end1; + if (beg > end) + break; // 不用计算了,直接跳出,否则hA2没有被赋值,里边是上一轮hA0的值,会出bug + + iend = D - (beg - 1); // ref开始计算的位置,倒序 + span = end - beg; + iStart = iend - span - 1; // 0开始的ref索引位置 + + // 每一轮需要记录的数据 + int m = 0, mj = -1, mi = -1; + max_vec = zero_vec; + + // 要处理边界 + // 左边界 处理f (insert) + if (iStart == 0) + { + hA1[end] = MAX(0, h0 - (o_ins + e_ins * end)); + } + // 上边界 + if (beg == 1) + { + hA1[0] = MAX(0, h0 - (o_del + e_del * iend)); + } + else + { + hA1[beg - 1] = 0; + eA1[beg - 1] = 0; + } + + for (j = beg, i = iend; j <= end + 1 - SIMD_WIDTH; j += SIMD_WIDTH, i -= SIMD_WIDTH) + { + // 取数据 + SIMD_LOAD; + // 比对seq,计算罚分 + SIMD_CMP_SEQ; + // 计算 + SIMD_COMPUTE; + // 存储结果 + SIMD_STORE; + } + // 剩下的计算单元 + if (j <= end) + { + // 取数据 + SIMD_LOAD; + // 比对seq,计算罚分 + SIMD_CMP_SEQ; + // 计算 + SIMD_COMPUTE; + // 去除多余计算的部分 + SIMD_REMOVE_EXTRA; + // 存储结果 + SIMD_STORE; + } + + SIMD_FIND_MAX; + + // 注意最后跳出循环j的值 + j = end + 1; + + if (j == qlen + 1) + { + max_ie = gscore > hA2[qlen] ? max_ie : iStart; + gscore = gscore > hA2[qlen] ? gscore : hA2[qlen]; + } + // if (m == 0 && m_last == 0) + // break; // 一定要注意,斜对角遍历和按列遍历的不同点 + if (m > max) + { + max = m, max_i = mi, max_j = mj; + max_off = max_off > abs(mj - mi) ? max_off : abs(mj - mi); + } + else if (zdrop > 0) + { + if (mi - max_i > mj - max_j) + { + if (max - m - ((mi - max_i) - (mj - max_j)) * e_del > zdrop) + break; + } + else + { + if (max - m - ((mj - max_j) - (mi - max_i)) * e_ins > zdrop) + break; + } + } + + // 调整计算的边界 + for (j = beg; LIKELY(j <= end); ++j) + { + int has_val = hA1[j - 1] | hA2[j]; + if (has_val) + break; + } + beg = j; + for (j = end + 1; LIKELY(j >= beg); --j) + { + int has_val = hA1[j - 1] | hA2[j]; + if (has_val) + break; + else + hA0[j - 1] = 0; + } + end = j + 1 <= qlen ? j + 1 : qlen; + // beg = 0; + // end = qlen; // uncomment this line for debugging + m_last = m; + // swap m, h, e, f + SWAP_DATA_POINTER; + } + + free(mem); + if (_qle) + *_qle = max_j + 1; + if (_tle) + *_tle = max_i + 1; + if (_gtle) + *_gtle = max_ie + 1; + if (_gscore) + *_gscore = gscore; + if (_max_off) + *_max_off = max_off; + return max; +} \ No newline at end of file diff --git a/ksw_avx2.c b/ksw_avx2.c index 233d672..b592da1 100644 --- a/ksw_avx2.c +++ b/ksw_avx2.c @@ -194,13 +194,6 @@ int ksw_avx2(int qlen, // query length 待匹配段碱基的query int *_gscore, // query的端到端匹配得分 int *_max_off) // 取得最大得分时在query和reference上位置差的 最大值 { - // ksw_extend2_origin - // return ksw_extend2_origin(qlen, query, tlen, target, is_left, m, mat, o_del, e_del, o_ins, e_ins, w, end_bonus, zdrop, h0, _qle, _tle, _gtle, _gscore, _max_off); - // fprintf(stderr, "qlen: %d, tlen: %d\n", qlen, tlen); - - // if (qlen * a + h0 < 255) - // return ksw_extend2_avx2_u8(qlen, query, tlen, target, is_left, m, mat, o_del, e_del, o_ins, e_ins, a, b, w, end_bonus, zdrop, h0, _qle, _tle, _gtle, _gscore, _max_off); - int16_t *mA, *hA, *eA, *fA, *mA1, *mA2, *hA0, *hA1, *eA1, *fA1, *hA2, *eA2, *fA2; // hA0保存上上个col的H,其他的保存上个H E F M int16_t *seq, *ref; uint8_t *mem; @@ -375,8 +368,8 @@ int ksw_avx2(int qlen, // query length 待匹配段碱基的query max_ie = gscore > hA2[qlen] ? max_ie : iStart; gscore = gscore > hA2[qlen] ? gscore : hA2[qlen]; } - if (m == 0 && m_last == 0) - break; // 一定要注意,斜对角遍历和按列遍历的不同点 + // if (m == 0 && m_last == 0) + // break; // 一定要注意,斜对角遍历和按列遍历的不同点 if (m > max) { max = m, max_i = mi, max_j = mj; @@ -413,7 +406,8 @@ int ksw_avx2(int qlen, // query length 待匹配段碱基的query hA0[j - 1] = 0; } end = j + 1 <= qlen ? j + 1 : qlen; - + // beg = 0; + // end = qlen; // uncomment this line for debugging m_last = m; // swap m, h, e, f SWAP_DATA_POINTER; diff --git a/ksw_normal.c b/ksw_normal.c index aee34e6..1ef1682 100644 --- a/ksw_normal.c +++ b/ksw_normal.c @@ -28,68 +28,69 @@ int ksw_normal(int qlen, const uint8_t *query, int tlen, const uint8_t *target, { const int8_t *p = &mat[k * m]; for (j = 0; j < qlen; ++j) - qp[i++] = p[query[j]]; + qp[i++] = p[query[j]]; // 对于qp数组,第0到qlen个元素,表示和A比对的分值,第qlen到2*qlen,表示和C比对的分值,以此类推 } // fill the first row + // 初始化第一行分值 eh[0].h = h0; eh[1].h = h0 > oe_ins ? h0 - oe_ins : 0; for (j = 2; j <= qlen && eh[j - 1].h > e_ins; ++j) eh[j].h = eh[j - 1].h - e_ins; // adjust $w if it is too large - k = m * m; + k = m * m; // 字符矩阵 for (i = 0, max = 0; i < k; ++i) // get the max score max = max > mat[i] ? max : mat[i]; - max_ins = (int)((double)(qlen * max + end_bonus - o_ins) / e_ins + 1.); + max_ins = (int)((double)(qlen * max + end_bonus - o_ins) / e_ins + 1.); // 最大可插入的长度? max_ins = max_ins > 1 ? max_ins : 1; w = w < max_ins ? w : max_ins; max_del = (int)((double)(qlen * max + end_bonus - o_del) / e_del + 1.); max_del = max_del > 1 ? max_del : 1; - w = w < max_del ? w : max_del; // TODO: is this necessary? + w = w < max_del ? w : max_del; // TODO: is this necessary? 上述几行代码都是为了看看能否缩小窗口,减小计算 // DP loop max = h0, max_i = max_j = -1; max_ie = -1, gscore = -1; max_off = 0; beg = 0, end = qlen; - for (i = 0; LIKELY(i < tlen); ++i) + for (i = 0; LIKELY(i < tlen); ++i) // 对target逐个字符进行遍历 { int t, f = 0, h1, m = 0, mj = -1; - int8_t *q = &qp[target[i] * qlen]; + int8_t *q = &qp[target[i] * qlen]; // 对于target第i个字符,query中每个字符的分值,只有匹配和不匹配 // apply the band and the constraint (if provided) - if (beg < i - w) - beg = i - w; - if (end > i + w + 1) - end = i + w + 1; - if (end > qlen) - end = qlen; + // if (beg < i - w) // 检查开始点是否可以缩小一些 + // beg = i - w; + // if (end > i + w + 1) // 检查终点是否可以缩小,使得整体的遍历范围缩小 + // end = i + w + 1; + // if (end > qlen) // 终点不超过query长度 + // end = qlen; // compute the first column if (beg == 0) { - h1 = h0 - (o_del + e_del * (i + 1)); + h1 = h0 - (o_del + e_del * (i + 1)); // 只消耗了target序列,query从第一个字符开始匹配第i个target字符 if (h1 < 0) h1 = 0; } else h1 = 0; - for (j = beg; LIKELY(j < end); ++j) + for (j = beg; LIKELY(j < end); ++j) // 遍历query字符序列 { // At the beginning of the loop: eh[j] = { H(i-1,j-1), E(i,j) }, f = F(i,j) and h1 = H(i,j-1) // Similar to SSE2-SW, cells are computed in the following order: // H(i,j) = max{H(i-1,j-1)+S(i,j), E(i,j), F(i,j)} - // E(i+1,j) = max{H(i,j)-gapo, E(i,j)} - gape - // F(i,j+1) = max{H(i,j)-gapo, F(i,j)} - gape + // E(i+1,j) = max{H(i,j)-gapo, E(i,j)} - gape // E表示delete,只消耗target + // F(i,j+1) = max{H(i,j)-gapo, F(i,j)} - gape // F表示insert,只消耗query eh_t *p = &eh[j]; - int h, M = p->h, e = p->e; // get H(i-1,j-1) and E(i-1,j) - p->h = h1; // set H(i,j-1) for the next row - M = M ? M + q[j] : 0; // separating H and M to disallow a cigar like "100M3I3D20M" - h = M > e ? M : e; // e and f are guaranteed to be non-negative, so h>=0 even if M<0 + int h, M = p->h, e = p->e; // get H(i-1,j-1) and E(i-1,j) // 获取上一轮h值和e值 + p->h = h1; // set H(i,j-1) for the next row // h1是上一轮计算的结果 + M = M ? M + q[j] : 0; // separating H and M to disallow a cigar like "100M3I3D20M" // M大于0则当前两个字符进行匹配,无论是否相等,将分值加到M上,此时M可能变为负数 + h = M > e ? M : e; // e and f are guaranteed to be non-negative, so h>=0 even if M<0 // e和f保证是非负的,所以h肯定非负,即使M可能是负数,因为h取e,f和M的最大值 h = h > f ? h : f; - h1 = h; // save H(i,j) to h1 for the next column - mj = m > h ? mj : j; // record the position where max score is achieved - m = m > h ? m : h; // m is stored at eh[mj+1] - t = M - oe_del; + h1 = h; // save H(i,j) to h1 for the next column // 用h1来保存当前表格(i,j)对应的分值,用来下次计算 + mj = m > h ? mj : j; // record the position where max score is achieved // 记录取得最大值时query的字符位置 + m = m > h ? m : h; // m is stored at eh[mj+1] 因为eh[mj+1]->h表示的是H(i, mj),及上一轮记录的h + t = M - oe_del; // 用来计算delete,假设当前字符(i,j)匹配(无论match还是mismatch),target下一个字符串被空消耗(delete)的分值F(i+1, j) t = t > 0 ? t : 0; - e -= e_del; - e = e > t ? e : t; // computed E(i+1,j) + e -= e_del; // 假设当前query字符 + e = e > t ? e : t; // computed E(i+1,j) // t表示(i,j)强行匹配,(i+1, j)是delete的分数,此前e表示(i+1,j)继续delete的分数 p->e = e; // save E(i+1,j) for the next row t = M - oe_ins; t = t > 0 ? t : 0; @@ -98,28 +99,28 @@ int ksw_normal(int qlen, const uint8_t *query, int tlen, const uint8_t *target, } eh[end].h = h1; eh[end].e = 0; - if (j == qlen) + if (j == qlen) // 此轮遍历到了query的最后一个字符 { - max_ie = gscore > h1 ? max_ie : i; + max_ie = gscore > h1 ? max_ie : i; // max_ie表示取得全局最大分值时,target字符串的位置 gscore = gscore > h1 ? gscore : h1; } - if (m == 0) - break; - if (m > max) + // if (m == 0) // 遍历完query之后,当前轮次的最大分值为0,则跳出循环 + // break; + if (m > max) // 当前轮最大分值大于之前的最大分值 { - max = m, max_i = i, max_j = mj; - max_off = max_off > abs(mj - i) ? max_off : abs(mj - i); + max = m, max_i = i, max_j = mj; // 更新取得最大值的target和query的位置 + max_off = max_off > abs(mj - i) ? max_off : abs(mj - i); // 取得最大分值时,query和target对应字符串坐标的差值 } - else if (zdrop > 0) + else if (0) //(zdrop > 0) // 当前轮匹配之后取得的最大分值没有大于之前的最大值,而且zdrop值大于0 { if (i - max_i > mj - max_j) { - if (max - m - ((i - max_i) - (mj - max_j)) * e_del > zdrop) + if (max - m - ((i - max_i) - (mj - max_j)) * e_del > zdrop) // 之前最大分值 -(从取得最大值的点出发,当前的delete总长度对应的分值 + 当前轮取得的最大值) > zdrop break; } else { - if (max - m - ((mj - max_j) - (i - max_i)) * e_ins > zdrop) + if (max - m - ((mj - max_j) - (i - max_i)) * e_ins > zdrop) // 同上,不过这次是insert,可能是说明有很多mismatch break; } } @@ -130,7 +131,8 @@ int ksw_normal(int qlen, const uint8_t *query, int tlen, const uint8_t *target, for (j = end; LIKELY(j >= beg) && eh[j].h == 0 && eh[j].e == 0; --j) ; end = j + 2 < qlen ? j + 2 : qlen; - // beg = 0; end = qlen; // uncomment this line for debugging + beg = 0; + end = qlen; // uncomment this line for debugging } free(eh); free(qp); diff --git a/main.c b/main.c index 5e28fd9..20db7cb 100644 --- a/main.c +++ b/main.c @@ -36,6 +36,32 @@ extern int ksw_avx2(int qlen, const uint8_t *query, int tlen, const uint8_t *tar extern int ksw_avx2_u8(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int is_left, int m, const int8_t *mat, int o_del, int e_del, int o_ins, int e_ins, int a, int b, int w, int end_bonus, int zdrop, int h0, int *_qle, int *_tle, int *_gtle, int *_gscore, int *_max_off); +unsigned char nst_nt4_table[256] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5 /*'-'*/, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; +char t_2bit2char[5] = {'A', 'C', 'G', 'T'}; +// 将碱基字符转成2位编码 +void convert_char_to_2bit(char *str) +{ + int i; + for (i = 0; i < strlen(str); ++i) + str[i] = nst_nt4_table[str[i]]; +} + /* * 包含一个参数,用来区分调用那个sw算法 * 参数为 normal/avx2/cuda @@ -82,6 +108,12 @@ int main(int argc, char *argv[]) int *info_buf = (int *)malloc(SEQ_BUF_SIZE); int **info_arr = (int **)malloc(SEQ_BUF_SIZE); FILE *query_f = 0, *target_f = 0, *info_f = 0; + // const char *qf_path = "q.fa"; + // const char *tf_path = "t.fa"; + // const char *if_path = "i.txt"; + const char *qf_path = "bug_q.fa"; + const char *tf_path = "bug_t.fa"; + const char *if_path = "bug_i.txt"; // const char *qf_path = "/public/home/zzh/data/sw/q_s.fa"; // const char *tf_path = "/public/home/zzh/data/sw/t_s.fa"; // const char *if_path = "/public/home/zzh/data/sw/i_s.txt"; @@ -91,13 +123,18 @@ int main(int argc, char *argv[]) // const char *qf_path = "/public/home/zzh/data/sw/q_l.fa"; // const char *tf_path = "/public/home/zzh/data/sw/t_l.fa"; // const char *if_path = "/public/home/zzh/data/sw/i_l.txt"; - const char *qf_path = "/public/home/zzh/data/sw/query.fa"; - const char *tf_path = "/public/home/zzh/data/sw/target.fa"; - const char *if_path = "/public/home/zzh/data/sw/info.txt"; + // const char *qf_path = "/public/home/zzh/data/sw/query.fa"; + // const char *tf_path = "/public/home/zzh/data/sw/target.fa"; + // const char *if_path = "/public/home/zzh/data/sw/info.txt"; query_f = fopen(qf_path, "r"); target_f = fopen(tf_path, "r"); info_f = fopen(if_path, "r"); + // 将每次比对的得分等信息写入文件,进行debug + FILE *normal_out_f = fopen("normal_out.txt", "w"); + FILE *avx2_out_f = fopen("avx2_out.txt", "w"); + FILE *avx2_u8_out_f = fopen("avx2_u8_out.txt", "w"); + // 每次读取一定量的数据,然后执行,直到处理完所有数据 int total_line_num = 0; // 目前处理的总的数据行数 int block_line_num = 0; // 当前循环包含的数据行数 @@ -119,6 +156,7 @@ int main(int argc, char *argv[]) } int score_normal = 0, score_avx2 = 0, score_avx2_u8 = 0; + int score_normal_total = 0, score_avx2_total = 0, score_avx2_u8_total = 0; while (!feof(target_f)) { @@ -129,8 +167,14 @@ int main(int argc, char *argv[]) { if (fgets(read_buf, READ_BUF_SIZE, target_f) == NULL) break; - const int line_size = strlen(read_buf); + int line_size = strlen(read_buf); assert(line_size < READ_BUF_SIZE); + if (read_buf[line_size - 1] == '\n') + { + read_buf[line_size - 1] = '\0'; + line_size--; + } + convert_char_to_2bit(read_buf); ++block_line_num; ++total_line_num; strncpy(target_arr + cur_read_size, read_buf, line_size); @@ -144,8 +188,14 @@ int main(int argc, char *argv[]) { if (fgets(read_buf, READ_BUF_SIZE, query_f) == NULL) break; - const int line_size = strlen(read_buf); + int line_size = strlen(read_buf); assert(line_size < READ_BUF_SIZE); + if (read_buf[line_size - 1] == '\n') + { + read_buf[line_size - 1] = '\0'; + line_size--; + } + convert_char_to_2bit(read_buf); strncpy(query_arr + cur_read_size, read_buf, line_size); cur_read_size += line_size; } @@ -174,7 +224,7 @@ int main(int argc, char *argv[]) #ifdef SHOW_PERF int64_t start_time = get_mseconds(); #endif - score_normal += ksw_normal( + score_normal = ksw_normal( info_arr[i][0], (uint8_t *)query_arr + cur_query_pos, info_arr[i][1], @@ -185,21 +235,14 @@ int main(int argc, char *argv[]) #ifdef SHOW_PERF time_sw_normal += get_mseconds() - start_time; #endif - // 更新query和target位置信息 - cur_query_pos += info_arr[i][0]; - cur_target_pos += info_arr[i][1]; - // fprintf(stderr, "%d %d %d %d %d %d %d\n", score_normal, qle, tle, gtle, gscore, max_off[0], max_off[1]); - } + score_normal_total += score_normal; + // fprintf(normal_out_f, "%d %d\n", info_arr[i][2], score_normal); + fprintf(normal_out_f, "%d %d %d %d %d %d %d\n", info_arr[i][2], score_normal, qle, tle, gtle, gscore, max_off[0]); - // avx2 sw - cur_query_pos = 0; - cur_target_pos = 0; - for (i = 0; i < block_line_num; ++i) - { #ifdef SHOW_PERF - int64_t start_time = get_mseconds(); + start_time = get_mseconds(); #endif - score_avx2 += ksw_avx2( + score_avx2 = ksw_avx2( info_arr[i][0], (uint8_t *)query_arr + cur_query_pos, info_arr[i][1], @@ -212,21 +255,13 @@ int main(int argc, char *argv[]) #ifdef SHOW_PERF time_sw_avx2 += get_mseconds() - start_time; #endif - // 更新query和target位置信息 - cur_query_pos += info_arr[i][0]; - cur_target_pos += info_arr[i][1]; - // fprintf(stderr, "%d %d %d %d %d %d %d\n", score_avx2, qle, tle, gtle, gscore, max_off[0], max_off[1]); - } - - // avx2 u8 sw - cur_query_pos = 0; - cur_target_pos = 0; - for (i = 0; i < block_line_num; ++i) - { + score_avx2_total += score_avx2; + // fprintf(avx2_out_f, "%d %d\n", info_arr[i][2], score_avx2); + fprintf(avx2_out_f, "%d %d %d %d %d %d %d\n", info_arr[i][2], score_avx2, qle, tle, gtle, gscore, max_off[0]); #ifdef SHOW_PERF - int64_t start_time = get_mseconds(); + start_time = get_mseconds(); #endif - score_avx2_u8 += ksw_avx2_u8( + score_avx2_u8 = ksw_avx2_u8( info_arr[i][0], (uint8_t *)query_arr + cur_query_pos, info_arr[i][1], @@ -239,21 +274,77 @@ int main(int argc, char *argv[]) #ifdef SHOW_PERF time_sw_avx2_u8 += get_mseconds() - start_time; #endif + score_avx2_u8_total += score_avx2_u8; + fprintf(avx2_u8_out_f, "%d %d %d %d %d %d\n", score_avx2_u8, qle, tle, gtle, gscore, max_off[0]); // 更新query和target位置信息 cur_query_pos += info_arr[i][0]; cur_target_pos += info_arr[i][1]; // fprintf(stderr, "%d %d %d %d %d %d %d\n", score_normal, qle, tle, gtle, gscore, max_off[0], max_off[1]); } + /* + // avx2 sw + cur_query_pos = 0; + cur_target_pos = 0; + for (i = 0; i < block_line_num; ++i) + { + #ifdef SHOW_PERF + int64_t start_time = get_mseconds(); + #endif + score_avx2 += ksw_avx2( + info_arr[i][0], + (uint8_t *)query_arr + cur_query_pos, + info_arr[i][1], + (uint8_t *)target_arr + cur_target_pos, + 0, 5, mat, 6, 1, 6, 1, + 1, 4, + 100, 5, 100, + info_arr[i][2], + &qle, &tle, >le, &gscore, &max_off[0]); + #ifdef SHOW_PERF + time_sw_avx2 += get_mseconds() - start_time; + #endif + // 更新query和target位置信息 + cur_query_pos += info_arr[i][0]; + cur_target_pos += info_arr[i][1]; + // fprintf(stderr, "%d %d %d %d %d %d %d\n", score_avx2, qle, tle, gtle, gscore, max_off[0], max_off[1]); + } + // avx2 u8 sw + cur_query_pos = 0; + cur_target_pos = 0; + for (i = 0; i < block_line_num; ++i) + { + #ifdef SHOW_PERF + int64_t start_time = get_mseconds(); + #endif + score_avx2_u8 += ksw_avx2_u8( + info_arr[i][0], + (uint8_t *)query_arr + cur_query_pos, + info_arr[i][1], + (uint8_t *)target_arr + cur_target_pos, + 0, 5, mat, 6, 1, 6, 1, + 1, 4, + 100, 5, 100, + info_arr[i][2], + &qle, &tle, >le, &gscore, &max_off[0]); + #ifdef SHOW_PERF + time_sw_avx2_u8 += get_mseconds() - start_time; + #endif + // 更新query和target位置信息 + cur_query_pos += info_arr[i][0]; + cur_target_pos += info_arr[i][1]; + // fprintf(stderr, "%d %d %d %d %d %d %d\n", score_normal, qle, tle, gtle, gscore, max_off[0], max_off[1]); + } + */ // fprintf(stderr, "%d %d \n", block_line_num, total_line_num); } // fprintf(stderr, "%d \n", score_normal); #ifdef SHOW_PERF - fprintf(stderr, "time_sw_normal: %f s; score: %d\n", time_sw_normal / 1000.0, score_normal); - fprintf(stderr, "time_sw_avx2: %f s; score: %d\n", time_sw_avx2 / 1000.0, score_avx2); - fprintf(stderr, "time_sw_avx2_u8: %f s; score: %d\n", time_sw_avx2_u8 / 1000.0, score_avx2_u8); + fprintf(stderr, "time_sw_normal: %f s; score: %d\n", time_sw_normal / 1000.0, score_normal_total); + fprintf(stderr, "time_sw_avx2: %f s; score: %d\n", time_sw_avx2 / 1000.0, score_avx2_total); + fprintf(stderr, "time_sw_avx2_u8: %f s; score: %d\n", time_sw_avx2_u8 / 1000.0, score_avx2_u8_total); #endif if (query_f != 0) @@ -262,4 +353,10 @@ int main(int argc, char *argv[]) fclose(target_f); if (info_f != 0) fclose(info_f); + if (avx2_out_f != 0) + fclose(avx2_out_f); + if (avx2_u8_out_f != 0) + fclose(avx2_u8_out_f); + if (normal_out_f != 0) + fclose(normal_out_f); } \ No newline at end of file