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

/data/development/ViennaCL/dev/viennacl/traits/size.hpp

Go to the documentation of this file.
00001 #ifndef VIENNACL_TRAITS_SIZE_HPP_
00002 #define VIENNACL_TRAITS_SIZE_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 
00024 #include <string>
00025 #include <fstream>
00026 #include <sstream>
00027 #include "viennacl/forwards.h"
00028 #include "viennacl/meta/result_of.hpp"
00029 
00030 #ifdef VIENNACL_HAVE_UBLAS  
00031 #include <boost/numeric/ublas/matrix_sparse.hpp>
00032 #include <boost/numeric/ublas/matrix.hpp>
00033 #endif
00034 
00035 #ifdef VIENNACL_HAVE_EIGEN  
00036 #include <Eigen/Core>
00037 #include <Eigen/Sparse>
00038 #endif
00039 
00040 #ifdef VIENNACL_HAVE_MTL4
00041 #include <boost/numeric/mtl/mtl.hpp>
00042 #endif
00043 
00044 #include <vector>
00045 #include <map>
00046 
00047 namespace viennacl
00048 {
00049 
00050   namespace traits
00051   {
00052     //
00053     // Resize: Change the size of vectors and matrices
00054     //
00055     template <typename MatrixType>
00056     void resize(MatrixType & matrix, size_t rows, size_t cols)
00057     {
00058       matrix.resize(rows, cols); 
00059     }
00060     
00061     template <typename VectorType>
00062     void resize(VectorType & vec, size_t new_size)
00063     {
00064       vec.resize(new_size); 
00065     }
00066     
00067     #ifdef VIENNACL_HAVE_UBLAS  
00068     //ublas needs separate treatment:
00069     template <typename ScalarType>
00070     void resize(boost::numeric::ublas::compressed_matrix<ScalarType> & matrix,
00071                 size_t rows,
00072                 size_t cols)
00073     {
00074       matrix.resize(rows, cols, false); //Note: omitting third parameter leads to compile time error (not implemented in ublas <= 1.42) 
00075     }
00076     #endif  
00077     
00078     
00079     #ifdef VIENNACL_HAVE_MTL4
00080     template <typename ScalarType>
00081     void resize(mtl::compressed2D<ScalarType> & matrix,
00082                 size_t rows,
00083                 size_t cols)
00084     {
00085       matrix.change_dim(rows, cols);
00086     }
00087     
00088     template <typename ScalarType>
00089     void resize(mtl::dense_vector<ScalarType> & vec,
00090                 size_t new_size)
00091     {
00092       vec.change_dim(new_size);
00093     }
00094     #endif      
00095 
00096     #ifdef VIENNACL_HAVE_EIGEN
00097     inline void resize(Eigen::MatrixXf & m,
00098                        std::size_t new_rows,
00099                        std::size_t new_cols)
00100     {
00101       m.resize(new_rows, new_cols);
00102     }
00103     
00104     inline void resize(Eigen::MatrixXd & m,
00105                        std::size_t new_rows,
00106                        std::size_t new_cols)
00107     {
00108       m.resize(new_rows, new_cols);
00109     }
00110     
00111     template <typename T, int options>
00112     inline void resize(Eigen::SparseMatrix<T, options> & m,
00113                        std::size_t new_rows,
00114                        std::size_t new_cols)
00115     {
00116       m.resize(new_rows, new_cols);
00117     }    
00118     
00119     inline void resize(Eigen::VectorXf & v,
00120                        std::size_t new_size)
00121     {
00122       v.resize(new_size);
00123     }
00124     
00125     inline void resize(Eigen::VectorXd & v,
00126                        std::size_t new_size)
00127     {
00128       v.resize(new_size);
00129     }
00130     #endif
00131 
00132 
00133     //
00134     // size: Returns the length of vectors
00135     //
00136     template <typename VectorType>
00137     typename result_of::size_type<VectorType>::type size(VectorType const & vec)
00138     {
00139       return vec.size(); 
00140     }
00141 
00142     #ifdef VIENNACL_HAVE_MTL4
00143     template <typename ScalarType>
00144     typename result_of::size_type< mtl::dense_vector<ScalarType> >::type
00145     size(mtl::dense_vector<ScalarType> const & vec) { return vec.used_memory(); }
00146     #endif
00147     
00148     #ifdef VIENNACL_HAVE_EIGEN
00149     inline std::size_t size(Eigen::VectorXf const & v) { return v.rows(); }
00150     inline std::size_t size(Eigen::VectorXd const & v) { return v.rows(); }
00151     #endif
00152 
00153     //
00154     // size1: No. of rows for matrices
00155     //
00156     template <typename MatrixType>
00157     typename result_of::size_type<MatrixType>::type
00158     size1(MatrixType const & mat) { return mat.size1(); }
00159 
00160     #ifdef VIENNACL_HAVE_EIGEN
00161     inline std::size_t size1(Eigen::MatrixXf const & m) { return m.rows(); }
00162     inline std::size_t size1(Eigen::MatrixXd const & m) { return m.rows(); }
00163     template <typename T, int options>
00164     inline std::size_t size1(Eigen::SparseMatrix<T, options> & m) { return m.rows(); }    
00165     #endif
00166 
00167     //
00168     // size2: No. of columns for matrices
00169     //
00170     template <typename MatrixType>
00171     typename result_of::size_type<MatrixType>::type
00172     size2(MatrixType const & mat) { return mat.size2(); }
00173  
00174     #ifdef VIENNACL_HAVE_EIGEN
00175     inline std::size_t size2(Eigen::MatrixXf const & m) { return m.cols(); }
00176     inline std::size_t size2(Eigen::MatrixXd const & m) { return m.cols(); }
00177     template <typename T, int options>
00178     inline std::size_t size2(Eigen::SparseMatrix<T, options> & m) { return m.cols(); }    
00179     #endif
00180  
00181     //
00182     // internal_size: Returns the internal (padded) length of vectors
00183     //
00184     template <typename VectorType>
00185     typename result_of::size_type<VectorType>::type 
00186     internal_size(VectorType const & vec)
00187     {
00188       return vec.internal_size(); 
00189     }
00190 
00191     template <typename VectorType>
00192     typename result_of::size_type<VectorType>::type 
00193     internal_size(viennacl::vector_range<VectorType> const & vec)
00194     {
00195       return vec.get().internal_size(); 
00196     }
00197 
00198     //
00199     // internal_size1: No. of internal (padded) rows for matrices
00200     //
00201     template <typename MatrixType>
00202     typename result_of::size_type<MatrixType>::type
00203     internal_size1(MatrixType const & mat) { return mat.internal_size1(); }
00204 
00205     template <typename MatrixType>
00206     typename result_of::size_type<MatrixType>::type
00207     internal_size1(viennacl::matrix_range<MatrixType> const & mat) { return mat.get().internal_size1(); }
00208 
00209     //
00210     // internal_size2: No. of internal (padded) columns for matrices
00211     //
00212     template <typename MatrixType>
00213     typename result_of::size_type<MatrixType>::type
00214     internal_size2(MatrixType const & mat) { return mat.internal_size2(); }
00215  
00216     template <typename MatrixType>
00217     typename result_of::size_type<MatrixType>::type
00218     internal_size2(viennacl::matrix_range<MatrixType> const & mat) { return mat.get().internal_size2(); }
00219 
00220  
00221   } //namespace traits
00222 } //namespace viennacl
00223     
00224 
00225 #endif

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