From f4943b77f3bce6a1988c453c527b64bfabfcc94d Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 30 Sep 2019 05:18:53 +0700 Subject: [PATCH] Class OclInterleave renamed to OclSharedData and added class OclSharedState. --- src/backend/opencl/OclBackend.cpp | 5 ++ src/backend/opencl/OclConfig.cpp | 3 - src/backend/opencl/OclLaunchData.h | 3 +- src/backend/opencl/OclWorker.cpp | 20 +++--- src/backend/opencl/OclWorker.h | 2 +- src/backend/opencl/opencl.cmake | 6 +- .../tools/OclSharedData.cpp} | 22 +++++-- .../tools/OclSharedData.h} | 17 ++--- .../opencl/runners/tools/OclSharedState.cpp | 62 +++++++++++++++++++ .../opencl/runners/tools/OclSharedState.h | 48 ++++++++++++++ 10 files changed, 154 insertions(+), 34 deletions(-) rename src/backend/opencl/{OclInterleave.cpp => runners/tools/OclSharedData.cpp} (89%) rename src/backend/opencl/{OclInterleave.h => runners/tools/OclSharedData.h} (84%) create mode 100644 src/backend/opencl/runners/tools/OclSharedState.cpp create mode 100644 src/backend/opencl/runners/tools/OclSharedState.h diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 9b226811f..718fd9660 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -35,6 +35,7 @@ #include "backend/opencl/OclConfig.h" #include "backend/opencl/OclLaunchData.h" #include "backend/opencl/OclWorker.h" +#include "backend/opencl/runners/tools/OclSharedState.h" #include "backend/opencl/wrappers/OclContext.h" #include "backend/opencl/wrappers/OclLib.h" #include "base/io/log/Log.h" @@ -194,6 +195,8 @@ public: i++; } + OclSharedState::start(threads); + status.start(threads.size()); workers.start(threads); } @@ -371,6 +374,8 @@ void xmrig::OclBackend::stop() d_ptr->workers.stop(); d_ptr->threads.clear(); + OclSharedState::release(); + LOG_INFO("%s" YELLOW(" stopped") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts); } diff --git a/src/backend/opencl/OclConfig.cpp b/src/backend/opencl/OclConfig.cpp index 7424dba7a..fc4f9ef0c 100644 --- a/src/backend/opencl/OclConfig.cpp +++ b/src/backend/opencl/OclConfig.cpp @@ -198,11 +198,8 @@ std::vector xmrig::OclConfig::get(const Miner *miner, cons # endif if (thread.threads().size() > 1) { - auto interleave = std::make_shared(thread.threads().size()); - for (int64_t affinity : thread.threads()) { OclLaunchData data(miner, algorithm, *this, platform, thread, devices[thread.index()], affinity); - data.interleave = interleave; # ifdef XMRIG_ALGO_RANDOMX data.dataset = dataset; diff --git a/src/backend/opencl/OclLaunchData.h b/src/backend/opencl/OclLaunchData.h index ab4ca09b9..8c2def2d5 100644 --- a/src/backend/opencl/OclLaunchData.h +++ b/src/backend/opencl/OclLaunchData.h @@ -27,8 +27,8 @@ #define XMRIG_OCLLAUNCHDATA_H -#include "backend/opencl/OclInterleave.h" #include "backend/opencl/OclThread.h" +#include "backend/opencl/runners/tools/OclSharedData.h" #include "backend/opencl/wrappers/OclDevice.h" #include "backend/opencl/wrappers/OclPlatform.h" #include "crypto/common/Algorithm.h" @@ -72,7 +72,6 @@ public: const OclDevice device; const OclPlatform platform; const OclThread thread; - OclInterleavePtr interleave; # ifdef XMRIG_ALGO_RANDOMX OclRxDatasetPtr dataset; diff --git a/src/backend/opencl/OclWorker.cpp b/src/backend/opencl/OclWorker.cpp index b4132e3dd..f79bc59b6 100644 --- a/src/backend/opencl/OclWorker.cpp +++ b/src/backend/opencl/OclWorker.cpp @@ -28,6 +28,8 @@ #include "backend/common/Tags.h" #include "backend/opencl/runners/OclCnRunner.h" +#include "backend/opencl/runners/tools/OclSharedData.h" +#include "backend/opencl/runners/tools/OclSharedState.h" #include "base/io/log/Log.h" #include "base/tools/Chrono.h" #include "core/Miner.h" @@ -75,7 +77,7 @@ xmrig::OclWorker::OclWorker(size_t id, const OclLaunchData &data) : m_algorithm(data.algorithm), m_miner(data.miner), m_intensity(data.thread.intensity()), - m_interleave(data.interleave) + m_sharedData(OclSharedState::get(data.device.index())) { switch (m_algorithm.family()) { case Algorithm::RANDOM_X: @@ -149,9 +151,7 @@ void xmrig::OclWorker::start() while (Nonce::sequence(Nonce::OPENCL) > 0) { if (!isReady()) { - if (m_interleave) { - m_interleave->setResumeCounter(0); - } + m_sharedData.setResumeCounter(0); do { std::this_thread::sleep_for(std::chrono::milliseconds(200)); @@ -162,9 +162,7 @@ void xmrig::OclWorker::start() break; } - if (m_interleave) { - m_interleave->resumeDelay(m_id); - } + m_sharedData.resumeDelay(m_id); if (!consumeJob()) { return; @@ -172,9 +170,7 @@ void xmrig::OclWorker::start() } while (!Nonce::isOutdated(Nonce::OPENCL, m_job.sequence())) { - if (m_interleave) { - m_interleave->adjustDelay(m_id); - } + m_sharedData.adjustDelay(m_id); const uint64_t t = Chrono::steadyMSecs(); @@ -233,9 +229,7 @@ void xmrig::OclWorker::storeStats(uint64_t t) m_count += m_intensity; - if (m_interleave) { - m_interleave->setRunTime(Chrono::steadyMSecs() - t); - } + m_sharedData.setRunTime(Chrono::steadyMSecs() - t); Worker::storeStats(); } diff --git a/src/backend/opencl/OclWorker.h b/src/backend/opencl/OclWorker.h index 93de8afd7..76cb63dbc 100644 --- a/src/backend/opencl/OclWorker.h +++ b/src/backend/opencl/OclWorker.h @@ -67,7 +67,7 @@ private: const Miner *m_miner; const uint32_t m_intensity; IOclRunner *m_runner = nullptr; - OclInterleavePtr m_interleave; + OclSharedData &m_sharedData; WorkerJob<1> m_job; }; diff --git a/src/backend/opencl/opencl.cmake b/src/backend/opencl/opencl.cmake index c5a6335fb..d45ef5e33 100644 --- a/src/backend/opencl/opencl.cmake +++ b/src/backend/opencl/opencl.cmake @@ -14,7 +14,6 @@ if (WITH_OPENCL) src/backend/opencl/OclCache.h src/backend/opencl/OclConfig.h src/backend/opencl/OclGenerator.h - src/backend/opencl/OclInterleave.h src/backend/opencl/OclLaunchData.h src/backend/opencl/OclThread.h src/backend/opencl/OclThreads.h @@ -22,6 +21,8 @@ if (WITH_OPENCL) src/backend/opencl/runners/OclBaseRunner.h src/backend/opencl/runners/OclCnRunner.h src/backend/opencl/runners/tools/OclCnR.h + src/backend/opencl/runners/tools/OclSharedData.h + src/backend/opencl/runners/tools/OclSharedState.h src/backend/opencl/wrappers/OclContext.h src/backend/opencl/wrappers/OclDevice.h src/backend/opencl/wrappers/OclError.h @@ -42,7 +43,6 @@ if (WITH_OPENCL) src/backend/opencl/OclBackend.cpp src/backend/opencl/OclCache.cpp src/backend/opencl/OclConfig.cpp - src/backend/opencl/OclInterleave.cpp src/backend/opencl/OclLaunchData.cpp src/backend/opencl/OclThread.cpp src/backend/opencl/OclThreads.cpp @@ -50,6 +50,8 @@ if (WITH_OPENCL) src/backend/opencl/runners/OclBaseRunner.cpp src/backend/opencl/runners/OclCnRunner.cpp src/backend/opencl/runners/tools/OclCnR.cpp + src/backend/opencl/runners/tools/OclSharedData.cpp + src/backend/opencl/runners/tools/OclSharedState.cpp src/backend/opencl/wrappers/OclContext.cpp src/backend/opencl/wrappers/OclDevice.cpp src/backend/opencl/wrappers/OclError.cpp diff --git a/src/backend/opencl/OclInterleave.cpp b/src/backend/opencl/runners/tools/OclSharedData.cpp similarity index 89% rename from src/backend/opencl/OclInterleave.cpp rename to src/backend/opencl/runners/tools/OclSharedData.cpp index d7226f416..3aed25380 100644 --- a/src/backend/opencl/OclInterleave.cpp +++ b/src/backend/opencl/runners/tools/OclSharedData.cpp @@ -23,7 +23,7 @@ */ -#include "backend/opencl/OclInterleave.h" +#include "backend/opencl/runners/tools/OclSharedData.h" #include "base/io/log/Log.h" #include "base/tools/Chrono.h" @@ -32,8 +32,12 @@ #include -uint64_t xmrig::OclInterleave::adjustDelay(size_t id) +uint64_t xmrig::OclSharedData::adjustDelay(size_t id) { + if (m_threads < 2) { + return 0; + } + const uint64_t t0 = Chrono::steadyMSecs(); uint64_t delay = 0; @@ -69,8 +73,12 @@ uint64_t xmrig::OclInterleave::adjustDelay(size_t id) } -uint64_t xmrig::OclInterleave::resumeDelay(size_t id) +uint64_t xmrig::OclSharedData::resumeDelay(size_t id) { + if (m_threads < 2) { + return 0; + } + uint64_t delay = 0; { @@ -99,14 +107,18 @@ uint64_t xmrig::OclInterleave::resumeDelay(size_t id) } -void xmrig::OclInterleave::setResumeCounter(uint32_t value) +void xmrig::OclSharedData::setResumeCounter(uint32_t value) { + if (m_threads < 2) { + return; + } + std::lock_guard lock(m_mutex); m_resumeCounter = value; } -void xmrig::OclInterleave::setRunTime(uint64_t time) +void xmrig::OclSharedData::setRunTime(uint64_t time) { // averagingBias = 1.0 - only the last delta time is taken into account // averagingBias = 0.5 - the last delta time has the same weight as all the previous ones combined diff --git a/src/backend/opencl/OclInterleave.h b/src/backend/opencl/runners/tools/OclSharedData.h similarity index 84% rename from src/backend/opencl/OclInterleave.h rename to src/backend/opencl/runners/tools/OclSharedData.h index 663272955..05f725a6d 100644 --- a/src/backend/opencl/OclInterleave.h +++ b/src/backend/opencl/runners/tools/OclSharedData.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef XMRIG_OCLINTERLEAVE_H -#define XMRIG_OCLINTERLEAVE_H +#ifndef XMRIG_OCLSHAREDDATA_H +#define XMRIG_OCLSHAREDDATA_H #include @@ -33,31 +33,32 @@ namespace xmrig { -class OclInterleave +class OclSharedData { public: - OclInterleave() = delete; - inline OclInterleave(size_t threads) : m_threads(threads) {} + OclSharedData() = default; uint64_t adjustDelay(size_t id); uint64_t resumeDelay(size_t id); void setResumeCounter(uint32_t value); void setRunTime(uint64_t time); + inline OclSharedData &operator++() { ++m_threads; return *this; } + private: - const size_t m_threads; double m_averageRunTime = 0.0; double m_threshold = 0.95; + size_t m_threads = 0; std::mutex m_mutex; uint32_t m_resumeCounter = 0; uint64_t m_timestamp = 0; }; -using OclInterleavePtr = std::shared_ptr; +using OclSharedDataPtr = std::shared_ptr; } /* namespace xmrig */ -#endif /* XMRIG_OCLINTERLEAVE_H */ +#endif /* XMRIG_OCLSHAREDDATA_H */ diff --git a/src/backend/opencl/runners/tools/OclSharedState.cpp b/src/backend/opencl/runners/tools/OclSharedState.cpp new file mode 100644 index 000000000..608fefbba --- /dev/null +++ b/src/backend/opencl/runners/tools/OclSharedState.cpp @@ -0,0 +1,62 @@ +/* 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/OclSharedState.h" +#include "backend/opencl/runners/tools/OclSharedData.h" + + +#include +#include + + +namespace xmrig { + + +static std::map map; + + +} // namespace xmrig + + +xmrig::OclSharedData &xmrig::OclSharedState::get(uint32_t index) +{ + return map[index]; +} + + +void xmrig::OclSharedState::release() +{ + map.clear(); +} + + +void xmrig::OclSharedState::start(const std::vector &threads) +{ + assert(map.empty()); + + for (const auto &data : threads) { + ++map[data.device.index()]; + } +} diff --git a/src/backend/opencl/runners/tools/OclSharedState.h b/src/backend/opencl/runners/tools/OclSharedState.h new file mode 100644 index 000000000..ed7c4f166 --- /dev/null +++ b/src/backend/opencl/runners/tools/OclSharedState.h @@ -0,0 +1,48 @@ +/* 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_OCLSHAREDSTATE_H +#define XMRIG_OCLSHAREDSTATE_H + + +#include "backend/opencl/OclLaunchData.h" + + +namespace xmrig { + + +class OclSharedState +{ +public: + static OclSharedData &get(uint32_t index); + static void release(); + static void start(const std::vector &threads); + +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLSHAREDSTATE_H */