ViennaCL - The Vienna Computing Library  1.2.0
inner_prod.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_INNER_PROD_HPP_
2 #define VIENNACL_LINALG_INNER_PROD_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #include "viennacl/forwards.h"
25 #include "viennacl/tools/tools.hpp"
27 #include "viennacl/meta/tag_of.hpp"
28 
29 namespace viennacl
30 {
31  //
32  // generic inner_prod function
33  // uses tag dispatch to identify which algorithm
34  // should be called
35  //
36  namespace linalg
37  {
38 
39  #ifdef VIENNACL_HAVE_EIGEN
40  // ----------------------------------------------------
41  // EIGEN
42  //
43  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
44  float
45  inner_prod(Eigen::VectorXf const & v1,
46  Eigen::VectorXf const & v2)
47  {
48  return v1 * v2;
49  }
50 
51  double
52  inner_prod(Eigen::VectorXd const & v1,
53  Eigen::VectorXd const & v2)
54  {
55  return v1 * v2;
56  }
57 
58  #else
59  template< typename VectorT1, typename VectorT2 >
60  typename VectorT1::RealScalar
61  inner_prod(VectorT1 const& v1, VectorT2 const& v2,
63  >::type* dummy = 0)
64  {
65  //std::cout << "eigen .. " << std::endl;
66  return v1.dot(v2);
67  }
68  #endif
69  #endif
70 
71  #ifdef VIENNACL_HAVE_MTL4
72  // ----------------------------------------------------
73  // MTL4
74  //
75  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
76  template <typename ScalarType>
77  ScalarType inner_prod(mtl::dense_vector<ScalarType> const & v1,
78  mtl::dense_vector<ScalarType> const & v2)
79  {
80  return mtl::dot(v1, v2);
81  }
82  #else
83  template< typename VectorT1, typename VectorT2 >
84  typename VectorT1::value_type
85  inner_prod(VectorT1 const& v1, VectorT2 const& v2,
87  >::type* dummy = 0)
88  {
89  //std::cout << "mtl4 .. " << std::endl;
90  return mtl::dot(v1, v2);
91  }
92  #endif
93  #endif
94 
95  #ifdef VIENNACL_HAVE_UBLAS
96  // ----------------------------------------------------
97  // UBLAS
98  //
99  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
100  template< typename ScalarType >
101  ScalarType
102  inner_prod(boost::numeric::ublas::vector<ScalarType> const & v1,
103  boost::numeric::ublas::vector<ScalarType> const & v2)
104  {
105  // std::cout << "ublas .. " << std::endl;
106  return boost::numeric::ublas::inner_prod(v1, v2);
107  }
108  #else
109  template< typename VectorT1, typename VectorT2 >
110  typename VectorT1::value_type
111  inner_prod(VectorT1 const& v1, VectorT2 const& v2,
113  >::type* dummy = 0)
114  {
115  //std::cout << "ublas .. " << std::endl;
116  return boost::numeric::ublas::inner_prod(v1, v2);
117  }
118  #endif
119  #endif
120 
121  // ----------------------------------------------------
122  // STL
123  //
124  template< typename VectorT1, typename VectorT2 >
125  typename VectorT1::value_type
126  inner_prod(VectorT1 const& v1, VectorT2 const& v2,
128  >::type* dummy = 0)
129  {
130  assert(v1.size() == v2.size());
131  //std::cout << "stl .. " << std::endl;
132  typename VectorT1::value_type result = 0;
133  for (typename VectorT1::size_type i=0; i<v1.size(); ++i)
134  result += v1[i] * v2[i];
135 
136  return result;
137  }
138 
139  // ----------------------------------------------------
140  // VIENNACL
141  //
142  template< typename ScalarType, unsigned int alignment1, unsigned int alignment2 >
145  viennacl::op_inner_prod >
148  >::type* dummy = 0)
149  {
150  //std::cout << "viennacl .. " << std::endl;
151  return viennacl::linalg::inner_prod_impl(vector1, vector2);
152  }
153  } // end namespace linalg
154 } // end namespace viennacl
155 #endif
156 
157