ViennaCL - The Vienna Computing Library  1.2.0
entry_proxy.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_ENTRY_PROXY_HPP_
2 #define VIENNACL_TOOLS_ENTRY_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 
25 #include "viennacl/forwards.h"
26 #include "viennacl/ocl/backend.hpp"
27 #include "viennacl/scalar.hpp"
28 
29 namespace viennacl
30 {
31  //proxy class for single vector entries (this is a slow operation!!)
39  template <typename SCALARTYPE>
41  {
42  public:
48  explicit entry_proxy(unsigned int mem_offset,
49  viennacl::ocl::handle<cl_mem> const & mem_handle)
50  : _index(mem_offset), _mem_handle(mem_handle) {};
51 
52 
53  //operators:
56  entry_proxy & operator+=(SCALARTYPE value)
57  {
58  SCALARTYPE temp = read();
59  temp += value;
60  write(temp);
61  return *this;
62  }
63 
66  entry_proxy & operator-=(SCALARTYPE value)
67  {
68  SCALARTYPE temp = read();
69  temp -= value;
70  write(temp);
71  return *this;
72  }
73 
76  entry_proxy & operator*=(SCALARTYPE value)
77  {
78  SCALARTYPE temp = read();
79  temp *= value;
80  write(temp);
81  return *this;
82  }
83 
86  entry_proxy & operator/=(SCALARTYPE value)
87  {
88  SCALARTYPE temp = read();
89  temp /= value;
90  write(temp);
91  return *this;
92  }
93 
96  entry_proxy & operator=(SCALARTYPE value)
97  {
98  write(value);
99  return *this;
100  }
101 
105  {
106  cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(), value.handle(), _mem_handle, 0, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), 0, NULL, NULL);
107  //assert(err == CL_SUCCESS);
108  VIENNACL_ERR_CHECK(err);
109  return *this;
110  }
111 
115  {
116  cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(),
117  other._mem_handle, //src
118  _mem_handle, //dest
119  sizeof(SCALARTYPE) * other._index, //offset src
120  sizeof(SCALARTYPE) * _index, //offset dest
121  sizeof(SCALARTYPE), 0, NULL, NULL);
122  VIENNACL_ERR_CHECK(err);
123  return *this;
124  }
125 
126  //type conversion:
127  // allows to write something like:
128  // double test = vector(4);
135  operator SCALARTYPE () const
136  {
137  SCALARTYPE temp = read();
138  return temp;
139  }
140 
143  unsigned int index() const { return _index; }
144 
147  viennacl::ocl::handle<cl_mem> const & handle() const { return _mem_handle; }
148 
149  private:
152  SCALARTYPE read() const
153  {
154  SCALARTYPE temp;
155  cl_int err;
156  err = clEnqueueReadBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &temp, 0, NULL, NULL);
157  //assert(err == CL_SUCCESS);
158  VIENNACL_ERR_CHECK(err);
160  return temp;
161  }
162 
165  void write(SCALARTYPE value)
166  {
167  cl_int err;
168  err = clEnqueueWriteBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &value, 0, NULL, NULL);
169  //assert(err == CL_SUCCESS);
170  VIENNACL_ERR_CHECK(err);
171  }
172 
173  unsigned int _index;
174  viennacl::ocl::handle<cl_mem> const & _mem_handle;
175  }; //entry_proxy
176 
177 }
178 
179 #endif