ViennaCL - The Vienna Computing Library  1.6.2
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-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"
27 
28 namespace viennacl
29 {
30 
35 template<typename NumericT>
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 
53  {
54  if (index_.first)
55  return (i==index_.second)?value_:0;
56  return value_;
57  }
58 
60  {
61  if (index_.first)
62  return (i==index_.second)?value_:0;
63  return
64  value_;
65  }
66 
67 protected:
69  std::pair<bool, vcl_size_t> index_;
70  NumericT value_;
72 };
73 
75 template<typename NumericT>
76 struct unit_vector : public implicit_vector_base<NumericT>
77 {
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 {
96 };
97 
98 
103 template<class NumericT, typename SizeT /* see forwards.h for default type */, typename DistanceT /* see forwards.h for default type */>
105 {
107 
108 public:
110  typedef NumericT cpu_value_type;
112  typedef SizeT size_type;
113  typedef DistanceT difference_type;
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_; }
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>
166 
167  // Copy CTOR:
168  vector_base(const self_type & other);
169 
172  self_type & operator=(const self_type & vec);
176  template<typename LHS, typename RHS, typename OP>
177  self_type & operator=(const vector_expression<const LHS, const RHS, OP> & proxy);
178  // assign vector range or vector slice
179  template<typename T>
180  self_type & operator = (const vector_base<T> & v1);
182  self_type & operator = (unit_vector<NumericT> const & v);
184  self_type & operator = (zero_vector<NumericT> const & v);
186  self_type & operator = (scalar_vector<NumericT> const & v);
187 
188 
190 
194 
195  //transposed_matrix_proxy:
200  const vector_base<NumericT>,
201  op_prod> & proxy);
202 
204 
205 
206  //read-write access to an element of the vector
215  self_type & operator += (const self_type & vec);
216  self_type & operator -= (const self_type & vec);
217 
219  self_type & operator *= (char val);
221  self_type & operator *= (short val);
223  self_type & operator *= (int val);
225  self_type & operator *= (long val);
227  self_type & operator *= (float val);
229  self_type & operator *= (double val);
230 
231 
233  self_type & operator /= (char val);
235  self_type & operator /= (short val);
237  self_type & operator /= (int val);
239  self_type & operator /= (long val);
241  self_type & operator /= (float val);
243  self_type & operator /= (double val);
244 
247  operator * (char value) const;
250  operator * (short value) const;
253  operator * (int value) const;
256  operator * (long value) const;
259  operator * (float value) const;
262  operator * (double value) const;
263 
266  operator / (char value) const;
269  operator / (short value) const;
272  operator / (int value) const;
275  operator / (long value) const;
278  operator / (float value) const;
281  operator / (double value) const;
282 
286  iterator begin();
288  iterator end();
290  const_iterator begin() const;
292  const_iterator end() const;
294  self_type & swap(self_type & other);
295 
297  void clear();
298 
299 protected:
300 
301  void set_handle(viennacl::backend::mem_handle const & h) { elements_ = h; }
302 
304  self_type & fast_swap(self_type & other);
305 
307  void pad();
308 
310 
311  //TODO: Think about implementing the following public member functions
312  //void insert_element(unsigned int i, NumericT val){}
313  //void erase_element(unsigned int i){}
314 
315  //enlarge or reduce allocated memory and set unused memory to zero
321  void resize(size_type new_size, bool preserve = true);
322 
329  void resize(size_type new_size, viennacl::context ctx, bool preserve = true);
330 private:
331 
332  void resize_impl(size_type new_size, viennacl::context ctx, bool preserve = true);
333 
334  size_type size_;
335  size_type start_;
336  size_type stride_;
337  size_type internal_size_;
338  handle_type elements_;
339 }; //vector_base
340 
343 } // namespace viennacl
344 
345 #endif
cpu_value_type value() const
Definition: vector_def.hpp:48
A STL-type const-iterator for vector elements. Elements can be accessed, but cannot be manipulated...
Definition: forwards.h:244
vector_base()
Default constructor in order to be compatible with various containers.
Definition: vector.hpp:252
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
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:728
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:235
unit_vector(vcl_size_t s, vcl_size_t ind, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:78
vcl_size_t index() const
Definition: vector_def.hpp:49
void switch_memory_context(viennacl::context new_ctx)
Definition: vector.hpp:885
entry_proxy< NumericT > operator[](size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:557
self_type & swap(self_type &other)
Swaps the entries of the two vectors.
Definition: vector.hpp:853
A proxy class for entries in a vector.
NumericT const & const_reference
Definition: vector_def.hpp:43
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:340
void pad()
Pads vectors with alignment > 1 with trailing zeros if the internal size is larger than the visible s...
Definition: vector.hpp:875
cpu_value_type operator[](vcl_size_t i) const
Definition: vector_def.hpp:59
This file provides the forward declarations for the main types used within ViennaCL.
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:772
self_type & operator-=(const self_type &vec)
Definition: vector.hpp:602
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:238
zero_vector(vcl_size_t s, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:95
viennacl::backend::mem_handle handle_type
Definition: vector_def.hpp:111
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:895
self_type & operator+=(const self_type &vec)
Definition: vector.hpp:590
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:816
entry_proxy< NumericT > operator()(size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:548
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::context context() const
Definition: vector_def.hpp:46
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(NumericT))
Definition: vector_def.hpp:124
viennacl::vector< float > v1
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:827
scalar_vector(vcl_size_t s, NumericT val, viennacl::context ctx=viennacl::context())
Definition: vector_def.hpp:89
scalar< NumericT > value_type
Definition: vector_def.hpp:109
vcl_size_t size() const
Definition: vector_def.hpp:47
A STL-type iterator for vector elements. Elements can be accessed and manipulated. VERY SLOW!!
Definition: forwards.h:241
vector_iterator< NumericT, 1 > iterator
Definition: vector_def.hpp:115
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
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:341
std::size_t vcl_size_t
Definition: forwards.h:74
bool empty() const
Returns true is the size is zero.
Definition: vector_def.hpp:126
implicit_vector_base(vcl_size_t s, NumericT v, viennacl::context ctx)
Definition: vector_def.hpp:40
handle_type & handle()
Returns the memory handle.
Definition: vector_def.hpp:130
cpu_value_type operator()(vcl_size_t i) const
Definition: vector_def.hpp:52
A tag class representing matrix-vector products and element-wise multiplications. ...
Definition: forwards.h:93
void clear()
Resets all entries to zero. Does not change the size of the vector.
Definition: vector.hpp:861
self_type & operator/=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:671
Represents a vector consisting of 1 at a given index and zeros otherwise.
Definition: vector_def.hpp:76
implicit_vector_base(vcl_size_t s, vcl_size_t i, NumericT v, viennacl::context ctx)
Definition: vector_def.hpp:39
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
std::pair< bool, vcl_size_t > index_
Definition: vector_def.hpp:69
const_vector_iterator< NumericT, 1 > const_iterator
Definition: vector_def.hpp:114
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
A tag class representing transposed matrices.
Definition: forwards.h:219
Common base class for representing vectors where the entries are not all stored explicitly.
Definition: vector_def.hpp:36
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
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
Definition: vector.hpp:834
memory_types
Definition: forwards.h:344
size_type start() const
Returns the offset within the buffer.
Definition: vector_def.hpp:122
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:232
void set_handle(viennacl::backend::mem_handle const &h)
Definition: vector_def.hpp:301
self_type & operator*=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:615
viennacl::memory_types memory_domain() const
Definition: vector_def.hpp:131
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
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:867
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