发现并解决开辟内存的bug(开辟指针时候没有考虑每个指针占用的字节数),用clock试试计算运行时间

This commit is contained in:
zzh 2023-08-18 20:27:55 +08:00
parent 53332e34e1
commit c9e4d9645c
2 changed files with 45 additions and 35 deletions

View File

@ -127,12 +127,12 @@ int ksw_normal(int qlen, const uint8_t *query, int tlen, const uint8_t *target,
}
}
// update beg and end for the next round
for (j = beg; LIKELY(j < end) && eh[j].h == 0 && eh[j].e == 0; ++j)
;
beg = j;
for (j = end; LIKELY(j >= beg) && eh[j].h == 0 && eh[j].e == 0; --j)
;
end = j + 2 < qlen ? j + 2 : qlen; // 剪枝没考虑f即insert
// for (j = beg; LIKELY(j < end) && eh[j].h == 0 && eh[j].e == 0; ++j)
// ;
// beg = j;
// for (j = end; LIKELY(j >= beg) && eh[j].h == 0 && eh[j].e == 0; --j)
// ;
// end = j + 2 < qlen ? j + 2 : qlen; // 剪枝没考虑f即insert
beg = 0, end = qlen; // uncomment this line for debugging
// fprintf(stderr, "\n");
// fprintf(stderr, "%d\n", end);

68
main.c
View File

@ -3,6 +3,7 @@
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include "sys/time.h"
#define SW_NORMAL 0
@ -14,14 +15,17 @@
#define READ_BUF_SIZE 2048
#define SEQ_BUF_SIZE (BLOCK_BUF_SIZE + READ_BUF_SIZE)
#define DIVIDE_BY (CLOCKS_PER_SEC * 1.0)
#ifdef SHOW_PERF
// 用来调试,计算感兴趣部分的运行时间
// 获取当前毫秒数
int64_t get_mseconds()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)1000 * (tv.tv_sec + ((1e-6) * tv.tv_usec));
// struct timeval tv;
// gettimeofday(&tv, NULL);
// return (int64_t)1000 * (tv.tv_sec + ((1e-6) * tv.tv_usec));
return clock();
}
int64_t time_sw_normal = 0,
@ -111,8 +115,8 @@ int main(int argc, char *argv[])
// 读取测试数据
char *query_arr = (char *)malloc(SEQ_BUF_SIZE);
char *target_arr = (char *)malloc(SEQ_BUF_SIZE);
int *info_buf = (int *)malloc(SEQ_BUF_SIZE);
int **info_arr = (int **)malloc(SEQ_BUF_SIZE);
int *info_buf = (int *)malloc(SEQ_BUF_SIZE * sizeof(int));
int **info_arr = (int **)malloc(SEQ_BUF_SIZE * sizeof(int *));
FILE *query_f = 0, *target_f = 0, *info_f = 0;
// const char *qf_path = "q.fa";
// const char *tf_path = "t.fa";
@ -120,18 +124,18 @@ int main(int argc, char *argv[])
// 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";
// const char *qf_path = "/public/home/zzh/data/sw/q_m.fa";
// const char *tf_path = "/public/home/zzh/data/sw/t_m.fa";
// const char *if_path = "/public/home/zzh/data/sw/i_m.txt";
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 = "/home/zzh/data/sw/q_s.fa";
// const char *tf_path = "/home/zzh/data/sw/t_s.fa";
// const char *if_path = "/home/zzh/data/sw/i_s.txt";
const char *qf_path = "/home/zzh/data/sw/q_m.fa";
const char *tf_path = "/home/zzh/data/sw/t_m.fa";
const char *if_path = "/home/zzh/data/sw/i_m.txt";
// const char *qf_path = "/home/zzh/data/sw/q_l.fa";
// const char *tf_path = "/home/zzh/data/sw/t_l.fa";
// const char *if_path = "/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";
query_f = fopen(qf_path, "r");
target_f = fopen(tf_path, "r");
info_f = fopen(if_path, "r");
@ -149,7 +153,6 @@ int main(int argc, char *argv[])
// const int max_read = READ_BUF_SIZE; // 每次最多读取的字符
char read_buf[READ_BUF_SIZE]; // 读文件缓存
// int ret_code = 0;
// 初始化info_arr数组
i = 0;
j = 0;
@ -160,6 +163,7 @@ int main(int argc, char *argv[])
info_arr[i] = &info_buf[j];
i += 1;
j += 3;
// fprintf(stderr, "%d\t%d\n", i, j);
}
int score_normal = 0, score_avx2 = 0, score_avx2_u8 = 0, score_bsw_avx2 = 0;
@ -167,6 +171,7 @@ int main(int argc, char *argv[])
while (!feof(target_f))
{
// fprintf(stderr, "debug\n");
block_line_num = 0;
// target序列一般占用存储最多先读取target看一个buf能读多少行query和info就按照这个行数来读
int cur_read_size = 0;
@ -187,8 +192,10 @@ int main(int argc, char *argv[])
strncpy(target_arr + cur_read_size, read_buf, line_size);
cur_read_size += line_size;
// fprintf(stderr, "%d %d \n", line_size, cur_read_size);
// fprintf(stderr, "%d %d \n", block_line_num, total_line_num);
}
// fprintf(stderr, "here\n");
// 读query
cur_read_size = 0;
for (i = 0; i < block_line_num; ++i)
@ -223,7 +230,6 @@ int main(int argc, char *argv[])
// 性能测试
// 普通 sw
int cur_query_pos = 0;
int cur_target_pos = 0;
for (i = 0; i < block_line_num; ++i)
@ -231,6 +237,7 @@ int main(int argc, char *argv[])
#ifdef SHOW_PERF
int64_t start_time = get_mseconds();
#endif
// 普通 sw
score_normal = ksw_normal(
info_arr[i][0],
(uint8_t *)query_arr + cur_query_pos,
@ -370,16 +377,19 @@ int main(int argc, char *argv[])
// 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_total);
fprintf(stderr, "time_bsw_avx2: %f s; score: %d\n", time_bsw_avx2 / 1000.0, score_bsw_avx2_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);
fprintf(stderr, "time_sw_normal: %f s; score: %d\n", time_sw_normal / DIVIDE_BY, score_normal_total);
fprintf(stderr, "time_bsw_avx2: %f s; score: %d\n", time_bsw_avx2 / DIVIDE_BY, score_bsw_avx2_total);
// fprintf(stderr, "time_sw_avx2: %f s; score: %d\n", time_sw_avx2 / DIVIDE_BY, score_avx2_total);
fprintf(stderr, "time_sw_avx2_u8: %f s; score: %d\n", time_sw_avx2_u8 / DIVIDE_BY, score_avx2_u8_total);
fprintf(stderr, "time_bsw_init: %f s\n", time_bsw_init / 1000.0);
fprintf(stderr, "time_bsw_main_loop: %f s\n", (time_bsw_main_loop - time_compare) / 1000.0);
fprintf(stderr, "time_bsw_find_max: %f s\n", (time_bsw_find_max - time_compare) / 1000.0);
fprintf(stderr, "time_bsw_adjust_bound: %f s\n", (time_bsw_adjust_bound - time_compare) / 1000.0);
fprintf(stderr, "time_compare: %f s\n", time_compare / 1000.0);
fprintf(stderr, "time_bsw_init: %f s\n", time_bsw_init / DIVIDE_BY);
// fprintf(stderr, "time_bsw_main_loop: %f s\n", (time_bsw_main_loop) / DIVIDE_BY);
// fprintf(stderr, "time_bsw_find_max: %f s\n", (time_bsw_find_max) / DIVIDE_BY);
// fprintf(stderr, "time_bsw_adjust_bound: %f s\n", (time_bsw_adjust_bound) / DIVIDE_BY);
fprintf(stderr, "time_bsw_main_loop: %f s\n", (time_bsw_main_loop - time_compare) / DIVIDE_BY);
fprintf(stderr, "time_bsw_find_max: %f s\n", (time_bsw_find_max - time_compare) / DIVIDE_BY);
fprintf(stderr, "time_bsw_adjust_bound: %f s\n", (time_bsw_adjust_bound - time_compare) / DIVIDE_BY);
fprintf(stderr, "time_compare: %f s\n", time_compare / DIVIDE_BY);
#endif
if (query_f != 0)
@ -396,4 +406,4 @@ int main(int argc, char *argv[])
fclose(normal_out_f);
if (bsw_avx2_out_f != 0)
fclose(bsw_avx2_out_f);
}
}