ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-2015, 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 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_ARMADILLO
42 // ----------------------------------------------------
43 // Armadillo
44 //
45 template<typename NumericT>
46 NumericT inner_prod(arma::Col<NumericT> const& v1, arma::Col<NumericT> const& v2)
47 {
48  return dot(v1, v2);
49 }
50 #endif
51 
52 #ifdef VIENNACL_WITH_EIGEN
53 // ----------------------------------------------------
54 // EIGEN
55 //
56 template<typename VectorT1, typename VectorT2>
58  typename VectorT1::RealScalar>::type
59 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
60 {
61  //std::cout << "eigen .. " << std::endl;
62  return v1.dot(v2);
63 }
64 #endif
65 
66 #ifdef VIENNACL_WITH_MTL4
67 // ----------------------------------------------------
68 // MTL4
69 //
70 template<typename VectorT1, typename VectorT2>
72  typename VectorT1::value_type>::type
73 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
74 {
75  //std::cout << "mtl4 .. " << std::endl;
76  return mtl::dot(v1, v2);
77 }
78 #endif
79 
80 #ifdef VIENNACL_WITH_UBLAS
81 // ----------------------------------------------------
82 // UBLAS
83 //
84 template<typename VectorT1, typename VectorT2>
86  typename VectorT1::value_type>::type
87 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
88 {
89  //std::cout << "ublas .. " << std::endl;
90  return boost::numeric::ublas::inner_prod(v1, v2);
91 }
92 #endif
93 
94 // ----------------------------------------------------
95 // STL
96 //
97 template<typename VectorT1, typename VectorT2>
99  typename VectorT1::value_type>::type
100 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
101 {
102  assert(v1.size() == v2.size() && bool("Vector sizes mismatch"));
103  //std::cout << "stl .. " << std::endl;
104  typename VectorT1::value_type result = 0;
105  for (typename VectorT1::size_type i=0; i<v1.size(); ++i)
106  result += v1[i] * v2[i];
107 
108  return result;
109 }
110 
111 // ----------------------------------------------------
112 // VIENNACL
113 //
114 template<typename NumericT>
116 inner_prod(vector_base<NumericT> const & vector1,
117  vector_base<NumericT> const & vector2)
118 {
119  //std::cout << "viennacl .. " << std::endl;
121  const vector_base<NumericT>,
122  viennacl::op_inner_prod >(vector1, vector2);
123 }
124 
125 
126 // expression on lhs:
127 template< typename LHS, typename RHS, typename OP, typename NumericT>
129  const vector_base<NumericT>,
132  vector_base<NumericT> const & vector2)
133 {
134  //std::cout << "viennacl .. " << std::endl;
136  const vector_base<NumericT>,
137  viennacl::op_inner_prod >(vector1, vector2);
138 }
139 
140 // expression on rhs:
141 template<typename NumericT, typename LHS, typename RHS, typename OP>
145 inner_prod(vector_base<NumericT> const & vector1,
146  viennacl::vector_expression<LHS, RHS, OP> const & vector2)
147 {
148  //std::cout << "viennacl .. " << std::endl;
150  const viennacl::vector_expression<LHS, RHS, OP>,
151  viennacl::op_inner_prod >(vector1, vector2);
152 }
153 
154 // expression on lhs and rhs:
155 template<typename LHS1, typename RHS1, typename OP1,
156  typename LHS2, typename RHS2, typename OP2>
161  viennacl::vector_expression<LHS2, RHS2, OP2> const & vector2)
162 {
163  //std::cout << "viennacl .. " << std::endl;
164  return viennacl::scalar_expression< const viennacl::vector_expression<LHS1, RHS1, OP1>,
165  const viennacl::vector_expression<LHS2, RHS2, OP2>,
166  viennacl::op_inner_prod >(vector1, vector2);
167 }
168 
169 
170 // Multiple inner products:
171 template<typename NumericT>
173 inner_prod(vector_base<NumericT> const & x,
174  vector_tuple<NumericT> const & y_tuple)
175 {
177  const vector_tuple<NumericT>,
178  viennacl::op_inner_prod >(x, y_tuple);
179 }
180 
181 
182 } // end namespace linalg
183 } // end namespace viennacl
184 #endif
185 
186 
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:100
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:173
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
float NumericT
Definition: bisect.cpp:40
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
viennacl::vector< float > v1
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Definition: forwards.h:269
viennacl::vector< int > v2
A tag class representing inner products of two vectors.
Definition: forwards.h:198
A collection of compile time type deductions.
Simple enable-if variant that uses the SFINAE pattern.