ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-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 <string>
26 #include <fstream>
27 #include <sstream>
28 #include "viennacl/forwards.h"
31 
32 #ifdef VIENNACL_WITH_UBLAS
33 #include <boost/numeric/ublas/matrix_sparse.hpp>
34 #include <boost/numeric/ublas/matrix.hpp>
35 #endif
36 
37 #ifdef VIENNACL_WITH_ARMADILLO
38 #include <armadillo>
39 #endif
40 
41 #ifdef VIENNACL_WITH_EIGEN
42 #include <Eigen/Core>
43 #include <Eigen/Sparse>
44 #endif
45 
46 #ifdef VIENNACL_WITH_MTL4
47 #include <boost/numeric/mtl/mtl.hpp>
48 #endif
49 
50 #include <vector>
51 #include <map>
52 
53 namespace viennacl
54 {
55 namespace traits
56 {
57 
58 //
59 // Resize: Change the size of vectors and matrices
60 //
62 template<typename MatrixType>
63 void resize(MatrixType & matrix, vcl_size_t rows, vcl_size_t cols)
64 {
65  matrix.resize(rows, cols);
66 }
67 
69 template<typename VectorType>
70 void resize(VectorType & vec, vcl_size_t new_size)
71 {
72  vec.resize(new_size);
73 }
74 
76 #ifdef VIENNACL_WITH_UBLAS
77 //ublas needs separate treatment:
78 template<typename ScalarType>
79 void resize(boost::numeric::ublas::compressed_matrix<ScalarType> & matrix,
80  vcl_size_t rows,
81  vcl_size_t cols)
82 {
83  matrix.resize(rows, cols, false); //Note: omitting third parameter leads to compile time error (not implemented in ublas <= 1.42)
84 }
85 #endif
86 
87 
88 #ifdef VIENNACL_WITH_MTL4
89 template<typename ScalarType>
90 void resize(mtl::compressed2D<ScalarType> & matrix,
91  vcl_size_t rows,
92  vcl_size_t cols)
93 {
94  matrix.change_dim(rows, cols);
95 }
96 
97 template<typename ScalarType>
98 void resize(mtl::dense_vector<ScalarType> & vec,
99  vcl_size_t new_size)
100 {
101  vec.change_dim(new_size);
102 }
103 #endif
104 
105 #ifdef VIENNACL_WITH_ARMADILLO
106 template<typename NumericT>
107 inline void resize(arma::Mat<NumericT> & A,
108  vcl_size_t new_rows,
109  vcl_size_t new_cols)
110 {
111  A.resize(new_rows, new_cols);
112 }
113 
114 template<typename NumericT>
115 inline void resize(arma::SpMat<NumericT> & A,
116  vcl_size_t new_rows,
117  vcl_size_t new_cols)
118 {
119  A.set_size(new_rows, new_cols);
120 }
121 #endif
122 
123 #ifdef VIENNACL_WITH_EIGEN
124 template<typename NumericT, int Options>
125 inline void resize(Eigen::Matrix<NumericT, Eigen::Dynamic, Eigen::Dynamic, Options> & m,
126  vcl_size_t new_rows,
127  vcl_size_t new_cols)
128 {
129  m.resize(new_rows, new_cols);
130 }
131 
132 template<typename T, int options>
133 inline void resize(Eigen::SparseMatrix<T, options> & m,
134  vcl_size_t new_rows,
135  vcl_size_t new_cols)
136 {
137  m.resize(new_rows, new_cols);
138 }
139 
140 inline void resize(Eigen::VectorXf & v,
141  vcl_size_t new_size)
142 {
143  v.resize(new_size);
144 }
145 
146 inline void resize(Eigen::VectorXd & v,
147  vcl_size_t new_size)
148 {
149  v.resize(new_size);
150 }
151 #endif
152 
157 //
158 // size1: No. of rows for matrices
159 //
161 template<typename MatrixType>
163 size1(MatrixType const & mat) { return mat.size1(); }
164 
166 template<typename RowType>
168 size1(std::vector< RowType > const & mat) { return mat.size(); }
169 
170 #ifdef VIENNACL_WITH_ARMADILLO
171 template<typename NumericT>
172 inline vcl_size_t size1(arma::Mat<NumericT> const & A) { return A.n_rows; }
173 template<typename NumericT>
174 inline vcl_size_t size1(arma::SpMat<NumericT> const & A) { return A.n_rows; }
175 #endif
176 
177 #ifdef VIENNACL_WITH_EIGEN
178 template<typename NumericT, int Options>
179 vcl_size_t size1(Eigen::Matrix<NumericT, Eigen::Dynamic, Eigen::Dynamic, Options> const & m) { return static_cast<vcl_size_t>(m.rows()); }
180 template<typename NumericT, int Options>
181 vcl_size_t size1(Eigen::Map< Eigen::Matrix<NumericT, Eigen::Dynamic, Eigen::Dynamic, Options> > const & m) { return static_cast<vcl_size_t>(m.rows()); }
182 template<typename T, int options>
183 inline vcl_size_t size1(Eigen::SparseMatrix<T, options> & m) { return static_cast<vcl_size_t>(m.rows()); }
184 #endif
185 
186 #ifdef VIENNACL_WITH_MTL4
187 template<typename NumericT, typename T>
188 vcl_size_t size1(mtl::dense2D<NumericT, T> const & m) { return static_cast<vcl_size_t>(m.num_rows()); }
189 template<typename NumericT>
190 vcl_size_t size1(mtl::compressed2D<NumericT> const & m) { return static_cast<vcl_size_t>(m.num_rows()); }
191 #endif
192 
195 //
196 // size2: No. of columns for matrices
197 //
199 template<typename MatrixType>
200 typename result_of::size_type<MatrixType>::type
201 size2(MatrixType const & mat) { return mat.size2(); }
202 
204 #ifdef VIENNACL_WITH_ARMADILLO
205 template<typename NumericT>
206 inline vcl_size_t size2(arma::Mat<NumericT> const & A) { return A.n_cols; }
207 template<typename NumericT>
208 inline vcl_size_t size2(arma::SpMat<NumericT> const & A) { return A.n_cols; }
209 #endif
210 
211 #ifdef VIENNACL_WITH_EIGEN
212 template<typename NumericT, int Options>
213 inline vcl_size_t size2(Eigen::Matrix<NumericT, Eigen::Dynamic, Eigen::Dynamic, Options> const & m) { return m.cols(); }
214 template<typename NumericT, int Options>
215 inline vcl_size_t size2(Eigen::Map< Eigen::Matrix<NumericT, Eigen::Dynamic, Eigen::Dynamic, Options> > const & m) { return m.cols(); }
216 template<typename T, int options>
217 inline vcl_size_t size2(Eigen::SparseMatrix<T, options> & m) { return m.cols(); }
218 #endif
219 
220 #ifdef VIENNACL_WITH_MTL4
221 template<typename NumericT, typename T>
222 vcl_size_t size2(mtl::dense2D<NumericT, T> const & m) { return static_cast<vcl_size_t>(m.num_cols()); }
223 template<typename NumericT>
224 vcl_size_t size2(mtl::compressed2D<NumericT> const & m) { return static_cast<vcl_size_t>(m.num_cols()); }
225 #endif
226 
230 //
231 // size: Returns the length of vectors
232 //
234 template<typename VectorType>
235 vcl_size_t size(VectorType const & vec)
236 {
237  return vec.size();
238 }
239 
241 template<typename SparseMatrixType, typename VectorType>
243 {
244  return size1(proxy.lhs());
245 }
246 
247 template<typename T, unsigned int A, typename VectorType>
248 vcl_size_t size(vector_expression<const circulant_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1(); }
249 
250 template<typename T, unsigned int A, typename VectorType>
251 vcl_size_t size(vector_expression<const hankel_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1(); }
252 
253 template<typename T, unsigned int A, typename VectorType>
254 vcl_size_t size(vector_expression<const toeplitz_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1(); }
255 
256 template<typename T, unsigned int A, typename VectorType>
257 vcl_size_t size(vector_expression<const vandermonde_matrix<T, A>, const VectorType, op_prod> const & proxy) { return proxy.lhs().size1(); }
258 
259 template<typename NumericT>
260 vcl_size_t size(vector_expression<const matrix_base<NumericT>, const vector_base<NumericT>, op_prod> const & proxy) //matrix-vector product
261 {
262  return proxy.lhs().size1();
263 }
264 
265 template<typename NumericT, typename LhsT, typename RhsT, typename OpT>
266 vcl_size_t size(vector_expression<const matrix_base<NumericT>, const vector_expression<LhsT, RhsT, OpT>, op_prod> const & proxy) //matrix-vector product
267 {
268  return proxy.lhs().size1();
269 }
270 
271 template<typename NumericT>
272 vcl_size_t size(vector_expression<const matrix_expression<const matrix_base<NumericT>, const matrix_base<NumericT>, op_trans>,
273  const vector_base<NumericT>,
274  op_prod> const & proxy) //transposed matrix-vector product
275 {
276  return proxy.lhs().lhs().size2();
277 }
278 
279 
280 #ifdef VIENNACL_WITH_MTL4
281 template<typename ScalarType>
282 vcl_size_t size(mtl::dense_vector<ScalarType> const & vec) { return vec.used_memory(); }
283 #endif
284 
285 #ifdef VIENNACL_WITH_ARMADILLO
286 template<typename NumericT>
287 inline vcl_size_t size(arma::Mat<NumericT> const & A) { return A.n_elem; }
288 #endif
289 
290 #ifdef VIENNACL_WITH_EIGEN
291 inline vcl_size_t size(Eigen::VectorXf const & v) { return v.rows(); }
292 inline vcl_size_t size(Eigen::VectorXd const & v) { return v.rows(); }
293 #endif
294 
295 template<typename LHS, typename RHS, typename OP>
296 vcl_size_t size(vector_expression<LHS, RHS, OP> const & proxy)
297 {
298  return size(proxy.lhs());
299 }
300 
301 template<typename LHS, typename RHS>
302 vcl_size_t size(vector_expression<LHS, const vector_tuple<RHS>, op_inner_prod> const & proxy)
303 {
304  return proxy.rhs().const_size();
305 }
306 
307 template<typename LhsT, typename RhsT, typename OpT, typename VectorT>
308 vcl_size_t size(vector_expression<const matrix_expression<const LhsT, const RhsT, OpT>,
309  VectorT,
310  op_prod> const & proxy)
311 {
312  return size1(proxy.lhs());
313 }
314 
315 template<typename LhsT, typename RhsT, typename OpT, typename NumericT>
316 vcl_size_t size(vector_expression<const matrix_expression<const LhsT, const RhsT, OpT>,
317  const vector_base<NumericT>,
318  op_prod> const & proxy)
319 {
320  return size1(proxy.lhs());
321 }
322 
323 template<typename LhsT1, typename RhsT1, typename OpT1,
324  typename LhsT2, typename RhsT2, typename OpT2>
325 vcl_size_t size(vector_expression<const matrix_expression<const LhsT1, const RhsT1, OpT1>,
326  const vector_expression<const LhsT2, const RhsT2, OpT2>,
327  op_prod> const & proxy)
328 {
329  return size1(proxy.lhs());
330 }
331 
332 template<typename NumericT>
333 vcl_size_t size(vector_expression<const matrix_base<NumericT>,
334  const matrix_base<NumericT>,
335  op_row_sum> const & proxy)
336 {
337  return size1(proxy.lhs());
338 }
339 
340 template<typename LhsT, typename RhsT, typename OpT>
341 vcl_size_t size(vector_expression<const matrix_expression<const LhsT, const RhsT, OpT>,
342  const matrix_expression<const LhsT, const RhsT, OpT>,
343  op_row_sum> const & proxy)
344 {
345  return size1(proxy.lhs());
346 }
347 
348 template<typename NumericT>
349 vcl_size_t size(vector_expression<const matrix_base<NumericT>,
350  const matrix_base<NumericT>,
351  op_col_sum> const & proxy)
352 {
353  return size2(proxy.lhs());
354 }
355 
356 template<typename LhsT, typename RhsT, typename OpT>
357 vcl_size_t size(vector_expression<const matrix_expression<const LhsT, const RhsT, OpT>,
358  const matrix_expression<const LhsT, const RhsT, OpT>,
359  op_col_sum> const & proxy)
360 {
361  return size2(proxy.lhs());
362 }
363 
366 //
367 // internal_size: Returns the internal (padded) length of vectors
368 //
370 template<typename NumericT>
372 {
373  return vec.internal_size();
374 }
375 
376 
377 //
378 // internal_size1: No. of internal (padded) rows for matrices
379 //
381 template<typename NumericT>
383 
384 
385 //
386 // internal_size2: No. of internal (padded) columns for matrices
387 //
389 template<typename NumericT>
391 
393 template<typename NumericT>
395 {
396  if (mat.row_major())
397  return mat.internal_size2();
398  return mat.internal_size1();
399 }
400 
401 template<typename NumericT>
403 {
404  if (mat.row_major())
405  return mat.stride2();
406  return mat.stride1();
407 }
408 
409 template<typename LHS>
411 {
412  int k = proxy.rhs();
413  int A_size1 = static_cast<int>(size1(proxy.lhs()));
414  int A_size2 = static_cast<int>(size2(proxy.lhs()));
415 
416  int row_depth = std::min(A_size1, A_size1 + k);
417  int col_depth = std::min(A_size2, A_size2 - k);
418 
419  return vcl_size_t(std::min(row_depth, col_depth));
420 }
421 
422 template<typename LHS>
424 {
425  return size2(proxy.lhs());
426 }
427 
428 template<typename LHS>
430 {
431  return size1(proxy.lhs());
432 }
433 
434 } //namespace traits
435 } //namespace viennacl
436 
437 
438 #endif
vcl_size_t nld(matrix_base< NumericT > const &mat)
Definition: size.hpp:402
vcl_size_t internal_size1(matrix_base< NumericT > const &mat)
Helper routine for obtaining the internal number of entries per row of a ViennaCL matrix...
Definition: size.hpp:382
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
vcl_size_t internal_size2(matrix_base< NumericT > const &mat)
Helper routine for obtaining the internal number of entries per column of a ViennaCL matrix...
Definition: size.hpp:390
size_type stride2() const
Returns the number of columns.
Definition: matrix_def.hpp:234
This file provides the forward declarations for the main types used within ViennaCL.
A dense matrix class.
Definition: forwards.h:375
vcl_size_t internal_size(vector_base< NumericT > const &vec)
Helper routine for obtaining the buffer length of a ViennaCL vector.
Definition: size.hpp:371
lhs_reference_type lhs() const
Get left hand side operand.
Definition: vector.hpp:74
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:201
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:235
vcl_size_t ld(matrix_base< NumericT > const &mat)
Helper routine for obtaining the internal number of entries per row of a ViennaCL matrix...
Definition: size.hpp:394
void resize(MatrixType &matrix, vcl_size_t rows, vcl_size_t cols)
Generic resize routine for resizing a matrix (ViennaCL, uBLAS, etc.) to a new size/dimension.
Definition: size.hpp:63
rhs_reference_type rhs() const
Get right hand side operand.
Definition: vector.hpp:77
size_type stride1() const
Returns the number of rows.
Definition: matrix_def.hpp:232
std::size_t vcl_size_t
Definition: forwards.h:75
All the predicates used within ViennaCL. Checks for expressions to be vectors, etc.
bool row_major() const
Definition: matrix_def.hpp:248
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
T min(const T &lhs, const T &rhs)
Minimum.
Definition: util.hpp:45
size_type internal_size() const
Returns the internal length of the vector, which is given by size() plus the extra memory due to padd...
Definition: vector_def.hpp:120
A collection of compile time type deductions.