From d5f57c35e2a9ba94868df5a027f4ade1cd100645 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Aug 2019 19:47:22 +0700 Subject: [PATCH] Added OclBackend, OclConfig, OclLaunchData, OclThread and OclThreads stubs. --- src/backend/common/Threads.cpp | 9 + src/backend/common/Workers.cpp | 14 +- src/backend/common/Workers.h | 16 +- src/backend/opencl/OclBackend.cpp | 318 +++++++++++++++++++++++++++ src/backend/opencl/OclBackend.h | 75 +++++++ src/backend/opencl/OclConfig.cpp | 90 ++++++++ src/backend/opencl/OclConfig.h | 62 ++++++ src/backend/opencl/OclLaunchData.cpp | 48 ++++ src/backend/opencl/OclLaunchData.h | 65 ++++++ src/backend/opencl/OclThread.cpp | 58 +++++ src/backend/opencl/OclThread.h | 62 ++++++ src/backend/opencl/OclThreads.cpp | 71 ++++++ src/backend/opencl/OclThreads.h | 64 ++++++ src/backend/opencl/opencl.cmake | 10 + src/core/Miner.cpp | 9 + src/core/config/Config.cpp | 29 +++ src/core/config/Config.h | 15 +- 17 files changed, 1010 insertions(+), 5 deletions(-) create mode 100644 src/backend/opencl/OclBackend.cpp create mode 100644 src/backend/opencl/OclBackend.h create mode 100644 src/backend/opencl/OclConfig.cpp create mode 100644 src/backend/opencl/OclConfig.h create mode 100644 src/backend/opencl/OclLaunchData.cpp create mode 100644 src/backend/opencl/OclLaunchData.h create mode 100644 src/backend/opencl/OclThread.cpp create mode 100644 src/backend/opencl/OclThread.h create mode 100644 src/backend/opencl/OclThreads.cpp create mode 100644 src/backend/opencl/OclThreads.h diff --git a/src/backend/common/Threads.cpp b/src/backend/common/Threads.cpp index 17fc29511..09e41ebdb 100644 --- a/src/backend/common/Threads.cpp +++ b/src/backend/common/Threads.cpp @@ -28,6 +28,11 @@ #include "rapidjson/document.h" +#ifdef XMRIG_FEATURE_OPENCL +# include "backend/opencl/OclThreads.h" +#endif + + namespace xmrig { @@ -148,4 +153,8 @@ namespace xmrig { template class Threads; +#ifdef XMRIG_FEATURE_OPENCL +template class Threads; +#endif + } // namespace xmrig diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index 036bc8b66..6e89be38d 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -137,7 +137,7 @@ void xmrig::Workers::tick(uint64_t) template -xmrig::IWorker *xmrig::Workers::create(Thread *) +xmrig::IWorker *xmrig::Workers::create(Thread *) { return nullptr; } @@ -201,4 +201,16 @@ xmrig::IWorker *xmrig::Workers::create(Thread *han template class Workers; +#ifdef XMRIG_FEATURE_OPENCL +template<> +xmrig::IWorker *xmrig::Workers::create(Thread *handle) +{ + return nullptr; +} + + +template class Workers; +#endif + + } // namespace xmrig diff --git a/src/backend/common/Workers.h b/src/backend/common/Workers.h index 77dd434c2..bb35157a4 100644 --- a/src/backend/common/Workers.h +++ b/src/backend/common/Workers.h @@ -31,6 +31,11 @@ #include "backend/cpu/CpuLaunchData.h" +#ifdef XMRIG_FEATURE_OPENCL +# include "backend/opencl/OclLaunchData.h" +#endif + + namespace xmrig { @@ -52,7 +57,7 @@ public: void tick(uint64_t ticks); private: - static IWorker *create(Thread *handle); + static IWorker *create(Thread *handle); static void onReady(void *arg); std::vector *> m_workers; @@ -62,11 +67,16 @@ private: template<> IWorker *Workers::create(Thread *handle); - - extern template class Workers; +#ifdef XMRIG_FEATURE_OPENCL +template<> +IWorker *Workers::create(Thread *handle); +extern template class Workers; +#endif + + } // namespace xmrig diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp new file mode 100644 index 000000000..6c7bd7da1 --- /dev/null +++ b/src/backend/opencl/OclBackend.cpp @@ -0,0 +1,318 @@ +/* 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 + + +#include "backend/common/Hashrate.h" +#include "backend/common/interfaces/IWorker.h" +#include "backend/common/Workers.h" +#include "backend/opencl/OclBackend.h" +#include "backend/opencl/OclLaunchData.h" +#include "base/io/log/Log.h" +#include "base/net/stratum/Job.h" +#include "base/tools/Chrono.h" +#include "base/tools/String.h" +#include "core/config/Config.h" +#include "core/Controller.h" +#include "rapidjson/document.h" + + +#ifdef XMRIG_FEATURE_API +# include "base/api/interfaces/IApiRequest.h" +#endif + + +namespace xmrig { + + +extern template class Threads; + + +static const char *tag = MAGENTA_BG_BOLD(" ocl "); +static const String kType = "opencl"; + + +struct LaunchStatus +{ +public: + inline void reset() + { + hugePages = 0; + memory = 0; + pages = 0; + started = 0; + threads = 0; + ways = 0; + ts = Chrono::steadyMSecs(); + } + + size_t hugePages = 0; + size_t memory = 0; + size_t pages = 0; + size_t started = 0; + size_t threads = 0; + size_t ways = 0; + uint64_t ts = 0; +}; + + +class OclBackendPrivate +{ +public: + inline OclBackendPrivate(Controller *controller) : + controller(controller) + { + } + + + inline void start() + { + 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, + profileName.data(), + threads.size(), + algo.l3() / 1024 + ); + + workers.stop(); + + status.reset(); + status.memory = algo.l3(); + status.threads = threads.size(); + + for (const OclLaunchData &data : threads) { + status.ways += static_cast(data.intensity); + } + + workers.start(threads); + } + + + size_t ways() + { + std::lock_guard lock(mutex); + + return status.ways; + } + + + Algorithm algo; + Controller *controller; + LaunchStatus status; + std::mutex mutex; + std::vector threads; + String profileName; + Workers workers; +}; + + +} // namespace xmrig + + +xmrig::OclBackend::OclBackend(Controller *controller) : + d_ptr(new OclBackendPrivate(controller)) +{ + d_ptr->workers.setBackend(this); +} + + +xmrig::OclBackend::~OclBackend() +{ + delete d_ptr; +} + + +bool xmrig::OclBackend::isEnabled() const +{ + return d_ptr->controller->config()->cl().isEnabled(); +} + + +bool xmrig::OclBackend::isEnabled(const Algorithm &algorithm) const +{ + return !d_ptr->controller->config()->cl().threads().get(algorithm).isEmpty(); +} + + +const xmrig::Hashrate *xmrig::OclBackend::hashrate() const +{ + return d_ptr->workers.hashrate(); +} + + +const xmrig::String &xmrig::OclBackend::profileName() const +{ + return d_ptr->profileName; +} + + +const xmrig::String &xmrig::OclBackend::type() const +{ + return kType; +} + + +void xmrig::OclBackend::prepare(const Job &) +{ +} + + +void xmrig::OclBackend::printHashrate(bool details) +{ + if (!details || !hashrate()) { + return; + } + + char num[8 * 3] = { 0 }; + + Log::print(WHITE_BOLD_S "| OPENCL THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |"); + + size_t i = 0; + for (const OclLaunchData &data : d_ptr->threads) { + Log::print("| %13zu | %8" PRId64 " | %7s | %7s | %7s |", + i, + 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) + ); + + i++; + } +} + + +void xmrig::OclBackend::setJob(const Job &job) +{ + if (!isEnabled()) { + return stop(); + } + + const OclConfig &cl = d_ptr->controller->config()->cl(); + + std::vector threads = cl.get(d_ptr->controller->miner(), job.algorithm()); +// if (d_ptr->threads.size() == threads.size() && std::equal(d_ptr->threads.begin(), d_ptr->threads.end(), threads.begin())) { +// return; +// } + + d_ptr->algo = job.algorithm(); + d_ptr->profileName = cl.threads().profileName(job.algorithm()); + +// if (d_ptr->profileName.isNull() || threads.empty()) { +// d_ptr->workers.stop(); + +// LOG_WARN(YELLOW_BOLD_S "CPU disabled, no suitable configuration for algo %s", job.algorithm().shortName()); + +// return; +// } + + d_ptr->threads = std::move(threads); + d_ptr->start(); +} + + +void xmrig::OclBackend::start(IWorker *worker) +{ + d_ptr->mutex.lock(); + + d_ptr->status.started++; + + if (d_ptr->status.started == d_ptr->status.threads) { + } + + d_ptr->mutex.unlock(); + + worker->start(); +} + + +void xmrig::OclBackend::stop() +{ + const uint64_t ts = Chrono::steadyMSecs(); + + d_ptr->workers.stop(); + d_ptr->threads.clear(); + + LOG_INFO("%s" YELLOW(" stopped") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts); +} + + +void xmrig::OclBackend::tick(uint64_t ticks) +{ + d_ptr->workers.tick(ticks); +} + + +#ifdef XMRIG_FEATURE_API +rapidjson::Value xmrig::OclBackend::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + const CpuConfig &cpu = d_ptr->controller->config()->cpu(); + + Value out(kObjectType); + out.AddMember("type", type().toJSON(), allocator); + out.AddMember("enabled", isEnabled(), allocator); + out.AddMember("algo", d_ptr->algo.toJSON(), allocator); + out.AddMember("profile", profileName().toJSON(), allocator); + out.AddMember("hw-aes", cpu.isHwAES(), allocator); + out.AddMember("priority", cpu.priority(), allocator); + out.AddMember("memory", static_cast(d_ptr->algo.isValid() ? (d_ptr->ways() * d_ptr->algo.l3()) : 0), allocator); + + if (d_ptr->threads.empty() || !hashrate()) { + return out; + } + + Value threads(kArrayType); + const Hashrate *hr = hashrate(); + + size_t i = 0; + for (const OclLaunchData &data : d_ptr->threads) { + Value thread(kObjectType); + thread.AddMember("intensity", data.intensity, allocator); + thread.AddMember("affinity", data.affinity, allocator); + + Value hashrate(kArrayType); + hashrate.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); + hashrate.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); + hashrate.PushBack(Hashrate::normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); + + i++; + + thread.AddMember("hashrate", hashrate, allocator); + threads.PushBack(thread, allocator); + } + + out.AddMember("threads", threads, allocator); + + return out; +} + + +void xmrig::OclBackend::handleRequest(IApiRequest &) +{ +} +#endif diff --git a/src/backend/opencl/OclBackend.h b/src/backend/opencl/OclBackend.h new file mode 100644 index 000000000..bb44b109d --- /dev/null +++ b/src/backend/opencl/OclBackend.h @@ -0,0 +1,75 @@ +/* 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_OCLBACKEND_H +#define XMRIG_OCLBACKEND_H + + +#include + + +#include "backend/common/interfaces/IBackend.h" + + +namespace xmrig { + + +class Controller; +class OclBackendPrivate; +class Miner; + + +class OclBackend : public IBackend +{ +public: + OclBackend(Controller *controller); + ~OclBackend() override; + +protected: + bool isEnabled() const override; + bool isEnabled(const Algorithm &algorithm) const override; + const Hashrate *hashrate() const override; + const String &profileName() const override; + const String &type() const override; + void prepare(const Job &nextJob) override; + void printHashrate(bool details) override; + void setJob(const Job &job) override; + void start(IWorker *worker) override; + void stop() override; + void tick(uint64_t ticks) override; + +# ifdef XMRIG_FEATURE_API + rapidjson::Value toJSON(rapidjson::Document &doc) const override; + void handleRequest(IApiRequest &request) override; +# endif + +private: + OclBackendPrivate *d_ptr; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLBACKEND_H */ diff --git a/src/backend/opencl/OclConfig.cpp b/src/backend/opencl/OclConfig.cpp new file mode 100644 index 000000000..59b726ffa --- /dev/null +++ b/src/backend/opencl/OclConfig.cpp @@ -0,0 +1,90 @@ +/* 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/OclConfig.h" +#include "base/io/json/Json.h" +#include "rapidjson/document.h" + + +namespace xmrig { + +static const char *kEnabled = "enabled"; + + +extern template class Threads; + +} + + +xmrig::OclConfig::OclConfig() +{ +} + + +rapidjson::Value xmrig::OclConfig::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value obj(kObjectType); + + obj.AddMember(StringRef(kEnabled), m_enabled, allocator); + + m_threads.toJSON(obj, doc); + + return obj; +} + + +std::vector xmrig::OclConfig::get(const Miner *miner, const Algorithm &algorithm) const +{ + std::vector out; + + return out; +} + + +void xmrig::OclConfig::read(const rapidjson::Value &value) +{ + if (value.IsObject()) { + m_enabled = Json::getBool(value, kEnabled, m_enabled); + + if (!m_threads.read(value)) { + generate(); + } + } + else if (value.IsBool() && value.IsFalse()) { + m_enabled = false; + } + else { + generate(); + } +} + + +void xmrig::OclConfig::generate() +{ + m_shouldSave = true; +} diff --git a/src/backend/opencl/OclConfig.h b/src/backend/opencl/OclConfig.h new file mode 100644 index 000000000..bf7d0abb3 --- /dev/null +++ b/src/backend/opencl/OclConfig.h @@ -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 . + */ + +#ifndef XMRIG_OCLCONFIG_H +#define XMRIG_OCLCONFIG_H + + +#include "backend/common/Threads.h" +#include "backend/opencl/OclLaunchData.h" +#include "backend/opencl/OclThreads.h" + + +namespace xmrig { + + +class OclConfig +{ +public: + OclConfig(); + + rapidjson::Value toJSON(rapidjson::Document &doc) const; + std::vector get(const Miner *miner, const Algorithm &algorithm) const; + void read(const rapidjson::Value &value); + + inline bool isEnabled() const { return m_enabled; } + inline bool isShouldSave() const { return m_shouldSave; } + inline const Threads &threads() const { return m_threads; } + +private: + void generate(); + + bool m_enabled = true; + bool m_shouldSave = false; + Threads m_threads; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLCONFIG_H */ diff --git a/src/backend/opencl/OclLaunchData.cpp b/src/backend/opencl/OclLaunchData.cpp new file mode 100644 index 000000000..3704489dc --- /dev/null +++ b/src/backend/opencl/OclLaunchData.cpp @@ -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 Lee Clagett + * 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/OclLaunchData.h" +#include "backend/opencl/OclConfig.h" + + +xmrig::OclLaunchData::OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclThread &thread) : + algorithm(algorithm), + intensity(thread.intensity()), + priority(-1), + affinity(thread.affinity()), + miner(miner) +{ +} + + +bool xmrig::OclLaunchData::isEqual(const OclLaunchData &other) const +{ + return (algorithm.l3() == other.algorithm.l3() + && intensity == other.intensity + && priority == other.priority + && affinity == other.affinity + ); +} diff --git a/src/backend/opencl/OclLaunchData.h b/src/backend/opencl/OclLaunchData.h new file mode 100644 index 000000000..535fb815b --- /dev/null +++ b/src/backend/opencl/OclLaunchData.h @@ -0,0 +1,65 @@ +/* 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 Lee Clagett + * 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_OCLLAUNCHDATA_H +#define XMRIG_OCLLAUNCHDATA_H + + +#include "crypto/common/Algorithm.h" +#include "crypto/common/Nonce.h" + + +namespace xmrig { + + +class OclConfig; +class OclThread; +class Miner; + + +class OclLaunchData +{ +public: + OclLaunchData(const Miner *miner, const Algorithm &algorithm, const OclConfig &config, const OclThread &thread); + + bool isEqual(const OclLaunchData &other) const; + + inline constexpr static Nonce::Backend backend() { return Nonce::OPENCL; } + + inline bool operator!=(const OclLaunchData &other) const { return !isEqual(other); } + inline bool operator==(const OclLaunchData &other) const { return isEqual(other); } + + const Algorithm algorithm; + const int intensity; + const int priority; + const int64_t affinity; + const Miner *miner; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_OCLLAUNCHDATA_H */ diff --git a/src/backend/opencl/OclThread.cpp b/src/backend/opencl/OclThread.cpp new file mode 100644 index 000000000..7c5f830e8 --- /dev/null +++ b/src/backend/opencl/OclThread.cpp @@ -0,0 +1,58 @@ +/* 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/OclThread.h" +#include "base/io/json/Json.h" +#include "rapidjson/document.h" + + +xmrig::OclThread::OclThread(const rapidjson::Value &value) +{ + if (value.IsArray() && value.Size() >= 2) { + m_intensity = value[0].GetInt(); + m_affinity = value[1].GetInt(); + } + else if (value.IsInt()) { + m_intensity = -1; + m_affinity = value.GetInt(); + } +} + + +rapidjson::Value xmrig::OclThread::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + if (m_intensity == -1) { + return Value(m_affinity); + } + + auto &allocator = doc.GetAllocator(); + + Value out(kArrayType); + out.PushBack(m_intensity, allocator); + out.PushBack(m_affinity, allocator); + + return out; +} diff --git a/src/backend/opencl/OclThread.h b/src/backend/opencl/OclThread.h new file mode 100644 index 000000000..066d3cabc --- /dev/null +++ b/src/backend/opencl/OclThread.h @@ -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 . + */ + +#ifndef XMRIG_OCLTHREAD_H +#define XMRIG_OCLTHREAD_H + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class OclThread +{ +public: + inline constexpr OclThread() {} + inline constexpr OclThread(int64_t affinity, int intensity) : m_intensity(intensity), m_affinity(affinity) {} + + OclThread(const rapidjson::Value &value); + + inline bool isEqual(const OclThread &other) const { return other.m_affinity == m_affinity && other.m_intensity == m_intensity; } + inline bool isValid() const { return m_intensity == -1 || (m_intensity >= 1 && m_intensity <= 5); } + inline int intensity() const { return m_intensity == -1 ? 1 : m_intensity; } + inline int64_t affinity() const { return m_affinity; } + + inline bool operator!=(const OclThread &other) const { return !isEqual(other); } + inline bool operator==(const OclThread &other) const { return isEqual(other); } + + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + int m_intensity = -1; + int64_t m_affinity = -1; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLTHREAD_H */ diff --git a/src/backend/opencl/OclThreads.cpp b/src/backend/opencl/OclThreads.cpp new file mode 100644 index 000000000..abfd7f34c --- /dev/null +++ b/src/backend/opencl/OclThreads.cpp @@ -0,0 +1,71 @@ +/* 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 + + +#include "backend/opencl/OclThreads.h" +#include "base/io/json/Json.h" +#include "rapidjson/document.h" + + +xmrig::OclThreads::OclThreads(const rapidjson::Value &value) +{ + if (value.IsArray()) { + for (auto &v : value.GetArray()) { + OclThread thread(v); + if (thread.isValid()) { + add(std::move(thread)); + } + } + } +} + + +xmrig::OclThreads::OclThreads(size_t count, int intensity) +{ + m_data.reserve(count); + + for (size_t i = 0; i < count; ++i) { + add(-1, intensity); + } +} + + +rapidjson::Value xmrig::OclThreads::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value out(kArrayType); + + out.SetArray(); + + for (const OclThread &thread : m_data) { + out.PushBack(thread.toJSON(doc), allocator); + } + + return out; +} diff --git a/src/backend/opencl/OclThreads.h b/src/backend/opencl/OclThreads.h new file mode 100644 index 000000000..f345398c3 --- /dev/null +++ b/src/backend/opencl/OclThreads.h @@ -0,0 +1,64 @@ +/* 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_OCLTHREADS_H +#define XMRIG_OCLTHREADS_H + + +#include + + +#include "backend/opencl/OclThread.h" + + +namespace xmrig { + + +class OclThreads +{ +public: + inline OclThreads() {} + inline OclThreads(size_t count) : m_data(count) {} + + OclThreads(const rapidjson::Value &value); + OclThreads(size_t count, int intensity); + + inline bool isEmpty() const { return m_data.empty(); } + inline const std::vector &data() const { return m_data; } + inline size_t count() const { return m_data.size(); } + inline void add(OclThread &&thread) { m_data.push_back(thread); } + inline void add(int64_t affinity, int intensity) { add(OclThread(affinity, intensity)); } + inline void reserve(size_t capacity) { m_data.reserve(capacity); } + + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + std::vector m_data; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLTHREADS_H */ diff --git a/src/backend/opencl/opencl.cmake b/src/backend/opencl/opencl.cmake index 60a36ff3d..e5305850a 100644 --- a/src/backend/opencl/opencl.cmake +++ b/src/backend/opencl/opencl.cmake @@ -4,13 +4,23 @@ if (WITH_OPENCL) add_definitions(/DXMRIG_FEATURE_OPENCL) set(HEADERS_BACKEND_OPENCL + src/backend/opencl/OclBackend.h + src/backend/opencl/OclConfig.h src/backend/opencl/OclError.h + src/backend/opencl/OclLaunchData.h src/backend/opencl/OclLib.h + src/backend/opencl/OclThread.h + src/backend/opencl/OclThreads.h ) set(SOURCES_BACKEND_OPENCL + src/backend/opencl/OclBackend.cpp + src/backend/opencl/OclConfig.cpp src/backend/opencl/OclError.cpp + src/backend/opencl/OclLaunchData.cpp src/backend/opencl/OclLib.cpp + src/backend/opencl/OclThread.cpp + src/backend/opencl/OclThreads.cpp ) else() remove_definitions(/DXMRIG_FEATURE_OPENCL) diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 84bce1f94..448c19948 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -50,6 +50,11 @@ #endif +#ifdef XMRIG_FEATURE_OPENCL +# include "backend/opencl/OclBackend.h" +#endif + + namespace xmrig { @@ -252,6 +257,10 @@ xmrig::Miner::Miner(Controller *controller) d_ptr->backends.push_back(new CpuBackend(controller)); +# ifdef XMRIG_FEATURE_OPENCL + d_ptr->backends.push_back(new OclBackend(controller)); +# endif + d_ptr->rebuild(); } diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 44aa60299..ba4e6d0ef 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -46,6 +46,10 @@ static const char *kCPU = "cpu"; static const char *kRandomX = "randomx"; #endif +#ifdef XMRIG_FEATURE_OPENCL +static const char *kOcl = "opencl"; +#endif + } @@ -54,6 +58,22 @@ xmrig::Config::Config() : BaseConfig() } +bool xmrig::Config::isShouldSave() const +{ + if (!isAutoSave()) { + return false; + } + +# ifdef XMRIG_FEATURE_OPENCL + if (m_cl.isShouldSave()) { + return true; + } +# endif + + return (m_shouldSave || m_upgrade || m_cpu.isShouldSave()); +} + + bool xmrig::Config::read(const IJsonReader &reader, const char *fileName) { if (!BaseConfig::read(reader, fileName)) { @@ -68,6 +88,10 @@ bool xmrig::Config::read(const IJsonReader &reader, const char *fileName) } # endif +# ifdef XMRIG_FEATURE_OPENCL + m_cl.read(reader.getValue(kOcl)); +# endif + return true; } @@ -95,6 +119,11 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const # endif doc.AddMember(StringRef(kCPU), m_cpu.toJSON(doc), allocator); + +# ifdef XMRIG_FEATURE_OPENCL + doc.AddMember(StringRef(kOcl), m_cl.toJSON(doc), allocator); +# endif + doc.AddMember("donate-level", m_pools.donateLevel(), allocator); doc.AddMember("donate-over-proxy", m_pools.proxyDonate(), allocator); doc.AddMember("log-file", m_logFile.toJSON(), allocator); diff --git a/src/core/config/Config.h b/src/core/config/Config.h index 0014cb05a..d250ec4b7 100644 --- a/src/core/config/Config.h +++ b/src/core/config/Config.h @@ -39,6 +39,11 @@ #endif +#ifdef XMRIG_FEATURE_OPENCL +# include "backend/opencl/OclConfig.h" +#endif + + namespace xmrig { @@ -50,16 +55,20 @@ class Config : public BaseConfig public: Config(); + bool isShouldSave() const; bool read(const IJsonReader &reader, const char *fileName) override; void getJSON(rapidjson::Document &doc) const override; - inline bool isShouldSave() const { return (m_shouldSave || m_upgrade || m_cpu.isShouldSave()) && isAutoSave(); } inline const CpuConfig &cpu() const { return m_cpu; } # ifdef XMRIG_ALGO_RANDOMX inline const RxConfig &rx() const { return m_rx; } # endif +# ifdef XMRIG_FEATURE_OPENCL + inline const OclConfig &cl() const { return m_cl; } +# endif + private: bool m_shouldSave = false; CpuConfig m_cpu; @@ -67,6 +76,10 @@ private: # ifdef XMRIG_ALGO_RANDOMX RxConfig m_rx; # endif + +# ifdef XMRIG_FEATURE_OPENCL + OclConfig m_cl; +# endif };