ViennaCL - The Vienna Computing Library: viennacl/detail/vector_def.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_def.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_DETAIL_VECTOR_DEF_HPP_
2 #define VIENNACL_DETAIL_VECTOR_DEF_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/tools/entry_proxy.hpp"
27
28 namespace viennacl
29 {
30
35 template<typename NumericT>
36 class implicit_vector_base
37 {
38 protected:
39 implicit_vector_base(vcl_size_t s, vcl_size_t i, NumericT v, viennacl::context ctx) : size_(s), index_(std::make_pair(true,i)), value_(v), ctx_(ctx){ }
40 implicit_vector_base(vcl_size_t s, NumericT v, viennacl::context ctx) : size_(s), index_(std::make_pair(false,0)), value_(v), ctx_(ctx){ }
41
42 public:
43 typedef NumericT const & const_reference;
44 typedef NumericT cpu_value_type;
45
46 viennacl::context context() const { return ctx_; }
47 vcl_size_t size() const { return size_; }
48 cpu_value_type value() const { return value_; }
49 vcl_size_t index() const { return index_.second; }
50 bool has_index() const { return index_.first; }
51
52 cpu_value_type operator()(vcl_size_t i) const
53 {
54 if (index_.first)
55 return (i==index_.second)?value_:0;
56 return value_;
57 }
58
59 cpu_value_type operator[](vcl_size_t i) const
60 {
61 if (index_.first)
62 return (i==index_.second)?value_:0;
63 return
64 value_;
65 }
66
67 protected:
68 vcl_size_t size_;
69 std::pair<bool, vcl_size_t> index_;
70 NumericT value_;
71 viennacl::context ctx_;
72 };
73
75 template<typename NumericT>
76 struct unit_vector : public implicit_vector_base<NumericT>
77 {
78 unit_vector(vcl_size_t s, vcl_size_t ind, viennacl::context ctx = viennacl::context()) : implicit_vector_base<NumericT>(s, ind, 1, ctx)
79 {
80 assert( (ind < s) && bool("Provided index out of range!") );
81 }
82 };
83
84
86 template<typename NumericT>
87 struct scalar_vector : public implicit_vector_base<NumericT>
88 {
89 scalar_vector(vcl_size_t s, NumericT val, viennacl::context ctx = viennacl::context()) : implicit_vector_base<NumericT>(s, val, ctx) {}
90 };
91
92 template<typename NumericT>
93 struct zero_vector : public scalar_vector<NumericT>
94 {
95 zero_vector(vcl_size_t s, viennacl::context ctx = viennacl::context()) : scalar_vector<NumericT>(s, 0, ctx){}
96 };
97
98
103 template<class NumericT, typename SizeT /* see forwards.h for default type */, typename DistanceT /* see forwards.h for default type */>
104 class vector_base
105 {
106 typedef vector_base<NumericT, SizeT, DistanceT> self_type;
107
108 public:
109 typedef scalar<NumericT> value_type;
110 typedef NumericT cpu_value_type;
111 typedef viennacl::backend::mem_handle handle_type;
112 typedef SizeT size_type;
113 typedef DistanceT difference_type;
114 typedef const_vector_iterator<NumericT, 1> const_iterator;
115 typedef vector_iterator<NumericT, 1> iterator;
116
118 size_type size() const { return size_; }
120 size_type internal_size() const { return internal_size_; }
122 size_type start() const { return start_; }
124 size_type stride() const { return stride_; }
126 bool empty() const { return size_ == 0; }
128 const handle_type & handle() const { return elements_; }
130 handle_type & handle() { return elements_; }
131 viennacl::memory_types memory_domain() const { return elements_.get_active_handle_id(); }
132
135 explicit vector_base();
136
144 explicit vector_base(viennacl::backend::mem_handle & h, size_type vec_size, size_type vec_start, size_type vec_stride);
145
147 explicit vector_base(size_type vec_size, viennacl::context ctx = viennacl::context());
148
149 // CUDA or host memory:
150 explicit vector_base(NumericT * ptr_to_mem, viennacl::memory_types mem_type, size_type vec_size, vcl_size_t start = 0, size_type stride = 1);
151
152 #ifdef VIENNACL_WITH_OPENCL
153
161 explicit vector_base(cl_mem existing_mem, size_type vec_size, size_type start = 0, size_type stride = 1, viennacl::context ctx = viennacl::context());
162 #endif
163
164 template<typename LHS, typename RHS, typename OP>
165 explicit vector_base(vector_expression<const LHS, const RHS, OP> const & proxy);
166
167 // Copy CTOR:
168 vector_base(const self_type & other);
169
170 // Conversion CTOR:
171 template<typename OtherNumericT>
172 vector_base(const vector_base<OtherNumericT> & v1);
173
176 self_type & operator=(const self_type & vec);
180 template<typename LHS, typename RHS, typename OP>
181 self_type & operator=(const vector_expression<const LHS, const RHS, OP> & proxy);
183 template<typename OtherNumericT>
184 self_type & operator = (const vector_base<OtherNumericT> & v1);
186 self_type & operator = (unit_vector<NumericT> const & v);
188 self_type & operator = (zero_vector<NumericT> const & v);
190 self_type & operator = (scalar_vector<NumericT> const & v);
191
192
194
197 self_type & operator=(const viennacl::vector_expression< const matrix_base<NumericT>, const vector_base<NumericT>, viennacl::op_prod> & proxy);
198
199 //transposed_matrix_proxy:
203 self_type & operator=(const vector_expression< const matrix_expression< const matrix_base<NumericT>, const matrix_base<NumericT>, op_trans >,
204 const vector_base<NumericT>,
205 op_prod> & proxy);
206
208
209
210 //read-write access to an element of the vector
212 entry_proxy<NumericT> operator()(size_type index);
214 entry_proxy<NumericT> operator[](size_type index);
216 const_entry_proxy<NumericT> operator()(size_type index) const;
218 const_entry_proxy<NumericT> operator[](size_type index) const;
219 self_type & operator += (const self_type & vec);
220 self_type & operator -= (const self_type & vec);
221
223 self_type & operator *= (char val);
225 self_type & operator *= (short val);
227 self_type & operator *= (int val);
229 self_type & operator *= (long val);
231 self_type & operator *= (float val);
233 self_type & operator *= (double val);
234
235
237 self_type & operator /= (char val);
239 self_type & operator /= (short val);
241 self_type & operator /= (int val);
243 self_type & operator /= (long val);
245 self_type & operator /= (float val);
247 self_type & operator /= (double val);
248
250 vector_expression< const self_type, const NumericT, op_mult>
251 operator * (char value) const;
253 vector_expression< const self_type, const NumericT, op_mult>
254 operator * (short value) const;
256 vector_expression< const self_type, const NumericT, op_mult>
257 operator * (int value) const;
259 vector_expression< const self_type, const NumericT, op_mult>
260 operator * (long value) const;
262 vector_expression< const self_type, const NumericT, op_mult>
263 operator * (float value) const;
265 vector_expression< const self_type, const NumericT, op_mult>
266 operator * (double value) const;
267
269 vector_expression< const self_type, const NumericT, op_div>
270 operator / (char value) const;
272 vector_expression< const self_type, const NumericT, op_div>
273 operator / (short value) const;
275 vector_expression< const self_type, const NumericT, op_div>
276 operator / (int value) const;
278 vector_expression< const self_type, const NumericT, op_div>
279 operator / (long value) const;
281 vector_expression< const self_type, const NumericT, op_div>
282 operator / (float value) const;
284 vector_expression< const self_type, const NumericT, op_div>
285 operator / (double value) const;
286
288 vector_expression<const self_type, const NumericT, op_mult> operator-() const;
290 iterator begin();
292 iterator end();
294 const_iterator begin() const;
296 const_iterator end() const;
298 self_type & swap(self_type & other);
299
301 void clear();
302
303 protected:
304
305 void set_handle(viennacl::backend::mem_handle const & h) { elements_ = h; }
306
308 self_type & fast_swap(self_type & other);
309
311 void pad();
312
313 void switch_memory_context(viennacl::context new_ctx);
314
315 //TODO: Think about implementing the following public member functions
316 //void insert_element(unsigned int i, NumericT val){}
317 //void erase_element(unsigned int i){}
318
319 //enlarge or reduce allocated memory and set unused memory to zero
325 void resize(size_type new_size, bool preserve = true);
326
333 void resize(size_type new_size, viennacl::context ctx, bool preserve = true);
334 private:
335
336 void resize_impl(size_type new_size, viennacl::context ctx, bool preserve = true);
337
338 size_type size_;
339 size_type start_;
340 size_type stride_;
341 size_type internal_size_;
342 handle_type elements_;
343 }; //vector_base
344
347 } // namespace viennacl
348
349 #endif
viennacl::vector_base::difference_type
DistanceT difference_type
Definition: vector_def.hpp:113
viennacl::implicit_vector_base::value
cpu_value_type value() const
Definition: vector_def.hpp:48
viennacl::const_vector_iterator
A STL-type const-iterator for vector elements. Elements can be accessed, but cannot be manipulated...
Definition: forwards.h:245
viennacl::implicit_vector_base::value_
NumericT value_
Definition: vector_def.hpp:70
viennacl::vector_base::vector_base
vector_base()
Default constructor in order to be compatible with various containers.
Definition: vector.hpp:251
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::vector_base::operator*
vector_expression< const self_type, const NumericT, op_mult > operator*(char value) const
Scales the vector by a char (8-bit integer) 'alpha' and returns an expression template.
Definition: vector.hpp:742
viennacl::const_entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:236
viennacl::unit_vector::unit_vector
unit_vector(vcl_size_t s, vcl_size_t ind, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:78
viennacl::implicit_vector_base::index
vcl_size_t index() const
Definition: vector_def.hpp:49
viennacl::vector_base::switch_memory_context
void switch_memory_context(viennacl::context new_ctx)
Definition: vector.hpp:899
viennacl::vector_base::operator[]
entry_proxy< NumericT > operator[](size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:571
viennacl::vector_base::swap
self_type & swap(self_type &other)
Swaps the entries of the two vectors.
Definition: vector.hpp:867
viennacl::implicit_vector_base::size_
vcl_size_t size_
Definition: vector_def.hpp:68
entry_proxy.hpp
A proxy class for entries in a vector.
viennacl::implicit_vector_base::const_reference
NumericT const & const_reference
Definition: vector_def.hpp:43
viennacl::matrix_base< NumericT >
viennacl::matrix_expression
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
viennacl::vector_base::pad
void pad()
Pads vectors with alignment > 1 with trailing zeros if the internal size is larger than the visible s...
Definition: vector.hpp:889
viennacl::implicit_vector_base::operator[]
cpu_value_type operator[](vcl_size_t i) const
Definition: vector_def.hpp:59
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::zero_vector
Definition: vector_def.hpp:93
viennacl::vector_base::operator/
vector_expression< const self_type, const NumericT, op_div > operator/(char value) const
Scales the vector by a char (8-bit integer) 'alpha' and returns an expression template.
Definition: vector.hpp:786
viennacl::vector_base::operator-=
self_type & operator-=(const self_type &vec)
Definition: vector.hpp:616
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
viennacl::zero_vector::zero_vector
zero_vector(vcl_size_t s, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:95
viennacl::vector_base::handle_type
viennacl::backend::mem_handle handle_type
Definition: vector_def.hpp:111
viennacl::vector_base::resize
void resize(size_type new_size, bool preserve=true)
Resizes the allocated memory for the vector. Pads the memory to be a multiple of 'AlignmentV'.
Definition: vector.hpp:909
viennacl::vector_base::operator+=
self_type & operator+=(const self_type &vec)
Definition: vector.hpp:604
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::vector_base::operator-
vector_expression< const self_type, const NumericT, op_mult > operator-() const
Sign flip for the vector. Emulated to be equivalent to -1.0 * vector.
Definition: vector.hpp:830
viennacl::vector_base::operator()
entry_proxy< NumericT > operator()(size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:562
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::implicit_vector_base::context
viennacl::context context() const
Definition: vector_def.hpp:46
viennacl::vector_base::stride
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(NumericT))
Definition: vector_def.hpp:124
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
viennacl::vector_base::begin
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:841
viennacl::scalar_vector::scalar_vector
scalar_vector(vcl_size_t s, NumericT val, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:89
viennacl::vector_base::value_type
scalar< NumericT > value_type
Definition: vector_def.hpp:109
viennacl::implicit_vector_base::size
vcl_size_t size() const
Definition: vector_def.hpp:47
viennacl::vector_iterator
A STL-type iterator for vector elements. Elements can be accessed and manipulated. VERY SLOW!!
Definition: forwards.h:242
viennacl::vector_base::iterator
vector_iterator< NumericT, 1 > iterator
Definition: vector_def.hpp:115
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::vector_base::cpu_value_type
NumericT cpu_value_type
Definition: vector_def.hpp:110
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::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector_base::empty
bool empty() const
Returns true is the size is zero.
Definition: vector_def.hpp:126
viennacl::implicit_vector_base::implicit_vector_base
implicit_vector_base(vcl_size_t s, NumericT v, viennacl::context ctx)
Definition: vector_def.hpp:40
viennacl::vector_base::handle
handle_type & handle()
Returns the memory handle.
Definition: vector_def.hpp:130
viennacl::implicit_vector_base::operator()
cpu_value_type operator()(vcl_size_t i) const
Definition: vector_def.hpp:52
viennacl::op_prod
A tag class representing matrix-vector products and element-wise multiplications. ...
Definition: forwards.h:94
viennacl::vector_base::clear
void clear()
Resets all entries to zero. Does not change the size of the vector.
Definition: vector.hpp:875
viennacl::vector_base::operator/=
self_type & operator/=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:685
viennacl::unit_vector
Represents a vector consisting of 1 at a given index and zeros otherwise.
Definition: vector_def.hpp:76
viennacl::implicit_vector_base::implicit_vector_base
implicit_vector_base(vcl_size_t s, vcl_size_t i, NumericT v, viennacl::context ctx)
Definition: vector_def.hpp:39
viennacl::scalar_vector
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
Definition: vector_def.hpp:87
viennacl::implicit_vector_base::ctx_
viennacl::context ctx_
Definition: vector_def.hpp:71
viennacl::implicit_vector_base::index_
std::pair< bool, vcl_size_t > index_
Definition: vector_def.hpp:69
viennacl::vector_base::const_iterator
const_vector_iterator< NumericT, 1 > const_iterator
Definition: vector_def.hpp:114
viennacl::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::op_trans
A tag class representing transposed matrices.
Definition: forwards.h:220
viennacl::implicit_vector_base::has_index
bool has_index() const
Definition: vector_def.hpp:50
viennacl::implicit_vector_base
Common base class for representing vectors where the entries are not all stored explicitly.
Definition: vector_def.hpp:36
viennacl::vector_base::internal_size
size_type internal_size() const
Returns the internal length of the vector, which is given by size() plus the extra memory due to padd...
Definition: vector_def.hpp:120
viennacl::vector_base::end
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
Definition: vector.hpp:848
viennacl::memory_types
memory_types
Definition: forwards.h:345
viennacl::vector_base::start
size_type start() const
Returns the offset within the buffer.
Definition: vector_def.hpp:122
viennacl::entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:233
viennacl::vector_base::set_handle
void set_handle(viennacl::backend::mem_handle const &h)
Definition: vector_def.hpp:305
viennacl::vector_base::operator*=
self_type & operator*=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:629
viennacl::implicit_vector_base::cpu_value_type
NumericT cpu_value_type
Definition: vector_def.hpp:44
viennacl::vector_base::memory_domain
viennacl::memory_types memory_domain() const
Definition: vector_def.hpp:131
viennacl::vector_base::handle
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
viennacl::vector_base::fast_swap
self_type & fast_swap(self_type &other)
Swaps the handles of two vectors by swapping the OpenCL handles only, no data copy.
Definition: vector.hpp:881
viennacl::vector_base::size_type
SizeT size_type
Definition: vector_def.hpp:112
viennacl::backend::mem_handle::get_active_handle_id
memory_types get_active_handle_id() const
Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no da...
Definition: mem_handle.hpp:118