Added OclContext class.
This commit is contained in:
parent
2a07bc4ef3
commit
cf123b7d88
9 changed files with 161 additions and 16 deletions
58
src/backend/opencl/wrappers/OclContext.cpp
Normal file
58
src/backend/opencl/wrappers/OclContext.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* 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 "backend/opencl/wrappers/OclLib.h"
|
||||
#include "backend/opencl/wrappers/OclContext.h"
|
||||
|
||||
|
||||
xmrig::OclContext::~OclContext()
|
||||
{
|
||||
if (m_ctx) {
|
||||
OclLib::releaseContext(m_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool xmrig::OclContext::init(const std::vector<OclDevice> &devices, std::vector<OclLaunchData> &threads)
|
||||
{
|
||||
if (!m_ctx) {
|
||||
std::vector<cl_device_id> ids(devices.size());
|
||||
for (size_t i = 0; i < devices.size(); ++i) {
|
||||
ids[i] = devices[i].id();
|
||||
}
|
||||
|
||||
m_ctx = OclLib::createContext(ids);
|
||||
}
|
||||
|
||||
if (!m_ctx) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (OclLaunchData &data : threads) {
|
||||
data.ctx = m_ctx;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
58
src/backend/opencl/wrappers/OclContext.h
Normal file
58
src/backend/opencl/wrappers/OclContext.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* 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_OCLCONTEXT_H
|
||||
#define XMRIG_OCLCONTEXT_H
|
||||
|
||||
|
||||
#include "backend/opencl/OclLaunchData.h"
|
||||
#include "backend/opencl/wrappers/OclDevice.h"
|
||||
|
||||
|
||||
typedef struct _cl_context *cl_context;
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class OclContext
|
||||
{
|
||||
public:
|
||||
OclContext() = default;
|
||||
~OclContext();
|
||||
|
||||
bool init(const std::vector<OclDevice> &devices, std::vector<OclLaunchData> &threads);
|
||||
|
||||
inline bool isValid() const { return m_ctx != nullptr; }
|
||||
inline cl_context ctx() const { return m_ctx; }
|
||||
|
||||
private:
|
||||
cl_context m_ctx = nullptr;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
#endif /* XMRIG_OCLCONTEXT_H */
|
|
@ -29,6 +29,7 @@
|
|||
#include "backend/opencl/OclThreads.h"
|
||||
#include "backend/opencl/wrappers/OclDevice.h"
|
||||
#include "backend/opencl/wrappers/OclLib.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "crypto/cn/CnAlgo.h"
|
||||
#include "crypto/common/Algorithm.h"
|
||||
#include "rapidjson/document.h"
|
||||
|
@ -151,6 +152,22 @@ size_t xmrig::OclDevice::globalMem() const
|
|||
}
|
||||
|
||||
|
||||
xmrig::String xmrig::OclDevice::printableName() const
|
||||
{
|
||||
const size_t size = m_board.size() + m_name.size() + 64;
|
||||
char *buf = new char[size]();
|
||||
|
||||
if (m_board.isNull()) {
|
||||
snprintf(buf, size, GREEN_BOLD(" %s"), m_name.data());
|
||||
}
|
||||
else {
|
||||
snprintf(buf, size, GREEN_BOLD(" %s") " (" CYAN_BOLD("%s") ")", m_board.data(), m_name.data());
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
uint32_t xmrig::OclDevice::clock() const
|
||||
{
|
||||
return OclLib::getDeviceUint(id(), CL_DEVICE_MAX_CLOCK_FREQUENCY);
|
||||
|
|
|
@ -64,6 +64,7 @@ public:
|
|||
|
||||
size_t freeMem() const;
|
||||
size_t globalMem() const;
|
||||
String printableName() const;
|
||||
uint32_t clock() const;
|
||||
void generate(const Algorithm &algorithm, OclThreads &threads) const;
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
|
||||
static uv_lib_t oclLib;
|
||||
|
||||
static const char *kErrorTemplate = "Error %s when calling %s.";
|
||||
static const char *kErrorTemplate = MAGENTA_BG_BOLD(WHITE_BOLD_S " ocl ") RED(" error ") RED_BOLD("%s") RED(" when calling ") RED_BOLD("%s");
|
||||
|
||||
static const char *kBuildProgram = "clBuildProgram";
|
||||
static const char *kCreateBuffer = "clCreateBuffer";
|
||||
|
@ -235,12 +235,21 @@ cl_context xmrig::OclLib::createContext(const cl_context_properties *properties,
|
|||
auto result = pCreateContext(properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
|
||||
if (*errcode_ret != CL_SUCCESS) {
|
||||
LOG_ERR(kErrorTemplate, OclError::toString(*errcode_ret), kCreateContext);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
cl_context xmrig::OclLib::createContext(const std::vector<cl_device_id> &ids)
|
||||
{
|
||||
cl_int ret;
|
||||
return OclLib::createContext(nullptr, static_cast<cl_uint>(ids.size()), ids.data(), nullptr, nullptr, &ret);
|
||||
}
|
||||
|
||||
|
||||
cl_int xmrig::OclLib::buildProgram(cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data)
|
||||
{
|
||||
assert(pBuildProgram != nullptr);
|
||||
|
|
|
@ -48,6 +48,7 @@ public:
|
|||
|
||||
static cl_command_queue createCommandQueue(cl_context context, cl_device_id device, cl_int *errcode_ret);
|
||||
static cl_context createContext(const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *devices, void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void *user_data, cl_int *errcode_ret);
|
||||
static cl_context createContext(const std::vector<cl_device_id> &ids);
|
||||
static cl_int buildProgram(cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options = nullptr, void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data) = nullptr, void *user_data = nullptr);
|
||||
static cl_int enqueueNDRangeKernel(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *global_work_offset, const size_t *global_work_size, const size_t *local_work_size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event);
|
||||
static cl_int enqueueReadBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue