forked from Nobay3679/profanity-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLMemory.hpp
83 lines (67 loc) · 2.43 KB
/
CLMemory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef HPP_CLMEMORY
#define HPP_CLMEMORY
#include "lexical_cast.hpp"
template<typename T> class CLMemory {
public:
CLMemory(cl_context & clContext, cl_command_queue & clQueue, const cl_mem_flags flags, const size_t size, T * const pData)
: 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);
}
~CLMemory() {
if(m_bFree) {
delete [] m_pData;
}
}
static void setKernelArg(cl_kernel & clKernel, const cl_uint arg_index, const T & t) {
const cl_int ret = clSetKernelArg(clKernel, arg_index, sizeof(T), (void *) &t);
if (ret != CL_SUCCESS) {
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;
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;
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];
}
T * operator->() const {
return m_pData;
}
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;
};
#endif /* HPP_CLMEMORY */