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
Vector.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 
39 
40 #ifndef VECTOR_H_
41 #define VECTOR_H_
42 
43 #include <map>
44 #include <memory>
45 #include <string>
46 #include <vector>
47 
48 #define __CL_ENABLE_EXCEPTIONS
49 #include <CL/cl.hpp>
50 #undef __CL_ENABLE_EXCEPTIONS
51 
52 #include "detail/CopyDistribution.h"
53 #include "detail/Device.h"
54 #include "detail/DeviceBuffer.h"
55 #include "detail/Distribution.h"
56 
57 namespace skelcl {
58 
61 namespace detail {
62 
63  class Event;
64 
65  class Sizes {
66  public:
67  Sizes() : _sizes() {}
68  void push_back(detail::DeviceBuffer::size_type size) {
69  _sizes.push_back(size);
70  }
71 
72  cl_uint operator[](size_t index) const {
73  return static_cast<cl_uint>(_sizes[index]);
74  }
75  private:
76  std::vector<detail::DeviceBuffer::size_type> _sizes;
77  };
78 
79 } // namespace detail
81 
82 
86 template <typename T>
87 class RegisterVectorDeviceFunctions {
88 public:
89  RegisterVectorDeviceFunctions();
90 };
92 
97 
112 template <typename T>
113 class Vector {
114 public:
117  typedef std::vector<T> host_buffer_type;
120  typedef typename host_buffer_type::value_type value_type;
123  typedef typename host_buffer_type::pointer pointer;
126  typedef typename host_buffer_type::const_pointer const_pointer;
129  typedef typename host_buffer_type::reference reference;
132  typedef typename host_buffer_type::const_reference const_reference;
135  typedef typename host_buffer_type::iterator iterator;
138  typedef typename host_buffer_type::const_iterator const_iterator;
141  typedef typename host_buffer_type::reverse_iterator reverse_iterator;
144  typedef typename host_buffer_type::const_reverse_iterator
148  typedef typename host_buffer_type::size_type size_type;
151  typedef typename host_buffer_type::difference_type difference_type;
154  typedef typename host_buffer_type::allocator_type allocator_type;
155 
158  Vector();
159 
169  Vector(const size_type size, const value_type& value = value_type(),
170  const detail::Distribution<Vector<T>>& distribution =
171  detail::Distribution<Vector<T>>());
172 
180  template <class InputIterator>
181  Vector(InputIterator first, InputIterator last);
182 
191  template <class InputIterator>
192  Vector(InputIterator first, InputIterator last,
193  const detail::Distribution<Vector<T>>& distribution);
194 
201  Vector(const Vector<T>& rhs);
202 
209  Vector(Vector<T>&& rhs);
210 
218  Vector<T>& operator=(const Vector<T>& rhs);
219 
227  Vector<T>& operator=(Vector<T>&& rhs);
228 
232  ~Vector();
233 
245  iterator begin();
246 
258  const_iterator begin() const;
259 
270  iterator end();
271 
282  const_iterator end() const;
283 
284 #if 0
285  reverse_iterator rbegin();
286 
287  const_reverse_iterator rbegin() const;
288 
289  reverse_iterator rend();
290 
291  const_reverse_iterator rend() const;
292 #endif
293 
299  size_type size() const;
300 
307  detail::Sizes sizes() const;
308 
313  size_type max_size() const;
314 
323  void resize( size_type count, T value = T() );
324 
330  size_type capacity() const;
331 
337  bool empty() const;
338 
349  void reserve( size_type n );
350 
362 
373  const_reference operator[]( size_type pos ) const;
374 
388  reference at( size_type pos );
389 
403  const_reference at( size_type pos ) const;
404 
414  reference front();
415 
425  const_reference front() const;
426 
436  reference back();
437 
447  const_reference back() const;
448 
455  template <class InputIterator>
456  void assign(InputIterator first, InputIterator last);
457 
464  void assign(size_type count, const T& value);
465 
470  void push_back(const T& value);
471 
477  void pop_back();
478 
487  iterator insert(iterator pos, const T& value);
488 
499  iterator insert( iterator pos, size_type count, const T& value );
500 
510  template <class InputIterator>
511  void insert(iterator pos, InputIterator first, InputIterator last);
512 
518  iterator erase(iterator pos);
519 
527  iterator erase(iterator first, iterator last);
528 
535  void swap( Vector<T>& rhs );
536 
540  void clear();
541 
546  detail::Distribution<Vector<T>>& distribution() const;
547 
557  template <typename U>
558  void setDistribution(const detail::Distribution<Vector<U>>& distribution)
559  const;
560 
570  template <typename U>
571  void setDistribution(const std::unique_ptr<detail::Distribution<Vector<U>>>&
572  distribution) const;
573 
583  void setDistribution(
584  std::unique_ptr<detail::Distribution<Vector<T>>>&& distribution) const;
585 
593  void createDeviceBuffers() const;
594 
598  //
603  void forceCreateDeviceBuffers() const;
604 
618  detail::Event startUpload() const;
619 
628  void copyDataToDevices() const;
629 
643  detail::Event startDownload() const;
644 
652  void copyDataToHost() const;
653 
657  void dataOnDeviceModified() const;
658 
662  void dataOnHostModified() const;
663 
671  bool hostIsUpToDate() const;
672 
680  bool devicesAreUpToDate() const;
681 
693  const detail::DeviceBuffer& deviceBuffer(const detail::Device& device) const;
694 
702  detail::DeviceBuffer& deviceBuffer(const detail::Device& device);
703 
710  host_buffer_type& hostBuffer() const;
711 
717  static std::string deviceFunctions();
718 
719 private:
720  std::string getInfo() const;
721 
722  std::string getDebugInfo() const;
723 
724  static RegisterVectorDeviceFunctions<T> registerVectorDeviceFunctions;
725 
726  size_type _size;
727  mutable
728  std::unique_ptr<detail::Distribution<Vector<T>>> _distribution;
729  mutable bool _hostBufferUpToDate;
730  mutable bool _deviceBuffersUpToDate;
731  mutable host_buffer_type _hostBuffer;
732  // _deviceBuffers empty => buffers not created yet
733  mutable std::map< detail::Device::id_type,
734  detail::DeviceBuffer > _deviceBuffers;
735 };
736 
737 template <typename T>
738 RegisterVectorDeviceFunctions<T> Vector<T>::registerVectorDeviceFunctions;
739 
740 } // namespace skelcl
741 
742 #include "detail/VectorDef.h"
743 
744 #endif // VECTOR_H_
host_buffer_type::iterator iterator
The type of an iterator.
Definition: Vector.h:135
reference at(size_type pos)
Returns a reference to the element at the specified location pos. Boundary checks are performed...
detail::Event startUpload() const
Starts copying data from the host to the devices involved in the current distribution.
void createDeviceBuffers() const
Create buffers on the devices involved in the current distribution.
void pop_back()
Removes the last element of the Vector.
detail::Event startDownload() const
Starts copying data from the devices involved in the current distribution to the host.
~Vector()
Destructs the Vector.
size_type size() const
Returns the number of elements in the Vector.
host_buffer_type::const_iterator const_iterator
The type of an const iterator.
Definition: Vector.h:138
void setDistribution(const detail::Distribution< Vector< U >> &distribution) const
Set a new distribution to the vector.
iterator begin()
Returns an iterator to the first element of the Vector.
void copyDataToHost() const
Copies data from the devices involved in the current distribution to the host.
std::vector< T > host_buffer_type
The type used to store the elements on the host.
Definition: Vector.h:117
void clear()
Removes all elements from the Vector.
void assign(InputIterator first, InputIterator last)
Replaces the contents of the Vector with copies of the elements in the range [first, last).
void resize(size_type count, T value=T())
Resizes the container to contain count elements.
const detail::DeviceBuffer & deviceBuffer(const detail::Device &device) const
Returns the buffer for the given device used to store elements of the vector accordingly to the curre...
host_buffer_type::reference reference
The type of a reference to an element.
Definition: Vector.h:129
host_buffer_type::size_type size_type
The integral type used to define the number of the elements in the Vector.
Definition: Vector.h:148
static std::string deviceFunctions()
Returns the source code of helper functions simplifying access to the vector on the device...
void dataOnDeviceModified() const
Marks the data on the device as been modified.
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::difference_type difference_type
The integral type used to define differences of two size_type values.
Definition: Vector.h:151
iterator end()
Returns an iterator to the element following the last element of the Vector.
void swap(Vector< T > &rhs)
Exchanges the contents of the Vectors with those of rhs.
reference operator[](size_type pos)
Returns a reference to the element at the specified location pos. No boundary checks are performed...
detail::Distribution< Vector< T > > & distribution() const
Returns the current distribution of the vector.
The Vector class is a one dimensional container which makes its data accessible on the host as well a...
Definition: Vector.h:113
host_buffer_type::reverse_iterator reverse_iterator
The type of a reverse iterator.
Definition: Vector.h:141
iterator insert(iterator pos, const T &value)
Inserts copy of value at the specified location in the Vector.
iterator erase(iterator pos)
Remove the specified element from the Vector.
bool hostIsUpToDate() const
Returns if the elements stored on the host are up to date, or if the elements are outdated because th...
host_buffer_type::allocator_type allocator_type
The type of the allocator used on the host.
Definition: Vector.h:154
Vector< T > & operator=(const Vector< T > &rhs)
Copy assignment operator. Replaces the content with a copy of the content of rhs. ...
void push_back(const T &value)
Appends a copy of the given element value to the end of the Vector.
host_buffer_type::const_reference const_reference
The type of a const reference to an element.
Definition: Vector.h:132
void copyDataToDevices() const
Copies data from the host to the devices involved in the current distribution.
host_buffer_type::pointer pointer
The type of a pointer to an element.
Definition: Vector.h:123
host_buffer_type::const_reverse_iterator const_reverse_iterator
The type of a const reverse iterator.
Definition: Vector.h:145
host_buffer_type::const_pointer const_pointer
The type of a const pointer to an element.
Definition: Vector.h:126
size_type capacity() const
Returns the number of elements that the Vector has currently allocated space for. ...
host_buffer_type::value_type value_type
The type of the elements.
Definition: Vector.h:120
detail::Sizes sizes() const
Returns the number of elements stored on each device.
void dataOnHostModified() const
Marks the data on the host as been modified.
reference front()
Returns a reference to the first element in the Vector. Calling front() on an empty Vector is undefin...
size_type max_size() const
Return the maximum number of elements the Vector is able to hold.
Vector()
Creates a new empty Vector.
bool empty() const
Checks if the Vector has no elements, i.e. whether begin() == end()
void reserve(size_type n)
Increases the capacity of the Vector to a value that's greater or equal to n.
void forceCreateDeviceBuffers() const
Forces the creation of buffers on the devices involved in the current distribution, even if this means replacing existing buffers.
bool devicesAreUpToDate() const
Returns if the elements stored on the devices are up to date, or if the elements are outdated because...
host_buffer_type & hostBuffer() const
Returns a reference to the underlying object storing the elements on the host.
reference back()
Returns a reference to the last element in the Vector. Calling back() on an empty Vector is undefined...