From 1ad30d50a62cb30e2b79e47f7c5dada619cee738 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 5 Sep 2019 09:27:29 +0700 Subject: [PATCH] Define double OpenCL threads in simple way. --- src/backend/cpu/CpuThread.h | 2 +- src/backend/opencl/OclBackend.cpp | 4 +- src/backend/opencl/OclConfig.cpp | 8 ++- src/backend/opencl/OclLaunchData.cpp | 3 +- src/backend/opencl/OclLaunchData.h | 5 +- src/backend/opencl/OclThread.cpp | 73 ++++++++++++----------- src/backend/opencl/OclThread.h | 22 +++---- src/backend/opencl/OclWorker.cpp | 2 +- src/backend/opencl/wrappers/OclDevice.cpp | 9 +-- src/backend/opencl/wrappers/OclDevice.h | 6 +- src/net/JobResult.h | 6 +- 11 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/backend/cpu/CpuThread.h b/src/backend/cpu/CpuThread.h index a56c4bd3f..91d63a1c9 100644 --- a/src/backend/cpu/CpuThread.h +++ b/src/backend/cpu/CpuThread.h @@ -35,7 +35,7 @@ namespace xmrig { class CpuThread { public: - inline constexpr CpuThread() {} + inline constexpr CpuThread() = default; inline constexpr CpuThread(int64_t affinity, uint32_t intensity) : m_affinity(affinity), m_intensity(intensity) {} CpuThread(const rapidjson::Value &value); diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index d5cf5eac0..d53c0db92 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -227,7 +227,7 @@ void xmrig::OclBackend::printHashrate(bool details) for (const OclLaunchData &data : d_ptr->threads) { Log::print("| %8zu | %8" PRId64 " | %7s | %7s | %7s |" CYAN_BOLD(" #%u") YELLOW(" %s") " %s", i, - data.thread.affinity(), + data.affinity, Hashrate::format(hashrate()->calc(i, Hashrate::ShortInterval), num, sizeof num / 3), Hashrate::format(hashrate()->calc(i, Hashrate::MediumInterval), num + 8, sizeof num / 3), Hashrate::format(hashrate()->calc(i, Hashrate::LargeInterval), num + 8 * 2, sizeof num / 3), @@ -341,7 +341,7 @@ rapidjson::Value xmrig::OclBackend::toJSON(rapidjson::Document &doc) const for (const OclLaunchData &data : d_ptr->threads) { Value thread(kObjectType); thread.AddMember("intensity", data.thread.intensity(), allocator); - thread.AddMember("affinity", data.thread.affinity(), allocator); + thread.AddMember("affinity", data.affinity, allocator); Value hashrate(kArrayType); hashrate.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); diff --git a/src/backend/opencl/OclConfig.cpp b/src/backend/opencl/OclConfig.cpp index 20978429e..b78af3fd8 100644 --- a/src/backend/opencl/OclConfig.cpp +++ b/src/backend/opencl/OclConfig.cpp @@ -96,7 +96,7 @@ xmrig::OclPlatform xmrig::OclConfig::platform() const { const auto platforms = OclPlatform::get(); if (platforms.empty()) { - return OclPlatform(); + return {}; } if (!m_platformVendor.isEmpty()) { @@ -127,7 +127,7 @@ xmrig::OclPlatform xmrig::OclConfig::platform() const return platforms[m_platformIndex]; } - return OclPlatform(); + return {}; } @@ -166,7 +166,9 @@ std::vector xmrig::OclConfig::get(const Miner *miner, cons continue; } - out.emplace_back(miner, algorithm, *this, platform, thread, devices[thread.index()]); + for (int64_t affinity : thread.threads()) { + out.emplace_back(miner, algorithm, *this, platform, thread, devices[thread.index()], affinity); + } } return out; diff --git a/src/backend/opencl/OclLaunchData.cpp b/src/backend/opencl/OclLaunchData.cpp index fc83ce531..f09e7b195 100644 --- a/src/backend/opencl/OclLaunchData.cpp +++ b/src/backend/opencl/OclLaunchData.cpp @@ -28,9 +28,10 @@ #include "backend/opencl/OclConfig.h" -xmrig::OclLaunchData::OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclPlatform &platform, const OclThread &thread, const OclDevice &device) : +xmrig::OclLaunchData::OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclPlatform &platform, const OclThread &thread, const OclDevice &device, int64_t affinity) : algorithm(algorithm), cache(config.isCacheEnabled()), + affinity(affinity), miner(miner), device(device), platform(platform), diff --git a/src/backend/opencl/OclLaunchData.h b/src/backend/opencl/OclLaunchData.h index b4adb85c9..9b63e8e75 100644 --- a/src/backend/opencl/OclLaunchData.h +++ b/src/backend/opencl/OclLaunchData.h @@ -34,7 +34,7 @@ #include "crypto/common/Nonce.h" -typedef struct _cl_context *cl_context; +using cl_context = struct _cl_context *; namespace xmrig { @@ -47,7 +47,7 @@ class Miner; class OclLaunchData { public: - OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclPlatform &platform, const OclThread &thread, const OclDevice &device); + OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclPlatform &platform, const OclThread &thread, const OclDevice &device, int64_t affinity); bool isEqual(const OclLaunchData &other) const; @@ -59,6 +59,7 @@ public: cl_context ctx = nullptr; const Algorithm algorithm; const bool cache; + const int64_t affinity; const Miner *miner; const OclDevice device; const OclPlatform platform; diff --git a/src/backend/opencl/OclThread.cpp b/src/backend/opencl/OclThread.cpp index fbd8ac29f..940a907a9 100644 --- a/src/backend/opencl/OclThread.cpp +++ b/src/backend/opencl/OclThread.cpp @@ -23,22 +23,21 @@ */ -#include - - #include "backend/opencl/OclThread.h" + #include "base/io/json/Json.h" #include "rapidjson/document.h" +#include + + namespace xmrig { -static const char *kAffinity = "affinity"; -static const char *kCompMode = "comp_mode"; static const char *kIndex = "index"; static const char *kIntensity = "intensity"; -static const char *kMemChunk = "mem_chunk"; static const char *kStridedIndex = "strided_index"; +static const char *kThreads = "threads"; static const char *kUnroll = "unroll"; static const char *kWorksize = "worksize"; @@ -53,14 +52,11 @@ static const char* kDatasetHost = "dataset_host"; xmrig::OclThread::OclThread(const rapidjson::Value &value) { - m_index = Json::getUint(value, kIndex); - m_intensity = Json::getUint(value, kIntensity); - m_worksize = Json::getUint(value, kWorksize); - m_affinity = Json::getInt64(value, kAffinity, -1); - m_memChunk = std::min(Json::getUint(value, kMemChunk, m_memChunk), 18u); - m_compMode = Json::getBool(value, kCompMode, m_compMode); + m_index = Json::getUint(value, kIndex); + m_worksize = std::max(std::min(Json::getUint(value, kWorksize), 128u), 1u); + m_unrollFactor = std::max(std::min(Json::getUint(value, kUnroll, m_unrollFactor), 128u), 1u); - setUnrollFactor(Json::getUint(value, kUnroll, m_unrollFactor)); + setIntensity(Json::getUint(value, kIntensity)); # ifdef XMRIG_ALGO_RANDOMX m_bfactor = Json::getUint(value, kBFactor, 6); @@ -68,20 +64,31 @@ xmrig::OclThread::OclThread(const rapidjson::Value &value) m_datasetHost = Json::getInt(value, kDatasetHost, m_datasetHost); # endif - const rapidjson::Value &stridedIndex = Json::getValue(value, kStridedIndex); - if (stridedIndex.IsBool()) { - m_stridedIndex = stridedIndex.GetBool() ? 1 : 0; + const rapidjson::Value &si = Json::getArray(value, kStridedIndex); + if (si.IsArray() && si.Size() >= 2) { + m_stridedIndex = std::min(si[0].GetUint(), 2u); + m_memChunk = std::min(si[1].GetUint(), 18u); } - else if (stridedIndex.IsUint()) { - m_stridedIndex = std::min(stridedIndex.GetUint(), 2u); + + const rapidjson::Value &threads = Json::getArray(value, kThreads); + if (threads.IsArray()) { + m_threads.reserve(threads.Size()); + + for (const auto &affinity : threads.GetArray()) { + m_threads.emplace_back(affinity.GetInt64()); + } + } + + if (m_threads.empty()) { + m_threads.emplace_back(-1); } } bool xmrig::OclThread::isEqual(const OclThread &other) const { - return other.m_compMode == m_compMode && - other.m_affinity == m_affinity && + return other.m_threads.size() == m_threads.size() && + std::equal(m_threads.begin(), m_threads.end(), other.m_threads.begin()) && other.m_bfactor == m_bfactor && other.m_datasetHost == m_datasetHost && other.m_gcnAsm == m_gcnAsm && @@ -104,18 +111,22 @@ rapidjson::Value xmrig::OclThread::toJSON(rapidjson::Document &doc) const out.AddMember(StringRef(kIndex), index(), allocator); out.AddMember(StringRef(kIntensity), intensity(), allocator); out.AddMember(StringRef(kWorksize), worksize(), allocator); - out.AddMember(StringRef(kStridedIndex), stridedIndex(), allocator); - if (stridedIndex() == 2) { - out.AddMember(StringRef(kMemChunk), memChunk(), allocator); + Value si(kArrayType); + si.Reserve(2, allocator); + si.PushBack(stridedIndex(), allocator); + si.PushBack(memChunk(), allocator); + + Value threads(kArrayType); + threads.Reserve(m_threads.size(), allocator); + + for (auto thread : m_threads) { + threads.PushBack(thread, allocator); } + out.AddMember(StringRef(kStridedIndex), si, allocator); + out.AddMember(StringRef(kThreads), threads, allocator); out.AddMember(StringRef(kUnroll), unrollFactor(), allocator); - out.AddMember(StringRef(kAffinity), affinity(), allocator); - - if (isCompMode()) { - out.AddMember(StringRef(kCompMode), true, allocator); - } # ifdef XMRIG_ALGO_RANDOMX if (m_datasetHost != -1) { @@ -127,9 +138,3 @@ rapidjson::Value xmrig::OclThread::toJSON(rapidjson::Document &doc) const return out; } - - -void xmrig::OclThread::setUnrollFactor(uint32_t unrollFactor) -{ - m_unrollFactor = unrollFactor == 0 ? 1 : std::min(unrollFactor, 128u); -} diff --git a/src/backend/opencl/OclThread.h b/src/backend/opencl/OclThread.h index 3c6a98c9b..96ea10a69 100644 --- a/src/backend/opencl/OclThread.h +++ b/src/backend/opencl/OclThread.h @@ -29,27 +29,30 @@ #include "rapidjson/fwd.h" +#include + + namespace xmrig { class OclThread { public: - OclThread() = default; - OclThread(uint32_t index, uint32_t intensity, uint32_t worksize, uint32_t stridedIndex, uint32_t memChunk, int64_t affinity = -1) : - m_affinity(affinity), + OclThread() = delete; + OclThread(uint32_t index, uint32_t intensity, uint32_t worksize, uint32_t stridedIndex, uint32_t memChunk, uint32_t threads) : + m_threads(threads, -1), m_index(index), - m_intensity(intensity), m_memChunk(memChunk), m_stridedIndex(stridedIndex), m_worksize(worksize) - {} + { + setIntensity(intensity); + } OclThread(const rapidjson::Value &value); - inline bool isCompMode() const { return m_compMode; } inline bool isValid() const { return m_intensity > 0; } - inline int64_t affinity() const { return m_affinity; } + inline const std::vector &threads() const { return m_threads; } inline uint32_t bfactor() const { return m_bfactor; } inline uint32_t datasetHost() const { return m_datasetHost < 0 ? 0 : static_cast(m_datasetHost); } inline uint32_t gcnAsm() const { return m_gcnAsm; } @@ -67,11 +70,10 @@ public: rapidjson::Value toJSON(rapidjson::Document &doc) const; private: - void setUnrollFactor(uint32_t unrollFactor); + inline void setIntensity(uint32_t intensity) { m_intensity = intensity / m_worksize * m_worksize; } - bool m_compMode = false; int m_datasetHost = -1; - int64_t m_affinity = -1; + std::vector m_threads; uint32_t m_bfactor = 6; uint32_t m_gcnAsm = 1; uint32_t m_index = 0; diff --git a/src/backend/opencl/OclWorker.cpp b/src/backend/opencl/OclWorker.cpp index 6b6c0da01..ac8c0b2dd 100644 --- a/src/backend/opencl/OclWorker.cpp +++ b/src/backend/opencl/OclWorker.cpp @@ -54,7 +54,7 @@ static inline uint32_t roundSize(uint32_t intensity) { return kReserveCount / in xmrig::OclWorker::OclWorker(size_t id, const OclLaunchData &data) : - Worker(id, data.thread.affinity(), -1), + Worker(id, data.affinity, -1), m_algorithm(data.algorithm), m_miner(data.miner), m_intensity(data.thread.intensity()) diff --git a/src/backend/opencl/wrappers/OclDevice.cpp b/src/backend/opencl/wrappers/OclDevice.cpp index 3de323c34..179f2b692 100644 --- a/src/backend/opencl/wrappers/OclDevice.cpp +++ b/src/backend/opencl/wrappers/OclDevice.cpp @@ -184,14 +184,9 @@ void xmrig::OclDevice::generate(const Algorithm &algorithm, OclThreads &threads) const uint32_t worksize = getWorksize(algorithm); const uint32_t stridedIndex = getStridedIndex(algorithm); const uint32_t memChunk = getMemChunk(algorithm); + const uint32_t threadCount = ((globalMem() - intensity * 2 * algorithm.l3()) > 128 * oneMiB) ? 2 : 1; - intensity -= intensity % worksize; - - threads.add(OclThread(index(), intensity, worksize, stridedIndex, memChunk)); - - if ((globalMem() - intensity * 2 * algorithm.l3()) > 128 * oneMiB) { - threads.add(OclThread(index(), intensity, worksize, stridedIndex, memChunk)); - } + threads.add(OclThread(index(), intensity, worksize, stridedIndex, memChunk, threadCount)); } diff --git a/src/backend/opencl/wrappers/OclDevice.h b/src/backend/opencl/wrappers/OclDevice.h index 25d574c88..8b502da95 100644 --- a/src/backend/opencl/wrappers/OclDevice.h +++ b/src/backend/opencl/wrappers/OclDevice.h @@ -34,8 +34,8 @@ #include "base/tools/String.h" -typedef struct _cl_device_id *cl_device_id; -typedef struct _cl_platform_id *cl_platform_id; +using cl_device_id = struct _cl_device_id *; +using cl_platform_id = struct _cl_platform_id *; namespace xmrig { @@ -59,7 +59,7 @@ public: Navi_10 }; - OclDevice() = default; + OclDevice() = delete; OclDevice(uint32_t index, cl_device_id id, cl_platform_id platform); size_t freeMem() const; diff --git a/src/net/JobResult.h b/src/net/JobResult.h index aed20841a..87bf3be92 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -28,7 +28,7 @@ #include -#include +#include #include "base/tools/String.h" @@ -41,7 +41,7 @@ namespace xmrig { class JobResult { public: - inline JobResult() {} + JobResult() = delete; inline JobResult(const Job &job, uint32_t nonce, const uint8_t *result) : algorithm(job.algorithm()), @@ -66,7 +66,7 @@ public: const uint8_t index = 0; private: - uint8_t m_result[32]; + uint8_t m_result[32] = { 0 }; };