ViennaCL - The Vienna Computing Library  1.2.0
vector_proxy.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_VECTOR_PROXY_HPP_
2 #define VIENNACL_VECTOR_PROXY_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #include "viennacl/forwards.h"
25 #include "viennacl/range.hpp"
26 #include "viennacl/vector.hpp"
27 
28 namespace viennacl
29 {
30 
31  template <typename VectorType>
33  {
35 
36  public:
37  typedef typename VectorType::value_type value_type;
41  typedef const value_type & const_reference;
42 
43  static const int alignment = VectorType::alignment;
44 
45  vector_range(VectorType & v,
46  range const & entry_range) : v_(v), entry_range_(entry_range) {}
47 
48  size_type start() const { return entry_range_.start(); }
49  size_type size() const { return entry_range_.size(); }
50 
51  template <typename LHS, typename RHS, typename OP>
53  RHS,
54  OP > & proxy)
55  {
56  assert( false && "Not implemented!");
57  return *this;
58  }
59 
60  self_type & operator += (self_type const & other)
61  {
62  viennacl::linalg::inplace_add(*this, other);
63  return *this;
64  }
65 
66 
67  //const_reference operator()(size_type i, size_type j) const { return A_(start1() + i, start2() + i); }
68  //reference operator()(size_type i, size_type j) { return A_(start1() + i, start2() + i); }
69 
70  VectorType & get() { return v_; }
71  const VectorType & get() const { return v_; }
72 
73  private:
74  VectorType & v_;
75  range entry_range_;
76  };
77 
78 
79  template<typename VectorType>
80  std::ostream & operator<<(std::ostream & s, vector_range<VectorType> const & proxy)
81  {
82  typedef typename VectorType::value_type ScalarType;
83  std::vector<ScalarType> temp(proxy.size());
84  viennacl::copy(proxy, temp);
85 
86  //instead of printing 'temp' directly, let's reuse the existing functionality for viennacl::vector. It certainly adds overhead, but printing a vector is typically not about performance...
87  VectorType temp2(temp.size());
88  viennacl::copy(temp, temp2);
89  s << temp2;
90  return s;
91  }
92 
93 
94 
95 
99 
100  //row_major:
101  template <typename VectorType, typename SCALARTYPE>
102  void copy(const VectorType & cpu_vector,
103  vector_range<vector<SCALARTYPE> > & gpu_vector_range )
104  {
105  assert(cpu_vector.end() - cpu_vector.begin() >= 0);
106 
107  if (cpu_vector.end() - cpu_vector.begin() > 0)
108  {
109  //we require that the size of the gpu_vector is larger or equal to the cpu-size
110  std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
111  std::copy(cpu_vector.begin(), cpu_vector.end(), temp_buffer.begin());
112  cl_int err = clEnqueueWriteBuffer(viennacl::ocl::get_queue().handle(),
113  gpu_vector_range.get().handle(), CL_TRUE, sizeof(SCALARTYPE)*gpu_vector_range.start(),
114  sizeof(SCALARTYPE)*temp_buffer.size(),
115  &(temp_buffer[0]), 0, NULL, NULL);
116  VIENNACL_ERR_CHECK(err);
117  }
118  }
119 
120 
124 
125 
126  template <typename VectorType, typename SCALARTYPE>
127  void copy(vector_range<vector<SCALARTYPE> > const & gpu_vector_range,
128  VectorType & cpu_vector)
129  {
130  assert(cpu_vector.end() - cpu_vector.begin() >= 0);
131 
132  if (cpu_vector.end() > cpu_vector.begin())
133  {
134  std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
135  cl_int err = clEnqueueReadBuffer(viennacl::ocl::get_queue().handle(),
136  gpu_vector_range.get().handle(), CL_TRUE, sizeof(SCALARTYPE)*gpu_vector_range.start(),
137  sizeof(SCALARTYPE)*temp_buffer.size(),
138  &(temp_buffer[0]), 0, NULL, NULL);
139  VIENNACL_ERR_CHECK(err);
141 
142  //now copy entries to cpu_vec:
143  std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_vector.begin());
144  }
145  }
146 
147 
148 }
149 
150 #endif