From badba2ab654c8aec3be1df8e9f4589bac68dfc4b Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 21 Dec 2017 15:21:37 -0600 Subject: [PATCH] Making opencl opaque to work_pool. --- rai/core_test/work_pool.cpp | 10 ++++++---- rai/node/work.cpp | 9 ++++----- rai/node/work.hpp | 4 ++-- rai/rai_node/daemon.cpp | 8 ++++++-- rai/rai_node/entry.cpp | 7 +++++-- rai/rai_wallet/entry.cpp | 6 +++++- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/rai/core_test/work_pool.cpp b/rai/core_test/work_pool.cpp index 2b997e88..3cc5b666 100644 --- a/rai/core_test/work_pool.cpp +++ b/rai/core_test/work_pool.cpp @@ -2,7 +2,6 @@ #include #include -#include TEST (work, one) { @@ -56,10 +55,13 @@ TEST (work, DISABLED_opencl) { rai::logging logging; logging.init (rai::unique_path ()); - auto work (rai::opencl_work::create (true, {0, 1, 1024 * 1024}, logging)); - if (work != nullptr) + auto opencl (rai::opencl_work::create (true, {0, 1, 1024 * 1024}, logging)); + if (opencl != nullptr) { - rai::work_pool pool (std::numeric_limits ::max (), std::move (work)); + rai::work_pool pool (std::numeric_limits ::max (), opencl ? [&opencl] (rai::uint256_union const & root_a) + { + return opencl->generate_work (root_a); + } : std::function (rai::uint256_union const &)> (nullptr)); ASSERT_NE (nullptr, pool.opencl); rai::uint256_union root; for (auto i (0); i < 1; ++i) diff --git a/rai/node/work.cpp b/rai/node/work.cpp index 58923c75..4e283d8b 100644 --- a/rai/node/work.cpp +++ b/rai/node/work.cpp @@ -1,7 +1,6 @@ #include #include -#include #include @@ -27,10 +26,10 @@ uint64_t rai::work_value (rai::block_hash const & root_a, uint64_t work_a) return result; } -rai::work_pool::work_pool (unsigned max_threads_a, std::unique_ptr opencl_a) : +rai::work_pool::work_pool (unsigned max_threads_a, std::function (rai::uint256_union const &)> opencl_a) : ticket (0), done (false), -opencl (std::move (opencl_a)) +opencl (opencl_a) { static_assert (ATOMIC_INT_LOCK_FREE == 2, "Atomic int needed"); auto count (rai::rai_network == rai::rai_networks::rai_test_network ? 1 : std::max (1u, std::min (max_threads_a, std::thread::hardware_concurrency ()))); @@ -156,9 +155,9 @@ void rai::work_pool::generate (rai::uint256_union const & root_a, std::function { assert (!root_a.is_zero ()); boost::optional result; - if (opencl != nullptr) + if (opencl) { - result = opencl->generate_work (root_a); + result = opencl (root_a); } if (!result) { diff --git a/rai/node/work.hpp b/rai/node/work.hpp index dd4b0166..c607b2b2 100644 --- a/rai/node/work.hpp +++ b/rai/node/work.hpp @@ -18,7 +18,7 @@ class opencl_work; class work_pool { public: - work_pool (unsigned, std::unique_ptr ); + work_pool (unsigned, std::function (rai::uint256_union const &)> = nullptr); ~work_pool (); void loop (uint64_t); void stop (); @@ -31,7 +31,7 @@ public: std::list const &)>>> pending; std::mutex mutex; std::condition_variable producer_condition; - std::unique_ptr opencl; + std::function (rai::uint256_union const &)> opencl; rai::observer_set work_observers; // Local work threshold for rate-limiting publishing blocks. ~5 seconds of work. static uint64_t const publish_test_threshold = 0xff00000000000000; diff --git a/rai/rai_node/daemon.cpp b/rai/rai_node/daemon.cpp index 824774b0..85eeb7b1 100644 --- a/rai/rai_node/daemon.cpp +++ b/rai/rai_node/daemon.cpp @@ -107,10 +107,14 @@ void rai_daemon::daemon::run (boost::filesystem::path const & data_path) config.node.logging.init (data_path); config_file.close (); boost::asio::io_service service; - rai::work_pool work (config.node.work_threads, rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging)); + auto opencl (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging)); + rai::work_pool opencl_work (config.node.work_threads, opencl ? [&opencl] (rai::uint256_union const & root_a) + { + return opencl->generate_work (root_a); + } : std::function (rai::uint256_union const &)> (nullptr)); rai::alarm alarm (service); rai::node_init init; - auto node (std::make_shared (init, service, data_path, alarm, config.node, work)); + auto node (std::make_shared (init, service, data_path, alarm, config.node, opencl_work)); if (!init.error ()) { node->start (); diff --git a/rai/rai_node/entry.cpp b/rai/rai_node/entry.cpp index c6ffbe4a..9e292635 100644 --- a/rai/rai_node/entry.cpp +++ b/rai/rai_node/entry.cpp @@ -317,8 +317,11 @@ int main (int argc, char * const * argv) if (!error) { rai::logging logging; - auto work (rai::opencl_work::create (true, {platform, device, threads}, logging)); - rai::work_pool work_pool (std::numeric_limits ::max (), std::move (work)); + auto opencl (rai::opencl_work::create (true, {platform, device, threads}, logging)); + rai::work_pool work_pool (std::numeric_limits ::max (), opencl ? [&opencl] (rai::uint256_union const & root_a) + { + return opencl->generate_work (root_a); + } : std::function (rai::uint256_union const &)> (nullptr)); rai::change_block block (0, 0, rai::keypair ().prv, 0, 0); std::cerr << boost::str (boost::format ("Starting OpenCL generation profiling. Platform: %1%. Device: %2%. Threads: %3%\n") % platform % device % threads); for (uint64_t i (0); true; ++i) diff --git a/rai/rai_wallet/entry.cpp b/rai/rai_wallet/entry.cpp index 4c68e6cd..d38eb812 100755 --- a/rai/rai_wallet/entry.cpp +++ b/rai/rai_wallet/entry.cpp @@ -211,7 +211,11 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost std::shared_ptr node; std::shared_ptr gui; rai::set_application_icon (application); - rai::work_pool work (config.node.work_threads, rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging)); + auto opencl (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging)); + rai::work_pool work (config.node.work_threads, opencl ? [&opencl] (rai::uint256_union const & root_a) + { + return opencl->generate_work (root_a); + } : std::function (rai::uint256_union const &)> (nullptr)); rai::alarm alarm (service); rai::node_init init; node = std::make_shared (init, service, data_path, alarm, config.node, work);