diff --git a/src/backend/common/Workers.cpp b/src/backend/common/Workers.cpp index 313ecd21..1d3c5620 100644 --- a/src/backend/common/Workers.cpp +++ b/src/backend/common/Workers.cpp @@ -31,6 +31,11 @@ #include "base/io/log/Log.h" +#ifdef XMRIG_FEATURE_OPENCL +# include "backend/opencl/OclWorker.h" +#endif + + namespace xmrig { @@ -155,6 +160,8 @@ void xmrig::Workers::onReady(void *arg) return; } + assert(handle->backend() != nullptr); + handle->setWorker(worker); handle->backend()->start(worker); } @@ -194,7 +201,7 @@ template class Workers; template<> xmrig::IWorker *xmrig::Workers::create(Thread *handle) { - return nullptr; + return new OclWorker(handle->index(), handle->config()); } diff --git a/src/backend/cpu/CpuBackend.cpp b/src/backend/cpu/CpuBackend.cpp index ec4788a7..11317212 100644 --- a/src/backend/cpu/CpuBackend.cpp +++ b/src/backend/cpu/CpuBackend.cpp @@ -63,7 +63,7 @@ static const char *tag = CYAN_BG_BOLD(" cpu "); static const String kType = "cpu"; -struct LaunchStatus +struct CpuLaunchStatus { public: inline void reset() @@ -161,7 +161,7 @@ public: Algorithm algo; Controller *controller; - LaunchStatus status; + CpuLaunchStatus status; std::mutex mutex; std::vector threads; String profileName; diff --git a/src/backend/cpu/CpuWorker.h b/src/backend/cpu/CpuWorker.h index 4cdd10f8..c3fb49f1 100644 --- a/src/backend/cpu/CpuWorker.h +++ b/src/backend/cpu/CpuWorker.h @@ -30,7 +30,6 @@ #include "backend/common/Worker.h" #include "backend/common/WorkerJob.h" #include "backend/cpu/CpuLaunchData.h" -#include "base/net/stratum/Job.h" #include "net/JobResult.h" diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 30b942bd..2eaa1b49 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -54,9 +54,10 @@ namespace xmrig { extern template class Threads; +constexpr const size_t oneGiB = 1024u * 1024u * 1024u; static const char *tag = MAGENTA_BG_BOLD(WHITE_BOLD_S " ocl "); static const String kType = "opencl"; -constexpr const size_t oneGiB = 1024u * 1024u * 1024u; +static std::mutex mutex; static void printDisabled(const char *reason) @@ -65,25 +66,18 @@ static void printDisabled(const char *reason) } -struct LaunchStatus +struct OclLaunchStatus { public: - inline void reset() - { - hugePages = 0; - memory = 0; - pages = 0; - started = 0; - threads = 0; - ts = Chrono::steadyMSecs(); - } + inline bool started() { m_started++; return m_started == m_threads; } + inline size_t threads() const { return m_threads; } + inline uint64_t ts() const { return Chrono::steadyMSecs() - m_ts; } + inline void start(size_t threads) { m_started = 0; m_threads = threads; m_ts = Chrono::steadyMSecs(); } - size_t hugePages = 0; - size_t memory = 0; - size_t pages = 0; - size_t started = 0; - size_t threads = 0; - uint64_t ts = 0; +private: + size_t m_started = 0; + size_t m_threads = 0; + uint64_t m_ts = 0; }; @@ -143,20 +137,16 @@ public: workers.stop(); - status.reset(); - status.memory = algo.l3(); - status.threads = threads.size(); - - //workers.start(threads); // FIXME + status.start(threads.size()); + workers.start(threads); } Algorithm algo; Controller *controller; - LaunchStatus status; OclContext context; + OclLaunchStatus status; OclPlatform platform; - std::mutex mutex; std::vector devices; std::vector threads; String profileName; @@ -277,14 +267,17 @@ void xmrig::OclBackend::setJob(const Job &job) void xmrig::OclBackend::start(IWorker *worker) { - d_ptr->mutex.lock(); + mutex.lock(); - d_ptr->status.started++; - - if (d_ptr->status.started == d_ptr->status.threads) { + if (d_ptr->status.started()) { + LOG_INFO("%s" GREEN_BOLD(" READY") " threads " CYAN_BOLD("%zu") BLACK_BOLD(" (%" PRIu64 " ms)"), + tag, + d_ptr->status.threads(), + d_ptr->status.ts() + ); } - d_ptr->mutex.unlock(); + mutex.unlock(); worker->start(); } diff --git a/src/backend/opencl/OclWorker.cpp b/src/backend/opencl/OclWorker.cpp new file mode 100644 index 00000000..4d349f38 --- /dev/null +++ b/src/backend/opencl/OclWorker.cpp @@ -0,0 +1,106 @@ +/* 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 +#include + + +#include "backend/opencl/OclWorker.h" +#include "core/Miner.h" +#include "crypto/common/Nonce.h" +#include "net/JobResults.h" + + +namespace xmrig { + +static constexpr uint32_t kReserveCount = 4096; + +} // namespace xmrig + + + +xmrig::OclWorker::OclWorker(size_t index, const OclLaunchData &data) : + Worker(index, data.thread.affinity(), -1), + m_algorithm(data.algorithm), + m_miner(data.miner) +{ +} + + +xmrig::OclWorker::~OclWorker() +{ +} + + +bool xmrig::OclWorker::selfTest() +{ + return true; +} + + +void xmrig::OclWorker::start() +{ + while (Nonce::sequence(Nonce::OPENCL) > 0) { + if (Nonce::isPaused()) { + do { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } + while (Nonce::isPaused() && Nonce::sequence(Nonce::OPENCL) > 0); + + if (Nonce::sequence(Nonce::OPENCL) == 0) { + break; + } + + consumeJob(); + } + + while (!Nonce::isOutdated(Nonce::OPENCL, m_job.sequence())) { + if ((m_count & 0x7) == 0) { + storeStats(); + } + + const Job &job = m_job.currentJob(); + + if (job.algorithm().l3() != m_algorithm.l3()) { + break; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // FIXME + + m_job.nextRound(kReserveCount); + + std::this_thread::yield(); + } + + consumeJob(); + } +} + + +void xmrig::OclWorker::consumeJob() +{ + m_job.add(m_miner->job(), Nonce::sequence(Nonce::OPENCL), kReserveCount); +} diff --git a/src/backend/opencl/OclWorker.h b/src/backend/opencl/OclWorker.h new file mode 100644 index 00000000..cbe50524 --- /dev/null +++ b/src/backend/opencl/OclWorker.h @@ -0,0 +1,61 @@ +/* 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_OCLWORKER_H +#define XMRIG_OCLWORKER_H + + +#include "backend/common/Worker.h" +#include "backend/common/WorkerJob.h" +#include "backend/opencl/OclLaunchData.h" +#include "net/JobResult.h" + + +namespace xmrig { + + +class OclWorker : public Worker +{ +public: + OclWorker(size_t index, const OclLaunchData &data); + ~OclWorker() override; + +protected: + bool selfTest() override; + void start() override; + +private: + void consumeJob(); + + const Algorithm m_algorithm; + const Miner *m_miner; + WorkerJob<1> m_job; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_OCLWORKER_H */ diff --git a/src/backend/opencl/opencl.cmake b/src/backend/opencl/opencl.cmake index ec5f26aa..2130a14d 100644 --- a/src/backend/opencl/opencl.cmake +++ b/src/backend/opencl/opencl.cmake @@ -10,6 +10,7 @@ if (WITH_OPENCL) src/backend/opencl/OclLaunchData.h src/backend/opencl/OclThread.h src/backend/opencl/OclThreads.h + src/backend/opencl/OclWorker.h src/backend/opencl/wrappers/OclContext.h src/backend/opencl/wrappers/OclDevice.h src/backend/opencl/wrappers/OclError.h @@ -25,6 +26,7 @@ if (WITH_OPENCL) src/backend/opencl/OclLaunchData.cpp src/backend/opencl/OclThread.cpp src/backend/opencl/OclThreads.cpp + src/backend/opencl/OclWorker.cpp src/backend/opencl/wrappers/OclContext.cpp src/backend/opencl/wrappers/OclDevice.cpp src/backend/opencl/wrappers/OclError.cpp