ViennaCL - The Vienna Computing Library: viennacl/vector_proxy.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2016, 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 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"
29 #include "viennacl/tools/entry_proxy.hpp"
30
31 namespace viennacl
32 {
33
38 template<typename VectorType>
39 class vector_range : public vector_base<typename VectorType::cpu_value_type>
40 {
41 typedef vector_range<VectorType> self_type;
42 typedef vector_base<typename VectorType::cpu_value_type> base_type;
43
44 public:
45 typedef typename VectorType::value_type value_type;
46 typedef range::size_type size_type;
47 typedef range::difference_type difference_type;
48 typedef value_type reference;
49 typedef const value_type & const_reference;
50 typedef typename VectorType::const_iterator const_iterator;
51 typedef typename VectorType::iterator iterator;
52 typedef typename VectorType::handle_type handle_type;
53
54 typedef typename VectorType::cpu_value_type cpu_value_type;
55
56 vector_range(VectorType const & v, range const & entry_range)
57 : base_type(const_cast<handle_type &>(v.handle()),
58 entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
59
60 vector_range(self_type const & v, range const & entry_range)
61 : base_type(const_cast<handle_type &>(v.handle()),
62 entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
63
64 vector_range(self_type const & other)
65 : base_type(const_cast<handle_type &>(other.handle()),
66 other.size(), other.start(), other.stride()) {}
67
68 using base_type::operator=;
69
70 // the following are needed for Visual Studio:
71 template<typename OtherNumericT>
72 base_type & operator=(viennacl::vector<OtherNumericT> const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
73
74 template<typename OtherNumericT>
75 base_type & operator=(viennacl::vector_range<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
76
77 template<typename OtherNumericT>
78 base_type & operator=(viennacl::vector_slice<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
79 };
80
81 template<typename VectorType>
82 class vector_range<vector_range<VectorType> > : public vector_base<typename VectorType::cpu_value_type>
83 {
84 typedef vector_base<typename VectorType::cpu_value_type> base_type;
85
86 public:
87 typedef typename VectorType::handle_type handle_type;
88
89 vector_range(VectorType const & v, range const & entry_range)
90 : base_type(const_cast<handle_type &>(v.handle()),
91 entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
92
93 vector_range(vector_range<VectorType> const & v, range const & entry_range)
94 : base_type(const_cast<handle_type &>(v.handle()),
95 entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
96 };
97
101
102 template<typename VectorType, typename NumericT>
103 void copy(const VectorType & cpu_vector,
104 vector_range<vector<NumericT> > & gpu_vector_range )
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() > 0)
109 {
110 //we require that the size of the gpu_vector is larger or equal to the cpu-size
111 std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(cpu_vector.end() - cpu_vector.begin()));
112 std::copy(cpu_vector.begin(), cpu_vector.end(), temp_buffer.begin());
113 viennacl::backend::memory_write(gpu_vector_range.handle(), sizeof(NumericT)*gpu_vector_range.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
114 }
115 }
116
117
123 template<typename CPUVECTOR, typename VectorType>
124 void fast_copy(const CPUVECTOR & cpu_vec, vector_range<VectorType> & gpu_vec)
125 {
126 viennacl::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
127 }
128
132
133
134 template<typename NumericT, typename VectorType>
135 void copy(vector_range<vector<NumericT> > const & gpu_vector_range,
136 VectorType & cpu_vector)
137 {
138 assert(cpu_vector.end() - cpu_vector.begin() >= 0 && bool("Range must have nonnegative length!"));
139
140 if (cpu_vector.end() > cpu_vector.begin())
141 {
142 std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(cpu_vector.end() - cpu_vector.begin()));
143 viennacl::backend::memory_read(gpu_vector_range.handle(), sizeof(NumericT)*gpu_vector_range.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
144
145 //now copy entries to cpu_vec:
146 std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_vector.begin());
147 }
148 }
149
150
156 template<typename VectorType, typename CPUVECTOR>
157 void fast_copy(vector_range< VectorType > const & gpu_vec,
158 CPUVECTOR & cpu_vec )
159 {
160 viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
161 }
162
163
164
165 //
166 // Convenience function
167 //
168 template<typename VectorType>
169 vector_range<VectorType> project(VectorType const & vec, viennacl::range const & r1)
170 {
171 return vector_range<VectorType>(vec, r1);
172 }
173
174 template<typename VectorType>
175 vector_range<VectorType> project(viennacl::vector_range<VectorType> const & vec, viennacl::range const & r1)
176 {
177 assert(r1.size() <= vec.size() && bool("Size of range invalid!"));
178 return vector_range<VectorType>(vec, r1);
179 }
180
181 //
182 //
183 //
185 //
186 //
187 //
188
189
190
195 template<typename VectorType>
196 class vector_slice : public vector_base<typename VectorType::cpu_value_type>
197 {
198 typedef vector_slice<VectorType> self_type;
199 typedef vector_base<typename VectorType::cpu_value_type> base_type;
200
201 public:
202 typedef typename VectorType::value_type value_type;
203 typedef slice::size_type size_type;
204 typedef slice::difference_type difference_type;
205 typedef value_type reference;
206 typedef const value_type & const_reference;
207 typedef typename VectorType::const_iterator const_iterator;
208 typedef typename VectorType::iterator iterator;
209 typedef typename VectorType::handle_type handle_type;
210
211 typedef typename VectorType::cpu_value_type cpu_value_type;
212
213 vector_slice(VectorType const & v, slice const & entry_slice)
214 : base_type(const_cast<handle_type &>(v.handle()),
215 entry_slice.size(), v.start() + v.stride() * entry_slice.start(), v.stride() * entry_slice.stride()) {}
216
217 vector_slice(self_type const & v, slice const & entry_slice)
218 : base_type(const_cast<handle_type &>(v.handle()),
219 entry_slice.size(), v.start() + v.stride() * entry_slice.start(), v.stride() * entry_slice.stride()) {}
220
221 vector_slice(self_type const & other)
222 : base_type(const_cast<handle_type &>(other.handle()),
223 other.size(), other.start(), other.stride()) {}
224
225 using base_type::operator=;
226
227 // the following are needed for Visual Studio:
228 template<typename OtherNumericT>
229 base_type & operator=(viennacl::vector<OtherNumericT> const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
230
231 template<typename OtherNumericT>
232 base_type & operator=(viennacl::vector_range<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
233
234 template<typename OtherNumericT>
235 base_type & operator=(viennacl::vector_slice<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
236 };
237
238
239 template<typename VectorType>
240 class vector_slice<vector_slice<VectorType> > : public vector_base<typename VectorType::cpu_value_type>
241 {
242 typedef vector_base<typename VectorType::cpu_value_type> base_type;
243
244 public:
245 typedef typename VectorType::handle_type handle_type;
246
247 vector_slice(VectorType const & v, slice const & entry_slice)
248 : base_type(const_cast<handle_type &>(v.handle()),
249 entry_slice.size(), v.start() + v.stride() * entry_slice.start(), entry_slice.stride() * v.stride()) {}
250
251 vector_slice(vector_slice<VectorType> const & v, slice const & entry_slice)
252 : base_type(const_cast<handle_type &>(v.handle()),
253 entry_slice.size(), v.start() + v.stride() * entry_slice.start(), entry_slice.stride() * v.stride()) {}
254 };
255
259
260 template<typename VectorType, typename NumericT>
261 void copy(const VectorType & cpu_vector,
262 vector_slice<vector<NumericT> > & gpu_vector_slice )
263 {
264 if (cpu_vector.size() > 0)
265 {
266 std::vector<NumericT> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
267
268 viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
269
270 for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
271 temp_buffer[i * gpu_vector_slice.stride()] = cpu_vector[i];
272
273 viennacl::backend::memory_write(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
274 }
275 }
276
277
278
282
283
284 template<typename VectorType, typename NumericT>
285 void copy(vector_slice<vector<NumericT> > const & gpu_vector_slice,
286 VectorType & cpu_vector)
287 {
288 assert(gpu_vector_slice.end() - gpu_vector_slice.begin() >= 0 && bool("Range must have nonnegative length!"));
289
290 if (gpu_vector_slice.end() - gpu_vector_slice.begin() > 0)
291 {
292 std::vector<NumericT> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
293 viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
294
295 for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
296 cpu_vector[i] = temp_buffer[i * gpu_vector_slice.stride()];
297 }
298 }
299
300
301
302
303
304 //
305 // Convenience functions
306 //
307 template<typename VectorType>
308 vector_slice<VectorType> project(VectorType const & vec, viennacl::slice const & s1)
309 {
310 assert(s1.size() <= vec.size() && bool("Size of slice larger than vector size!"));
311 return vector_slice<VectorType>(vec, s1);
312 }
313
314 template<typename VectorType>
315 vector_slice<VectorType> project(viennacl::vector_slice<VectorType> const & vec, viennacl::slice const & s1)
316 {
317 assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
318 return vector_slice<VectorType>(vec, s1);
319 }
320
321 // interaction with range and vector_range:
322
323 template<typename VectorType>
324 vector_slice<VectorType> project(viennacl::vector_slice<VectorType> const & vec, viennacl::range const & r1)
325 {
326 assert(r1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
327 return vector_slice<VectorType>(vec, viennacl::slice(r1.start(), 1, r1.size()));
328 }
329
330 template<typename VectorType>
331 vector_slice<VectorType> project(viennacl::vector_range<VectorType> const & vec, viennacl::slice const & s1)
332 {
333 assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
334 return vector_slice<VectorType>(vec, s1);
335 }
336
337
338 }
339
340 #endif
viennacl::backend::cpu_ram::handle_type
viennacl::tools::shared_ptr< char > handle_type
Definition: cpu_ram.hpp:40
viennacl::vector_range::reference
value_type reference
Definition: vector_proxy.hpp:48
viennacl::basic_range::difference_type
DistanceT difference_type
Definition: range.hpp:43
viennacl::backend::memory_write
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
viennacl::vector_slice< vector_slice< VectorType > >::handle_type
VectorType::handle_type handle_type
Definition: vector_proxy.hpp:245
viennacl::vector_slice::handle_type
VectorType::handle_type handle_type
Definition: vector_proxy.hpp:209
viennacl::basic_range::start
size_type start() const
Definition: range.hpp:55
viennacl::basic_slice::size
size_type size() const
Definition: slice.hpp:56
viennacl::vector_range< vector_range< VectorType > >::handle_type
VectorType::handle_type handle_type
Definition: vector_proxy.hpp:87
viennacl::vector_slice::difference_type
slice::difference_type difference_type
Definition: vector_proxy.hpp:204
entry_proxy.hpp
A proxy class for entries in a vector.
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::vector_slice< vector_slice< VectorType > >::vector_slice
vector_slice(VectorType const &v, slice const &entry_slice)
Definition: vector_proxy.hpp:247
viennacl::backend::memory_read
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
viennacl::vector_slice::reference
value_type reference
Definition: vector_proxy.hpp:205
viennacl::vector_slice::operator=
base_type & operator=(viennacl::vector_slice< viennacl::vector< OtherNumericT > > const &v)
Definition: vector_proxy.hpp:235
viennacl::vector_range::value_type
VectorType::value_type value_type
Definition: vector_proxy.hpp:45
viennacl::basic_slice::size_type
SizeT size_type
Definition: slice.hpp:42
s1
viennacl::scalar< float > s1
Definition: global_variables.cpp:57
viennacl::vector_slice::const_iterator
VectorType::const_iterator const_iterator
Definition: vector_proxy.hpp:207
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::vector_base< VectorType::cpu_value_type >::stride
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(NumericT))
Definition: vector_def.hpp:124
viennacl::vector_slice::operator=
base_type & operator=(viennacl::vector< OtherNumericT > const &v)
Definition: vector_proxy.hpp:229
viennacl::vector_range< vector_range< VectorType > >::vector_range
vector_range(vector_range< VectorType > const &v, range const &entry_range)
Definition: vector_proxy.hpp:93
viennacl::vector_slice::value_type
VectorType::value_type value_type
Definition: vector_proxy.hpp:202
viennacl::vector_range
Class for representing non-strided subvectors of a bigger vector x.
Definition: forwards.h:434
viennacl::vector_range< vector_range< VectorType > >::vector_range
vector_range(VectorType const &v, range const &entry_range)
Definition: vector_proxy.hpp:89
viennacl::vector_range::const_reference
const value_type & const_reference
Definition: vector_proxy.hpp:49
viennacl::vector_slice
Class for representing strided subvectors of a bigger vector x.
Definition: forwards.h:437
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::project
matrix_range< MatrixType > project(MatrixType const &A, viennacl::range const &r1, viennacl::range const &r2)
Definition: matrix_proxy.hpp:326
viennacl::vector_base::operator=
self_type & operator=(const self_type &vec)
Assignment operator. Other vector needs to be of the same size, or this vector is not yet initialized...
Definition: vector.hpp:356
viennacl::vector_range::operator=
base_type & operator=(viennacl::vector< OtherNumericT > const &v)
Definition: vector_proxy.hpp:72
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::copy
void copy(vector_slice< vector< NumericT > > const &gpu_vector_slice, VectorType &cpu_vector)
Definition: vector_proxy.hpp:285
viennacl::vector_range::iterator
VectorType::iterator iterator
Definition: vector_proxy.hpp:51
viennacl::vector
Definition: forwards.h:266
viennacl::vector_slice::operator=
base_type & operator=(viennacl::vector_range< viennacl::vector< OtherNumericT > > const &v)
Definition: vector_proxy.hpp:232
viennacl::vector_range::const_iterator
VectorType::const_iterator const_iterator
Definition: vector_proxy.hpp:50
viennacl::vector_slice::vector_slice
vector_slice(self_type const &v, slice const &entry_slice)
Definition: vector_proxy.hpp:217
viennacl::vector_slice::iterator
VectorType::iterator iterator
Definition: vector_proxy.hpp:208
viennacl::vector_range::vector_range
vector_range(self_type const &other)
Definition: vector_proxy.hpp:64
viennacl::vector_range::vector_range
vector_range(self_type const &v, range const &entry_range)
Definition: vector_proxy.hpp:60
viennacl::slice
basic_slice slice
Definition: forwards.h:429
viennacl::vector_slice::cpu_value_type
VectorType::cpu_value_type cpu_value_type
Definition: vector_proxy.hpp:211
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &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
slice.hpp
Implementation of a slice object for use with proxy objects.
viennacl::vector_base< VectorType::cpu_value_type >::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
viennacl::vector_range::vector_range
vector_range(VectorType const &v, range const &entry_range)
Definition: vector_proxy.hpp:56
viennacl::basic_range
A range class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:424
viennacl::vector_slice::vector_slice
vector_slice(VectorType const &v, slice const &entry_slice)
Definition: vector_proxy.hpp:213
viennacl::vector_range::size_type
range::size_type size_type
Definition: vector_proxy.hpp:46
viennacl::vector_slice::size_type
slice::size_type size_type
Definition: vector_proxy.hpp:203
range.hpp
Implementation of a range object for use with proxy objects.
viennacl::basic_slice::difference_type
DistanceT difference_type
Definition: slice.hpp:43
viennacl::vector_range::operator=
base_type & operator=(viennacl::vector_range< viennacl::vector< OtherNumericT > > const &v)
Definition: vector_proxy.hpp:75
viennacl::basic_range::size_type
SizeT size_type
Definition: range.hpp:42
viennacl::vector_range::handle_type
VectorType::handle_type handle_type
Definition: vector_proxy.hpp:52
viennacl::vector_base< VectorType::cpu_value_type >::start
size_type start() const
Returns the offset within the buffer.
Definition: vector_def.hpp:122
viennacl::basic_slice
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:429
viennacl::vector_slice::const_reference
const value_type & const_reference
Definition: vector_proxy.hpp:206
viennacl::vector_range::difference_type
range::difference_type difference_type
Definition: vector_proxy.hpp:47
viennacl::vector_base< VectorType::cpu_value_type >::handle
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
viennacl::vector_range::operator=
base_type & operator=(viennacl::vector_slice< viennacl::vector< OtherNumericT > > const &v)
Definition: vector_proxy.hpp:78
viennacl::vector_slice< vector_slice< VectorType > >::vector_slice
vector_slice(vector_slice< VectorType > const &v, slice const &entry_slice)
Definition: vector_proxy.hpp:251
viennacl::vector_range::cpu_value_type
VectorType::cpu_value_type cpu_value_type
Definition: vector_proxy.hpp:54
viennacl::basic_range::size
size_type size() const
Definition: range.hpp:56
viennacl::vector_slice::vector_slice
vector_slice(self_type const &other)
Definition: vector_proxy.hpp:221
viennacl::fast_copy
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)