From 1be6d9d2b959a6fdc930bad5612cc60b9889d9e4 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sun, 20 Oct 2024 12:48:05 +0800 Subject: [PATCH] update --- CMakeLists.txt | 7 +++++-- src/main.cpp | 34 ++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dae334..ccb0fbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,9 +27,12 @@ foreach(SRC_CPP IN LISTS SRC_CPPS) endforeach() # add_executable(${PROJECT_NAME}_bin ${SRC_HPPS} ${SRC_CPPS}) - +set(TBB_TEST OFF) add_subdirectory(pybind11) +# add_subdirectory(oneTBB) pybind11_add_module(${PROJECT_NAME} ${SRC_HPPS} ${SRC_CPPS}) -target_compile_definitions(${PROJECT_NAME} PRIVATE DNA_IS_PYBIND) \ No newline at end of file +target_compile_definitions(${PROJECT_NAME} PRIVATE DNA_IS_PYBIND) + +# target_link_libraries(${PROJECT_NAME} PRIVATE TBB::tbb) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7af3ff9..6178a6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,10 +10,17 @@ #include #include #include +// #define DNA_USE_TBB +#ifdef DNA_USE_TBB + #include +#endif namespace py = pybind11; static constexpr size_t default_buffer_size{512*1024*1024}; + + +#define DNA_USE_BUFFER static const std::unordered_map complement = { //这里使用查表的方式大大提高CPU速度,因为if分支CPU不容易命中缓存,需要使用查表加速 {'A', 'T'}, {'a', 'T'}, {'T', 'A'}, {'t', 'A'}, @@ -25,14 +32,22 @@ void reverseComplement(char *begin, char *end) { //注意end是开区间,不能访问end std::reverse(begin, end); //翻转DNA序列 - - for (ptrdiff_t i = 0; i < (end - begin); ++i) { - // static int _ = (zt::print(NAME_VALUE(omp_get_num_threads()),"\n"),0); // 打印线程数量 - auto it = complement.find(begin[i]); - if (it != complement.end()) { - begin[i] = it->second; + #ifndef DNA_USE_TBB + for (ptrdiff_t i = 0; i < (end - begin); ++i) { + // static int _ = (zt::print(NAME_VALUE(omp_get_num_threads()),"\n"),0); // 打印线程数量 + auto it = complement.find(begin[i]); + if (it != complement.end()) { + begin[i] = it->second; + } } - } + #else + tbb::parallel_for_each(begin,end,[](char &c){ + const auto it = complement.find(c); + if (it != complement.end()) { + c = it->second; + } + }); + #endif } void convert_from_file( @@ -42,7 +57,7 @@ void convert_from_file( ) { // std::iostream::sync_with_stdio(false); - + const size_t max_size_pre_dna{(size_t)5e4+5}; const size_t all_buf_size = {buffer_size}; const size_t read_bufsize{all_buf_size/2},write_bufsize{all_buf_size/2}; @@ -57,9 +72,12 @@ void convert_from_file( std::ofstream ofs(destination_file); if(ofs.is_open()==false)throw std::runtime_error(fmt("Cannot open output file stream\nfilename: ",destination_file,'\n')); std::cout<<"Open file ok ,getting memory\n"; + + #ifdef DNA_USE_BUFFER std::vector read_buf(read_bufsize), write_buf(write_bufsize); ifs.rdbuf()->pubsetbuf(read_buf.data(), read_buf.size()); ofs.rdbuf()->pubsetbuf(write_buf.data(), write_buf.size()); + #endif std::array dna_buf; bool is_dna_line{false};