Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Major overhaul. Users now have more control over memory usage on the …
Browse files Browse the repository at this point in the history
…device. Overall a minor speed increase.
  • Loading branch information
Johan Gustafsson committed May 19, 2018
1 parent 9bc11f9 commit d8b5f01
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 382 deletions.
28 changes: 15 additions & 13 deletions CLMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ template<typename T> class CLMemory {
: m_clQueue(clQueue), m_bFree(false), m_size(size), m_pData(pData) {
m_clMem = clCreateBuffer(clContext, flags, m_size, NULL, NULL);
}

CLMemory(cl_context & clContext, cl_command_queue & clQueue, const cl_mem_flags flags, const size_t count, const bool noAllocation = false)
: m_clQueue(clQueue), m_bFree(true), m_size(sizeof(T) * count), m_pData(noAllocation ? NULL : new T[count]) {
m_clMem = clCreateBuffer(clContext, flags, m_size, NULL, NULL);
Expand All @@ -27,32 +27,34 @@ template<typename T> class CLMemory {
throw std::runtime_error("clSetKernelArg failed - " + toString(arg_index) + " - " + toString(ret));
}
}

void setKernelArg(cl_kernel & clKernel, const cl_uint arg_index) const {
const cl_int ret = clSetKernelArg(clKernel, arg_index, sizeof(cl_mem), (void *) &m_clMem );
if( ret != CL_SUCCESS ) {
throw std::runtime_error("clSetKernelArg failed - " + toString(arg_index) + " - " + toString(ret));
}
}

void read(const bool bBlock, cl_event * pEvent = NULL) const {
const cl_bool block = bBlock ? CL_TRUE : CL_FALSE;
if( clEnqueueReadBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, pEvent) != CL_SUCCESS ) {
throw std::runtime_error("clEnqueueReadBuffer failed");
auto res = clEnqueueReadBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, pEvent);
if(res != CL_SUCCESS) {
throw std::runtime_error("clEnqueueReadBuffer failed - " + toString(res));
}
}

void write(const bool bBlock) const {
const cl_bool block = bBlock ? CL_TRUE : CL_FALSE;
if( clEnqueueWriteBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, NULL) != CL_SUCCESS ) {
throw std::runtime_error("clEnqueueWriteBuffer failed");
auto res = clEnqueueWriteBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, NULL);
if( res != CL_SUCCESS ) {
throw std::runtime_error("clEnqueueWriteBuffer failed - " + toString(res));
}
}

T * const & data() const {
return m_pData;
}

T & operator[] (int x) const {
return m_pData[x];
}
Expand All @@ -64,16 +66,16 @@ template<typename T> class CLMemory {
T & operator*() const {
return *m_pData;
}

const size_t & size() const {
return m_size;
}

private:
const cl_command_queue m_clQueue;
const bool m_bFree;
const size_t m_size;

T * const m_pData;
cl_mem m_clMem;
};
Expand Down
Loading

0 comments on commit d8b5f01

Please sign in to comment.