• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

/data/development/ViennaCL/dev/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-2011, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008 
00009                             -----------------
00010                   ViennaCL - The Vienna Computing Library
00011                             -----------------
00012 
00013    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00014                
00015    (A list of authors and contributors can be found in the PDF manual)
00016 
00017    License:         MIT (X11), see file LICENSE in the base directory
00018 ============================================================================= */
00019 
00025 #include "viennacl/forwards.h"
00026 #include "viennacl/ocl/backend.hpp"
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:
00048         explicit entry_proxy(unsigned int mem_offset, 
00049                              viennacl::ocl::handle<cl_mem> const & mem_handle) 
00050          : _index(mem_offset), _mem_handle(mem_handle) {};
00051         
00052          
00053         //operators:
00056         entry_proxy & operator+=(SCALARTYPE value)
00057         {
00058           SCALARTYPE temp = read();
00059           temp += value; 
00060           write(temp);         
00061           return *this;
00062         }
00063 
00066         entry_proxy &  operator-=(SCALARTYPE value)
00067         {
00068           SCALARTYPE temp = read();
00069           temp -= value; 
00070           write(temp);         
00071           return *this;
00072         }
00073 
00076         entry_proxy &  operator*=(SCALARTYPE value)
00077         {
00078           SCALARTYPE temp = read();
00079           temp *= value; 
00080           write(temp);         
00081           return *this;
00082         }
00083 
00086         entry_proxy &  operator/=(SCALARTYPE value)
00087         {
00088           SCALARTYPE temp = read();
00089           temp /= value; 
00090           write(temp);         
00091           return *this;
00092         }
00093 
00096         entry_proxy &  operator=(SCALARTYPE value)
00097         {
00098           write(value);
00099           return *this;
00100         }
00101 
00104         entry_proxy & operator=(scalar<SCALARTYPE> const & value)
00105         {
00106           cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(), value.handle(), _mem_handle, 0, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), 0, NULL, NULL);
00107           //assert(err == CL_SUCCESS);
00108           VIENNACL_ERR_CHECK(err);
00109           return *this;
00110         }
00111 
00114         entry_proxy &  operator=(entry_proxy const & other)
00115         {
00116           cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(),
00117                                            other._mem_handle, //src
00118                                            _mem_handle,       //dest
00119                                            sizeof(SCALARTYPE) * other._index, //offset src
00120                                            sizeof(SCALARTYPE) * _index,       //offset dest
00121                                            sizeof(SCALARTYPE), 0, NULL, NULL);
00122           VIENNACL_ERR_CHECK(err);
00123           return *this;
00124         }
00125 
00126         //type conversion:
00127         // allows to write something like:
00128         //  double test = vector(4);
00135         operator SCALARTYPE () const
00136         {
00137           SCALARTYPE temp = read();
00138           return temp;
00139         }
00140         
00143         unsigned int index() const { return _index; }
00144         
00147         viennacl::ocl::handle<cl_mem> const & handle() const { return _mem_handle; }
00148 
00149       private:
00152         SCALARTYPE read() const
00153         {
00154           SCALARTYPE temp;
00155           cl_int err;
00156           err = clEnqueueReadBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &temp, 0, NULL, NULL);
00157           //assert(err == CL_SUCCESS);
00158           VIENNACL_ERR_CHECK(err);
00159           viennacl::ocl::get_queue().finish();
00160           return temp;
00161         }
00162         
00165         void write(SCALARTYPE value)
00166         {
00167           cl_int err;
00168           err = clEnqueueWriteBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &value, 0, NULL, NULL);
00169           //assert(err == CL_SUCCESS);
00170           VIENNACL_ERR_CHECK(err);
00171         }
00172         
00173         unsigned int _index;
00174         viennacl::ocl::handle<cl_mem> const & _mem_handle;
00175     }; //entry_proxy
00176     
00177 }
00178 
00179 #endif

Generated on Fri Dec 30 2011 23:20:43 for ViennaCL - The Vienna Computing Library by  doxygen 1.7.1