ViennaCL - The Vienna Computing Library  1.5.1
viennacl/tools/entry_proxy.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00002 #define VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00026 #include "viennacl/forwards.h"
00027 #include "viennacl/scalar.hpp"
00028 
00029 namespace viennacl
00030 {
00031     //proxy class for single vector entries (this is a slow operation!!)
00039     template <typename SCALARTYPE>
00040     class entry_proxy
00041     {
00042       public:
00043         typedef viennacl::backend::mem_handle      handle_type;
00044 
00050         explicit entry_proxy(vcl_size_t mem_offset,
00051                              handle_type & mem_handle)
00052          : index_(mem_offset), mem_handle_(mem_handle) {}
00053 
00054 
00055         //operators:
00058         entry_proxy & operator+=(SCALARTYPE value)
00059         {
00060           SCALARTYPE temp = read();
00061           temp += value;
00062           write(temp);
00063           return *this;
00064         }
00065 
00068         entry_proxy &  operator-=(SCALARTYPE value)
00069         {
00070           SCALARTYPE temp = read();
00071           temp -= value;
00072           write(temp);
00073           return *this;
00074         }
00075 
00078         entry_proxy &  operator*=(SCALARTYPE value)
00079         {
00080           SCALARTYPE temp = read();
00081           temp *= value;
00082           write(temp);
00083           return *this;
00084         }
00085 
00088         entry_proxy &  operator/=(SCALARTYPE value)
00089         {
00090           SCALARTYPE temp = read();
00091           temp /= value;
00092           write(temp);
00093           return *this;
00094         }
00095 
00098         entry_proxy &  operator=(SCALARTYPE value)
00099         {
00100           write(value);
00101           return *this;
00102         }
00103 
00106         entry_proxy & operator=(scalar<SCALARTYPE> const & value)
00107         {
00108           viennacl::backend::memory_copy(value.handle(), mem_handle_, 0, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE));
00109           return *this;
00110         }
00111 
00114         entry_proxy &  operator=(entry_proxy const & other)
00115         {
00116           viennacl::backend::memory_copy(other.handle(), mem_handle_, sizeof(SCALARTYPE) * other.index_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE));
00117           return *this;
00118         }
00119 
00120         //type conversion:
00121         // allows to write something like:
00122         //  double test = vector(4);
00129         operator SCALARTYPE () const
00130         {
00131           SCALARTYPE temp = read();
00132           return temp;
00133         }
00134 
00137         vcl_size_t index() const { return index_; }
00138 
00141         handle_type const & handle() const { return mem_handle_; }
00142 
00143       private:
00146         SCALARTYPE read() const
00147         {
00148           SCALARTYPE temp;
00149           viennacl::backend::memory_read(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &temp);
00150           return temp;
00151         }
00152 
00155         void write(SCALARTYPE value)
00156         {
00157           viennacl::backend::memory_write(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &value);
00158         }
00159 
00160         vcl_size_t index_;
00161         viennacl::backend::mem_handle & mem_handle_;
00162     }; //entry_proxy
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00177     template <typename SCALARTYPE>
00178     class const_entry_proxy
00179     {
00180         typedef const_entry_proxy<SCALARTYPE>      self_type;
00181       public:
00182         typedef viennacl::backend::mem_handle      handle_type;
00183 
00189         explicit const_entry_proxy(vcl_size_t mem_offset,
00190                                    handle_type const & mem_handle)
00191          : index_(mem_offset), mem_handle_(mem_handle) {}
00192 
00193 
00194         //type conversion:
00195         // allows to write something like:
00196         //  double test = vector(4);
00203         operator SCALARTYPE () const
00204         {
00205           SCALARTYPE temp = read();
00206           return temp;
00207         }
00208 
00211         unsigned int index() const { return index_; }
00212 
00215         handle_type const & handle() const { return mem_handle_; }
00216 
00217       private:
00220         SCALARTYPE read() const
00221         {
00222           SCALARTYPE temp;
00223           viennacl::backend::memory_read(mem_handle_, sizeof(SCALARTYPE)*index_, sizeof(SCALARTYPE), &temp);
00224           return temp;
00225         }
00226 
00227         vcl_size_t index_;
00228         viennacl::backend::mem_handle const & mem_handle_;
00229     }; //entry_proxy
00230 
00231 }
00232 
00233 #endif