SkelCL
SkelCL is a high level multi GPU skeleton library developed at the university of Münster, Germany.
 All Classes Namespaces Files Functions Variables Typedefs Groups
Matrix.h
1 /*****************************************************************************
2  * Copyright (c) 2011-2012 The SkelCL Team as listed in CREDITS.txt *
3  * http://skelcl.uni-muenster.de *
4  * *
5  * This file is part of SkelCL. *
6  * SkelCL is available under multiple licenses. *
7  * The different licenses are subject to terms and condition as provided *
8  * in the files specifying the license. See "LICENSE.txt" for details *
9  * *
10  *****************************************************************************
11  * *
12  * SkelCL is free software: you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation, either version 3 of the License, or *
15  * (at your option) any later version. See "LICENSE-gpl.txt" for details. *
16  * *
17  * SkelCL is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  *****************************************************************************
23  * *
24  * For non-commercial academic use see the license specified in the file *
25  * "LICENSE-academic.txt". *
26  * *
27  *****************************************************************************
28  * *
29  * If you are interested in other licensing models, including a commercial- *
30  * license, please contact the author at michel.steuwer@uni-muenster.de *
31  * *
32  *****************************************************************************/
33 
40 
41 #ifndef MATRIX_H_
42 #define MATRIX_H_
43 
44 #include <map>
45 #include <memory>
46 #include <string>
47 #include <vector>
48 
49 #define __CL_ENABLE_EXCEPTIONS
50 #include <CL/cl.hpp>
51 #undef __CL_ENABLE_EXCEPTIONS
52 
53 #include "Distributions.h"
54 
55 #include "detail/Device.h"
56 #include "detail/DeviceBuffer.h"
57 #include "detail/Distribution.h"
58 #include "detail/Padding.h"
59 #include "detail/skelclDll.h"
60 
61 namespace skelcl {
62 
67 class SKELCL_DLL MatrixSize {
68 public:
69  typedef size_t size_type;
70 
76  MatrixSize(size_type rowCount, size_type columnCount);
77 
82  size_type elemCount() const;
83 
87  size_type rowCount() const;
88 
92  size_type columnCount() const;
93 
100  bool operator==(const MatrixSize& rhs) const;
101 
107  bool operator!=(const MatrixSize& rhs) const;
108 
119  bool operator> (const MatrixSize& rhs) const;
120 
130  bool operator< (const MatrixSize& rhs) const;
131 
142  bool operator>=(const MatrixSize& rhs) const;
143 
153  bool operator<=(const MatrixSize& rhs) const;
154 
155 private:
156  size_type _rowCount;
157  size_type _columnCount;
158 };
159 
163 template <typename T>
164 class RegisterMatrixDeviceFunctions {
165 public:
166  RegisterMatrixDeviceFunctions();
167 };
169 
174 
189 template <typename T>
190 class Matrix {
191 public:
194  typedef std::vector<T> host_buffer_type;
197  typedef typename host_buffer_type::value_type value_type;
200  typedef typename host_buffer_type::pointer pointer;
203  typedef typename host_buffer_type::const_pointer const_pointer;
206  typedef typename host_buffer_type::reference reference;
209  typedef typename host_buffer_type::const_reference const_reference;
212  typedef typename host_buffer_type::iterator iterator;
215  typedef typename host_buffer_type::const_iterator const_iterator;
218  typedef typename skelcl::MatrixSize size_type;
221  typedef typename host_buffer_type::difference_type difference_type;
224  typedef typename host_buffer_type::allocator_type allocator_type;
225 
229  struct coordinate {
232  typedef size_type::size_type index_type;
233 
240  };
241 
244  Matrix();
245 
253  Matrix(const size_type size, const value_type& value = value_type(),
254  const detail::Distribution<Matrix<T>>& distribution =
255  detail::Distribution<Matrix<T>>());
256 
261  Matrix(const std::vector<T>& vector,
262  const typename size_type::size_type columnCount,
263  const detail::Distribution<Matrix<T>>& distribution =
264  detail::Distribution<Matrix<T>>());
265 
269  Matrix(const std::vector<T>& vector, const size_type size,
270  const detail::Distribution<Matrix<T>>& distribution =
271  detail::Distribution<Matrix<T>>());
272 
277  static Matrix<T>
278  from2DVector(const std::vector<std::vector<T>>& matrix,
279  const detail::Distribution<Matrix<T>>& distribution =
280  detail::Distribution<Matrix<T>>());
281 
285  template <class InputIterator>
286  Matrix(InputIterator first, InputIterator last,
287  const typename size_type::size_type columnCount,
288  const detail::Distribution<Matrix<T>>& distribution =
289  detail::Distribution<Matrix<T>>());
290 
294  template <class InputIterator>
295  Matrix(InputIterator first, InputIterator last, const size_type size,
296  const detail::Distribution<Matrix<T>>& distribution =
297  detail::Distribution<Matrix<T>>());
298 
302  Matrix(Matrix<T>&& rhs);
303 
307  Matrix<T>& operator=(Matrix<T>&& rhs);
308 
312  ~Matrix();
313 
314  // matrix interface
315 
316  iterator begin();
317  const_iterator begin() const;
318 
319  iterator end();
320  const_iterator end() const;
321 
322  iterator row_begin(typename coordinate::index_type rowIndex);
323  const_iterator row_begin(typename coordinate::index_type rowIndex) const;
324 
325  iterator row_end(typename coordinate::index_type rowIndex);
326  const_iterator row_end(typename coordinate::index_type rowIndex) const;
327 
328  iterator operator[]( typename coordinate::index_type rowIndex );
329 
330  const_iterator operator[]( typename coordinate::index_type rowIndex ) const;
331 
332  size_type size() const;
333  typename size_type::size_type rowCount() const;
334  typename size_type::size_type columnCount() const;
335 
336 // detail::Sizes sizes() const;
337 
338  size_type::size_type max_size() const;
339  void resize(const size_type& size, T c = T());
340  size_type::size_type capacity() const;
341  bool empty() const;
342  void reserve(size_type::size_type bytes);
343 
344  // Element access
345  reference operator()( coordinate c );
346  const_reference operator()( coordinate c ) const;
347 
348  reference at( coordinate c);
349  const_reference at( coordinate c) const;
350 
351  reference front();
352  const_reference front() const;
353 
354  reference back();
355  const_reference back() const;
356 
357  // Modifiers
358  template <class InputIterator>
359  void assign(InputIterator first, InputIterator last);
360 
361  void assign(size_type size, const T& v);
362 
363  template <class InputIterator>
364  void push_back_row(InputIterator first, InputIterator last);
365 
366  void push_back_row(std::initializer_list<T> list);
367  void pop_back_row();
368 
369  template <class InputIterator>
370  void insert_row(typename coordinate::index_type rowIndex,
371  InputIterator first, InputIterator last);
372  void insert_row(typename coordinate::index_type rowIndex,
373  std::initializer_list<T> list);
374 
375  iterator erase_row(typename coordinate::index_type rowIndex);
376 
377  void clear();
378 
379  // Some functions from parent Container
380  detail::Distribution<Matrix<T>>& distribution() const;
381 
382  template <typename U>
383  void setDistribution(const detail::Distribution<Matrix<U>>&
384  distribution) const;
385 
386  template <typename U>
387  void setDistribution(const std::unique_ptr<detail::Distribution<Matrix<U>>>&
388  newDistribution) const;
389 
390  void setDistribution(std::unique_ptr<detail::Distribution<Matrix<T>>>&&
391  newDistribution) const;
392 
393  void createDeviceBuffers() const;
394 
395  void forceCreateDeviceBuffers() const;
396 
397  detail::Event startUpload() const;
398 
399  void copyDataToDevices() const;
400 
401  detail::Event startDownload() const;
402 
403  void copyDataToHost() const;
404 
405  void dataOnDeviceModified() const;
406 
407  void dataOnHostModified() const;
408 
409  const detail::DeviceBuffer& deviceBuffer(const detail::Device& device)const;
410 
411  host_buffer_type& hostBuffer() const;
412 
413  static std::string deviceFunctions();
414 
415 private:
419  Matrix(const Matrix<T>&);// = delete;
420 
424  Matrix<T>& operator=(const Matrix<T>&);// = delete;
425 
426  std::string getInfo() const;
427  std::string getDebugInfo() const;
428 
429 
430  static RegisterMatrixDeviceFunctions<T> registerMatrixDeviceFunctions;
431 
432  MatrixSize _size;
433  mutable
434  std::unique_ptr<detail::Distribution<Matrix<T>>> _distribution;
435  mutable bool _hostBufferUpToDate;
436  mutable bool _deviceBuffersUpToDate;
437  mutable host_buffer_type _hostBuffer;
438  // _deviceBuffers empty => buffers not created
439  mutable std::map< detail::Device::id_type,
440  detail::DeviceBuffer > _deviceBuffers;
441 };
442 
443 template <typename T>
444 RegisterMatrixDeviceFunctions<T> Matrix<T>::registerMatrixDeviceFunctions;
445 
446 } // namespace skelcl
447 
448 #include "detail/MatrixDef.h"
449 
450 #endif // MATRIX_H_
host_buffer_type::allocator_type allocator_type
The type of the allocator used on the host.
Definition: Matrix.h:224
The Matrix class is a two dimensional container which makes its data accessible on the host as well a...
Definition: Matrix.h:190
host_buffer_type::difference_type difference_type
The integral type used to define differences of two size_type values.
Definition: Matrix.h:221
std::vector< T > host_buffer_type
The type used to store the elements on the host.
Definition: Matrix.h:194
host_buffer_type::reference reference
The type of a reference to an element.
Definition: Matrix.h:206
skelcl::MatrixSize size_type
The integral type used to define the number of the rows and columns in the Matrix.
Definition: Matrix.h:218
This struct represents two dimensional coordinates.
Definition: Matrix.h:229
SKELCL_DLL detail::DeviceID device(size_t dID)
Creates an OpenCL device ID to be used as parameter of the init(detail::PlatformID, detail::DeviceID) function.
Definition: SkelCL.cpp:76
host_buffer_type::const_iterator const_iterator
The type of an const iterator.
Definition: Matrix.h:215
Matrix()
Creates a new empty Matrix.
This class defines a two dimensional size for a Matrix.
Definition: Matrix.h:67
index_type rowIndex
The index for the row.
Definition: Matrix.h:236
host_buffer_type::iterator iterator
The type of an iterator.
Definition: Matrix.h:212
size_type::size_type index_type
The type used to define the numbers in a row or in a column of the Matrix.
Definition: Matrix.h:232
Matrix< T > & operator=(Matrix< T > &&rhs)
Move assignment operator.
host_buffer_type::pointer pointer
The type of a pointer to an element.
Definition: Matrix.h:200
host_buffer_type::value_type value_type
The type of the elements.
Definition: Matrix.h:197
index_type columnIndex
The index for the column.
Definition: Matrix.h:239
host_buffer_type::const_reference const_reference
The type of a const reference to an element.
Definition: Matrix.h:209
~Matrix()
Destructor.
host_buffer_type::const_pointer const_pointer
The type of a const pointer to an element.
Definition: Matrix.h:203
static Matrix< T > from2DVector(const std::vector< std::vector< T >> &matrix, const detail::Distribution< Matrix< T >> &distribution=detail::Distribution< Matrix< T >>())
static function creating a matrix from 2 dim std::vector as parameter