From 5febb8c4e2dc2c2d60aa04da473da16b76ae5ba4 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 18 Oct 2023 13:43:40 +0200 Subject: [PATCH] Self-test: multithreaded dataset initialization --- src/main.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 00441e5..c1f9d12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,7 +106,26 @@ int p2pool_test() return 1; } - randomx_init_dataset(myDataset, myCache, 0, randomx_dataset_item_count()); + { + const uint32_t numThreads = std::max(std::thread::hardware_concurrency(), 1U); + const uint32_t numItems = randomx_dataset_item_count(); + + std::vector threads; + threads.reserve(numThreads); + + for (uint32_t i = 1; i < numThreads; ++i) { + const uint32_t a = (numItems * i) / numThreads; + const uint32_t b = (numItems * (i + 1)) / numThreads; + + threads.emplace_back([myDataset, myCache, a, b]() { randomx_init_dataset(myDataset, myCache, a, b - a); }); + } + randomx_init_dataset(myDataset, myCache, 0, numItems / numThreads); + + for (std::thread& t : threads) { + t.join(); + } + } + randomx_release_cache(myCache); randomx_vm* myMachine = randomx_create_vm(flags, nullptr, myDataset);