Hex  2.2
Hydrogen-electron collision solver
clarrays.h
Go to the documentation of this file.
1 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
2 // //
3 // / / / / __ \ \ / / //
4 // / /__ / / / _ \ \ \/ / //
5 // / ___ / | |/_/ / /\ \ //
6 // / / / / \_\ / / \ \ //
7 // //
8 // //
9 // Copyright (c) 2016, Jakub Benda, Charles University in Prague //
10 // //
11 // MIT License: //
12 // //
13 // Permission is hereby granted, free of charge, to any person obtaining a //
14 // copy of this software and associated documentation files (the "Software"), //
15 // to deal in the Software without restriction, including without limitation //
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense, //
17 // and/or sell copies of the Software, and to permit persons to whom the //
18 // Software is furnished to do so, subject to the following conditions: //
19 // //
20 // The above copyright notice and this permission notice shall be included //
21 // in all copies or substantial portions of the Software. //
22 // //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS //
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, //
27 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF //
28 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //
29 // //
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 #ifndef HEX_OPENCL
33 #define HEX_OPENCL
34 
35 #ifdef WITH_OPENCL
36 
37 #include <CL/cl.h>
38 
39 #include "hex-arrays.h"
40 
48 template <class T> class clArrayView : public ArrayView<T>
49 {
50  protected:
51 
53  cl_mem cl_handle_;
54 
55  public:
56 
57  //
58  // aliases
59  //
60 
61  typedef T DataType;
62  typedef T * iterator;
63  typedef T const * const_iterator;
64 
65  //
66  // basic constructors
67  //
68 
70  : ArrayView<T>(), cl_handle_(nullptr) { }
71  clArrayView (std::size_t n, T const * ptr)
72  : ArrayView<T>(n, const_cast<T*>(ptr)), cl_handle_(nullptr) { }
73  clArrayView (ArrayView<T> v, std::size_t i = 0, std::size_t n = 0)
74  : ArrayView<T>(v, i, n), cl_handle_(nullptr) { }
76  : ArrayView<T>(v), cl_handle_(v.cl_handle_) { }
77  clArrayView (ArrayView<T> && v)
78  : ArrayView<T>(v), cl_handle_(nullptr) { }
80  : ArrayView<T>(), cl_handle_(nullptr)
81  {
82  std::swap (ArrayView<T>::array_, v.ArrayView<T>::array_);
83  std::swap (ArrayView<T>::N_, v.ArrayView<T>::N_ );
84  std::swap (cl_handle_, v.cl_handle_ );
85  }
86 
87  //
88  // destructor
89  //
90 
91  virtual ~clArrayView ()
92  {
93  }
94 
95  //
96  // STL interface (except changes in size -- we don't want to bother with GPU reallocation)
97  //
98 
99  std::size_t size () const { return ArrayView<T>::size(); }
100  T const & operator [] (std::size_t i) const { return ArrayView<T>::operator[](i); }
101  T & operator [] (std::size_t i) { return ArrayView<T>::operator[](i); }
102  T const * data () const { return ArrayView<T>::data(); }
103  T * data () { return ArrayView<T>::data(); }
104  const_iterator begin () const { return ArrayView<T>::begin(); }
105  iterator begin () { return ArrayView<T>::begin(); }
106  const_iterator end () const { return ArrayView<T>::end(); }
107  iterator end () { return ArrayView<T>::end(); }
108  T const & front (std::size_t i = 0) const { return ArrayView<T>::front(i); }
109  T & front (std::size_t i = 0) { return ArrayView<T>::front(i); }
110  T const & back (std::size_t i = 0) const { return ArrayView<T>::back(i); }
111  T & back (std::size_t i = 0) { return ArrayView<T>::back(i); }
112 
113  //
114  // OpenCL intrinsics
115  //
116 
117  bool is_connected () const
118  {
119  return cl_handle_ != nullptr;
120  }
121 
122  void connect (cl_context context, cl_mem_flags flags)
123  {
124  // release previous allocation prior to creating a new buffer !
125  if (cl_handle_ != nullptr)
126  HexException("[clArray::connect] Array is already connected!");
127 
128  // allocate memory on GPU
129  cl_handle_ = clCreateBuffer(context, flags, size() * sizeof(T), data(), nullptr);
130  }
131 
132  void disconnect ()
133  {
134  // release previous allocations
135  if (cl_handle_ != nullptr)
136  clReleaseMemObject(cl_handle_);
137 
138  // clear pointer
139  cl_handle_ = nullptr;
140  }
141 
142  cl_int EnqueueUpload (cl_command_queue queue)
143  {
144  return clEnqueueWriteBuffer(queue, cl_handle_, CL_TRUE, 0, size() * sizeof(T), data(), 0, nullptr, nullptr);
145  }
146 
147  cl_int EnqueueDownload (cl_command_queue queue)
148  {
149  return clEnqueueReadBuffer(queue, cl_handle_, CL_TRUE, 0, size() * sizeof(T), data(), 0, nullptr, nullptr);
150  }
151 
152  cl_mem const & handle () const
153  {
154  return cl_handle_;
155  }
156 };
157 
172 #ifndef NO_ALIGN
173 #define CL_ALIGNMENT 4096
174 template <class T, class Alloc = AlignedAllocator<T,CL_ALIGNMENT>> class clArray : public clArrayView<T>
175 #else
176 template <class T, class Alloc = PlainAllocator<T>> class clArray : public clArrayView<T>
177 #endif
178 {
179  public:
180 
181  //
182  // aliases
183  //
184 
185  typedef T DataType;
186  typedef T * iterator;
187  typedef T const * const_iterator;
188 
189  //
190  // basic constructors
191  //
192 
194  {
195  // nothing to do
196  }
197  clArray (std::size_t n, T x = T(0)) : clArrayView<T>(n, Alloc::alloc(n))
198  {
199  for (std::size_t i = 0; i < n; i++)
200  (*this)[i] = x;
201  }
202  clArray (std::size_t n, T const * px) : clArrayView<T>(n, Alloc::alloc(n))
203  {
204  for (std::size_t i = 0; i < n; i++)
205  (*this)[i] = px[i];
206  }
207  clArray (ArrayView<T> v, std::size_t i = 0, std::size_t n = 0)
208  : clArrayView<T>(((n == 0) ? v.size() : n), Alloc::alloc((n == 0) ? v.size() : n))
209  {
210  for (std::size_t j = 0; j < size(); j++)
211  (*this)[j] = v[i+j];
212  }
213  clArray (std::initializer_list<T> list) : clArrayView<T>(list.size(), Alloc::alloc(list.size()))
214  {
215  std::size_t i = 0;
216  for (auto it = list.begin(); it != list.end(); it++)
217  (*this)[i++] = *it;
218  }
219  clArray (ArrayView<T> && rvrf) : clArrayView<T>(std::move(rvrf))
220  {
221  // nothing to do
222  }
223  clArray (clArray<T,Alloc> && rvrf) : clArrayView<T>(std::move(rvrf))
224  {
225  // nothing to do
226  }
227 
228  //
229  // data assignment
230  //
231 
232  clArray & operator = (const ArrayView<T> v)
233  {
234  // realloc memory if needed
235  resize(v.size());
236 
237  // copy data
238  for (std::size_t j = 0; j < size(); j++)
239  (*this)[j] = v[j];
240 
241  return *this;
242  }
243 
244  void resize (std::size_t newsize)
245  {
246  if (newsize != size())
247  {
248  if (size() != 0)
249  Alloc::free(data());
250 
251  ArrayView<T>::array_ = Alloc::alloc(newsize);
252  ArrayView<T>::N_ = newsize;
253  }
254  }
255 
256  //
257  // destructor
258  //
259 
260  virtual ~clArray ()
261  {
262  // free GPU memory
263  disconnect();
264 
265  // free RAM memory
266  Alloc::free(data());
267  }
268 
269  //
270  // STL interface (except changes in size -- we don't want to bother with GPU reallocation)
271  //
272 
273  std::size_t size () const { return clArrayView<T>::size(); }
274  T const & operator [] (std::size_t i) const { return clArrayView<T>::operator[](i); }
275  T & operator [] (std::size_t i) { return clArrayView<T>::operator[](i); }
276  T const * data () const { return clArrayView<T>::data(); }
277  T * data () { return clArrayView<T>::data(); }
278  const_iterator begin () const { return clArrayView<T>::begin(); }
279  iterator begin () { return clArrayView<T>::begin(); }
280  const_iterator end () const { return clArrayView<T>::end(); }
281  iterator end () { return clArrayView<T>::end(); }
282  T const & front (std::size_t i = 0) const { return clArrayView<T>::front(i); }
283  T & front (std::size_t i = 0) { return clArrayView<T>::front(i); }
284  T const & back (std::size_t i = 0) const { return clArrayView<T>::back(i); }
285  T & back (std::size_t i = 0) { return clArrayView<T>::back(i); }
286 
287  //
288  // OpenCL intrinsics
289  //
290 
291  bool is_connected () const
292  {
294  }
295 
296  void connect (cl_context context, cl_mem_flags flags = CL_MEM_COPY_HOST_PTR | CL_MEM_READ_WRITE)
297  {
298  clArrayView<T>::connect(context, flags);
299  }
300 
301  void disconnect ()
302  {
304  }
305 
306  cl_int EnqueueDownload (cl_command_queue queue)
307  {
308  return clArrayView<T>::EnqueueDownload(queue);
309  }
310 
311  cl_int EnqueueUpload (cl_command_queue queue)
312  {
313  return clArrayView<T>::EnqueueUpload(queue);
314  }
315 
316  cl_mem const & handle () const
317  {
318  return clArrayView<T>::handle();
319  }
320 };
321 
322 #endif // WITH_OPENCL
323 
324 #endif
T & front(std::size_t i=0)
Definition: clarrays.h:109
clArray(std::initializer_list< T > list)
Definition: clarrays.h:213
clArray(clArray< T, Alloc > &&rvrf)
Definition: clarrays.h:223
T DataType
Definition: clarrays.h:61
cl_mem const & handle() const
Definition: clarrays.h:316
T * data()
Definition: clarrays.h:103
cl_mem const & handle() const
Definition: clarrays.h:152
clArray(ArrayView< T > &&rvrf)
Definition: clarrays.h:219
const_iterator begin() const
Definition: clarrays.h:104
const_iterator end() const
Definition: clarrays.h:280
T DataType
Definition: clarrays.h:185
clArrayView(clArrayView< T > &&v)
Definition: clarrays.h:79
clArrayView(ArrayView< T > &&v)
Definition: clarrays.h:77
iterator end()
Definition: clarrays.h:107
iterator begin()
Definition: clarrays.h:279
void connect(cl_context context, cl_mem_flags flags=CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE)
Definition: clarrays.h:296
T * data()
Definition: clarrays.h:277
clArrayView()
Definition: clarrays.h:69
T const * data() const
Definition: clarrays.h:102
Definition: clarrays.h:174
std::size_t size() const
Definition: clarrays.h:99
T & back(std::size_t i=0)
Definition: clarrays.h:285
iterator end()
Definition: clarrays.h:281
clArrayView(ArrayView< T > v, std::size_t i=0, std::size_t n=0)
Definition: clarrays.h:73
OpenCL array wrapper.
Definition: clarrays.h:48
void connect(cl_context context, cl_mem_flags flags)
Definition: clarrays.h:122
T const & front(std::size_t i=0) const
Definition: clarrays.h:108
const_iterator begin() const
Definition: clarrays.h:278
const_iterator end() const
Definition: clarrays.h:106
T const * const_iterator
Definition: clarrays.h:63
clArrayView(std::size_t n, T const *ptr)
Definition: clarrays.h:71
T const & back(std::size_t i=0) const
Definition: clarrays.h:110
cl_mem cl_handle_
Memory handle to the data copy on the GPU.
Definition: clarrays.h:53
T const * const_iterator
Definition: clarrays.h:187
T * iterator
Definition: clarrays.h:62
T const & back(std::size_t i=0) const
Definition: clarrays.h:284
T const * data() const
Definition: clarrays.h:276
clArray(std::size_t n, T x=T(0))
Definition: clarrays.h:197
T * iterator
Definition: clarrays.h:186
T const & front(std::size_t i=0) const
Definition: clarrays.h:282
T & front(std::size_t i=0)
Definition: clarrays.h:283
clArray(std::size_t n, T const *px)
Definition: clarrays.h:202
void disconnect()
Definition: clarrays.h:132
clArray(ArrayView< T > v, std::size_t i=0, std::size_t n=0)
Definition: clarrays.h:207
T const & operator[](std::size_t i) const
Definition: clarrays.h:100
clArrayView(clArrayView< T > const &v)
Definition: clarrays.h:75
void resize(std::size_t newsize)
Definition: clarrays.h:244
iterator begin()
Definition: clarrays.h:105
cl_int EnqueueUpload(cl_command_queue queue)
Definition: clarrays.h:142
T & back(std::size_t i=0)
Definition: clarrays.h:111
cl_int EnqueueUpload(cl_command_queue queue)
Definition: clarrays.h:311
cl_int EnqueueDownload(cl_command_queue queue)
Definition: clarrays.h:306
void disconnect()
Definition: clarrays.h:301
bool is_connected() const
Definition: clarrays.h:291
bool is_connected() const
Definition: clarrays.h:117
clArray()
Definition: clarrays.h:193
std::size_t size() const
Definition: clarrays.h:273
cl_int EnqueueDownload(cl_command_queue queue)
Definition: clarrays.h:147
virtual ~clArray()
Definition: clarrays.h:260
virtual ~clArrayView()
Definition: clarrays.h:91