ViennaCL - The Vienna Computing Library  1.2.0
size.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TRAITS_SIZE_HPP_
2 #define VIENNACL_TRAITS_SIZE_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #include <string>
25 #include <fstream>
26 #include <sstream>
27 #include "viennacl/forwards.h"
29 
30 #ifdef VIENNACL_HAVE_UBLAS
31 #include <boost/numeric/ublas/matrix_sparse.hpp>
32 #include <boost/numeric/ublas/matrix.hpp>
33 #endif
34 
35 #ifdef VIENNACL_HAVE_EIGEN
36 #include <Eigen/Core>
37 #include <Eigen/Sparse>
38 #endif
39 
40 #ifdef VIENNACL_HAVE_MTL4
41 #include <boost/numeric/mtl/mtl.hpp>
42 #endif
43 
44 #include <vector>
45 #include <map>
46 
47 namespace viennacl
48 {
49 
50  namespace traits
51  {
52  //
53  // Resize: Change the size of vectors and matrices
54  //
55  template <typename MatrixType>
56  void resize(MatrixType & matrix, size_t rows, size_t cols)
57  {
58  matrix.resize(rows, cols);
59  }
60 
61  template <typename VectorType>
62  void resize(VectorType & vec, size_t new_size)
63  {
64  vec.resize(new_size);
65  }
66 
67  #ifdef VIENNACL_HAVE_UBLAS
68  //ublas needs separate treatment:
69  template <typename ScalarType>
70  void resize(boost::numeric::ublas::compressed_matrix<ScalarType> & matrix,
71  size_t rows,
72  size_t cols)
73  {
74  matrix.resize(rows, cols, false); //Note: omitting third parameter leads to compile time error (not implemented in ublas <= 1.42)
75  }
76  #endif
77 
78 
79  #ifdef VIENNACL_HAVE_MTL4
80  template <typename ScalarType>
81  void resize(mtl::compressed2D<ScalarType> & matrix,
82  size_t rows,
83  size_t cols)
84  {
85  matrix.change_dim(rows, cols);
86  }
87 
88  template <typename ScalarType>
89  void resize(mtl::dense_vector<ScalarType> & vec,
90  size_t new_size)
91  {
92  vec.change_dim(new_size);
93  }
94  #endif
95 
96  #ifdef VIENNACL_HAVE_EIGEN
97  inline void resize(Eigen::MatrixXf & m,
98  std::size_t new_rows,
99  std::size_t new_cols)
100  {
101  m.resize(new_rows, new_cols);
102  }
103 
104  inline void resize(Eigen::MatrixXd & m,
105  std::size_t new_rows,
106  std::size_t new_cols)
107  {
108  m.resize(new_rows, new_cols);
109  }
110 
111  template <typename T, int options>
112  inline void resize(Eigen::SparseMatrix<T, options> & m,
113  std::size_t new_rows,
114  std::size_t new_cols)
115  {
116  m.resize(new_rows, new_cols);
117  }
118 
119  inline void resize(Eigen::VectorXf & v,
120  std::size_t new_size)
121  {
122  v.resize(new_size);
123  }
124 
125  inline void resize(Eigen::VectorXd & v,
126  std::size_t new_size)
127  {
128  v.resize(new_size);
129  }
130  #endif
131 
132 
133  //
134  // size: Returns the length of vectors
135  //
136  template <typename VectorType>
137  typename result_of::size_type<VectorType>::type size(VectorType const & vec)
138  {
139  return vec.size();
140  }
141 
142  #ifdef VIENNACL_HAVE_MTL4
143  template <typename ScalarType>
145  size(mtl::dense_vector<ScalarType> const & vec) { return vec.used_memory(); }
146  #endif
147 
148  #ifdef VIENNACL_HAVE_EIGEN
149  inline std::size_t size(Eigen::VectorXf const & v) { return v.rows(); }
150  inline std::size_t size(Eigen::VectorXd const & v) { return v.rows(); }
151  #endif
152 
153  //
154  // size1: No. of rows for matrices
155  //
156  template <typename MatrixType>
157  typename result_of::size_type<MatrixType>::type
158  size1(MatrixType const & mat) { return mat.size1(); }
159 
160  #ifdef VIENNACL_HAVE_EIGEN
161  inline std::size_t size1(Eigen::MatrixXf const & m) { return m.rows(); }
162  inline std::size_t size1(Eigen::MatrixXd const & m) { return m.rows(); }
163  template <typename T, int options>
164  inline std::size_t size1(Eigen::SparseMatrix<T, options> & m) { return m.rows(); }
165  #endif
166 
167  //
168  // size2: No. of columns for matrices
169  //
170  template <typename MatrixType>
171  typename result_of::size_type<MatrixType>::type
172  size2(MatrixType const & mat) { return mat.size2(); }
173 
174  #ifdef VIENNACL_HAVE_EIGEN
175  inline std::size_t size2(Eigen::MatrixXf const & m) { return m.cols(); }
176  inline std::size_t size2(Eigen::MatrixXd const & m) { return m.cols(); }
177  template <typename T, int options>
178  inline std::size_t size2(Eigen::SparseMatrix<T, options> & m) { return m.cols(); }
179  #endif
180 
181  //
182  // internal_size: Returns the internal (padded) length of vectors
183  //
184  template <typename VectorType>
185  typename result_of::size_type<VectorType>::type
186  internal_size(VectorType const & vec)
187  {
188  return vec.internal_size();
189  }
190 
191  template <typename VectorType>
194  {
195  return vec.get().internal_size();
196  }
197 
198  //
199  // internal_size1: No. of internal (padded) rows for matrices
200  //
201  template <typename MatrixType>
203  internal_size1(MatrixType const & mat) { return mat.internal_size1(); }
204 
205  template <typename MatrixType>
207  internal_size1(viennacl::matrix_range<MatrixType> const & mat) { return mat.get().internal_size1(); }
208 
209  //
210  // internal_size2: No. of internal (padded) columns for matrices
211  //
212  template <typename MatrixType>
214  internal_size2(MatrixType const & mat) { return mat.internal_size2(); }
215 
216  template <typename MatrixType>
218  internal_size2(viennacl::matrix_range<MatrixType> const & mat) { return mat.get().internal_size2(); }
219 
220 
221  } //namespace traits
222 } //namespace viennacl
223 
224 
225 #endif