ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include "viennacl/forwards.h"
26 #include "viennacl/tools/tools.hpp"
28 #include "viennacl/meta/tag_of.hpp"
30 
31 namespace viennacl
32 {
33 //
34 // generic inner_prod function
35 // uses tag dispatch to identify which algorithm
36 // should be called
37 //
38 namespace linalg
39 {
40 
41 #ifdef VIENNACL_WITH_EIGEN
42 // ----------------------------------------------------
43 // EIGEN
44 //
45 template<typename VectorT1, typename VectorT2>
47  typename VectorT1::RealScalar>::type
48 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
49 {
50  //std::cout << "eigen .. " << std::endl;
51  return v1.dot(v2);
52 }
53 #endif
54 
55 #ifdef VIENNACL_WITH_MTL4
56 // ----------------------------------------------------
57 // MTL4
58 //
59 template<typename VectorT1, typename VectorT2>
61  typename VectorT1::value_type>::type
62 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
63 {
64  //std::cout << "mtl4 .. " << std::endl;
65  return mtl::dot(v1, v2);
66 }
67 #endif
68 
69 #ifdef VIENNACL_WITH_UBLAS
70 // ----------------------------------------------------
71 // UBLAS
72 //
73 template<typename VectorT1, typename VectorT2>
75  typename VectorT1::value_type>::type
76 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
77 {
78  //std::cout << "ublas .. " << std::endl;
79  return boost::numeric::ublas::inner_prod(v1, v2);
80 }
81 #endif
82 
83 // ----------------------------------------------------
84 // STL
85 //
86 template<typename VectorT1, typename VectorT2>
88  typename VectorT1::value_type>::type
89 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
90 {
91  assert(v1.size() == v2.size() && bool("Vector sizes mismatch"));
92  //std::cout << "stl .. " << std::endl;
93  typename VectorT1::value_type result = 0;
94  for (typename VectorT1::size_type i=0; i<v1.size(); ++i)
95  result += v1[i] * v2[i];
96 
97  return result;
98 }
99 
100 // ----------------------------------------------------
101 // VIENNACL
102 //
103 template<typename NumericT>
106  vector_base<NumericT> const & vector2)
107 {
108  //std::cout << "viennacl .. " << std::endl;
110  const vector_base<NumericT>,
111  viennacl::op_inner_prod >(vector1, vector2);
112 }
113 
114 
115 // expression on lhs:
116 template< typename LHS, typename RHS, typename OP, typename NumericT>
118  const vector_base<NumericT>,
121  vector_base<NumericT> const & vector2)
122 {
123  //std::cout << "viennacl .. " << std::endl;
125  const vector_base<NumericT>,
126  viennacl::op_inner_prod >(vector1, vector2);
127 }
128 
129 // expression on rhs:
130 template<typename NumericT, typename LHS, typename RHS, typename OP>
136 {
137  //std::cout << "viennacl .. " << std::endl;
140  viennacl::op_inner_prod >(vector1, vector2);
141 }
142 
143 // expression on lhs and rhs:
144 template<typename LHS1, typename RHS1, typename OP1,
145  typename LHS2, typename RHS2, typename OP2>
151 {
152  //std::cout << "viennacl .. " << std::endl;
155  viennacl::op_inner_prod >(vector1, vector2);
156 }
157 
158 
159 // Multiple inner products:
160 template<typename NumericT>
163  vector_tuple<NumericT> const & y_tuple)
164 {
167  viennacl::op_inner_prod >(x, y_tuple);
168 }
169 
170 
171 } // end namespace linalg
172 } // end namespace viennacl
173 #endif
174 
175 
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
Various little tools used here and there in ViennaCL.
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:89
viennacl::vector_expression< const vector_base< NumericT >, const vector_tuple< NumericT >, viennacl::op_inner_prod > inner_prod(vector_base< NumericT > const &x, vector_tuple< NumericT > const &y_tuple)
Definition: inner_prod.hpp:162
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:229
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:238
viennacl::vector< float > v1
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Definition: forwards.h:268
viennacl::vector< int > v2
A tag class representing inner products of two vectors.
Definition: forwards.h:197
A collection of compile time type deductions.
Simple enable-if variant that uses the SFINAE pattern.