This commit is contained in:
Zengtudor 2024-10-20 12:48:05 +08:00
parent 39e7c8320f
commit 1be6d9d2b9
2 changed files with 31 additions and 10 deletions

View File

@ -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)
# target_link_libraries(${PROJECT_NAME} PRIVATE TBB::tbb)

View File

@ -10,10 +10,17 @@
#include <pybind11/stl.h>
#include <stdexcept>
#include <string>
// #define DNA_USE_TBB
#ifdef DNA_USE_TBB
#include <tbb/parallel_for_each.h>
#endif
namespace py = pybind11;
static constexpr size_t default_buffer_size{512*1024*1024};
#define DNA_USE_BUFFER
static const std::unordered_map<char, char> 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(
@ -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<char> 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<char, max_size_pre_dna> dna_buf;
bool is_dna_line{false};