All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector_def.hpp
Go to the documentation of this file.
#ifndef VIENNACL_DETAIL_VECTOR_DEF_HPP_
#define VIENNACL_DETAIL_VECTOR_DEF_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include "viennacl/forwards.h"
#include "viennacl/tools/entry_proxy.hpp"
namespace viennacl
{
template<typename NumericT>
class implicit_vector_base
{
protected:
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){ }
implicit_vector_base(vcl_size_t s, NumericT v, viennacl::context ctx) : size_(s), index_(std::make_pair(false,0)), value_(v), ctx_(ctx){ }
public:
typedef NumericT const & const_reference;
typedef NumericT cpu_value_type;
viennacl::context context() const { return ctx_; }
vcl_size_t size() const { return size_; }
cpu_value_type value() const { return value_; }
vcl_size_t index() const { return index_.second; }
bool has_index() const { return index_.first; }
cpu_value_type operator()(vcl_size_t i) const
{
if (index_.first)
return (i==index_.second)?value_:0;
return value_;
}
cpu_value_type operator[](vcl_size_t i) const
{
if (index_.first)
return (i==index_.second)?value_:0;
return
value_;
}
protected:
vcl_size_t size_;
std::pair<bool, vcl_size_t> index_;
NumericT value_;
viennacl::context ctx_;
};
template<typename NumericT>
struct unit_vector : public implicit_vector_base<NumericT>
{
unit_vector(vcl_size_t s, vcl_size_t ind, viennacl::context ctx = viennacl::context()) : implicit_vector_base<NumericT>(s, ind, 1, ctx)
{
assert( (ind < s) && bool("Provided index out of range!") );
}
};
template<typename NumericT>
struct scalar_vector : public implicit_vector_base<NumericT>
{
scalar_vector(vcl_size_t s, NumericT val, viennacl::context ctx = viennacl::context()) : implicit_vector_base<NumericT>(s, val, ctx) {}
};
template<typename NumericT>
struct zero_vector : public scalar_vector<NumericT>
{
zero_vector(vcl_size_t s, viennacl::context ctx = viennacl::context()) : scalar_vector<NumericT>(s, 0, ctx){}
};
template<class NumericT, typename SizeT /* see forwards.h for default type */, typename DistanceT /* see forwards.h for default type */>
class vector_base
{
typedef vector_base<NumericT, SizeT, DistanceT> self_type;
public:
typedef scalar<NumericT> value_type;
typedef NumericT cpu_value_type;
typedef viennacl::backend::mem_handle handle_type;
typedef SizeT size_type;
typedef DistanceT difference_type;
typedef const_vector_iterator<NumericT, 1> const_iterator;
typedef vector_iterator<NumericT, 1> iterator;
size_type size() const { return size_; }
size_type internal_size() const { return internal_size_; }
size_type start() const { return start_; }
size_type stride() const { return stride_; }
bool empty() const { return size_ == 0; }
const handle_type & handle() const { return elements_; }
handle_type & handle() { return elements_; }
viennacl::memory_types memory_domain() const { return elements_.get_active_handle_id(); }
explicit vector_base();
explicit vector_base(viennacl::backend::mem_handle & h, size_type vec_size, size_type vec_start, size_type vec_stride);
explicit vector_base(size_type vec_size, viennacl::context ctx = viennacl::context());
// CUDA or host memory:
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);
#ifdef VIENNACL_WITH_OPENCL
explicit vector_base(cl_mem existing_mem, size_type vec_size, size_type start = 0, size_type stride = 1, viennacl::context ctx = viennacl::context());
#endif
template<typename LHS, typename RHS, typename OP>
explicit vector_base(vector_expression<const LHS, const RHS, OP> const & proxy);
// Copy CTOR:
vector_base(const self_type & other);
// Conversion CTOR:
template<typename OtherNumericT>
vector_base(const vector_base<OtherNumericT> & v1);
self_type & operator=(const self_type & vec);
template<typename LHS, typename RHS, typename OP>
self_type & operator=(const vector_expression<const LHS, const RHS, OP> & proxy);
template<typename OtherNumericT>
self_type & operator = (const vector_base<OtherNumericT> & v1);
self_type & operator = (unit_vector<NumericT> const & v);
self_type & operator = (zero_vector<NumericT> const & v);
self_type & operator = (scalar_vector<NumericT> const & v);
self_type & operator=(const viennacl::vector_expression< const matrix_base<NumericT>, const vector_base<NumericT>, viennacl::op_prod> & proxy);
//transposed_matrix_proxy:
self_type & operator=(const vector_expression< const matrix_expression< const matrix_base<NumericT>, const matrix_base<NumericT>, op_trans >,
const vector_base<NumericT>,
op_prod> & proxy);
//read-write access to an element of the vector
entry_proxy<NumericT> operator()(size_type index);
entry_proxy<NumericT> operator[](size_type index);
const_entry_proxy<NumericT> operator()(size_type index) const;
const_entry_proxy<NumericT> operator[](size_type index) const;
self_type & operator += (const self_type & vec);
self_type & operator -= (const self_type & vec);
self_type & operator *= (char val);
self_type & operator *= (short val);
self_type & operator *= (int val);
self_type & operator *= (long val);
self_type & operator *= (float val);
self_type & operator *= (double val);
self_type & operator /= (char val);
self_type & operator /= (short val);
self_type & operator /= (int val);
self_type & operator /= (long val);
self_type & operator /= (float val);
self_type & operator /= (double val);
vector_expression< const self_type, const NumericT, op_mult>
operator * (char value) const;
vector_expression< const self_type, const NumericT, op_mult>
operator * (short value) const;
vector_expression< const self_type, const NumericT, op_mult>
operator * (int value) const;
vector_expression< const self_type, const NumericT, op_mult>
operator * (long value) const;
vector_expression< const self_type, const NumericT, op_mult>
operator * (float value) const;
vector_expression< const self_type, const NumericT, op_mult>
operator * (double value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (char value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (short value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (int value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (long value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (float value) const;
vector_expression< const self_type, const NumericT, op_div>
operator / (double value) const;
vector_expression<const self_type, const NumericT, op_mult> operator-() const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
self_type & swap(self_type & other);
void clear();
protected:
void set_handle(viennacl::backend::mem_handle const & h) { elements_ = h; }
self_type & fast_swap(self_type & other);
void pad();
void switch_memory_context(viennacl::context new_ctx);
//TODO: Think about implementing the following public member functions
//void insert_element(unsigned int i, NumericT val){}
//void erase_element(unsigned int i){}
//enlarge or reduce allocated memory and set unused memory to zero
void resize(size_type new_size, bool preserve = true);
void resize(size_type new_size, viennacl::context ctx, bool preserve = true);
private:
void resize_impl(size_type new_size, viennacl::context ctx, bool preserve = true);
size_type size_;
size_type start_;
size_type stride_;
size_type internal_size_;
handle_type elements_;
}; //vector_base
} // namespace viennacl
#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