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

/data/development/ViennaCL/dev/viennacl/vector_proxy.hpp

Go to the documentation of this file.
00001 #ifndef VIENNACL_VECTOR_PROXY_HPP_
00002 #define VIENNACL_VECTOR_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 
00024 #include "viennacl/forwards.h"
00025 #include "viennacl/range.hpp"
00026 #include "viennacl/vector.hpp"
00027 
00028 namespace viennacl
00029 {
00030 
00031   template <typename VectorType>
00032   class vector_range
00033   {
00034       typedef vector_range<VectorType>            self_type;
00035     
00036     public:
00037       typedef typename VectorType::value_type     value_type;
00038       typedef range::size_type                    size_type;
00039       typedef range::difference_type              difference_type;
00040       typedef value_type                          reference;
00041       typedef const value_type &                  const_reference;
00042       
00043       static const int alignment = VectorType::alignment;
00044       
00045       vector_range(VectorType & v, 
00046                    range const & entry_range) : v_(v), entry_range_(entry_range) {}
00047                    
00048       size_type start() const { return entry_range_.start(); }
00049       size_type size() const { return entry_range_.size(); }
00050 
00051       template <typename LHS, typename RHS, typename OP>
00052       self_type & operator = (const vector_expression< LHS,
00053                                                        RHS,
00054                                                        OP > & proxy) 
00055       {
00056         assert( false && "Not implemented!");
00057         return *this;
00058       }      
00059       
00060       self_type & operator += (self_type const & other)
00061       {
00062         viennacl::linalg::inplace_add(*this, other);
00063         return *this;
00064       }
00065       
00066 
00067       //const_reference operator()(size_type i, size_type j) const { return A_(start1() + i, start2() + i); }
00068       //reference operator()(size_type i, size_type j) { return A_(start1() + i, start2() + i); }
00069 
00070       VectorType & get() { return v_; }
00071       const VectorType & get() const { return v_; }
00072 
00073     private:
00074       VectorType & v_;
00075       range entry_range_;
00076   };
00077 
00078   
00079   template<typename VectorType>
00080   std::ostream & operator<<(std::ostream & s, vector_range<VectorType> const & proxy)
00081   {
00082     typedef typename VectorType::value_type   ScalarType;
00083     std::vector<ScalarType> temp(proxy.size());
00084     viennacl::copy(proxy, temp);
00085     
00086     //instead of printing 'temp' directly, let's reuse the existing functionality for viennacl::vector. It certainly adds overhead, but printing a vector is typically not about performance...
00087     VectorType temp2(temp.size());
00088     viennacl::copy(temp, temp2);
00089     s << temp2;
00090     return s;
00091   }
00092   
00093   
00094   
00095   
00099   
00100   //row_major:
00101   template <typename VectorType, typename SCALARTYPE>
00102   void copy(const VectorType & cpu_vector,
00103             vector_range<vector<SCALARTYPE> > & gpu_vector_range )
00104   {
00105     assert(cpu_vector.end() - cpu_vector.begin() >= 0);
00106     
00107     if (cpu_vector.end() - cpu_vector.begin() > 0)
00108     {
00109       //we require that the size of the gpu_vector is larger or equal to the cpu-size
00110       std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
00111       std::copy(cpu_vector.begin(), cpu_vector.end(), temp_buffer.begin());
00112       cl_int err = clEnqueueWriteBuffer(viennacl::ocl::get_queue().handle(),
00113                                         gpu_vector_range.get().handle(), CL_TRUE, sizeof(SCALARTYPE)*gpu_vector_range.start(),
00114                                         sizeof(SCALARTYPE)*temp_buffer.size(),
00115                                         &(temp_buffer[0]), 0, NULL, NULL);
00116       VIENNACL_ERR_CHECK(err);
00117     }
00118   }
00119   
00120 
00124   
00125 
00126   template <typename VectorType, typename SCALARTYPE>
00127   void copy(vector_range<vector<SCALARTYPE> > const & gpu_vector_range,
00128             VectorType & cpu_vector)
00129   {
00130     assert(cpu_vector.end() - cpu_vector.begin() >= 0);
00131     
00132     if (cpu_vector.end() > cpu_vector.begin())
00133     {
00134       std::vector<SCALARTYPE> temp_buffer(cpu_vector.end() - cpu_vector.begin());
00135       cl_int err = clEnqueueReadBuffer(viennacl::ocl::get_queue().handle(),
00136                                         gpu_vector_range.get().handle(), CL_TRUE, sizeof(SCALARTYPE)*gpu_vector_range.start(), 
00137                                         sizeof(SCALARTYPE)*temp_buffer.size(),
00138                                         &(temp_buffer[0]), 0, NULL, NULL);
00139       VIENNACL_ERR_CHECK(err);
00140       viennacl::ocl::get_queue().finish();
00141       
00142       //now copy entries to cpu_vec:
00143       std::copy(temp_buffer.begin(), temp_buffer.end(), cpu_vector.begin());
00144     }
00145   }
00146 
00147 
00148 }
00149 
00150 #endif

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