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

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

Go to the documentation of this file.
00001 #ifndef VIENNACL_LINALG_PROD_HPP_
00002 #define VIENNACL_LINALG_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 
00026 #include "viennacl/forwards.h"
00027 #include "viennacl/tools/tools.hpp"
00028 #include "viennacl/meta/enable_if.hpp"
00029 #include "viennacl/meta/tag_of.hpp"
00030 #include <vector>
00031 #include <map>
00032 
00033 namespace viennacl
00034 {
00035   //
00036   // generic prod function
00037   //   uses tag dispatch to identify which algorithm
00038   //   should be called 
00039   //
00040   namespace linalg 
00041   {
00042     #ifdef VIENNACL_HAVE_MTL4
00043     // ----------------------------------------------------
00044     // mtl4
00045     //
00046     template< typename MatrixT, typename VectorT >
00047     VectorT 
00048     prod(MatrixT const& matrix, VectorT const& vector, 
00049          typename viennacl::enable_if< viennacl::is_mtl4< typename viennacl::traits::tag_of< MatrixT >::type >::value
00050                                      >::type* dummy = 0)
00051     {
00052       // std::cout << "mtl4 .. " << std::endl;
00053       return VectorT(matrix * vector);
00054     }
00055     #endif
00056     
00057     #ifdef VIENNACL_HAVE_EIGEN
00058     // ----------------------------------------------------
00059     // Eigen
00060     //
00061     template< typename MatrixT, typename VectorT >
00062     VectorT 
00063     prod(MatrixT const& matrix, VectorT const& vector, 
00064          typename viennacl::enable_if< viennacl::is_eigen< typename viennacl::traits::tag_of< MatrixT >::type >::value
00065                                      >::type* dummy = 0)
00066     {
00067       // std::cout << "ublas .. " << std::endl;
00068       return matrix * vector;
00069     }
00070     #endif
00071     
00072     #ifdef VIENNACL_HAVE_UBLAS
00073     // ----------------------------------------------------
00074     // UBLAS
00075     //
00076     template< typename MatrixT, typename VectorT >
00077     VectorT 
00078     prod(MatrixT const& matrix, VectorT const& vector, 
00079          typename viennacl::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< MatrixT >::type >::value
00080                                      >::type* dummy = 0)
00081     {
00082       // std::cout << "ublas .. " << std::endl;
00083       return boost::numeric::ublas::prod(matrix, vector);
00084     }
00085     #endif
00086 
00087 
00088     // ----------------------------------------------------
00089     // STL type
00090     //
00091     
00092     // dense matrix-vector product:
00093     template< typename T, typename A1, typename A2, typename VectorT >
00094     VectorT 
00095     prod_impl(std::vector< std::vector<T, A1>, A2 > const& matrix, VectorT const& vector)
00096     {
00097       VectorT result(matrix.size());
00098       for (typename std::vector<T, A1>::size_type i=0; i<matrix.size(); ++i)
00099       {
00100         result[i] = 0; //we will not assume that VectorT is initialized to zero
00101         for (typename std::vector<T, A1>::size_type j=0; j<matrix[i].size(); ++j)
00102           result[i] += matrix[i][j] * vector[j];
00103       }
00104       return result;
00105     }
00106     
00107     // sparse matrix-vector product:
00108     template< typename KEY, typename DATA, typename COMPARE, typename AMAP, typename AVEC, typename VectorT >
00109     VectorT 
00110     prod_impl(std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > const& matrix, VectorT const& vector)
00111     {
00112       typedef std::vector< std::map<KEY, DATA, COMPARE, AMAP>, AVEC > MatrixType;
00113       
00114       VectorT result(matrix.size());
00115       for (typename MatrixType::size_type i=0; i<matrix.size(); ++i)
00116       { 
00117         result[i] = 0; //we will not assume that VectorT is initialized to zero
00118         for (typename std::map<KEY, DATA, COMPARE, AMAP>::const_iterator row_entries = matrix[i].begin();
00119              row_entries != matrix[i].end();
00120              ++row_entries)
00121           result[i] += row_entries->second * vector[row_entries->first];
00122       }
00123       return result;
00124     }
00125     
00126     
00127     template< typename MatrixT, typename VectorT >
00128     VectorT 
00129     prod(MatrixT const& matrix, VectorT const& vector, 
00130          typename viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< MatrixT >::type >::value
00131                                      >::type* dummy = 0)
00132     {
00133       // std::cout << "std .. " << std::endl;
00134       return prod_impl(matrix, vector);
00135     }
00136 
00137     // ----------------------------------------------------
00138     // VIENNACL
00139     //
00140     template< typename MatrixT1, typename MatrixT2 >
00141     viennacl::matrix_expression< const MatrixT1, 
00142                                  const viennacl::matrix_range<MatrixT2>,
00143                                  viennacl::op_prod >
00144     prod(MatrixT1 const& A,
00145          viennacl::matrix_range<MatrixT2> const& B, 
00146          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT1 >::type >::value
00147                                      >::type* dummy = 0)
00148     {
00149       // std::cout << "viennacl .. " << std::endl;
00150       return viennacl::matrix_expression< const MatrixT1, 
00151                                           const viennacl::matrix_range<MatrixT2>,
00152                                           viennacl::op_prod >(A, B);
00153     }
00154 
00155 
00156     template< typename MatrixT1, typename MatrixT2 >
00157     viennacl::matrix_expression< const MatrixT1, 
00158                                  const viennacl::matrix_expression<const viennacl::matrix_range<MatrixT2>,
00159                                                                    const viennacl::matrix_range<MatrixT2>,
00160                                                                    op_trans>,
00161                                  viennacl::op_prod >
00162     prod(MatrixT1 const & A,
00163          viennacl::matrix_expression<const viennacl::matrix_range<MatrixT2>,
00164                                      const viennacl::matrix_range<MatrixT2>,
00165                                      op_trans> const & B, 
00166          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT2 >::type >::value
00167                                      >::type* dummy = 0)
00168     {
00169       // std::cout << "viennacl .. " << std::endl;
00170       return viennacl::matrix_expression< const MatrixT1, 
00171                                           const viennacl::matrix_expression<const viennacl::matrix_range<MatrixT2>,
00172                                                                             const viennacl::matrix_range<MatrixT2>,
00173                                                                             op_trans>,
00174                                           viennacl::op_prod >(A, B);
00175     }
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183     template< typename MatrixT, typename NumericT, unsigned int ALIGNMENT >
00184     viennacl::vector_expression< const MatrixT, 
00185                                  const viennacl::vector<NumericT, ALIGNMENT>,
00186                                  viennacl::op_prod >
00187     prod(MatrixT const& matrix,
00188          viennacl::vector<NumericT, ALIGNMENT> const& vector, 
00189          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value
00190                                      >::type* dummy = 0)
00191     {
00192       // std::cout << "viennacl .. " << std::endl;
00193       return viennacl::linalg::prod_impl(matrix, vector);
00194     }
00195 
00196     template< typename MatrixT, typename NumericT, typename F, unsigned int ALIGNMENT >
00197     viennacl::matrix_expression< const MatrixT, 
00198                                  const viennacl::matrix<NumericT, F, ALIGNMENT>,
00199                                  viennacl::op_prod >
00200     prod(MatrixT const& matrix_A,
00201          viennacl::matrix<NumericT, F, ALIGNMENT> const& matrix_B, 
00202          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value
00203                                      >::type* dummy = 0)
00204     {
00205       // std::cout << "viennacl .. " << std::endl;
00206       return viennacl::matrix_expression< const MatrixT, 
00207                                           const viennacl::matrix<NumericT, F, ALIGNMENT>,
00208                                           viennacl::op_prod >(matrix_A, matrix_B);
00209     }
00210 
00211     template< typename MatrixT, typename NumericT, typename F, unsigned int ALIGNMENT >
00212     viennacl::matrix_expression< const MatrixT, 
00213                                  const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 
00214                                                                     const viennacl::matrix<NumericT, F, ALIGNMENT>,
00215                                                                     viennacl::op_trans >,
00216                                  viennacl::op_prod >
00217     prod(MatrixT const& matrix_A,
00218          const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 
00219                                             const viennacl::matrix<NumericT, F, ALIGNMENT>,
00220                                             viennacl::op_trans > & matrix_B,
00221          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< MatrixT >::type >::value
00222                                      >::type* dummy = 0)
00223     {
00224       // std::cout << "viennacl .. " << std::endl;
00225       return viennacl::matrix_expression< const MatrixT, 
00226                                           const viennacl::matrix_expression< const viennacl::matrix<NumericT, F, ALIGNMENT>, 
00227                                                                              const viennacl::matrix<NumericT, F, ALIGNMENT>,
00228                                                                              viennacl::op_trans >,
00229                                           viennacl::op_prod >(matrix_A, matrix_B);
00230       //return viennacl::linalg::prod_impl(matrix_A, matrix_B);
00231     }
00232 
00233   } // end namespace linalg
00234 } // end namespace viennacl
00235 #endif
00236 
00237 
00238 
00239 
00240 

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