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

/data/development/ViennaCL/ViennaCL-1.1.2/viennacl/tools/entry_proxy.hpp

Go to the documentation of this file.
00001 /* =======================================================================
00002    Copyright (c) 2010, Institute for Microelectronics, TU Vienna.
00003    http://www.iue.tuwien.ac.at
00004                              -----------------
00005                      ViennaCL - The Vienna Computing Library
00006                              -----------------
00007                             
00008    authors:    Karl Rupp                          rupp@iue.tuwien.ac.at
00009                Florian Rudolf                     flo.rudy+viennacl@gmail.com
00010                Josef Weinbub                      weinbub@iue.tuwien.ac.at
00011 
00012    license:    MIT (X11), see file LICENSE in the ViennaCL base directory
00013 ======================================================================= */
00014 
00019 #ifndef _VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00020 #define _VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00021 
00022 #include "viennacl/forwards.h"
00023 #include "viennacl/ocl/backend.hpp"
00024 #include "viennacl/scalar.hpp"
00025 
00026 namespace viennacl
00027 {
00028     //proxy class for single vector entries (this is a slow operation!!)
00036     template <typename SCALARTYPE>
00037     class entry_proxy
00038     {
00039       public:
00045         explicit entry_proxy(unsigned int mem_offset, 
00046                              viennacl::ocl::handle<cl_mem> const & mem_handle) 
00047          : _index(mem_offset), _mem_handle(mem_handle) {};
00048         
00049          
00050         //operators:
00053         entry_proxy & operator+=(SCALARTYPE value)
00054         {
00055           SCALARTYPE temp = read();
00056           temp += value; 
00057           write(temp);         
00058           return *this;
00059         }
00060 
00063         entry_proxy &  operator-=(SCALARTYPE value)
00064         {
00065           SCALARTYPE temp = read();
00066           temp -= value; 
00067           write(temp);         
00068           return *this;
00069         }
00070 
00073         entry_proxy &  operator*=(SCALARTYPE value)
00074         {
00075           SCALARTYPE temp = read();
00076           temp *= value; 
00077           write(temp);         
00078           return *this;
00079         }
00080 
00083         entry_proxy &  operator/=(SCALARTYPE value)
00084         {
00085           SCALARTYPE temp = read();
00086           temp /= value; 
00087           write(temp);         
00088           return *this;
00089         }
00090 
00093         entry_proxy &  operator=(SCALARTYPE value)
00094         {
00095           write(value);
00096           return *this;
00097         }
00098 
00101         entry_proxy & operator=(scalar<SCALARTYPE> const & value)
00102         {
00103           cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(), value.handle(), _mem_handle, 0, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), 0, NULL, NULL);
00104           //assert(err == CL_SUCCESS);
00105           VIENNACL_ERR_CHECK(err);
00106           return *this;
00107         }
00108 
00111         entry_proxy &  operator=(entry_proxy const & other)
00112         {
00113           cl_int err = clEnqueueCopyBuffer(viennacl::ocl::get_queue().handle(),
00114                                            other._mem_handle, //src
00115                                            _mem_handle,       //dest
00116                                            sizeof(SCALARTYPE) * other._index, //offset src
00117                                            sizeof(SCALARTYPE) * _index,       //offset dest
00118                                            sizeof(SCALARTYPE), 0, NULL, NULL);
00119           VIENNACL_ERR_CHECK(err);
00120           return *this;
00121         }
00122 
00123         //type conversion:
00124         // allows to write something like:
00125         //  double test = vector(4);
00132         operator SCALARTYPE () const
00133         {
00134           SCALARTYPE temp = read();
00135           return temp;
00136         }
00137         
00140         unsigned int index() const { return _index; }
00141         
00144         viennacl::ocl::handle<cl_mem> const & handle() const { return _mem_handle; }
00145 
00146       private:
00149         SCALARTYPE read() const
00150         {
00151           SCALARTYPE temp;
00152           cl_int err;
00153           err = clEnqueueReadBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &temp, 0, NULL, NULL);
00154           //assert(err == CL_SUCCESS);
00155           VIENNACL_ERR_CHECK(err);
00156           viennacl::ocl::get_queue().finish();
00157           return temp;
00158         }
00159         
00162         void write(SCALARTYPE value)
00163         {
00164           cl_int err;
00165           err = clEnqueueWriteBuffer(viennacl::ocl::get_queue().handle(), _mem_handle, CL_TRUE, sizeof(SCALARTYPE)*_index, sizeof(SCALARTYPE), &value, 0, NULL, NULL);
00166           //assert(err == CL_SUCCESS);
00167           VIENNACL_ERR_CHECK(err);
00168         }
00169         
00170         unsigned int _index;
00171         viennacl::ocl::handle<cl_mem> const & _mem_handle;
00172     }; //entry_proxy
00173     
00174 }
00175 
00176 #endif

Generated on Sat May 21 2011 20:36:50 for ViennaCL - The Vienna Computing Library by  doxygen 1.7.1