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"
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;
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_;
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 {
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:
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 
170  // Conversion CTOR:
171  template<typename OtherNumericT>
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 
198 
199  //transposed_matrix_proxy:
204  const vector_base<NumericT>,
205  op_prod> & proxy);
206 
208 
209 
210  //read-write access to an element of the vector
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 
251  operator * (char value) const;
254  operator * (short value) const;
257  operator * (int value) const;
260  operator * (long value) const;
263  operator * (float value) const;
266  operator * (double value) const;
267 
270  operator / (char value) const;
273  operator / (short value) const;
276  operator / (int value) const;
279  operator / (long value) const;
282  operator / (float value) const;
285  operator / (double value) const;
286 
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 
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
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:245
vector_base()
Default constructor in order to be compatible with various containers.
Definition: vector.hpp:251
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
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
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
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:899
entry_proxy< NumericT > operator[](size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:571
self_type & swap(self_type &other)
Swaps the entries of the two vectors.
Definition: vector.hpp:867
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:341
void pad()
Pads vectors with alignment > 1 with trailing zeros if the internal size is larger than the visible s...
Definition: vector.hpp:889
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:786
self_type & operator-=(const self_type &vec)
Definition: vector.hpp:616
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
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:909
self_type & operator+=(const self_type &vec)
Definition: vector.hpp:604
float NumericT
Definition: bisect.cpp:40
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
entry_proxy< NumericT > operator()(size_type index)
Read-write access to a single element of the vector.
Definition: vector.hpp:562
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:841
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:242
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:356
std::size_t vcl_size_t
Definition: forwards.h:75
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:94
void clear()
Resets all entries to zero. Does not change the size of the vector.
Definition: vector.hpp:875
self_type & operator/=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:685
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:220
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:848
memory_types
Definition: forwards.h:345
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:233
void set_handle(viennacl::backend::mem_handle const &h)
Definition: vector_def.hpp:305
self_type & operator*=(char val)
Scales a vector (or proxy) by a char (8-bit integer)
Definition: vector.hpp:629
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:881
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