This commit is contained in:
Zengtudor 2024-09-19 23:47:23 +08:00
parent 85b73dcaca
commit 28aeddac17
2 changed files with 21 additions and 8 deletions

View File

@ -14,7 +14,6 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <type_traits>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -30,6 +29,7 @@ const size_t MAX_SIZE_PER_DNA = 5e4+5;
void reverseComplement(char *begin, char *end) //注意end是开区间不能访问end void reverseComplement(char *begin, char *end) //注意end是开区间不能访问end
{ {
const std::string_view sequence(begin,(size_t)(end-begin)/sizeof(char));
static const std::unordered_map<char, char> complement = { //这里使用查表的方式大大提高CPU速度因为if分支CPU不容易命中缓存需要使用查表加速 static const std::unordered_map<char, char> complement = { //这里使用查表的方式大大提高CPU速度因为if分支CPU不容易命中缓存需要使用查表加速
{'A', 'T'}, {'a', 'T'}, {'A', 'T'}, {'a', 'T'},
{'T', 'A'}, {'t', 'A'}, {'T', 'A'}, {'t', 'A'},
@ -37,12 +37,19 @@ void reverseComplement(char *begin, char *end) //注意end是开区间不能
{'G', 'C'}, {'g', 'C'} {'G', 'C'}, {'g', 'C'}
}; };
std::reverse(begin, end); //翻转DNA序列 // std::reverse(begin, end); //翻转DNA序列
// 并行翻转序列 //似乎没用
for (std::remove_const_t<decltype(begin)> i = begin; i < end; ++i) { //std::remove_const_t<decltype(buf_size)>意思是和buf_size相同的类型并去掉const #pragma omp parallel for
auto it = complement.find(*i);//查表并替换 for (ptrdiff_t i = 0; i < (end - begin) / 2; ++i) {
if (it != complement.end()) [[likely]] { std::swap(begin[i], begin[(end - begin) - i - 1]);
*i = it->second; }
// 并行查表替换
#pragma omp parallel for
for (ptrdiff_t i = 0; i < (end - begin); ++i) {
auto it = complement.find(begin[i]);
if (it != complement.end()) {
begin[i] = it->second;
} }
} }
} }
@ -139,7 +146,7 @@ int main()
while((end_pos=buf_str_v.find('\n',start_pos)) != std::string_view::npos){ while((end_pos=buf_str_v.find('\n',start_pos)) != std::string_view::npos){
if(get_lines_add()){ if(get_lines_add()){
reverseComplement(buf.data()+start_pos, buf.data()+end_pos); reverseComplement(buf.data()+start_pos, buf.data()+end_pos); //我想要这个函数并行优化
} }
// lines=!lines; // lines=!lines;
start_pos=end_pos+1; start_pos=end_pos+1;

View File

@ -15,6 +15,12 @@ if is_mode("release")then
end end
end end
if is_plat("windows")then
add_cxxflags("/openmp")
elseif is_plat("linux") or is_plat("mingw") or is_plat("clang")then
add_cxflags("-fopenmp")
end
add_includedirs("src/tools") add_includedirs("src/tools")
set_rundir("./") set_rundir("./")