From f332852474db6633d9ce56ffd24ae0b6953e4ae5 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Sun, 7 Apr 2024 19:18:02 -0400 Subject: [PATCH] llil.cc - wip --- examples/llil.cc | 98 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 19 deletions(-) diff --git a/examples/llil.cc b/examples/llil.cc index 0d2e144..ec963c3 100644 --- a/examples/llil.cc +++ b/examples/llil.cc @@ -228,31 +228,35 @@ namespace bip = boost::interprocess; namespace lf = boost::lockfree; // ------------------------------------------------------------------------------------------ -template +template class llil_t { public: using uint_t = string_cnt::uint_t; - static_assert(std::bit_ceil(num_wthreads) == num_wthreads); + static_assert(std::bit_ceil(num_consumers) == num_consumers); using string_cnt_set_t = phmap::parallel_flat_hash_set< string_cnt, phmap::priv::hash_default_hash, phmap::priv::hash_default_eq, phmap::priv::Allocator, - std::countr_zero(std::bit_ceil(num_wthreads)) + 1 + std::countr_zero(std::bit_ceil(num_consumers)) >; using string_cnt_vector_t = std::vector; using string_cnt_vector_t2 = std::array; - struct write_thread { + struct consumer { + consumer() : queue(10000) {} + lf::queue queue; std::thread thread; + std::atomic done {false}; }; - string_cnt_set_t set; - std::atomic num_lines = 0; - lf::queue file_queue; + string_cnt_set_t set; + std::atomic num_lines = 0; + size_t num_unique; + std::array consumers; void get_properties(const char* fname) { auto mapping = bip::file_mapping(fname, bip::read_only); @@ -262,7 +266,7 @@ public: size_t _num_lines = 0; - std::array vecs; + std::array vecs; constexpr size_t batch_size = 1024; for (auto& v : vecs) v.reserve(batch_size); @@ -279,29 +283,40 @@ public: uint_t count = fast_atoui(found + 1, end_ptr); size_t hashval = std::hash()(word); - auto& v = vecs[set.subidx(hashval)]; + auto subidx = set.subidx(hashval); + auto& v = vecs[subidx]; v.emplace_back(word, count); - if (v.size() == batch_size) - v.resize(0); // actually should enqueue to other thread + + if (v.size() == batch_size) { + // enqueue vector to consumer thread + enqueue_vec(std::move(v), subidx); + v.clear(); + v.reserve(batch_size); + } first = end_ptr + 1; ++_num_lines; } + for (size_t i=0; i void get_properties(char** fname, int nfiles) { - std::vector thr; - std::atomic done {false}; + std::vector producers; // produce blocks of word/count to add + lf::queue file_queue(4096); // queue of files to process + std::atomic done_adding_files {false}; // true when all files to process are enqueued - thr.reserve(num_producers); + producers.reserve(num_producers); for (size_t i=0; i(*it).cnt += sc.cnt; + } + }); + delete v; + } + void add_to_set(std::string_view word, uint_t count) { set.lazy_emplace_l( word, @@ -355,7 +415,7 @@ int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "usage: llil4map file1 file2 ... >out.txt\n"; return 1; } - llil_t<1> llil; + llil_t<32> llil; show_time("get properties ", [&]() { llil.get_properties<6>(&argv[1], argc - 1); });