ViennaCL - The Vienna Computing Library
1.5.1
|
00001 #ifndef VIENNACL_LINALG_HOST_BASED_COMMON_HPP_ 00002 #define VIENNACL_LINALG_HOST_BASED_COMMON_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 00025 #include "viennacl/traits/handle.hpp" 00026 00027 namespace viennacl 00028 { 00029 namespace linalg 00030 { 00031 namespace host_based 00032 { 00033 namespace detail 00034 { 00035 template <typename T, typename VectorType> 00036 T * extract_raw_pointer(VectorType & vec) 00037 { 00038 return reinterpret_cast<T *>(viennacl::traits::ram_handle(vec).get()); 00039 } 00040 00041 template <typename T, typename VectorType> 00042 T const * extract_raw_pointer(VectorType const & vec) 00043 { 00044 return reinterpret_cast<T const *>(viennacl::traits::ram_handle(vec).get()); 00045 } 00046 00048 template <typename NumericT> 00049 class vector_array_wrapper 00050 { 00051 public: 00052 typedef NumericT value_type; 00053 00054 vector_array_wrapper(value_type * A, 00055 vcl_size_t start, 00056 vcl_size_t inc) 00057 : A_(A), 00058 start_(start), 00059 inc_(inc) {} 00060 00061 value_type & operator()(vcl_size_t i) 00062 { 00063 return A_[i * inc_ + start_]; 00064 } 00065 00066 private: 00067 value_type * A_; 00068 vcl_size_t start_; 00069 vcl_size_t inc_; 00070 }; 00071 00072 00073 inline bool is_row_major(viennacl::row_major_tag) { return true; } 00074 inline bool is_row_major(viennacl::column_major_tag) { return false; } 00075 00077 template <typename T> 00078 struct majority_struct_for_orientation 00079 { 00080 typedef typename T::ERROR_UNRECOGNIZED_MAJORITY_CATEGORTY_TAG type; 00081 }; 00082 00084 template <> 00085 struct majority_struct_for_orientation<viennacl::row_major_tag> 00086 { 00087 typedef viennacl::row_major type; 00088 }; 00089 00090 template <> 00091 struct majority_struct_for_orientation<viennacl::column_major_tag> 00092 { 00093 typedef viennacl::column_major type; 00094 }; 00099 template <typename NumericT, typename MajorityCategory, bool is_transposed> 00100 class matrix_array_wrapper 00101 { 00102 typedef typename majority_struct_for_orientation<MajorityCategory>::type F; 00103 00104 public: 00105 typedef NumericT value_type; 00106 00107 matrix_array_wrapper(value_type * A, 00108 vcl_size_t start1, vcl_size_t start2, 00109 vcl_size_t inc1, vcl_size_t inc2, 00110 vcl_size_t internal_size1, vcl_size_t internal_size2) 00111 : A_(A), 00112 start1_(start1), start2_(start2), 00113 inc1_(inc1), inc2_(inc2), 00114 internal_size1_(internal_size1), internal_size2_(internal_size2) {} 00115 00116 value_type & operator()(vcl_size_t i, vcl_size_t j) 00117 { 00118 return A_[F::mem_index(i * inc1_ + start1_, j * inc2_ + start2_, internal_size1_, internal_size2_)]; 00119 } 00120 00121 private: 00122 value_type * A_; 00123 vcl_size_t start1_, start2_; 00124 vcl_size_t inc1_, inc2_; 00125 vcl_size_t internal_size1_, internal_size2_; 00126 }; 00127 00129 template <typename NumericT, typename MajorityCategory> 00130 class matrix_array_wrapper<NumericT, MajorityCategory, true> 00131 { 00132 typedef typename majority_struct_for_orientation<MajorityCategory>::type F; 00133 00134 public: 00135 typedef NumericT value_type; 00136 00137 matrix_array_wrapper(value_type * A, 00138 vcl_size_t start1, vcl_size_t start2, 00139 vcl_size_t inc1, vcl_size_t inc2, 00140 vcl_size_t internal_size1, vcl_size_t internal_size2) 00141 : A_(A), 00142 start1_(start1), start2_(start2), 00143 inc1_(inc1), inc2_(inc2), 00144 internal_size1_(internal_size1), internal_size2_(internal_size2) {} 00145 00146 value_type & operator()(vcl_size_t i, vcl_size_t j) 00147 { 00148 return A_[F::mem_index(j * inc1_ + start1_, i * inc2_ + start2_, internal_size1_, internal_size2_)]; //swapping row and column indices here 00149 } 00150 00151 private: 00152 value_type * A_; 00153 vcl_size_t start1_, start2_; 00154 vcl_size_t inc1_, inc2_; 00155 vcl_size_t internal_size1_, internal_size2_; 00156 }; 00159 } 00160 00161 } //namespace host_based 00162 } //namespace linalg 00163 } //namespace viennacl 00164 00165 00166 #endif