diff --git a/src/backend/opencl/runners/OclBaseRunner.cpp b/src/backend/opencl/runners/OclBaseRunner.cpp index 2e75d9b61..5c498dbd1 100644 --- a/src/backend/opencl/runners/OclBaseRunner.cpp +++ b/src/backend/opencl/runners/OclBaseRunner.cpp @@ -99,12 +99,14 @@ void xmrig::OclBaseRunner::init() { m_queue = OclLib::createCommandQueue(m_ctx, data().device.id()); - size_t size = align(bufferSize()); + size_t size = align(bufferSize()); + const size_t limit = data().device.freeMemSize(); - if (size < oneGiB && data().device.vendorId() == OCL_VENDOR_AMD && data().device.freeMemSize() >= oneGiB) { - m_buffer = OclSharedState::get(data().device.index()).createBuffer(m_ctx, size, m_offset); + if (size < oneGiB && data().device.vendorId() == OCL_VENDOR_AMD && limit >= oneGiB) { + m_buffer = OclSharedState::get(data().device.index()).createBuffer(m_ctx, size, m_offset, limit); } - else { + + if (!m_buffer) { m_buffer = OclLib::createBuffer(m_ctx, CL_MEM_READ_WRITE, size); } diff --git a/src/backend/opencl/runners/tools/OclSharedData.cpp b/src/backend/opencl/runners/tools/OclSharedData.cpp index 355ab7755..38e6a6769 100644 --- a/src/backend/opencl/runners/tools/OclSharedData.cpp +++ b/src/backend/opencl/runners/tools/OclSharedData.cpp @@ -40,12 +40,19 @@ constexpr size_t oneGiB = 1024 * 1024 * 1024; -cl_mem xmrig::OclSharedData::createBuffer(cl_context context, size_t size, size_t &offset) +cl_mem xmrig::OclSharedData::createBuffer(cl_context context, size_t size, size_t &offset, size_t limit) { std::lock_guard lock(m_mutex); - offset += size * m_offset++; - size = std::max(size * m_threads, oneGiB); + const size_t pos = offset + (size * m_offset); + size = std::max(size * m_threads, oneGiB); + + if (size > limit) { + return nullptr; + } + + offset = pos; + ++m_offset; if (!m_buffer) { m_buffer = OclLib::createBuffer(context, CL_MEM_READ_WRITE, size); diff --git a/src/backend/opencl/runners/tools/OclSharedData.h b/src/backend/opencl/runners/tools/OclSharedData.h index caf7b4d66..393ed3b8a 100644 --- a/src/backend/opencl/runners/tools/OclSharedData.h +++ b/src/backend/opencl/runners/tools/OclSharedData.h @@ -45,7 +45,7 @@ class OclSharedData public: OclSharedData() = default; - cl_mem createBuffer(cl_context context, size_t size, size_t &offset); + cl_mem createBuffer(cl_context context, size_t size, size_t &offset, size_t limit); uint64_t adjustDelay(size_t id); uint64_t resumeDelay(size_t id); void release();