|
ViennaCL - The Vienna Computing Library
1.1.2
|
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 00015 #ifndef _VIENNACL_PROD_HPP_ 00016 #define _VIENNACL_PROD_HPP_ 00017 00024 #include "viennacl/forwards.h" 00025 #include "tag_of.hpp" 00026 #include <vector> 00027 #include <map> 00028 00029 namespace viennacl 00030 { 00031 // 00032 // generic prod function 00033 // uses tag dispatch to identify which algorithm 00034 // should be called 00035 // 00036 namespace linalg 00037 { 00038 #ifdef VIENNACL_HAVE_MTL4 00039 // ---------------------------------------------------- 00040 // mtl4 00041 // 00042 template< typename MatrixT, typename VectorT > 00043 VectorT 00044 prod(MatrixT const& matrix, VectorT const& vector, 00045 typename viennacl::tools::enable_if< viennacl::is_mtl4< typename viennacl::traits::tag_of< MatrixT >::type >::value 00046 >::type* dummy = 0) 00047 { 00048 // std::cout << "mtl4 .. " << std::endl; 00049 return VectorT(matrix * vector); 00050 } 00051 #endif 00052 00053 #ifdef VIENNACL_HAVE_EIGEN 00054 // ---------------------------------------------------- 00055 // Eigen 00056 // 00057 template< typename MatrixT, typename VectorT > 00058 VectorT 00059 prod(MatrixT const& matrix, VectorT const& vector, 00060 typename viennacl::tools::enable_if< viennacl::is_eigen< typename viennacl::traits::tag_of< MatrixT >::type >::value 00061 >::type* dummy = 0) 00062 { 00063 // std::cout << "ublas .. " << std::endl; 00064 return matrix * vector; 00065 } 00066 #endif 00067 00068 #ifdef VIENNACL_HAVE_UBLAS 00069 // ---------------------------------------------------- 00070 // UBLAS 00071 // 00072 template< typename MatrixT, typename VectorT > 00073 VectorT 00074 prod(MatrixT const& matrix, VectorT const& vector, 00075 typename viennacl::tools::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< MatrixT >::type >::value 00076 >::type* dummy = 0) 00077 { 00078 // std::cout << "ublas .. " << std::endl; 00079 return boost::numeric::ublas::prod(matrix, vector); 00080 } 00081 #endif 00082 00083 00084 // ---------------------------------------------------- 00085 // STL type 00086 // 00087 00088 // dense matrix-vector product: 00089 template< typename T, typename A1, typename A2, typename VectorT > 00090 VectorT 00091 prod_impl(std::vector< std::vector<T, A1>, A2 > const& matrix, VectorT const& vector) 00092 { 00093 VectorT result(matrix.size()); 00094 for (typename std::vector<T, A1>::size_type i=0; i<matrix.size(); ++i) 00095 { 00096 result[i] = 0; //we will not assume that VectorT is initialized to zero 00097 for (typename std::vector<T, A1>::size_type j=0; j<matrix[i].size(); ++j) 00098 result[i] += matrix[i][j] * vector[j]; 00099 } 00100 return result; 00101 } 00102 00103 // sparse matrix-vector product: 00104 template< typename KEY, typename DATA, typename COMPARE, typename AMAP, typename AVEC, typename VectorT > 00105 VectorT 00106 prod_impl(std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > const& matrix, VectorT const& vector) 00107 { 00108 typedef std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > MatrixType; 00109 00110 VectorT result(matrix.size()); 00111 for (typename MatrixType::size_type i=0; i<matrix.size(); ++i) 00112 { 00113 result[i] = 0; //we will not assume that VectorT is initialized to zero 00114 for (typename std::map<KEY, DATA, COMPARE, AMAP>::const_iterator row_entries = matrix[i].begin(); 00115 row_entries != matrix[i].end(); 00116 ++row_entries) 00117 result[i] += row_entries->second * vector[row_entries->first]; 00118 } 00119 return result; 00120 } 00121 00122 00123 template< typename MatrixT, typename VectorT > 00124 VectorT 00125 prod(MatrixT const& matrix, VectorT const& vector, 00126 typename viennacl::tools::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< MatrixT >::type >::value 00127 >::type* dummy = 0) 00128 { 00129 // std::cout << "std .. " << std::endl; 00130 return prod_impl(matrix, vector); 00131 } 00132 00133 // ---------------------------------------------------- 00134 // VIENNACL 00135 // 00136 template< typename MatrixT, typename NumericT, unsigned int ALIGNMENT > 00137 viennacl::vector_expression< const MatrixT, 00138 const viennacl::vector<NumericT, ALIGNMENT>, 00139 viennacl::op_prod > 00140 prod(MatrixT const& matrix, 00141 viennacl::vector<NumericT, ALIGNMENT> const& vector, 00142 typename viennacl::tools::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value 00143 >::type* dummy = 0) 00144 { 00145 // std::cout << "viennacl .. " << std::endl; 00146 return viennacl::linalg::prod_impl(matrix, vector); 00147 } 00148 00149 template< typename MatrixT, typename NumericT, typename F, unsigned int ALIGNMENT > 00150 viennacl::matrix_expression< const MatrixT, 00151 const viennacl::matrix<NumericT, F, ALIGNMENT>, 00152 viennacl::op_prod > 00153 prod(MatrixT const& matrix_A, 00154 viennacl::matrix<NumericT, F, ALIGNMENT> const& matrix_B, 00155 typename viennacl::tools::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value 00156 >::type* dummy = 0) 00157 { 00158 // std::cout << "viennacl .. " << std::endl; 00159 return viennacl::matrix_expression< const MatrixT, 00160 const viennacl::matrix<NumericT, F, ALIGNMENT>, 00161 viennacl::op_prod >(matrix_A, matrix_B); 00162 } 00163 00164 template< typename MatrixT, typename NumericT, typename F, unsigned int ALIGNMENT > 00165 viennacl::matrix_expression< const MatrixT, 00166 const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 00167 const viennacl::matrix<NumericT, F, ALIGNMENT>, 00168 viennacl::op_trans >, 00169 viennacl::op_prod > 00170 prod(MatrixT const& matrix_A, 00171 const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 00172 const viennacl::matrix<NumericT, F, ALIGNMENT>, 00173 viennacl::op_trans > & matrix_B, 00174 typename viennacl::tools::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value 00175 >::type* dummy = 0) 00176 { 00177 // std::cout << "viennacl .. " << std::endl; 00178 return viennacl::matrix_expression< const MatrixT, 00179 const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 00180 const viennacl::matrix<NumericT, F, ALIGNMENT>, 00181 viennacl::op_trans >, 00182 viennacl::op_prod >(matrix_A, matrix_B); 00183 //return viennacl::linalg::prod_impl(matrix_A, matrix_B); 00184 } 00185 00186 } // end namespace linalg 00187 } // end namespace viennacl 00188 #endif 00189 00190 00191 00192 00193
1.7.6.1