From 10d2c0285c06226a6f25e8946443381b58ec83cf Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 1 Sep 2019 07:37:02 +0700 Subject: [PATCH] Better cl_context wrapping. --- src/backend/opencl/OclCache.cpp | 4 ++-- src/backend/opencl/interfaces/IOclRunner.h | 4 ++++ src/backend/opencl/runners/OclBaseRunner.cpp | 7 ++++--- src/backend/opencl/runners/OclBaseRunner.h | 2 ++ src/backend/opencl/runners/OclCnRunner.cpp | 6 +++--- src/backend/opencl/wrappers/OclContext.cpp | 7 +++++++ src/backend/opencl/wrappers/OclContext.h | 1 + 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/backend/opencl/OclCache.cpp b/src/backend/opencl/OclCache.cpp index fc4249e8..f22fea72 100644 --- a/src/backend/opencl/OclCache.cpp +++ b/src/backend/opencl/OclCache.cpp @@ -55,7 +55,7 @@ static cl_program createFromSource(const IOclRunner *runner) const char *source = runner->source(); const uint64_t ts = Chrono::steadyMSecs(); - cl_program program = OclLib::createProgramWithSource(runner->data().ctx, 1, &source, nullptr, &ret); + cl_program program = OclLib::createProgramWithSource(runner->ctx(), 1, &source, nullptr, &ret); if (ret != CL_SUCCESS) { return nullptr; } @@ -91,7 +91,7 @@ static cl_program createFromBinary(const IOclRunner *runner, const std::string & cl_int clStatus; cl_int ret; - cl_program program = OclLib::createProgramWithBinary(runner->data().ctx, 1, &device, &bin_size, reinterpret_cast(&data_ptr), &clStatus, &ret); + cl_program program = OclLib::createProgramWithBinary(runner->ctx(), 1, &device, &bin_size, reinterpret_cast(&data_ptr), &clStatus, &ret); if (ret != CL_SUCCESS) { return nullptr; } diff --git a/src/backend/opencl/interfaces/IOclRunner.h b/src/backend/opencl/interfaces/IOclRunner.h index 90b251b6..48f84831 100644 --- a/src/backend/opencl/interfaces/IOclRunner.h +++ b/src/backend/opencl/interfaces/IOclRunner.h @@ -29,6 +29,9 @@ #include +typedef struct _cl_context *cl_context; + + namespace xmrig { @@ -44,6 +47,7 @@ public: virtual bool run(uint32_t nonce, uint32_t *hashOutput) = 0; virtual bool selfTest() const = 0; virtual bool set(const Job &job, uint8_t *blob) = 0; + virtual cl_context ctx() const = 0; virtual const char *buildOptions() const = 0; virtual const char *deviceKey() const = 0; virtual const char *source() const = 0; diff --git a/src/backend/opencl/runners/OclBaseRunner.cpp b/src/backend/opencl/runners/OclBaseRunner.cpp index c13f5057..95d0c05a 100644 --- a/src/backend/opencl/runners/OclBaseRunner.cpp +++ b/src/backend/opencl/runners/OclBaseRunner.cpp @@ -34,18 +34,19 @@ xmrig::OclBaseRunner::OclBaseRunner(size_t id, const OclLaunchData &data) : m_algorithm(data.algorithm), + m_ctx(data.ctx), m_source(OclSource::get(data.algorithm)), m_data(data), m_threadId(id) { cl_int ret; - m_queue = OclLib::createCommandQueue(data.ctx, data.device.id(), &ret); + m_queue = OclLib::createCommandQueue(m_ctx, data.device.id(), &ret); if (ret != CL_SUCCESS) { return; } - m_input = OclLib::createBuffer(data.ctx, CL_MEM_READ_ONLY, Job::kMaxBlobSize, nullptr, &ret); - m_output = OclLib::createBuffer(data.ctx, CL_MEM_READ_WRITE, sizeof(cl_uint) * 0x100, nullptr, &ret); + m_input = OclLib::createBuffer(m_ctx, CL_MEM_READ_ONLY, Job::kMaxBlobSize, nullptr, &ret); + m_output = OclLib::createBuffer(m_ctx, CL_MEM_READ_WRITE, sizeof(cl_uint) * 0x100, nullptr, &ret); m_deviceKey = data.device.name(); diff --git a/src/backend/opencl/runners/OclBaseRunner.h b/src/backend/opencl/runners/OclBaseRunner.h index 57394ebb..c88f1419 100644 --- a/src/backend/opencl/runners/OclBaseRunner.h +++ b/src/backend/opencl/runners/OclBaseRunner.h @@ -47,6 +47,7 @@ public: ~OclBaseRunner() override; protected: + inline cl_context ctx() const override { return m_ctx; } 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; } @@ -60,6 +61,7 @@ protected: protected: Algorithm m_algorithm; cl_command_queue m_queue = nullptr; + cl_context m_ctx; cl_mem m_input = nullptr; cl_mem m_output = nullptr; cl_program m_program = nullptr; diff --git a/src/backend/opencl/runners/OclCnRunner.cpp b/src/backend/opencl/runners/OclCnRunner.cpp index 10765de3..c6070fae 100644 --- a/src/backend/opencl/runners/OclCnRunner.cpp +++ b/src/backend/opencl/runners/OclCnRunner.cpp @@ -41,18 +41,18 @@ xmrig::OclCnRunner::OclCnRunner(size_t index, const OclLaunchData &data) : OclBa const size_t g_thd = data.thread.intensity(); cl_int ret; - m_scratchpads = OclLib::createBuffer(data.ctx, CL_MEM_READ_WRITE, data.algorithm.l3() * g_thd, nullptr, &ret); + m_scratchpads = OclLib::createBuffer(m_ctx, CL_MEM_READ_WRITE, data.algorithm.l3() * g_thd, nullptr, &ret); if (ret != CL_SUCCESS) { return; } - m_states = OclLib::createBuffer(data.ctx, CL_MEM_READ_WRITE, 200 * g_thd, nullptr, &ret); + m_states = OclLib::createBuffer(m_ctx, CL_MEM_READ_WRITE, 200 * g_thd, nullptr, &ret); if (ret != CL_SUCCESS) { return; } for (size_t i = 0; i < BRANCH_MAX; ++i) { - m_branches[i] = OclLib::createBuffer(data.ctx, CL_MEM_READ_WRITE, sizeof(cl_uint) * (g_thd + 2), nullptr, &ret); + m_branches[i] = OclLib::createBuffer(m_ctx, CL_MEM_READ_WRITE, sizeof(cl_uint) * (g_thd + 2), nullptr, &ret); if (ret != CL_SUCCESS) { return; } diff --git a/src/backend/opencl/wrappers/OclContext.cpp b/src/backend/opencl/wrappers/OclContext.cpp index 8f283c78..04217f73 100644 --- a/src/backend/opencl/wrappers/OclContext.cpp +++ b/src/backend/opencl/wrappers/OclContext.cpp @@ -27,6 +27,13 @@ #include "backend/opencl/wrappers/OclContext.h" +xmrig::OclContext::OclContext(const OclDevice &device) +{ + std::vector ids = { device.id() }; + m_ctx = OclLib::createContext(ids); +} + + xmrig::OclContext::~OclContext() { if (m_ctx) { diff --git a/src/backend/opencl/wrappers/OclContext.h b/src/backend/opencl/wrappers/OclContext.h index 6902e805..d3107dac 100644 --- a/src/backend/opencl/wrappers/OclContext.h +++ b/src/backend/opencl/wrappers/OclContext.h @@ -40,6 +40,7 @@ class OclContext { public: OclContext() = default; + OclContext(const OclDevice &device); ~OclContext(); bool init(const std::vector &devices, std::vector &threads);