All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector_proxy.hpp
Go to the documentation of this file.
#ifndef VIENNACL_VECTOR_PROXY_HPP_
#define VIENNACL_VECTOR_PROXY_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/range.hpp"
#include "viennacl/slice.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/tools/entry_proxy.hpp"
namespace viennacl
{
template<typename VectorType>
class vector_range : public vector_base<typename VectorType::cpu_value_type>
{
typedef vector_range<VectorType> self_type;
typedef vector_base<typename VectorType::cpu_value_type> base_type;
public:
typedef typename VectorType::value_type value_type;
typedef range::size_type size_type;
typedef range::difference_type difference_type;
typedef value_type reference;
typedef const value_type & const_reference;
typedef typename VectorType::const_iterator const_iterator;
typedef typename VectorType::iterator iterator;
typedef typename VectorType::handle_type handle_type;
typedef typename VectorType::cpu_value_type cpu_value_type;
vector_range(VectorType const & v, range const & entry_range)
: base_type(const_cast<handle_type &>(v.handle()),
entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
vector_range(self_type const & v, range const & entry_range)
: base_type(const_cast<handle_type &>(v.handle()),
entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
vector_range(self_type const & other)
: base_type(const_cast<handle_type &>(other.handle()),
other.size(), other.start(), other.stride()) {}
using base_type::operator=;
// the following are needed for Visual Studio:
template<typename OtherNumericT>
base_type & operator=(viennacl::vector<OtherNumericT> const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
template<typename OtherNumericT>
base_type & operator=(viennacl::vector_range<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
template<typename OtherNumericT>
base_type & operator=(viennacl::vector_slice<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
};
template<typename VectorType>
class vector_range<vector_range<VectorType> > : public vector_base<typename VectorType::cpu_value_type>
{
typedef vector_base<typename VectorType::cpu_value_type> base_type;
public:
typedef typename VectorType::handle_type handle_type;
vector_range(VectorType const & v, range const & entry_range)
: base_type(const_cast<handle_type &>(v.handle()),
entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
vector_range(vector_range<VectorType> const & v, range const & entry_range)
: base_type(const_cast<handle_type &>(v.handle()),
entry_range.size(), v.start() + v.stride() * entry_range.start(), v.stride()) {}
};
template<typename VectorType, typename NumericT>
void copy(const VectorType & cpu_vector,
vector_range<vector<NumericT> > & gpu_vector_range )
{
assert(cpu_vector.end() - cpu_vector.begin() >= 0 && bool("Range must have nonnegative length!"));
if (cpu_vector.end() - cpu_vector.begin() > 0)
{
//we require that the size of the gpu_vector is larger or equal to the cpu-size
std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(cpu_vector.end() - cpu_vector.begin()));
std::copy(cpu_vector.begin(), cpu_vector.end(), temp_buffer.begin());
viennacl::backend::memory_write(gpu_vector_range.handle(), sizeof(NumericT)*gpu_vector_range.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
}
}
template<typename CPUVECTOR, typename VectorType>
void fast_copy(const CPUVECTOR & cpu_vec, vector_range<VectorType> & gpu_vec)
{
viennacl::fast_copy(cpu_vec.begin(), cpu_vec.end(), gpu_vec.begin());
}
template<typename NumericT, typename VectorType>
void copy(vector_range<vector<NumericT> > const & gpu_vector_range,
VectorType & cpu_vector)
{
assert(cpu_vector.end() - cpu_vector.begin() >= 0 && bool("Range must have nonnegative length!"));
if (cpu_vector.end() > cpu_vector.begin())
{
std::vector<NumericT> temp_buffer(static_cast<vcl_size_t>(cpu_vector.end() - cpu_vector.begin()));
viennacl::backend::memory_read(gpu_vector_range.handle(), sizeof(NumericT)*gpu_vector_range.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
//now copy entries to cpu_vec:
std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_vector.begin());
}
}
template<typename VectorType, typename CPUVECTOR>
void fast_copy(vector_range< VectorType > const & gpu_vec,
CPUVECTOR & cpu_vec )
{
viennacl::fast_copy(gpu_vec.begin(), gpu_vec.end(), cpu_vec.begin());
}
//
// Convenience function
//
template<typename VectorType>
vector_range<VectorType> project(VectorType const & vec, viennacl::range const & r1)
{
return vector_range<VectorType>(vec, r1);
}
template<typename VectorType>
vector_range<VectorType> project(viennacl::vector_range<VectorType> const & vec, viennacl::range const & r1)
{
assert(r1.size() <= vec.size() && bool("Size of range invalid!"));
return vector_range<VectorType>(vec, r1);
}
//
//
//
//
//
//
template<typename VectorType>
class vector_slice : public vector_base<typename VectorType::cpu_value_type>
{
typedef vector_slice<VectorType> self_type;
typedef vector_base<typename VectorType::cpu_value_type> base_type;
public:
typedef typename VectorType::value_type value_type;
typedef slice::size_type size_type;
typedef slice::difference_type difference_type;
typedef value_type reference;
typedef const value_type & const_reference;
typedef typename VectorType::const_iterator const_iterator;
typedef typename VectorType::iterator iterator;
typedef typename VectorType::handle_type handle_type;
typedef typename VectorType::cpu_value_type cpu_value_type;
vector_slice(VectorType const & v, slice const & entry_slice)
: base_type(const_cast<handle_type &>(v.handle()),
entry_slice.size(), v.start() + v.stride() * entry_slice.start(), v.stride() * entry_slice.stride()) {}
vector_slice(self_type const & v, slice const & entry_slice)
: base_type(const_cast<handle_type &>(v.handle()),
entry_slice.size(), v.start() + v.stride() * entry_slice.start(), v.stride() * entry_slice.stride()) {}
vector_slice(self_type const & other)
: base_type(const_cast<handle_type &>(other.handle()),
other.size(), other.start(), other.stride()) {}
using base_type::operator=;
// the following are needed for Visual Studio:
template<typename OtherNumericT>
base_type & operator=(viennacl::vector<OtherNumericT> const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
template<typename OtherNumericT>
base_type & operator=(viennacl::vector_range<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
template<typename OtherNumericT>
base_type & operator=(viennacl::vector_slice<viennacl::vector<OtherNumericT> > const & v) { return base_type::operator=(static_cast<viennacl::vector_base<OtherNumericT> const &>(v)); }
};
template<typename VectorType>
class vector_slice<vector_slice<VectorType> > : public vector_base<typename VectorType::cpu_value_type>
{
typedef vector_base<typename VectorType::cpu_value_type> base_type;
public:
typedef typename VectorType::handle_type handle_type;
vector_slice(VectorType const & v, slice const & entry_slice)
: base_type(const_cast<handle_type &>(v.handle()),
entry_slice.size(), v.start() + v.stride() * entry_slice.start(), entry_slice.stride() * v.stride()) {}
vector_slice(vector_slice<VectorType> const & v, slice const & entry_slice)
: base_type(const_cast<handle_type &>(v.handle()),
entry_slice.size(), v.start() + v.stride() * entry_slice.start(), entry_slice.stride() * v.stride()) {}
};
template<typename VectorType, typename NumericT>
void copy(const VectorType & cpu_vector,
vector_slice<vector<NumericT> > & gpu_vector_slice )
{
if (cpu_vector.size() > 0)
{
std::vector<NumericT> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
temp_buffer[i * gpu_vector_slice.stride()] = cpu_vector[i];
viennacl::backend::memory_write(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
}
}
template<typename VectorType, typename NumericT>
void copy(vector_slice<vector<NumericT> > const & gpu_vector_slice,
VectorType & cpu_vector)
{
assert(gpu_vector_slice.end() - gpu_vector_slice.begin() >= 0 && bool("Range must have nonnegative length!"));
if (gpu_vector_slice.end() - gpu_vector_slice.begin() > 0)
{
std::vector<NumericT> temp_buffer(gpu_vector_slice.stride() * gpu_vector_slice.size());
viennacl::backend::memory_read(gpu_vector_slice.handle(), sizeof(NumericT)*gpu_vector_slice.start(), sizeof(NumericT)*temp_buffer.size(), &(temp_buffer[0]));
for (vcl_size_t i=0; i<cpu_vector.size(); ++i)
cpu_vector[i] = temp_buffer[i * gpu_vector_slice.stride()];
}
}
//
// Convenience functions
//
template<typename VectorType>
vector_slice<VectorType> project(VectorType const & vec, viennacl::slice const & s1)
{
assert(s1.size() <= vec.size() && bool("Size of slice larger than vector size!"));
return vector_slice<VectorType>(vec, s1);
}
template<typename VectorType>
vector_slice<VectorType> project(viennacl::vector_slice<VectorType> const & vec, viennacl::slice const & s1)
{
assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
return vector_slice<VectorType>(vec, s1);
}
// interaction with range and vector_range:
template<typename VectorType>
vector_slice<VectorType> project(viennacl::vector_slice<VectorType> const & vec, viennacl::range const & r1)
{
assert(r1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
return vector_slice<VectorType>(vec, viennacl::slice(r1.start(), 1, r1.size()));
}
template<typename VectorType>
vector_slice<VectorType> project(viennacl::vector_range<VectorType> const & vec, viennacl::slice const & s1)
{
assert(s1.size() <= vec.size() && bool("Size of slice larger than vector proxy!"));
return vector_slice<VectorType>(vec, s1);
}
}
#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)