ViennaCL - The Vienna Computing Library  1.5.2
vector_proxy.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_VECTOR_PROXY_HPP_
2 #define VIENNACL_VECTOR_PROXY_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include "viennacl/forwards.h"
26 #include "viennacl/range.hpp"
27 #include "viennacl/slice.hpp"
28 #include "viennacl/vector.hpp"
30 
31 namespace viennacl
32 {
37  template <typename VectorType>
38  class vector_range : public vector_base<typename VectorType::cpu_value_type>
39  {
40  typedef vector_range<VectorType> self_type;
41  typedef vector_base<typename VectorType::cpu_value_type> base_type;
42 
43  public:
44  typedef typename VectorType::value_type value_type;
47  typedef value_type reference;
48  typedef const value_type & const_reference;
49  typedef typename VectorType::const_iterator const_iterator;
50  typedef typename VectorType::iterator iterator;
51 
52  typedef typename VectorType::cpu_value_type cpu_value_type;
53 
54  static const int alignment = VectorType::alignment;
55 
56  vector_range(VectorType & v, range const & entry_range)
57  : base_type(v.handle(), entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
58 
59 
60  using base_type::operator=;
61 
62  };
63 
64 
65 
69 
70  template <typename VectorType, typename SCALARTYPE>
71  void copy(const VectorType & cpu_vector,
72  vector_range<vector<SCALARTYPE> > & gpu_vector_range )
73  {
74  assert(cpu_vector.end() - cpu_vector.begin() >= 0 && bool("Range must have nonnegative length!"));
75 
76  if (cpu_vector.end() - cpu_vector.begin() > 0)
77  {
78  //we require that the size of the gpu_vector is larger or equal to the cpu-size
79  std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
80  std::copy(cpu_vector.begin(), cpu_vector.end(), temp_buffer.begin());
81  viennacl::backend::memory_write(gpu_vector_range.handle(), sizeof(SCALARTYPE)*gpu_vector_range.start(), sizeof(SCALARTYPE)*temp_buffer.size(), &(temp_buffer[0]));
82  }
83  }
84 
85 
91  template <typename CPUVECTOR, typename VectorType>
92  void fast_copy(const CPUVECTOR & cpu_vec, vector_range<VectorType> & gpu_vec)
93  {
94  viennacl::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
95  }
96 
100 
101 
102  template <typename SCALARTYPE, typename VectorType>
103  void copy(vector_range<vector<SCALARTYPE> > const & gpu_vector_range,
104  VectorType & cpu_vector)
105  {
106  assert(cpu_vector.end() - cpu_vector.begin() >= 0 && bool("Range must have nonnegative length!"));
107 
108  if (cpu_vector.end() > cpu_vector.begin())
109  {
110  std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
111  viennacl::backend::memory_read(gpu_vector_range.handle(), sizeof(SCALARTYPE)*gpu_vector_range.start(), sizeof(SCALARTYPE)*temp_buffer.size(), &(temp_buffer[0]));
112 
113  //now copy entries to cpu_vec:
114  std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_vector.begin());
115  }
116  }
117 
118 
124  template <typename VectorType, typename CPUVECTOR>
125  void fast_copy(vector_range< VectorType > const & gpu_vec,
126  CPUVECTOR & cpu_vec )
127  {
128  viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
129  }
130 
131 
132 
133  //
134  // Convenience function
135  //
136  template <typename VectorType>
137  vector_range<VectorType> project(VectorType & vec, viennacl::range const & r1)
138  {
139  return vector_range<VectorType>(vec, r1);
140  }
141 
142  template <typename VectorType>
144  {
145  assert(r1.size() <= vec.size() && bool("Size of range invalid!"));
146  return vector_range<VectorType>(vec, viennacl::range(vec.start() + r1.start(), vec.start() + r1.start() + r1.size()));
147  }
148 
149 //
150 //
151 //
153 //
154 //
155 //
156 
157 
158 
163  template <typename VectorType>
164  class vector_slice : public vector_base<typename VectorType::cpu_value_type>
165  {
166  typedef vector_slice<VectorType> self_type;
167  typedef vector_base<typename VectorType::cpu_value_type> base_type;
168 
169  public:
170  typedef typename VectorType::value_type value_type;
173  typedef value_type reference;
174  typedef const value_type & const_reference;
175  typedef typename VectorType::const_iterator const_iterator;
176  typedef typename VectorType::iterator iterator;
177 
178  typedef typename VectorType::cpu_value_type cpu_value_type;
179 
180  static const int alignment = VectorType::alignment;
181 
182  vector_slice(VectorType & v, slice const & entry_slice)
183  : base_type(v.handle(), entry_slice.size(), v.start() + v.stride() * entry_slice.start(), v.stride() * entry_slice.stride()) {}
184 
185 
186  using base_type::operator=;
187 
188  };
189 
190 
194 
195  template <typename VectorType, typename SCALARTYPE>
196  void copy(const VectorType & cpu_vector,
197  vector_slice<vector<SCALARTYPE> > & gpu_vector_slice )
198  {
199  if (cpu_vector.size() > 0)
200  {
201  std::vector<SCALARTYPE> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
202 
203  viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(SCALARTYPE)*gpu_vector_slice.start(), sizeof(SCALARTYPE)*temp_buffer.size(), &(temp_buffer[0]));
204 
205  for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
206  temp_buffer[i * gpu_vector_slice.stride()] = cpu_vector[i];
207 
208  viennacl::backend::memory_write(gpu_vector_slice.handle(), sizeof(SCALARTYPE)*gpu_vector_slice.start(), sizeof(SCALARTYPE)*temp_buffer.size(), &(temp_buffer[0]));
209  }
210  }
211 
212 
213 
217 
218 
219  template <typename VectorType, typename SCALARTYPE>
220  void copy(vector_slice<vector<SCALARTYPE> > const & gpu_vector_slice,
221  VectorType & cpu_vector)
222  {
223  assert(gpu_vector_slice.end() - gpu_vector_slice.begin() >= 0 && bool("Range must have nonnegative length!"));
224 
225  if (gpu_vector_slice.end() - gpu_vector_slice.begin() > 0)
226  {
227  std::vector<SCALARTYPE> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
228  viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(SCALARTYPE)*gpu_vector_slice.start(), sizeof(SCALARTYPE)*temp_buffer.size(), &(temp_buffer[0]));
229 
230  for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
231  cpu_vector[i] = temp_buffer[i * gpu_vector_slice.stride()];
232  }
233  }
234 
235 
236 
237 
238 
239  //
240  // Convenience functions
241  //
242  template <typename VectorType>
243  vector_slice<VectorType> project(VectorType & vec, viennacl::slice const & s1)
244  {
245  assert(s1.size() <= vec.size() && bool("Size of slice larger than vector size!"));
246  return vector_slice<VectorType>(vec, s1);
247  }
248 
249  template <typename VectorType>
251  {
252  assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
253  return vector_slice<VectorType>(vec, viennacl::slice(vec.start() + s1.start(), vec.stride() * s1.stride(), s1.size()));
254  }
255 
256  // interaction with range and vector_range:
257 
258  template <typename VectorType>
260  {
261  assert(r1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
262  return vector_slice<VectorType>(vec, viennacl::slice(vec.start() + r1.start(), vec.stride(), r1.size()));
263  }
264 
265  template <typename VectorType>
267  {
268  assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
269  return vector_slice<VectorType>(vec, viennacl::range(vec.start() + s1.start(), s1.stride(), s1.size()));
270  }
271 
272 
273 }
274 
275 #endif
VectorType::cpu_value_type cpu_value_type
Definition: vector_proxy.hpp:178
std::size_t vcl_size_t
Definition: forwards.h:58
Class for representing non-strided subvectors of a bigger vector x.
Definition: forwards.h:349
value_type reference
Definition: vector_proxy.hpp:173
void memory_write(mem_handle &dst_buffer, vcl_size_t dst_offset, vcl_size_t bytes_to_write, const void *ptr, bool async=false)
Writes data from main RAM identified by 'ptr' to the buffer identified by 'dst_buffer'.
Definition: memory.hpp:220
VectorType::cpu_value_type cpu_value_type
Definition: vector_proxy.hpp:52
basic_slice slice
Definition: forwards.h:344
slice::difference_type difference_type
Definition: vector_proxy.hpp:172
difference_type stride() const
Definition: slice.hpp:55
matrix_range< MatrixType > project(MatrixType &A, viennacl::range const &r1, viennacl::range const &r2)
Definition: matrix_proxy.hpp:251
A proxy class for entries in a vector.
A range class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:339
VectorType::const_iterator const_iterator
Definition: vector_proxy.hpp:49
size_type start() const
Definition: range.hpp:55
range::size_type size_type
Definition: vector_proxy.hpp:45
This file provides the forward declarations for the main types used within ViennaCL.
void memory_read(mem_handle const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_read, void *ptr, bool async=false)
Reads data from a buffer back to main RAM.
Definition: memory.hpp:261
slice::size_type size_type
Definition: vector_proxy.hpp:171
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
range::difference_type difference_type
Definition: vector_proxy.hpp:46
SizeType size_type
Definition: slice.hpp:42
static const int alignment
Definition: vector_proxy.hpp:180
size_type size() const
Definition: slice.hpp:56
size_type size() const
Definition: range.hpp:56
VectorType::value_type value_type
Definition: vector_proxy.hpp:170
void copy(std::vector< SCALARTYPE > &cpu_vec, circulant_matrix< SCALARTYPE, ALIGNMENT > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector.hpp:837
SizeType size_type
Definition: range.hpp:42
DistanceType difference_type
Definition: slice.hpp:43
static const int alignment
Definition: vector_proxy.hpp:54
size_type start() const
Returns the offset within the buffer.
Definition: vector.hpp:845
value_type reference
Definition: vector_proxy.hpp:47
void copy(vector_slice< vector< SCALARTYPE > > const &gpu_vector_slice, VectorType &cpu_vector)
Definition: vector_proxy.hpp:220
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
VectorType::iterator iterator
Definition: vector_proxy.hpp:176
VectorType::iterator iterator
Definition: vector_proxy.hpp:50
vector_slice(VectorType &v, slice const &entry_slice)
Definition: vector_proxy.hpp:182
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
const value_type & const_reference
Definition: vector_proxy.hpp:48
Implementation of a slice object for use with proxy objects.
size_type start() const
Definition: slice.hpp:54
VectorType::const_iterator const_iterator
Definition: vector_proxy.hpp:175
const handle_type & handle() const
Returns the memory handle.
Definition: vector.hpp:856
DistanceType difference_type
Definition: range.hpp:43
vector_range(VectorType &v, range const &entry_range)
Definition: vector_proxy.hpp:56
Class for representing strided subvectors of a bigger vector x.
Definition: forwards.h:352
Implementation of a range object for use with proxy objects.
basic_range range
Definition: forwards.h:339
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)
STL-like transfer of a GPU vector to the CPU. The cpu type is assumed to reside in a linear piece of ...
Definition: vector.hpp:1262
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:344
const value_type & const_reference
Definition: vector_proxy.hpp:174
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(SCALARTYPE))
Definition: vector.hpp:849
VectorType::value_type value_type
Definition: vector_proxy.hpp:44