diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 718fd9660..5b0780069 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -165,7 +165,7 @@ public: } - inline void start() + inline void start(const Job &job) { LOG_INFO("%s use profile " BLUE_BG(WHITE_BOLD_S " %s ") WHITE_BOLD_S " (" CYAN_BOLD("%zu") WHITE_BOLD(" threads)") " scratchpad " CYAN_BOLD("%zu KB"), tag, @@ -195,7 +195,7 @@ public: i++; } - OclSharedState::start(threads); + OclSharedState::start(threads, job); status.start(threads.size()); workers.start(threads); @@ -332,7 +332,7 @@ void xmrig::OclBackend::setJob(const Job &job) return stop(); } - if (!d_ptr->context.init(d_ptr->devices, threads, job)) { + if (!d_ptr->context.init(d_ptr->devices, threads)) { LOG_WARN("%s " RED_BOLD("disabled") YELLOW(" (OpenCL context unavailable)"), tag); return stop(); @@ -341,7 +341,7 @@ void xmrig::OclBackend::setJob(const Job &job) stop(); d_ptr->threads = std::move(threads); - d_ptr->start(); + d_ptr->start(job); } diff --git a/src/backend/opencl/OclConfig.cpp b/src/backend/opencl/OclConfig.cpp index fc4f9ef0c..bdefcaae3 100644 --- a/src/backend/opencl/OclConfig.cpp +++ b/src/backend/opencl/OclConfig.cpp @@ -193,29 +193,13 @@ std::vector xmrig::OclConfig::get(const Miner *miner, cons continue; } -# ifdef XMRIG_ALGO_RANDOMX - auto dataset = algorithm.family() == Algorithm::RANDOM_X ? std::make_shared() : nullptr; -# endif - if (thread.threads().size() > 1) { for (int64_t affinity : thread.threads()) { - OclLaunchData data(miner, algorithm, *this, platform, thread, devices[thread.index()], affinity); - -# ifdef XMRIG_ALGO_RANDOMX - data.dataset = dataset; -# endif - - out.emplace_back(std::move(data)); + out.emplace_back(miner, algorithm, *this, platform, thread, devices[thread.index()], affinity); } } else { - OclLaunchData data(miner, algorithm, *this, platform, thread, devices[thread.index()], thread.threads().front()); - -# ifdef XMRIG_ALGO_RANDOMX - data.dataset = dataset; -# endif - - out.emplace_back(std::move(data)); + out.emplace_back(miner, algorithm, *this, platform, thread, devices[thread.index()], thread.threads().front()); } } diff --git a/src/backend/opencl/OclLaunchData.h b/src/backend/opencl/OclLaunchData.h index 8c2def2d5..644023be8 100644 --- a/src/backend/opencl/OclLaunchData.h +++ b/src/backend/opencl/OclLaunchData.h @@ -35,11 +35,6 @@ #include "crypto/common/Nonce.h" -#ifdef XMRIG_ALGO_RANDOMX -# include "backend/opencl/runners/tools/OclRxDataset.h" -#endif - - using cl_context = struct _cl_context *; @@ -72,10 +67,6 @@ public: const OclDevice device; const OclPlatform platform; const OclThread thread; - -# ifdef XMRIG_ALGO_RANDOMX - OclRxDatasetPtr dataset; -# endif }; diff --git a/src/backend/opencl/opencl.cmake b/src/backend/opencl/opencl.cmake index d45ef5e33..376757170 100644 --- a/src/backend/opencl/opencl.cmake +++ b/src/backend/opencl/opencl.cmake @@ -80,7 +80,6 @@ if (WITH_OPENCL) src/backend/opencl/runners/OclRxBaseRunner.h src/backend/opencl/runners/OclRxJitRunner.h src/backend/opencl/runners/OclRxVmRunner.h - src/backend/opencl/runners/tools/OclRxDataset.h ) list(APPEND SOURCES_BACKEND_OPENCL @@ -97,7 +96,6 @@ if (WITH_OPENCL) src/backend/opencl/runners/OclRxBaseRunner.cpp src/backend/opencl/runners/OclRxJitRunner.cpp src/backend/opencl/runners/OclRxVmRunner.cpp - src/backend/opencl/runners/tools/OclRxDataset.cpp ) endif() diff --git a/src/backend/opencl/runners/OclRxBaseRunner.cpp b/src/backend/opencl/runners/OclRxBaseRunner.cpp index 767b10834..0d6915a89 100644 --- a/src/backend/opencl/runners/OclRxBaseRunner.cpp +++ b/src/backend/opencl/runners/OclRxBaseRunner.cpp @@ -30,6 +30,7 @@ #include "backend/opencl/kernels/rx/FindSharesKernel.h" #include "backend/opencl/kernels/rx/HashAesKernel.h" #include "backend/opencl/OclLaunchData.h" +#include "backend/opencl/runners/tools/OclSharedState.h" #include "backend/opencl/wrappers/OclLib.h" #include "base/net/stratum/Job.h" #include "crypto/rx/Rx.h" @@ -120,7 +121,7 @@ void xmrig::OclRxBaseRunner::set(const Job &job, uint8_t *blob) m_seed = job.seed(); auto dataset = Rx::dataset(job, 0); - enqueueWriteBuffer(data().dataset->get(), CL_TRUE, 0, dataset->size(), dataset->raw()); + enqueueWriteBuffer(m_dataset, CL_TRUE, 0, dataset->size(), dataset->raw()); } if (job.size() < Job::kMaxBlobSize) { @@ -177,4 +178,5 @@ void xmrig::OclRxBaseRunner::init() m_hashes = createSubBuffer(CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS, 64 * m_intensity); m_entropy = createSubBuffer(CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS, (128 + 2560) * m_intensity); m_rounding = createSubBuffer(CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS, sizeof(uint32_t) * m_intensity); + m_dataset = OclSharedState::get(data().device.index()).dataset(); } diff --git a/src/backend/opencl/runners/OclRxBaseRunner.h b/src/backend/opencl/runners/OclRxBaseRunner.h index 48b0ff8e8..c7770e6c7 100644 --- a/src/backend/opencl/runners/OclRxBaseRunner.h +++ b/src/backend/opencl/runners/OclRxBaseRunner.h @@ -62,6 +62,7 @@ protected: Blake2bHashRegistersKernel *m_blake2b_hash_registers_64 = nullptr; Blake2bInitialHashKernel *m_blake2b_initial_hash = nullptr; Buffer m_seed; + cl_mem m_dataset = nullptr; cl_mem m_entropy = nullptr; cl_mem m_hashes = nullptr; cl_mem m_rounding = nullptr; diff --git a/src/backend/opencl/runners/OclRxJitRunner.cpp b/src/backend/opencl/runners/OclRxJitRunner.cpp index 29020fe17..ca3163336 100644 --- a/src/backend/opencl/runners/OclRxJitRunner.cpp +++ b/src/backend/opencl/runners/OclRxJitRunner.cpp @@ -74,7 +74,7 @@ void xmrig::OclRxJitRunner::build() } m_randomx_run = new RxRunKernel(m_asmProgram); - m_randomx_run->setArgs(data().dataset->get(), m_scratchpads, m_registers, m_rounding, m_programs, m_intensity, m_algorithm); + m_randomx_run->setArgs(m_dataset, m_scratchpads, m_registers, m_rounding, m_programs, m_intensity, m_algorithm); } diff --git a/src/backend/opencl/runners/OclRxVmRunner.cpp b/src/backend/opencl/runners/OclRxVmRunner.cpp index 193b68f02..3a30d5610 100644 --- a/src/backend/opencl/runners/OclRxVmRunner.cpp +++ b/src/backend/opencl/runners/OclRxVmRunner.cpp @@ -70,7 +70,7 @@ void xmrig::OclRxVmRunner::build() m_init_vm->setArgs(m_entropy, m_vm_states, m_rounding); m_execute_vm = new ExecuteVmKernel(m_program); - m_execute_vm->setArgs(m_vm_states, m_rounding, m_scratchpads, data().dataset->get(), m_intensity); + m_execute_vm->setArgs(m_vm_states, m_rounding, m_scratchpads, m_dataset, m_intensity); } diff --git a/src/backend/opencl/runners/tools/OclRxDataset.cpp b/src/backend/opencl/runners/tools/OclRxDataset.cpp deleted file mode 100644 index b1d83bd0b..000000000 --- a/src/backend/opencl/runners/tools/OclRxDataset.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include "backend/opencl/runners/tools/OclRxDataset.h" -#include "backend/opencl/wrappers/OclLib.h" -#include "crypto/rx/Rx.h" -#include "crypto/rx/RxDataset.h" - - -void xmrig::OclRxDataset::createBuffer(cl_context ctx, const Job &job, bool host) -{ - if (m_dataset) { - return; - } - - cl_int ret; - - if (host) { - auto dataset = Rx::dataset(job, 0); - - m_dataset = OclLib::createBuffer(ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, RxDataset::maxSize(), dataset->raw(), &ret); - } - else { - m_dataset = OclLib::createBuffer(ctx, CL_MEM_READ_ONLY, RxDataset::maxSize(), nullptr, &ret); - } -} - - -xmrig::OclRxDataset::~OclRxDataset() -{ - OclLib::release(m_dataset); -} diff --git a/src/backend/opencl/runners/tools/OclRxDataset.h b/src/backend/opencl/runners/tools/OclRxDataset.h deleted file mode 100644 index 314a2f5d6..000000000 --- a/src/backend/opencl/runners/tools/OclRxDataset.h +++ /dev/null @@ -1,68 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef XMRIG_OCLRXDATASET_H -#define XMRIG_OCLRXDATASET_H - - -#include "base/tools/Object.h" - - -#include - - -using cl_context = struct _cl_context *; -using cl_mem = struct _cl_mem *; - - -namespace xmrig { - - -class Job; - - -class OclRxDataset -{ -public: - XMRIG_DISABLE_COPY_MOVE(OclRxDataset) - - OclRxDataset() = default; - ~OclRxDataset(); - - inline cl_mem get() const { return m_dataset; } - - void createBuffer(cl_context ctx,const Job &job, bool host); - -private: - cl_mem m_dataset = nullptr; -}; - - -using OclRxDatasetPtr = std::shared_ptr; - - -} /* namespace xmrig */ - - -#endif /* XMRIG_OCLINTERLEAVE_H */ diff --git a/src/backend/opencl/runners/tools/OclSharedData.cpp b/src/backend/opencl/runners/tools/OclSharedData.cpp index 3aed25380..24ef5aff0 100644 --- a/src/backend/opencl/runners/tools/OclSharedData.cpp +++ b/src/backend/opencl/runners/tools/OclSharedData.cpp @@ -24,11 +24,15 @@ #include "backend/opencl/runners/tools/OclSharedData.h" +#include "backend/opencl/wrappers/OclLib.h" #include "base/io/log/Log.h" #include "base/tools/Chrono.h" +#include "crypto/rx/Rx.h" +#include "crypto/rx/RxDataset.h" #include +#include #include @@ -107,6 +111,14 @@ uint64_t xmrig::OclSharedData::resumeDelay(size_t id) } +void xmrig::OclSharedData::release() +{ +# ifdef XMRIG_ALGO_RANDOMX + OclLib::release(m_dataset); +# endif +} + + void xmrig::OclSharedData::setResumeCounter(uint32_t value) { if (m_threads < 2) { @@ -128,3 +140,34 @@ void xmrig::OclSharedData::setRunTime(uint64_t time) std::lock_guard lock(m_mutex); m_averageRunTime = m_averageRunTime * (1.0 - averagingBias) + time * averagingBias; } + + +#ifdef XMRIG_ALGO_RANDOMX +cl_mem xmrig::OclSharedData::dataset() const +{ + if (!m_dataset) { + throw std::runtime_error("RandomX dataset is not available"); + } + + return m_dataset; +} + + +void xmrig::OclSharedData::createDataset(cl_context ctx, const Job &job, bool host) +{ + if (m_dataset) { + return; + } + + cl_int ret; + + if (host) { + auto dataset = Rx::dataset(job, 0); + + m_dataset = OclLib::createBuffer(ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, RxDataset::maxSize(), dataset->raw(), &ret); + } + else { + m_dataset = OclLib::createBuffer(ctx, CL_MEM_READ_ONLY, RxDataset::maxSize(), nullptr, &ret); + } +} +#endif diff --git a/src/backend/opencl/runners/tools/OclSharedData.h b/src/backend/opencl/runners/tools/OclSharedData.h index 05f725a6d..75ce04830 100644 --- a/src/backend/opencl/runners/tools/OclSharedData.h +++ b/src/backend/opencl/runners/tools/OclSharedData.h @@ -30,9 +30,16 @@ #include +using cl_context = struct _cl_context *; +using cl_mem = struct _cl_mem *; + + namespace xmrig { +class Job; + + class OclSharedData { public: @@ -40,11 +47,17 @@ public: uint64_t adjustDelay(size_t id); uint64_t resumeDelay(size_t id); + void release(); void setResumeCounter(uint32_t value); void setRunTime(uint64_t time); inline OclSharedData &operator++() { ++m_threads; return *this; } +# ifdef XMRIG_ALGO_RANDOMX + cl_mem dataset() const; + void createDataset(cl_context ctx, const Job &job, bool host); +# endif + private: double m_averageRunTime = 0.0; double m_threshold = 0.95; @@ -52,12 +65,13 @@ private: std::mutex m_mutex; uint32_t m_resumeCounter = 0; uint64_t m_timestamp = 0; + +# ifdef XMRIG_ALGO_RANDOMX + cl_mem m_dataset = nullptr; +# endif }; -using OclSharedDataPtr = std::shared_ptr; - - } /* namespace xmrig */ diff --git a/src/backend/opencl/runners/tools/OclSharedState.cpp b/src/backend/opencl/runners/tools/OclSharedState.cpp index 608fefbba..0b16a301f 100644 --- a/src/backend/opencl/runners/tools/OclSharedState.cpp +++ b/src/backend/opencl/runners/tools/OclSharedState.cpp @@ -48,15 +48,27 @@ xmrig::OclSharedData &xmrig::OclSharedState::get(uint32_t index) void xmrig::OclSharedState::release() { + for (auto &kv : map) { + kv.second.release(); + } + map.clear(); } -void xmrig::OclSharedState::start(const std::vector &threads) +void xmrig::OclSharedState::start(const std::vector &threads, const Job &job) { assert(map.empty()); for (const auto &data : threads) { - ++map[data.device.index()]; + auto &sharedData = map[data.device.index()]; + + ++sharedData; + +# ifdef XMRIG_ALGO_RANDOMX + if (data.algorithm.family() == Algorithm::RANDOM_X) { + sharedData.createDataset(data.ctx, job, data.thread.isDatasetHost()); + } +# endif } } diff --git a/src/backend/opencl/runners/tools/OclSharedState.h b/src/backend/opencl/runners/tools/OclSharedState.h index ed7c4f166..610056cd8 100644 --- a/src/backend/opencl/runners/tools/OclSharedState.h +++ b/src/backend/opencl/runners/tools/OclSharedState.h @@ -37,8 +37,7 @@ class OclSharedState public: static OclSharedData &get(uint32_t index); static void release(); - static void start(const std::vector &threads); - + static void start(const std::vector &threads, const Job &job); }; diff --git a/src/backend/opencl/wrappers/OclContext.cpp b/src/backend/opencl/wrappers/OclContext.cpp index a25593350..eed4272c8 100644 --- a/src/backend/opencl/wrappers/OclContext.cpp +++ b/src/backend/opencl/wrappers/OclContext.cpp @@ -24,6 +24,7 @@ #include "backend/opencl/wrappers/OclContext.h" +#include "backend/opencl/runners/tools/OclSharedState.h" #include "backend/opencl/wrappers/OclLib.h" @@ -42,7 +43,7 @@ xmrig::OclContext::~OclContext() } -bool xmrig::OclContext::init(const std::vector &devices, std::vector &threads, const Job &job) +bool xmrig::OclContext::init(const std::vector &devices, std::vector &threads) { if (!m_ctx) { std::vector ids(devices.size()); @@ -59,12 +60,6 @@ bool xmrig::OclContext::init(const std::vector &devices, std::vector< for (OclLaunchData &data : threads) { data.ctx = m_ctx; - -# ifdef XMRIG_ALGO_RANDOMX - if (data.algorithm.family() == Algorithm::RANDOM_X) { - data.dataset->createBuffer(m_ctx, job, data.thread.isDatasetHost()); - } -# endif } return true; diff --git a/src/backend/opencl/wrappers/OclContext.h b/src/backend/opencl/wrappers/OclContext.h index 8f2cc432c..fb45358e9 100644 --- a/src/backend/opencl/wrappers/OclContext.h +++ b/src/backend/opencl/wrappers/OclContext.h @@ -49,7 +49,7 @@ public: OclContext(const OclDevice &device); ~OclContext(); - bool init(const std::vector &devices, std::vector &threads, const Job &job); + bool init(const std::vector &devices, std::vector &threads); inline bool isValid() const { return m_ctx != nullptr; } inline cl_context ctx() const { return m_ctx; }