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

/data/development/ViennaCL/dev/viennacl/linalg/inner_prod.hpp

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

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