ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-2015, 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 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 
32 //proxy class for single vector entries (this is a slow operation!!)
40 template<typename NumericT>
41 class entry_proxy
42 {
43 public:
45 
51  explicit entry_proxy(vcl_size_t mem_offset,
52  handle_type & mem_handle)
53  : index_(mem_offset), mem_handle_(mem_handle) {}
54 
55 
56  //operators:
60  {
61  NumericT temp = read();
62  temp += value;
63  write(temp);
64  return *this;
65  }
66 
70  {
71  NumericT temp = read();
72  temp -= value;
73  write(temp);
74  return *this;
75  }
76 
80  {
81  NumericT temp = read();
82  temp *= value;
83  write(temp);
84  return *this;
85  }
86 
90  {
91  NumericT temp = read();
92  temp /= value;
93  write(temp);
94  return *this;
95  }
96 
100  {
101  write(value);
102  return *this;
103  }
104 
108  {
109  viennacl::backend::memory_copy(value.handle(), mem_handle_, 0, sizeof(NumericT)*index_, sizeof(NumericT));
110  return *this;
111  }
112 
116  {
117  viennacl::backend::memory_copy(other.handle(), mem_handle_, sizeof(NumericT) * other.index_, sizeof(NumericT)*index_, sizeof(NumericT));
118  return *this;
119  }
120 
121  //type conversion:
122  // allows to write something like:
123  // double test = vector(4);
130  operator NumericT () const
131  {
132  NumericT temp = read();
133  return temp;
134  }
135 
138  vcl_size_t index() const { return index_; }
139 
142  handle_type const & handle() const { return mem_handle_; }
143 
144 private:
147  NumericT read() const
148  {
149  NumericT temp;
150  viennacl::backend::memory_read(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &temp);
151  return temp;
152  }
153 
156  void write(NumericT value)
157  {
158  viennacl::backend::memory_write(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &value);
159  }
160 
161  vcl_size_t index_;
162  viennacl::backend::mem_handle & mem_handle_;
163 }; //entry_proxy
164 
165 
166 
167 
168 
169 
170 
178 template<typename NumericT>
179 class const_entry_proxy
180 {
181  typedef const_entry_proxy<NumericT> self_type;
182 public:
184 
190  explicit const_entry_proxy(vcl_size_t mem_offset,
191  handle_type const & mem_handle)
192  : index_(mem_offset), mem_handle_(mem_handle) {}
193 
194 
195  //type conversion:
196  // allows to write something like:
197  // double test = vector(4);
204  operator NumericT () const
205  {
206  NumericT temp = read();
207  return temp;
208  }
209 
212  unsigned int index() const { return index_; }
213 
216  handle_type const & handle() const { return mem_handle_; }
217 
218 private:
221  NumericT read() const
222  {
223  NumericT temp;
224  viennacl::backend::memory_read(mem_handle_, sizeof(NumericT)*index_, sizeof(NumericT), &temp);
225  return temp;
226  }
227 
228  vcl_size_t index_;
229  viennacl::backend::mem_handle const & mem_handle_;
230 }; //entry_proxy
231 
232 }
233 
234 #endif
vcl_size_t index() const
Returns the index of the represented element.
viennacl::backend::mem_handle handle_type
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:227
unsigned int index() const
Returns the index of the represented element.
entry_proxy & operator=(entry_proxy const &other)
Assignment of another GPU value.
entry_proxy & operator*=(NumericT value)
Inplace multiplication with a CPU floating point value.
Definition: entry_proxy.hpp:79
entry_proxy & operator=(NumericT value)
Assignment of a CPU floating point value.
Definition: entry_proxy.hpp:99
This file provides the forward declarations for the main types used within ViennaCL.
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
float NumericT
Definition: bisect.cpp:40
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
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...
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
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:51
entry_proxy & operator=(scalar< NumericT > const &value)
Assignment of a GPU floating point value. Avoids unnecessary GPU->CPU->GPU transfers.
std::size_t vcl_size_t
Definition: forwards.h:75
entry_proxy & operator-=(NumericT value)
Inplace subtraction of a CPU floating point value.
Definition: entry_proxy.hpp:69
entry_proxy & operator+=(NumericT value)
Inplace addition of a CPU floating point value.
Definition: entry_proxy.hpp:59
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
handle_type const & handle() const
Returns the memory handle.
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
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
entry_proxy & operator/=(NumericT value)
Inplace division by a CPU floating point value.
Definition: entry_proxy.hpp:89
Implementation of the ViennaCL scalar class.
viennacl::backend::mem_handle handle_type
Definition: entry_proxy.hpp:44