cn/r part 1 of 2.

This commit is contained in:
XMRig 2019-09-03 14:36:27 +07:00
parent 9b6ab55936
commit b9e15389ca
16 changed files with 1484 additions and 734 deletions

View file

@ -48,6 +48,7 @@ public:
protected:
inline cl_context ctx() const override { return m_ctx; }
inline const Algorithm &algorithm() const override { return m_algorithm; }
inline const char *buildOptions() const override { return m_options.c_str(); }
inline const char *deviceKey() const override { return m_deviceKey.c_str(); }
inline const char *source() const override { return m_source; }

View file

@ -29,6 +29,7 @@
#include "backend/opencl/kernels/CnBranchKernel.h"
#include "backend/opencl/OclLaunchData.h"
#include "backend/opencl/runners/OclCnRunner.h"
#include "backend/opencl/runners/tools/OclCnR.h"
#include "backend/opencl/wrappers/OclLib.h"
#include "base/io/log/Log.h"
#include "base/net/stratum/Job.h"
@ -169,9 +170,15 @@ bool xmrig::OclCnRunner::run(uint32_t nonce, uint32_t *hashOutput)
bool xmrig::OclCnRunner::selfTest() const
{
return OclBaseRunner::selfTest() &&
m_cn0->isValid() &&
m_cn1->isValid() && m_cn2->isValid();
if (OclBaseRunner::selfTest() && m_cn0->isValid() && m_cn2->isValid()) {
if (m_algorithm != Algorithm::CN_R) {
return m_cn1->isValid();
}
return true;
}
return false;
}
@ -194,6 +201,13 @@ bool xmrig::OclCnRunner::set(const Job &job, uint8_t *blob)
return false;
}
if (m_algorithm == Algorithm::CN_R && m_height != job.height()) {
delete m_cn1;
m_height = job.height();
m_cn1 = new Cn1Kernel(OclCnR::get(*this, m_height), m_height);
}
if (!m_cn1->setArgs(m_input, m_scratchpads, m_states, intensity)) {
return false;
}
@ -221,9 +235,12 @@ void xmrig::OclCnRunner::build()
}
m_cn0 = new Cn0Kernel(m_program);
m_cn1 = new Cn1Kernel(m_program);
m_cn2 = new Cn2Kernel(m_program);
if (m_algorithm != Algorithm::CN_R) {
m_cn1 = new Cn1Kernel(m_program);
}
for (size_t i = 0; i < BRANCH_MAX; ++i) {
m_branchKernels[i] = new CnBranchKernel(i, m_program);
}

View file

@ -66,6 +66,7 @@ private:
Cn0Kernel *m_cn0 = nullptr;
Cn1Kernel *m_cn1 = nullptr;
Cn2Kernel *m_cn2 = nullptr;
uint64_t m_height = 0;
std::vector<cl_mem> m_branches = { nullptr, nullptr, nullptr, nullptr };
std::vector<CnBranchKernel *> m_branchKernels = { nullptr, nullptr, nullptr, nullptr };

View file

@ -0,0 +1,178 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <mutex>
#include <regex>
#include <sstream>
#include <string>
#include <thread>
#include "backend/opencl/cl/cn/cryptonight_r_cl.h"
#include "backend/opencl/interfaces/IOclRunner.h"
#include "backend/opencl/OclCache.h"
#include "backend/opencl/OclLaunchData.h"
#include "backend/opencl/OclThread.h"
#include "backend/opencl/runners/tools/OclCnR.h"
#include "backend/opencl/wrappers/OclError.h"
#include "backend/opencl/wrappers/OclLib.h"
#include "base/io/log/Log.h"
#include "base/tools/Chrono.h"
#include "crypto/cn/CryptoNight_monero.h"
namespace xmrig {
static std::string getCode(const V4_Instruction *code, int code_size)
{
std::stringstream s;
for (int i = 0; i < code_size; ++i) {
const V4_Instruction inst = code[i];
const uint32_t a = inst.dst_index;
const uint32_t b = inst.src_index;
switch (inst.opcode)
{
case MUL:
s << 'r' << a << "*=r" << b << ';';
break;
case ADD:
s << 'r' << a << "+=r" << b << '+' << inst.C << "U;";
break;
case SUB:
s << 'r' << a << "-=r" << b << ';';
break;
case ROR:
case ROL:
s << 'r' << a << "=rotate(r" << a << ((inst.opcode == ROR) ? ",ROT_BITS-r" : ",r") << b << ");";
break;
case XOR:
s << 'r' << a << "^=r" << b << ';';
break;
}
s << '\n';
}
return s.str();
}
class CacheEntry
{
public:
inline CacheEntry(const Algorithm &algorithm, uint64_t heightOffset, uint32_t deviceIndex, cl_program program) :
algorithm(algorithm),
program(program),
deviceIndex(deviceIndex),
heightOffset(heightOffset)
{}
const Algorithm algorithm;
const cl_program program;
const uint32_t deviceIndex;
const uint64_t heightOffset;
};
static std::mutex mutex;
static std::vector<CacheEntry> cache;
static cl_program search(const Algorithm &algorithm, uint64_t offset, uint32_t index)
{
std::lock_guard<std::mutex> lock(mutex);
for (const CacheEntry &entry : cache) {
if (entry.heightOffset == offset && entry.deviceIndex == index && entry.algorithm == algorithm) {
return entry.program;
}
}
return nullptr;
}
static inline cl_program search(const IOclRunner &runner, uint64_t offset) { return search(runner.algorithm(), offset, runner.data().thread.index()); }
cl_program build(const IOclRunner &runner, const std::string &source, uint64_t offset)
{
std::lock_guard<std::mutex> lock(mutex);
cl_int ret;
cl_device_id device = runner.data().device.id();
const char *s = source.c_str();
cl_program program = OclLib::createProgramWithSource(runner.ctx(), 1, &s, nullptr, &ret);
if (ret != CL_SUCCESS) {
return nullptr;
}
if (OclLib::buildProgram(program, 1, &device, runner.buildOptions()) != CL_SUCCESS) {
printf("BUILD LOG:\n%s\n", OclLib::getProgramBuildLog(program, device).data());
OclLib::releaseProgram(program);
return nullptr;
}
cache.emplace_back(runner.algorithm(), offset, runner.data().thread.index(), program);
return program;
}
} // namespace xmrig
cl_program xmrig::OclCnR::get(const IOclRunner &runner, uint64_t height, bool background)
{
const uint64_t offset = (height / kHeightChunkSize) * kHeightChunkSize;
cl_program program = search(runner, offset);
if (program) {
return program;
}
std::string source(cryptonight_r_defines_cl);
for (size_t i = 0; i < kHeightChunkSize; ++i) {
V4_Instruction code[256];
const int code_size = v4_random_math_init<Algorithm::CN_R>(code, offset + i);
const std::string kernel = std::regex_replace(cryptonight_r_cl, std::regex("XMRIG_INCLUDE_RANDOM_MATH"), getCode(code, code_size));
source += std::regex_replace(kernel, std::regex("KERNEL_NAME"), "cn1_" + std::to_string(offset + i));
}
return build(runner, source, offset);;
}

View file

@ -0,0 +1,55 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_OCLCNR_H
#define XMRIG_OCLCNR_H
#include <stdint.h>
typedef struct _cl_program *cl_program;
namespace xmrig {
class Algorithm;
class IOclRunner;
class OclCnR
{
public:
constexpr static size_t kPrecompilationDepth = 1;
constexpr static size_t kHeightChunkSize = 10;
static cl_program get(const IOclRunner &runner, uint64_t height, bool background = false);
};
} // namespace xmrig
#endif /* XMRIG_OCLCNR_H */