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

Commit d8b5f01

Browse files
author
Johan Gustafsson
committed
Major overhaul. Users now have more control over memory usage on the device. Overall a minor speed increase.
1 parent 9bc11f9 commit d8b5f01

11 files changed

+472
-382
lines changed

CLMemory.hpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ template<typename T> class CLMemory {
99
: m_clQueue(clQueue), m_bFree(false), m_size(size), m_pData(pData) {
1010
m_clMem = clCreateBuffer(clContext, flags, m_size, NULL, NULL);
1111
}
12-
12+
1313
CLMemory(cl_context & clContext, cl_command_queue & clQueue, const cl_mem_flags flags, const size_t count, const bool noAllocation = false)
1414
: m_clQueue(clQueue), m_bFree(true), m_size(sizeof(T) * count), m_pData(noAllocation ? NULL : new T[count]) {
1515
m_clMem = clCreateBuffer(clContext, flags, m_size, NULL, NULL);
@@ -27,32 +27,34 @@ template<typename T> class CLMemory {
2727
throw std::runtime_error("clSetKernelArg failed - " + toString(arg_index) + " - " + toString(ret));
2828
}
2929
}
30-
30+
3131
void setKernelArg(cl_kernel & clKernel, const cl_uint arg_index) const {
3232
const cl_int ret = clSetKernelArg(clKernel, arg_index, sizeof(cl_mem), (void *) &m_clMem );
3333
if( ret != CL_SUCCESS ) {
3434
throw std::runtime_error("clSetKernelArg failed - " + toString(arg_index) + " - " + toString(ret));
3535
}
3636
}
37-
37+
3838
void read(const bool bBlock, cl_event * pEvent = NULL) const {
3939
const cl_bool block = bBlock ? CL_TRUE : CL_FALSE;
40-
if( clEnqueueReadBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, pEvent) != CL_SUCCESS ) {
41-
throw std::runtime_error("clEnqueueReadBuffer failed");
40+
auto res = clEnqueueReadBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, pEvent);
41+
if(res != CL_SUCCESS) {
42+
throw std::runtime_error("clEnqueueReadBuffer failed - " + toString(res));
4243
}
4344
}
44-
45+
4546
void write(const bool bBlock) const {
4647
const cl_bool block = bBlock ? CL_TRUE : CL_FALSE;
47-
if( clEnqueueWriteBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, NULL) != CL_SUCCESS ) {
48-
throw std::runtime_error("clEnqueueWriteBuffer failed");
48+
auto res = clEnqueueWriteBuffer(m_clQueue, m_clMem, block, 0, m_size, m_pData, 0, NULL, NULL);
49+
if( res != CL_SUCCESS ) {
50+
throw std::runtime_error("clEnqueueWriteBuffer failed - " + toString(res));
4951
}
5052
}
51-
53+
5254
T * const & data() const {
5355
return m_pData;
5456
}
55-
57+
5658
T & operator[] (int x) const {
5759
return m_pData[x];
5860
}
@@ -64,16 +66,16 @@ template<typename T> class CLMemory {
6466
T & operator*() const {
6567
return *m_pData;
6668
}
67-
69+
6870
const size_t & size() const {
6971
return m_size;
7072
}
71-
73+
7274
private:
7375
const cl_command_queue m_clQueue;
7476
const bool m_bFree;
7577
const size_t m_size;
76-
78+
7779
T * const m_pData;
7880
cl_mem m_clMem;
7981
};

0 commit comments

Comments
 (0)