HOPS
HOPS class reference
MHO_OpenCLNDArrayBuffer.hh
Go to the documentation of this file.
1 #ifndef MHO_OpenCLNDArrayBuffer_HH__
2 #define MHO_OpenCLNDArrayBuffer_HH__
3 
5 #include "MHO_OpenCLInterface.hh"
6 
7 namespace hops
8 {
9 
18 template< typename XArrayType > class MHO_OpenCLNDArrayBuffer
19 {
20  public:
22  : fElement(element), fDimensionBufferCL(nullptr), fDataBufferCL(nullptr)
23  {
24  fRank = XArrayType::rank::value;
25  fNDArray = dynamic_cast< XArrayType* >(element);
26  };
27 
29  {
30  delete fDimensionBufferCL;
31  delete fDataBufferCL;
32  };
33 
35  {
36  //buffer for the dimensions of the array
37  unsigned int n_bytes = (fRank) * sizeof(unsigned int);
39  fDimensionBufferCL = new cl::Buffer(MHO_OpenCLInterface::GetInstance()->GetContext(), CL_MEM_READ_ONLY, n_bytes);
41  }
42 
44  {
45  //buffer for the data
46  unsigned int n_bytes = (static_cast< unsigned int >(fNDArray->GetSize())) *
49  fDataBufferCL = new cl::Buffer(MHO_OpenCLInterface::GetInstance()->GetContext(), CL_MEM_READ_WRITE, n_bytes);
51  }
52 
53  cl::Buffer* GetDimensionBuffer() { return fDimensionBufferCL; };
54 
55  cl::Buffer* GetDataBuffer() { return fDataBufferCL; }
56 
58  {
59  //writes the dimensions of the array from the host to the device
60  cl::CommandQueue& Q = MHO_OpenCLInterface::GetInstance()->GetQueue();
61  unsigned int n_bytes = fRank * sizeof(unsigned int);
62  for(unsigned int i = 0; i < fRank; i++)
63  {
64  fDimensions[i] = static_cast< unsigned int >(fNDArray->GetDimension(i));
65  }
66  //we enqueue write the needed constants for this dimension
68  Q.enqueueWriteBuffer(*fDimensionBufferCL, CL_TRUE, 0, n_bytes, &(fDimensions[0]));
70 #ifdef ENFORCE_CL_FINISH
71  Q.finish();
72 #endif
73  }
74 
76  {
77  //Writes data from the host to device
78  cl::CommandQueue& Q = MHO_OpenCLInterface::GetInstance()->GetQueue();
79  unsigned int n_bytes = (static_cast< unsigned int >(fNDArray->GetSize())) *
83  fNDArray->GetData());
85  Q.enqueueWriteBuffer(*fDataBufferCL, CL_TRUE, 0, n_bytes, ptr);
87 #ifdef ENFORCE_CL_FINISH
88  Q.finish();
89 #endif
90  }
91 
93  {
94  //TODO FIXME -- need to make sure typename XArrayType::value_type is the same size as equivalent OpenCL type we are mapping to
95  //read out data from the GPU to the host memory
96  cl::CommandQueue& Q = MHO_OpenCLInterface::GetInstance()->GetQueue();
97  unsigned int n_bytes = (static_cast< unsigned int >(fNDArray->GetSize())) *
100  ptr = reinterpret_cast< typename MHO_OpenCLTypeMap< typename XArrayType::value_type >::mapped_type* >(
101  fNDArray->GetData());
103  Q.enqueueReadBuffer(*fDataBufferCL, CL_TRUE, 0, n_bytes, ptr);
105 #ifdef ENFORCE_CL_FINISH
106  Q.finish();
107 #endif
108  }
109 
110  protected:
112  XArrayType* fNDArray;
113  unsigned int fRank;
114 
115  //The OpenCL buffers associated with this table are:
116  //(1) A buffer containing the dimensions of the table + total size
117  //(2) A buffer for the ND-data array
118 
119  //buffer for the dimensions of the array
120  //this buffer must be made of unsigned int (fixed width), as opencl doesnt support size_t
121  unsigned int fDimensions[XArrayType::rank::value];
122 
123  //buffer for the array dimensions
124  cl::Buffer* fDimensionBufferCL;
125 
126  //buffer for the array data
127  cl::Buffer* fDataBufferCL;
128 };
129 
130 } // namespace hops
131 
132 #endif
XValueType mapped_type
Definition: MHO_OpenCLInterface.hh:28
#define CL_ERROR_CATCH
Definition: MHO_OpenCLInterface.hh:93
#define CL_ERROR_TRY
Definition: MHO_OpenCLInterface.hh:85
Class MHO_ExtensibleElement.
Definition: MHO_ExtensibleElement.hh:60
cl::CommandQueue & GetQueue(int i=-1) const
Definition: MHO_OpenCLInterface.cc:125
static MHO_OpenCLInterface * GetInstance()
Definition: MHO_OpenCLInterface.cc:32
Definition: MHO_OpenCLNDArrayBuffer.hh:19
XArrayType * fNDArray
Definition: MHO_OpenCLNDArrayBuffer.hh:112
void ConstructDataBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:43
void ReadDataBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:92
unsigned int fRank
Definition: MHO_OpenCLNDArrayBuffer.hh:113
unsigned int fDimensions[XArrayType::rank::value]
Definition: MHO_OpenCLNDArrayBuffer.hh:121
cl::Buffer * fDataBufferCL
Definition: MHO_OpenCLNDArrayBuffer.hh:127
cl::Buffer * fDimensionBufferCL
Definition: MHO_OpenCLNDArrayBuffer.hh:124
cl::Buffer * GetDimensionBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:53
MHO_ExtensibleElement * fElement
Definition: MHO_OpenCLNDArrayBuffer.hh:111
void WriteDataBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:75
virtual ~MHO_OpenCLNDArrayBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:28
void ConstructDimensionBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:34
void WriteDimensionBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:57
cl::Buffer * GetDataBuffer()
Definition: MHO_OpenCLNDArrayBuffer.hh:55
MHO_OpenCLNDArrayBuffer(MHO_ExtensibleElement *element)
Definition: MHO_OpenCLNDArrayBuffer.hh:21
Definition: MHO_ChannelLabeler.hh:17