From 2526b75e61fc1fcb8b4daaf182ece15edafb5205 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Fri, 23 Feb 2024 17:41:50 +0000 Subject: [PATCH] Introduce opencl_work_func_t to make code more readable (#4446) --- nano/lib/work.cpp | 2 +- nano/lib/work.hpp | 8 ++++++-- nano/nano_node/daemon.cpp | 12 ++++++++---- nano/nano_node/entry.cpp | 14 +++++++++----- nano/nano_wallet/entry.cpp | 14 +++++++++----- nano/node/openclwork.hpp | 6 +++++- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/nano/lib/work.cpp b/nano/lib/work.cpp index 07b27e3979..480dba578c 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -23,7 +23,7 @@ std::string nano::to_string (nano::work_version const version_a) return result; } -nano::work_pool::work_pool (nano::network_constants & network_constants, unsigned max_threads_a, std::chrono::nanoseconds pow_rate_limiter_a, std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> opencl_a) : +nano::work_pool::work_pool (nano::network_constants & network_constants, unsigned max_threads_a, std::chrono::nanoseconds pow_rate_limiter_a, nano::opencl_work_func_t opencl_a) : network_constants{ network_constants }, ticket (0), done (false), diff --git a/nano/lib/work.hpp b/nano/lib/work.hpp index 70df3b8144..60f84987ec 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,9 @@ namespace nano { std::string to_string (nano::work_version const version_a); +// type of function that does the work generation with an optional return value +using opencl_work_func_t = std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)>; + class block; class block_details; enum class block_type : uint8_t; @@ -36,7 +40,7 @@ class work_item final class work_pool final { public: - work_pool (nano::network_constants & network_constants, unsigned, std::chrono::nanoseconds = std::chrono::nanoseconds (0), std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> = nullptr); + work_pool (nano::network_constants & network_constants, unsigned, std::chrono::nanoseconds = std::chrono::nanoseconds (0), nano::opencl_work_func_t = nullptr); ~work_pool (); void loop (uint64_t); void stop (); @@ -55,7 +59,7 @@ class work_pool final nano::mutex mutex{ mutex_identifier (mutexes::work_pool) }; nano::condition_variable producer_condition; std::chrono::nanoseconds pow_rate_limiter; - std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> opencl; + nano::opencl_work_func_t opencl; nano::observer_set work_observers; }; diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index fed22d3bcf..2becd930a5 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -99,11 +99,15 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag } boost::asio::io_context io_ctx; - auto opencl (nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work)); - nano::work_pool opencl_work (config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl ? [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic & ticket_a) { - return opencl->generate_work (version_a, root_a, difficulty_a, ticket_a); + auto opencl = nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work); + nano::opencl_work_func_t opencl_work_func; + if (opencl) + { + opencl_work_func = [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic & ticket_a) { + return opencl->generate_work (version_a, root_a, difficulty_a, ticket_a); + }; } - : std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> (nullptr)); + nano::work_pool opencl_work (config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl_work_func); try { // This avoids a blank prompt during any node initialization delays diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 64a899fab0..534c368adc 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -615,11 +615,15 @@ int main (int argc, char * const * argv) { nano::logger logger; nano::opencl_config config (platform, device, threads); - auto opencl (nano::opencl_work::create (true, config, logger, network_params.work)); - nano::work_pool work_pool{ network_params.network, 0, std::chrono::nanoseconds (0), opencl ? [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { - return opencl->generate_work (version_a, root_a, difficulty_a); - } - : std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> (nullptr) }; + auto opencl = nano::opencl_work::create (true, config, logger, network_params.work); + nano::opencl_work_func_t opencl_work_func; + if (opencl) + { + opencl_work_func = [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { + return opencl->generate_work (version_a, root_a, difficulty_a); + }; + } + nano::work_pool work_pool{ network_params.network, 0, std::chrono::nanoseconds (0), opencl_work_func }; nano::change_block block (0, 0, nano::keypair ().prv, 0, 0); std::cerr << boost::str (boost::format ("Starting OpenCL generation profiling. Platform: %1%. Device: %2%. Threads: %3%. Difficulty: %4$#x (%5%x from base difficulty %6$#x)\n") % platform % device % threads % difficulty % nano::to_string (nano::difficulty::to_multiplier (difficulty, nano::work_thresholds::publish_full.base), 4) % nano::work_thresholds::publish_full.base); for (uint64_t i (0); true; ++i) diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index ec15e6fb98..69d8262aa7 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -128,11 +128,15 @@ int run_wallet (QApplication & application, int argc, char * const * argv, std:: std::shared_ptr node; std::shared_ptr gui; nano::set_application_icon (application); - auto opencl (nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work)); - nano::work_pool work{ config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl ? [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { - return opencl->generate_work (version_a, root_a, difficulty_a); - } - : std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> (nullptr) }; + auto opencl = nano::opencl_work::create (config.opencl_enable, config.opencl, logger, config.node.network_params.work); + nano::opencl_work_func_t opencl_work_func; + if (opencl) + { + opencl_work_func = [&opencl] (nano::work_version const version_a, nano::root const & root_a, uint64_t difficulty_a, std::atomic &) { + return opencl->generate_work (version_a, root_a, difficulty_a); + }; + } + nano::work_pool work{ config.node.network_params.network, config.node.work_threads, config.node.pow_sleep_interval, opencl_work_func }; node = std::make_shared (io_ctx, data_path, config.node, work, flags); if (!node->init_error ()) { diff --git a/nano/node/openclwork.hpp b/nano/node/openclwork.hpp index 15173c1dcc..c73b988440 100644 --- a/nano/node/openclwork.hpp +++ b/nano/node/openclwork.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -22,12 +22,14 @@ namespace nano { extern bool opencl_loaded; class logger; + class opencl_platform { public: cl_platform_id platform; std::vector devices; }; + class opencl_environment { public: @@ -35,8 +37,10 @@ class opencl_environment void dump (std::ostream & stream); std::vector platforms; }; + class root; class work_pool; + class opencl_work { public: