|
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_INNERPROD_HPP_ 00016 #define _VIENNACL_INNERPROD_HPP_ 00017 00022 #include "viennacl/forwards.h" 00023 #include "tag_of.hpp" 00024 00025 namespace viennacl 00026 { 00027 // 00028 // generic inner_prod function 00029 // uses tag dispatch to identify which algorithm 00030 // should be called 00031 // 00032 namespace linalg 00033 { 00034 00035 #ifdef VIENNACL_HAVE_EIGEN 00036 // ---------------------------------------------------- 00037 // EIGEN 00038 // 00039 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment 00040 float 00041 inner_prod(Eigen::VectorXf const & v1, 00042 Eigen::VectorXf const & v2) 00043 { 00044 return v1 * v2; 00045 } 00046 00047 double 00048 inner_prod(Eigen::VectorXd const & v1, 00049 Eigen::VectorXd const & v2) 00050 { 00051 return v1 * v2; 00052 } 00053 00054 #else 00055 template< typename VectorT1, typename VectorT2 > 00056 typename VectorT1::RealScalar 00057 inner_prod(VectorT1 const& v1, VectorT2 const& v2, 00058 typename viennacl::tools::enable_if< viennacl::is_eigen< typename viennacl::traits::tag_of< VectorT1 >::type >::value 00059 >::type* dummy = 0) 00060 { 00061 //std::cout << "eigen .. " << std::endl; 00062 return v1.dot(v2); 00063 } 00064 #endif 00065 #endif 00066 00067 #ifdef VIENNACL_HAVE_MTL4 00068 // ---------------------------------------------------- 00069 // MTL4 00070 // 00071 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment 00072 template <typename ScalarType> 00073 ScalarType inner_prod(mtl::dense_vector<ScalarType> const & v1, 00074 mtl::dense_vector<ScalarType> const & v2) 00075 { 00076 return mtl::dot(v1, v2); 00077 } 00078 #else 00079 template< typename VectorT1, typename VectorT2 > 00080 typename VectorT1::value_type 00081 inner_prod(VectorT1 const& v1, VectorT2 const& v2, 00082 typename viennacl::tools::enable_if< viennacl::is_mtl4< typename viennacl::traits::tag_of< VectorT1 >::type >::value 00083 >::type* dummy = 0) 00084 { 00085 //std::cout << "mtl4 .. " << std::endl; 00086 return mtl::dot(v1, v2); 00087 } 00088 #endif 00089 #endif 00090 00091 #ifdef VIENNACL_HAVE_UBLAS 00092 // ---------------------------------------------------- 00093 // UBLAS 00094 // 00095 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment 00096 template< typename ScalarType > 00097 ScalarType 00098 inner_prod(boost::numeric::ublas::vector<ScalarType> const & v1, 00099 boost::numeric::ublas::vector<ScalarType> const & v2) 00100 { 00101 // std::cout << "ublas .. " << std::endl; 00102 return boost::numeric::ublas::inner_prod(v1, v2); 00103 } 00104 #else 00105 template< typename VectorT1, typename VectorT2 > 00106 typename VectorT1::value_type 00107 inner_prod(VectorT1 const& v1, VectorT2 const& v2, 00108 typename viennacl::tools::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< VectorT1 >::type >::value 00109 >::type* dummy = 0) 00110 { 00111 //std::cout << "ublas .. " << std::endl; 00112 return boost::numeric::ublas::inner_prod(v1, v2); 00113 } 00114 #endif 00115 #endif 00116 00117 // ---------------------------------------------------- 00118 // STL 00119 // 00120 template< typename VectorT1, typename VectorT2 > 00121 typename VectorT1::value_type 00122 inner_prod(VectorT1 const& v1, VectorT2 const& v2, 00123 typename viennacl::tools::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value 00124 >::type* dummy = 0) 00125 { 00126 assert(v1.size() == v2.size()); 00127 //std::cout << "stl .. " << std::endl; 00128 typename VectorT1::value_type result = 0; 00129 for (typename VectorT1::size_type i=0; i<v1.size(); ++i) 00130 result += v1[i] * v2[i]; 00131 00132 return result; 00133 } 00134 00135 // ---------------------------------------------------- 00136 // VIENNACL 00137 // 00138 template< typename ScalarType, unsigned int alignment1, unsigned int alignment2 > 00139 viennacl::scalar_expression< const viennacl::vector<ScalarType, alignment1>, 00140 const viennacl::vector<ScalarType, alignment2>, 00141 viennacl::op_inner_prod > 00142 inner_prod(viennacl::vector<ScalarType, alignment1> const & vector1, viennacl::vector<ScalarType, alignment2> const & vector2, 00143 typename viennacl::tools::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< viennacl::vector<ScalarType, alignment1> >::type >::value 00144 >::type* dummy = 0) 00145 { 00146 //std::cout << "viennacl .. " << std::endl; 00147 return viennacl::linalg::inner_prod_impl(vector1, vector2); 00148 } 00149 } // end namespace linalg 00150 } // end namespace viennacl 00151 #endif 00152 00153
1.7.6.1