From 0fc215c457664b85251c7feb211daf9511943a75 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 23 Oct 2019 16:37:56 +0700 Subject: [PATCH] Added initial CUDA backend stub. --- CMakeLists.txt | 1 + src/backend/backend.cmake | 3 + src/backend/common/Tags.h | 6 + src/backend/common/Threads.cpp | 9 ++ src/backend/cuda/CudaBackend.cpp | 217 ++++++++++++++++++++++++++++++ src/backend/cuda/CudaBackend.h | 79 +++++++++++ src/backend/cuda/CudaConfig.cpp | 115 ++++++++++++++++ src/backend/cuda/CudaConfig.h | 62 +++++++++ src/backend/cuda/CudaConfig_gen.h | 42 ++++++ src/backend/cuda/CudaThread.cpp | 63 +++++++++ src/backend/cuda/CudaThread.h | 62 +++++++++ src/backend/cuda/CudaThreads.cpp | 71 ++++++++++ src/backend/cuda/CudaThreads.h | 64 +++++++++ src/backend/cuda/cuda.cmake | 23 ++++ src/backend/opencl/OclBackend.h | 9 +- src/core/Miner.cpp | 10 ++ src/core/config/Config.cpp | 35 +++++ src/core/config/Config.h | 7 +- 18 files changed, 871 insertions(+), 7 deletions(-) create mode 100644 src/backend/cuda/CudaBackend.cpp create mode 100644 src/backend/cuda/CudaBackend.h create mode 100644 src/backend/cuda/CudaConfig.cpp create mode 100644 src/backend/cuda/CudaConfig.h create mode 100644 src/backend/cuda/CudaConfig_gen.h create mode 100644 src/backend/cuda/CudaThread.cpp create mode 100644 src/backend/cuda/CudaThread.h create mode 100644 src/backend/cuda/CudaThreads.cpp create mode 100644 src/backend/cuda/CudaThreads.h create mode 100644 src/backend/cuda/cuda.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 90abfd423..1a195f6e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(WITH_TLS "Enable OpenSSL support" ON) option(WITH_ASM "Enable ASM PoW implementations" ON) option(WITH_EMBEDDED_CONFIG "Enable internal embedded JSON config" OFF) option(WITH_OPENCL "Enable OpenCL backend" ON) +option(WITH_CUDA "Enable CUDA backend" ON) option(WITH_STRICT_CACHE "Enable strict checks for OpenCL cache" ON) option(WITH_INTERLEAVE_DEBUG_LOG "Enable debug log for threads interleave" OFF) diff --git a/src/backend/backend.cmake b/src/backend/backend.cmake index 14f0ab9c6..6bf6c3b20 100644 --- a/src/backend/backend.cmake +++ b/src/backend/backend.cmake @@ -1,5 +1,6 @@ include (src/backend/cpu/cpu.cmake) include (src/backend/opencl/opencl.cmake) +include (src/backend/cuda/cuda.cmake) include (src/backend/common/common.cmake) @@ -7,10 +8,12 @@ set(HEADERS_BACKEND "${HEADERS_BACKEND_COMMON}" "${HEADERS_BACKEND_CPU}" "${HEADERS_BACKEND_OPENCL}" + "${HEADERS_BACKEND_CUDA}" ) set(SOURCES_BACKEND "${SOURCES_BACKEND_COMMON}" "${SOURCES_BACKEND_CPU}" "${SOURCES_BACKEND_OPENCL}" + "${SOURCES_BACKEND_CUDA}" ) diff --git a/src/backend/common/Tags.h b/src/backend/common/Tags.h index 16022e33e..54de37607 100644 --- a/src/backend/common/Tags.h +++ b/src/backend/common/Tags.h @@ -38,6 +38,12 @@ const char *ocl_tag(); #endif +#ifdef XMRIG_FEATURE_CUDA +const char *cuda_tag(); +#endif + + + #ifdef XMRIG_ALGO_RANDOMX const char *rx_tag(); #endif diff --git a/src/backend/common/Threads.cpp b/src/backend/common/Threads.cpp index bb9e440f6..f85e18f3d 100644 --- a/src/backend/common/Threads.cpp +++ b/src/backend/common/Threads.cpp @@ -34,6 +34,11 @@ #endif +#ifdef XMRIG_FEATURE_CUDA +# include "backend/cuda/CudaThreads.h" +#endif + + namespace xmrig { @@ -167,4 +172,8 @@ template class Threads; template class Threads; #endif +#ifdef XMRIG_FEATURE_CUDA +template class Threads; +#endif + } // namespace xmrig diff --git a/src/backend/cuda/CudaBackend.cpp b/src/backend/cuda/CudaBackend.cpp new file mode 100644 index 000000000..784d21eb1 --- /dev/null +++ b/src/backend/cuda/CudaBackend.cpp @@ -0,0 +1,217 @@ +/* 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 + + +#include "backend/cuda/CudaBackend.h" +#include "backend/common/Hashrate.h" +#include "backend/common/interfaces/IWorker.h" +#include "backend/common/Tags.h" +#include "backend/common/Workers.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; + + +constexpr const size_t oneMiB = 1024u * 1024u; +static const char *tag = MAGENTA_BG_BOLD(WHITE_BOLD_S " nv "); +static const String kType = "cuda"; +static std::mutex mutex; + + + +class CudaBackendPrivate +{ +public: + inline CudaBackendPrivate(Controller *controller) : + controller(controller) + { + } + + + void init(const OclConfig &cl) + { + } + + + inline void start(const Job &job) + { + } + + + Algorithm algo; + Controller *controller; + String profileName; +}; + + +} // namespace xmrig + + +const char *xmrig::cuda_tag() +{ + return tag; +} + + +xmrig::CudaBackend::CudaBackend(Controller *controller) : + d_ptr(new CudaBackendPrivate(controller)) +{ +} + + +xmrig::CudaBackend::~CudaBackend() +{ + delete d_ptr; +} + + +bool xmrig::CudaBackend::isEnabled() const +{ + return false; +} + + +bool xmrig::CudaBackend::isEnabled(const Algorithm &algorithm) const +{ + return false; +} + + +const xmrig::Hashrate *xmrig::CudaBackend::hashrate() const +{ + return nullptr; +// return d_ptr->workers.hashrate(); +} + + +const xmrig::String &xmrig::CudaBackend::profileName() const +{ + return d_ptr->profileName; +} + + +const xmrig::String &xmrig::CudaBackend::type() const +{ + return kType; +} + + +void xmrig::CudaBackend::prepare(const Job &) +{ +} + + +void xmrig::CudaBackend::printHashrate(bool details) +{ + if (!details || !hashrate()) { + return; + } + + char num[8 * 3] = { 0 }; + + Log::print(WHITE_BOLD_S "| CUDA # | AFFINITY | 10s H/s | 60s H/s | 15m H/s |"); + +// size_t i = 0; +// for (const OclLaunchData &data : d_ptr->threads) { +// Log::print("| %8zu | %8" PRId64 " | %7s | %7s | %7s |" CYAN_BOLD(" #%u") YELLOW(" %s") " %s", +// 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), +// data.device.index(), +// data.device.topology().toString().data(), +// data.device.printableName().data() +// ); + +// i++; +// } + + Log::print(WHITE_BOLD_S "| - | - | %7s | %7s | %7s |", + Hashrate::format(hashrate()->calc(Hashrate::ShortInterval), num, sizeof num / 3), + Hashrate::format(hashrate()->calc(Hashrate::MediumInterval), num + 8, sizeof num / 3), + Hashrate::format(hashrate()->calc(Hashrate::LargeInterval), num + 8 * 2, sizeof num / 3) + ); +} + + +void xmrig::CudaBackend::setJob(const Job &job) +{ +} + + +void xmrig::CudaBackend::start(IWorker *worker, bool ready) +{ +} + + +void xmrig::CudaBackend::stop() +{ +} + + +void xmrig::CudaBackend::tick(uint64_t ticks) +{ +} + + +#ifdef XMRIG_FEATURE_API +rapidjson::Value xmrig::CudaBackend::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + 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); + + return out; +} + + +void xmrig::CudaBackend::handleRequest(IApiRequest &) +{ +} +#endif diff --git a/src/backend/cuda/CudaBackend.h b/src/backend/cuda/CudaBackend.h new file mode 100644 index 000000000..0d2a2395d --- /dev/null +++ b/src/backend/cuda/CudaBackend.h @@ -0,0 +1,79 @@ +/* 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_CUDABACKEND_H +#define XMRIG_CUDABACKEND_H + + +#include + + +#include "backend/common/interfaces/IBackend.h" +#include "base/tools/Object.h" + + +namespace xmrig { + + +class Controller; +class CudaBackendPrivate; +class Miner; + + +class CudaBackend : public IBackend +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(CudaBackend) + + CudaBackend(Controller *controller); + + ~CudaBackend() 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, bool ready) 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: + CudaBackendPrivate *d_ptr; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CUDABACKEND_H */ diff --git a/src/backend/cuda/CudaConfig.cpp b/src/backend/cuda/CudaConfig.cpp new file mode 100644 index 000000000..792131de9 --- /dev/null +++ b/src/backend/cuda/CudaConfig.cpp @@ -0,0 +1,115 @@ +/* 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/cuda/CudaConfig.h" +#include "backend/cuda/CudaConfig_gen.h" +#include "base/io/json/Json.h" +#include "base/io/log/Log.h" +#include "rapidjson/document.h" + + +namespace xmrig { + + +static const char *kDevicesHint = "devices-hint"; +static const char *kEnabled = "enabled"; + + +extern template class Threads; + + +} + + +rapidjson::Value xmrig::CudaConfig::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; +} + + +void xmrig::CudaConfig::read(const rapidjson::Value &value) +{ + if (value.IsObject()) { + m_enabled = Json::getBool(value, kEnabled, m_enabled); + + setDevicesHint(Json::getString(value, kDevicesHint)); + + m_threads.read(value); + + generate(); + } + else if (value.IsBool()) { + m_enabled = value.GetBool(); + + generate(); + } + else { + m_shouldSave = true; + + generate(); + } +} + + +void xmrig::CudaConfig::generate() +{ + if (!isEnabled() || m_threads.has("*")) { + return; + } + + size_t count = 0; + +// count += xmrig::generate(m_threads, devices); +// count += xmrig::generate(m_threads, devices); +// count += xmrig::generate(m_threads, devices); +// count += xmrig::generate(m_threads, devices); +// count += xmrig::generate(m_threads, devices); + + m_shouldSave = count > 0; +} + + +void xmrig::CudaConfig::setDevicesHint(const char *devicesHint) +{ + if (devicesHint == nullptr) { + return; + } + + const auto indexes = String(devicesHint).split(','); + m_devicesHint.reserve(indexes.size()); + + for (const auto &index : indexes) { + m_devicesHint.push_back(strtoul(index, nullptr, 10)); + } +} diff --git a/src/backend/cuda/CudaConfig.h b/src/backend/cuda/CudaConfig.h new file mode 100644 index 000000000..6feaa1304 --- /dev/null +++ b/src/backend/cuda/CudaConfig.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_CUDACONFIG_H +#define XMRIG_CUDACONFIG_H + + +#include "backend/common/Threads.h" +#include "backend/cuda/CudaThreads.h" + + +namespace xmrig { + + +class CudaConfig +{ +public: + CudaConfig() = default; + + rapidjson::Value toJSON(rapidjson::Document &doc) 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(); + void setDevicesHint(const char *devicesHint); + + bool m_enabled = false; + bool m_shouldSave = false; + std::vector m_devicesHint; + Threads m_threads; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CUDACONFIG_H */ diff --git a/src/backend/cuda/CudaConfig_gen.h b/src/backend/cuda/CudaConfig_gen.h new file mode 100644 index 000000000..757635ee2 --- /dev/null +++ b/src/backend/cuda/CudaConfig_gen.h @@ -0,0 +1,42 @@ +/* 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_GEN_H +#define XMRIG_OCLCONFIG_GEN_H + + +#include "backend/common/Threads.h" +#include "backend/cuda/CudaThreads.h" + + +#include + + +namespace xmrig { + + +} /* namespace xmrig */ + + +#endif /* XMRIG_OCLCONFIG_GEN_H */ diff --git a/src/backend/cuda/CudaThread.cpp b/src/backend/cuda/CudaThread.cpp new file mode 100644 index 000000000..9dc77c4f6 --- /dev/null +++ b/src/backend/cuda/CudaThread.cpp @@ -0,0 +1,63 @@ +/* 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/cuda/CudaThread.h" + +#include "base/io/json/Json.h" +#include "rapidjson/document.h" + + +#include + + +namespace xmrig { + + +} // namespace xmrig + + +xmrig::CudaThread::CudaThread(const rapidjson::Value &value) +{ + if (!value.IsObject()) { + return; + } +} + + +bool xmrig::CudaThread::isEqual(const CudaThread &other) const +{ + return false; +} + + +rapidjson::Value xmrig::CudaThread::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value out(kObjectType); + + return out; +} diff --git a/src/backend/cuda/CudaThread.h b/src/backend/cuda/CudaThread.h new file mode 100644 index 000000000..ececf3ada --- /dev/null +++ b/src/backend/cuda/CudaThread.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_CUDATHREAD_H +#define XMRIG_CUDATHREAD_H + + +#include "crypto/common/Algorithm.h" +#include "rapidjson/fwd.h" + + +#include +#include + + +namespace xmrig { + + +class CudaThread +{ +public: + CudaThread() = delete; + + CudaThread(const rapidjson::Value &value); + + inline bool isValid() const { return false; } + + inline bool operator!=(const CudaThread &other) const { return !isEqual(other); } + inline bool operator==(const CudaThread &other) const { return isEqual(other); } + + bool isEqual(const CudaThread &other) const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CUDATHREAD_H */ diff --git a/src/backend/cuda/CudaThreads.cpp b/src/backend/cuda/CudaThreads.cpp new file mode 100644 index 000000000..9c8b1531b --- /dev/null +++ b/src/backend/cuda/CudaThreads.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 "backend/cuda/CudaThreads.h" +#include "base/io/json/Json.h" +#include "rapidjson/document.h" + + +#include + + +xmrig::CudaThreads::CudaThreads(const rapidjson::Value &value) +{ + if (value.IsArray()) { + for (auto &v : value.GetArray()) { + CudaThread thread(v); + if (thread.isValid()) { + add(std::move(thread)); + } + } + } +} + + +bool xmrig::CudaThreads::isEqual(const CudaThreads &other) const +{ + if (isEmpty() && other.isEmpty()) { + return true; + } + + return count() == other.count() && std::equal(m_data.begin(), m_data.end(), other.m_data.begin()); +} + + +rapidjson::Value xmrig::CudaThreads::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value out(kArrayType); + + out.SetArray(); + + for (const CudaThread &thread : m_data) { + out.PushBack(thread.toJSON(doc), allocator); + } + + return out; +} diff --git a/src/backend/cuda/CudaThreads.h b/src/backend/cuda/CudaThreads.h new file mode 100644 index 000000000..3c7627228 --- /dev/null +++ b/src/backend/cuda/CudaThreads.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_CUDATHREADS_H +#define XMRIG_CUDATHREADS_H + + +#include + + +#include "backend/cuda/CudaThread.h" + + +namespace xmrig { + + +class CudaThreads +{ +public: + CudaThreads() = default; + CudaThreads(const rapidjson::Value &value); + + 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(CudaThread &&thread) { m_data.push_back(thread); } + inline void reserve(size_t capacity) { m_data.reserve(capacity); } + + inline bool operator!=(const CudaThreads &other) const { return !isEqual(other); } + inline bool operator==(const CudaThreads &other) const { return isEqual(other); } + + bool isEqual(const CudaThreads &other) const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + std::vector m_data; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CUDATHREADS_H */ diff --git a/src/backend/cuda/cuda.cmake b/src/backend/cuda/cuda.cmake new file mode 100644 index 000000000..f8840c380 --- /dev/null +++ b/src/backend/cuda/cuda.cmake @@ -0,0 +1,23 @@ +if (WITH_CUDA) + add_definitions(/DXMRIG_FEATURE_CUDA) + + set(HEADERS_BACKEND_CUDA + src/backend/cuda/CudaBackend.h + src/backend/cuda/CudaConfig.h + src/backend/cuda/CudaConfig_gen.h + src/backend/cuda/CudaThread.h + src/backend/cuda/CudaThreads.h + ) + + set(SOURCES_BACKEND_CUDA + src/backend/cuda/CudaBackend.cpp + src/backend/cuda/CudaConfig.cpp + src/backend/cuda/CudaThread.cpp + src/backend/cuda/CudaThreads.cpp + ) +else() + remove_definitions(/DXMRIG_FEATURE_CUDA) + + set(HEADERS_BACKEND_CUDA "") + set(SOURCES_BACKEND_CUDA "") +endif() diff --git a/src/backend/opencl/OclBackend.h b/src/backend/opencl/OclBackend.h index ade560baf..57f5db38d 100644 --- a/src/backend/opencl/OclBackend.h +++ b/src/backend/opencl/OclBackend.h @@ -30,6 +30,7 @@ #include "backend/common/interfaces/IBackend.h" +#include "base/tools/Object.h" namespace xmrig { @@ -43,16 +44,12 @@ class Miner; class OclBackend : public IBackend { public: - OclBackend() = delete; - OclBackend(const OclBackend &other) = delete; + XMRIG_DISABLE_COPY_MOVE_DEFAULT(OclBackend) + OclBackend(Controller *controller); - OclBackend(OclBackend &&other) = delete; ~OclBackend() override; - OclBackend &operator=(const OclBackend &other) = delete; - OclBackend &operator=(OclBackend &&other) = delete; - protected: bool isEnabled() const override; bool isEnabled(const Algorithm &algorithm) const override; diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 49b548e9e..0caf298f8 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -56,6 +56,11 @@ #endif +#ifdef XMRIG_FEATURE_CUDA +# include "backend/cuda/CudaBackend.h" +#endif + + #ifdef XMRIG_ALGO_RANDOMX # include "crypto/rx/RxConfig.h" #endif @@ -270,12 +275,17 @@ xmrig::Miner::Miner(Controller *controller) d_ptr->timer = new Timer(this); + d_ptr->backends.reserve(3); d_ptr->backends.push_back(new CpuBackend(controller)); # ifdef XMRIG_FEATURE_OPENCL d_ptr->backends.push_back(new OclBackend(controller)); # endif +# ifdef XMRIG_FEATURE_CUDA + d_ptr->backends.push_back(new CudaBackend(controller)); +# endif + d_ptr->rebuild(); } diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 7ef7197f5..4fd88b90e 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -48,6 +48,11 @@ #endif +#ifdef XMRIG_FEATURE_CUDA +# include "backend/cuda/CudaConfig.h" +#endif + + namespace xmrig { static const char *kCPU = "cpu"; @@ -60,6 +65,10 @@ static const char *kRandomX = "randomx"; static const char *kOcl = "opencl"; #endif +#ifdef XMRIG_FEATURE_CUDA +static const char *kCuda = "cuda"; +#endif + class ConfigPrivate { @@ -73,6 +82,10 @@ public: # ifdef XMRIG_FEATURE_OPENCL OclConfig cl; # endif + +# ifdef XMRIG_FEATURE_CUDA + CudaConfig cuda; +# endif }; } @@ -104,6 +117,14 @@ const xmrig::OclConfig &xmrig::Config::cl() const #endif +#ifdef XMRIG_FEATURE_CUDA +const xmrig::CudaConfig &xmrig::Config::cuda() const +{ + return d_ptr->cuda; +} +#endif + + #ifdef XMRIG_ALGO_RANDOMX const xmrig::RxConfig &xmrig::Config::rx() const { @@ -124,6 +145,12 @@ bool xmrig::Config::isShouldSave() const } # endif +# ifdef XMRIG_FEATURE_CUDA + if (cuda().isShouldSave()) { + return true; + } +# endif + return (m_upgrade || cpu().isShouldSave()); } @@ -146,6 +173,10 @@ bool xmrig::Config::read(const IJsonReader &reader, const char *fileName) d_ptr->cl.read(reader.getValue(kOcl)); # endif +# ifdef XMRIG_FEATURE_CUDA + d_ptr->cuda.read(reader.getValue(kCuda)); +# endif + return true; } @@ -178,6 +209,10 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember(StringRef(kOcl), cl().toJSON(doc), allocator); # endif +# ifdef XMRIG_FEATURE_CUDA + doc.AddMember(StringRef(kCuda), cuda().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 22deb2c23..8becc0b80 100644 --- a/src/core/config/Config.h +++ b/src/core/config/Config.h @@ -39,9 +39,10 @@ namespace xmrig { class ConfigPrivate; +class CudaConfig; class IThread; -class RxConfig; class OclConfig; +class RxConfig; class Config : public BaseConfig @@ -58,6 +59,10 @@ public: const OclConfig &cl() const; # endif +# ifdef XMRIG_FEATURE_CUDA + const CudaConfig &cuda() const; +# endif + # ifdef XMRIG_ALGO_RANDOMX const RxConfig &rx() const; # endif