ViennaCL - The Vienna Computing Library  1.5.1
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-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
26 #include "viennacl/forwards.h"
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>
40  class entry_proxy
41  {
42  public:
44 
50  explicit entry_proxy(vcl_size_t mem_offset,
51  handle_type & mem_handle)
52  : index_(mem_offset), mem_handle_(mem_handle) {}
53 
54 
55  //operators:
58  entry_proxy & operator+=(SCALARTYPE value)
59  {
60  SCALARTYPE temp = read();
61  temp += value;
62  write(temp);
63  return *this;
64  }
65 
68  entry_proxy & operator-=(SCALARTYPE value)
69  {
70  SCALARTYPE temp = read();
71  temp -= value;
72  write(temp);
73  return *this;
74  }
75 
78  entry_proxy & operator*=(SCALARTYPE value)
79  {
80  SCALARTYPE temp = read();
81  temp *= value;
82  write(temp);
83  return *this;
84  }
85 
88  entry_proxy & operator/=(SCALARTYPE value)
89  {
90  SCALARTYPE temp = read();
91  temp /= value;
92  write(temp);
93  return *this;
94  }
95 
98  entry_proxy & operator=(SCALARTYPE value)
99  {
100  write(value);
101  return *this;
102  }
103 
107  {
108  viennacl::backend::memory_copy(value.handle(), mem_handle_, 0, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE));
109  return *this;
110  }
111 
115  {
116  viennacl::backend::memory_copy(other.handle(), mem_handle_, sizeof(SCALARTYPE) * other.index_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE));
117  return *this;
118  }
119 
120  //type conversion:
121  // allows to write something like:
122  // double test = vector(4);
129  operator SCALARTYPE () const
130  {
131  SCALARTYPE temp = read();
132  return temp;
133  }
134 
137  vcl_size_t index() const { return index_; }
138 
141  handle_type const & handle() const { return mem_handle_; }
142 
143  private:
146  SCALARTYPE read() const
147  {
148  SCALARTYPE temp;
149  viennacl::backend::memory_read(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &temp);
150  return temp;
151  }
152 
155  void write(SCALARTYPE value)
156  {
157  viennacl::backend::memory_write(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &value);
158  }
159 
160  vcl_size_t index_;
161  viennacl::backend::mem_handle & mem_handle_;
162  }; //entry_proxy
163 
164 
165 
166 
167 
168 
169 
177  template <typename SCALARTYPE>
179  {
181  public:
183 
189  explicit const_entry_proxy(vcl_size_t mem_offset,
190  handle_type const & mem_handle)
191  : index_(mem_offset), mem_handle_(mem_handle) {}
192 
193 
194  //type conversion:
195  // allows to write something like:
196  // double test = vector(4);
203  operator SCALARTYPE () const
204  {
205  SCALARTYPE temp = read();
206  return temp;
207  }
208 
211  unsigned int index() const { return index_; }
212 
215  handle_type const & handle() const { return mem_handle_; }
216 
217  private:
220  SCALARTYPE read() const
221  {
222  SCALARTYPE temp;
223  viennacl::backend::memory_read(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &temp);
224  return temp;
225  }
226 
227  vcl_size_t index_;
228  viennacl::backend::mem_handle const & mem_handle_;
229  }; //entry_proxy
230 
231 }
232 
233 #endif
std::size_t vcl_size_t
Definition: forwards.h:58
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
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
handle_type const & handle() const
Returns the memory handle.
Definition: entry_proxy.hpp:215
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: entry_proxy.hpp:178
entry_proxy & operator=(scalar< SCALARTYPE > const &value)
Assignment of a GPU floating point value. Avoids unnecessary GPU->CPU->GPU transfers.
Definition: entry_proxy.hpp:106
entry_proxy & operator=(entry_proxy const &other)
Assignment of another GPU value.
Definition: entry_proxy.hpp:114
viennacl::backend::mem_handle handle_type
Definition: entry_proxy.hpp:43
This file provides the forward declarations for the main types used within ViennaCL.
entry_proxy & operator+=(SCALARTYPE value)
Inplace addition of a CPU floating point value.
Definition: entry_proxy.hpp:58
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
entry_proxy & operator=(SCALARTYPE value)
Assignment of a CPU floating point value.
Definition: entry_proxy.hpp:98
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:704
entry_proxy & operator*=(SCALARTYPE value)
Inplace multiplication with a CPU floating point value.
Definition: entry_proxy.hpp:78
vcl_size_t index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:137
unsigned int index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:211
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
Definition: entry_proxy.hpp:141
void memory_copy(mem_handle const &src_buffer, mem_handle &dst_buffer, vcl_size_t src_offset, vcl_size_t dst_offset, vcl_size_t bytes_to_copy)
Copies 'bytes_to_copy' bytes from address 'src_buffer + src_offset' to memory starting at address 'ds...
Definition: memory.hpp:140
viennacl::backend::mem_handle handle_type
Definition: entry_proxy.hpp:182
const_entry_proxy(vcl_size_t mem_offset, handle_type const &mem_handle)
The constructor for the proxy class. Declared explicit to avoid any surprises created by the compiler...
Definition: entry_proxy.hpp:189
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:62
entry_proxy & operator/=(SCALARTYPE value)
Inplace division by a CPU floating point value.
Definition: entry_proxy.hpp:88
entry_proxy & operator-=(SCALARTYPE value)
Inplace subtraction of a CPU floating point value.
Definition: entry_proxy.hpp:68
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:178
Implementation of the ViennaCL scalar class.
entry_proxy(vcl_size_t mem_offset, handle_type &mem_handle)
The constructor for the proxy class. Declared explicit to avoid any surprises created by the compiler...
Definition: entry_proxy.hpp:50