ViennaCL - The Vienna Computing Library  1.5.1
prod.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_PROD_HPP_
2 #define VIENNACL_LINALG_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 
27 #include "viennacl/forwards.h"
28 #include "viennacl/tools/tools.hpp"
30 #include "viennacl/meta/tag_of.hpp"
31 #include <vector>
32 #include <map>
33 
34 namespace viennacl
35 {
36  //
37  // generic prod function
38  // uses tag dispatch to identify which algorithm
39  // should be called
40  //
41  namespace linalg
42  {
43  #ifdef VIENNACL_WITH_MTL4
44  // ----------------------------------------------------
45  // mtl4
46  //
47  template< typename MatrixT, typename VectorT >
49  VectorT>::type
50  prod(MatrixT const& matrix, VectorT const& vector)
51  {
52  return VectorT(matrix * vector);
53  }
54  #endif
55 
56  #ifdef VIENNACL_WITH_EIGEN
57  // ----------------------------------------------------
58  // Eigen
59  //
60  template< typename MatrixT, typename VectorT >
62  VectorT>::type
63  prod(MatrixT const& matrix, VectorT const& vector)
64  {
65  return matrix * vector;
66  }
67  #endif
68 
69  #ifdef VIENNACL_WITH_UBLAS
70  // ----------------------------------------------------
71  // UBLAS
72  //
73  template< typename MatrixT, typename VectorT >
75  VectorT>::type
76  prod(MatrixT const& matrix, VectorT const& vector)
77  {
78  // std::cout << "ublas .. " << std::endl;
79  return boost::numeric::ublas::prod(matrix, vector);
80  }
81  #endif
82 
83 
84  // ----------------------------------------------------
85  // STL type
86  //
87 
88  // dense matrix-vector product:
89  template< typename T, typename A1, typename A2, typename VectorT >
90  VectorT
91  prod(std::vector< std::vector<T, A1>, A2 > const & matrix, VectorT const& vector)
92  {
93  VectorT result(matrix.size());
94  for (typename std::vector<T, A1>::size_type i=0; i<matrix.size(); ++i)
95  {
96  result[i] = 0; //we will not assume that VectorT is initialized to zero
97  for (typename std::vector<T, A1>::size_type j=0; j<matrix[i].size(); ++j)
98  result[i] += matrix[i][j] * vector[j];
99  }
100  return result;
101  }
102 
103  // sparse matrix-vector product:
104  template< typename KEY, typename DATA, typename COMPARE, typename AMAP, typename AVEC, typename VectorT >
105  VectorT
106  prod(std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > const& matrix, VectorT const& vector)
107  {
108  typedef std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > MatrixType;
109 
110  VectorT result(matrix.size());
111  for (typename MatrixType::size_type i=0; i<matrix.size(); ++i)
112  {
113  result[i] = 0; //we will not assume that VectorT is initialized to zero
114  for (typename std::map<KEY, DATA, COMPARE, AMAP>::const_iterator row_entries = matrix[i].begin();
115  row_entries != matrix[i].end();
116  ++row_entries)
117  result[i] += row_entries->second * vector[row_entries->first];
118  }
119  return result;
120  }
121 
122 
123  /*template< typename MatrixT, typename VectorT >
124  VectorT
125  prod(MatrixT const& matrix, VectorT const& vector,
126  typename viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< MatrixT >::type >::value
127  >::type* dummy = 0)
128  {
129  // std::cout << "std .. " << std::endl;
130  return prod_impl(matrix, vector);
131  }*/
132 
133  // ----------------------------------------------------
134  // VIENNACL
135  //
136 
137  // standard product:
138  template< typename NumericT, typename F1, typename F2>
144  {
145  // std::cout << "viennacl .. " << std::endl;
149  }
150 
151  // right factor is transposed:
152  template< typename NumericT, typename F1, typename F2>
156  op_trans>,
161  op_trans> const & B)
162  {
163  // std::cout << "viennacl .. " << std::endl;
167  op_trans>,
169  }
170 
171  // left factor transposed:
172  template< typename NumericT, typename F1, typename F2>
175  op_trans>,
180  op_trans> const & A,
182  {
183  // std::cout << "viennacl .. " << std::endl;
186  op_trans>,
189  }
190 
191 
192  // both factors transposed:
193  template< typename NumericT, typename F1, typename F2>
196  op_trans>,
199  op_trans>,
203  op_trans> const & A,
206  op_trans> const & B)
207  {
208  // std::cout << "viennacl .. " << std::endl;
211  op_trans>,
214  op_trans>,
216  }
217 
218 
219 
220  // matrix-vector product
221  template< typename NumericT, typename F>
227  {
228  // std::cout << "viennacl .. " << std::endl;
231  viennacl::op_prod >(matrix, vector);
232  }
233 
234  // transposed matrix-vector product
235  template< typename NumericT, typename F>
238  op_trans>,
243  op_trans> const & matrix,
245  {
246  // std::cout << "viennacl .. " << std::endl;
249  op_trans>,
251  viennacl::op_prod >(matrix, vector);
252  }
253 
254 
255  template<typename SparseMatrixType, class SCALARTYPE>
257  vector_expression<const SparseMatrixType,
259  op_prod >
260  >::type
261  prod(const SparseMatrixType & mat,
262  const vector_base<SCALARTYPE> & vec)
263  {
264  return vector_expression<const SparseMatrixType,
266  op_prod >(mat, vec);
267  }
268 
269  template< typename SparseMatrixType, typename SCALARTYPE, typename F1>
271  viennacl::matrix_expression<const SparseMatrixType,
273  op_prod >
274  >::type
275  prod(const SparseMatrixType & sp_mat,
277  {
278  return viennacl::matrix_expression<const SparseMatrixType,
280  op_prod >(sp_mat, d_mat);
281  }
282 
283  // right factor is transposed
284  template< typename SparseMatrixType, typename SCALARTYPE, typename F1 >
286  viennacl::matrix_expression< const SparseMatrixType,
289  op_trans>,
291  >::type
292  prod(const SparseMatrixType & A,
295  op_trans> const & B)
296  {
297  return viennacl::matrix_expression< const SparseMatrixType,
300  op_trans>,
301  viennacl::op_prod >(A, B);
302  }
303 
304  template<typename StructuredMatrixType, class SCALARTYPE>
306  vector_expression<const StructuredMatrixType,
308  op_prod >
309  >::type
310  prod(const StructuredMatrixType & mat,
311  const vector_base<SCALARTYPE> & vec)
312  {
313  return vector_expression<const StructuredMatrixType,
315  op_prod >(mat, vec);
316  }
317 
318  } // end namespace linalg
319 } // end namespace viennacl
320 #endif
321 
322 
323 
324 
325 
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:29
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
Various little tools used here and there in ViennaCL.
viennacl::enable_if< viennacl::is_any_dense_structured_matrix< StructuredMatrixType >::value, vector_expression< const StructuredMatrixType, const vector_base< SCALARTYPE >, op_prod > >::type prod(const StructuredMatrixType &mat, const vector_base< SCALARTYPE > &vec)
Definition: prod.hpp:310
A dense matrix class.
Definition: forwards.h:290
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:283
This file provides the forward declarations for the main types used within ViennaCL.
A dense matrix class.
Definition: forwards.h:293
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:181
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
Common base class for dense vectors, vector ranges, and vector slices.
Definition: forwards.h:205
A tag class representing matrix-matrix products.
Definition: forwards.h:78
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
A tag class representing matrix-vector products and element-wise multiplications. ...
Definition: forwards.h:76
A tag class representing transposed matrices.
Definition: forwards.h:165
Simple enable-if variant that uses the SFINAE pattern.