Go to the documentation of this file.00001 #ifndef VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00002 #define VIENNACL_TOOLS_ENTRY_PROXY_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "viennacl/forwards.h"
00026 #include "viennacl/ocl/backend.hpp"
00027 #include "viennacl/scalar.hpp"
00028
00029 namespace viennacl
00030 {
00031
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
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
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,
00118 _mem_handle,
00119 sizeof(SCALARTYPE) * other._index,
00120 sizeof(SCALARTYPE) * _index,
00121 sizeof(SCALARTYPE), 0, NULL, NULL);
00122 VIENNACL_ERR_CHECK(err);
00123 return *this;
00124 }
00125
00126
00127
00128
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
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
00170 VIENNACL_ERR_CHECK(err);
00171 }
00172
00173 unsigned int _index;
00174 viennacl::ocl::handle<cl_mem> const & _mem_handle;
00175 };
00176
00177 }
00178
00179 #endif