ViennaCL - The Vienna Computing Library  1.5.2
result_of.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_META_RESULT_OF_HPP_
2 #define VIENNACL_META_RESULT_OF_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 
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include "viennacl/forwards.h"
29 
30 
31 #ifdef VIENNACL_WITH_UBLAS
32 #include <boost/numeric/ublas/matrix_sparse.hpp>
33 #include <boost/numeric/ublas/matrix.hpp>
34 #endif
35 
36 #ifdef VIENNACL_WITH_EIGEN
37 #include <Eigen/Core>
38 #include <Eigen/Sparse>
39 #endif
40 
41 #ifdef VIENNACL_WITH_MTL4
42 #include <boost/numeric/mtl/mtl.hpp>
43 #endif
44 
45 #ifdef VIENNACL_WITH_OPENCL
46 #ifdef __APPLE__
47 #include <OpenCL/cl.h>
48 #else
49 #include "CL/cl.h"
50 #endif
51 #endif
52 
53 #include <vector>
54 #include <map>
55 
56 namespace viennacl
57 {
58  namespace result_of
59  {
60  //
61  // Retrieve alignment from vector
62  //
64  template <typename T>
65  struct alignment
66  {
67  typedef typename T::ERROR_ARGUMENT_PROVIDED_IS_NOT_A_VECTOR_OR_A_MATRIX error_type;
68  enum { value = 1 };
69  };
70 
72  template <typename T>
73  struct alignment<const T>
74  {
75  enum { value = alignment<T>::value };
76  };
77 
78  template <typename SCALARTYPE, unsigned int ALIGNMENT>
79  struct alignment< vector<SCALARTYPE, ALIGNMENT> >
80  {
81  enum { value = ALIGNMENT };
82  };
83 
84  template <typename T>
85  struct alignment< vector_range<T> >
86  {
87  enum { value = alignment<T>::value };
88  };
89 
90  template <typename T>
91  struct alignment< vector_slice<T> >
92  {
93  enum { value = alignment<T>::value };
94  };
95 
96  // support for a*x with scalar a and vector x
97  template <typename LHS, typename RHS, typename OP>
98  struct alignment< vector_expression<LHS, RHS, OP> >
99  {
100  enum { value = alignment<LHS>::value };
101  };
102 
103 
104  // Matrices
105  template <typename SCALARTYPE, typename F, unsigned int ALIGNMENT>
106  struct alignment< matrix<SCALARTYPE, F, ALIGNMENT> >
107  {
108  enum { value = ALIGNMENT };
109  };
110 
111  template <typename T>
112  struct alignment< matrix_range<T> >
113  {
114  enum { value = alignment<T>::value };
115  };
116 
117  template <typename T>
118  struct alignment< matrix_slice<T> >
119  {
120  enum { value = alignment<T>::value };
121  };
122 
123  template <typename LHS, typename RHS>
124  struct alignment< matrix_expression<LHS, RHS, op_trans> >
125  {
126  enum { value = alignment<LHS>::value };
127  };
130  //
131  // Majority specifier for matrices (row_major, column_major)
132  //
134  template <typename T>
136  {
137  typedef typename T::ERROR_ARGUMENT_PROVIDED_IS_NOT_A_MATRIX type;
138  };
139 
141  template <typename T>
142  struct orientation_functor<const T>
143  {
144  typedef typename orientation_functor<T>::type type;
145  };
146 
147  template <typename SCALARTYPE, typename F, unsigned int ALIGNMENT>
148  struct orientation_functor< matrix<SCALARTYPE, F, ALIGNMENT> >
149  {
150  typedef F type;
151  };
152 
153  template <typename T>
154  struct orientation_functor< matrix_range<T> >
155  {
156  typedef typename orientation_functor<T>::type type;
157  };
158 
159  template <typename T>
160  struct orientation_functor< matrix_slice<T> >
161  {
162  typedef typename orientation_functor<T>::type type;
163  };
164 
165  template <typename SCALARTYPE, typename F>
166  struct orientation_functor< matrix_base<SCALARTYPE, F> >
167  {
168  typedef F type;
169  };
170 
171  template <typename LHS, typename RHS>
172  struct orientation_functor< matrix_expression<LHS, RHS, op_trans> >
173  {
174  typedef typename orientation_functor<LHS>::type type;
175  };
179  //
180  // Retrieve size_type
181  //
183  template <typename T>
184  struct size_type
185  {
186  typedef typename T::size_type type;
187  };
188 
190  template <typename T, typename SizeType>
191  struct size_type< vector_base<T, SizeType> >
192  {
193  typedef SizeType type;
194  };
195 
196  #ifdef VIENNACL_WITH_EIGEN
197  template <class T, int a, int b, int c, int d, int e>
198  struct size_type< Eigen::Matrix<T, a, b, c, d, e> >
199  {
200  typedef vcl_size_t type;
201  };
202 
203  template <>
204  struct size_type<Eigen::VectorXf>
205  {
206  typedef vcl_size_t type;
207  };
208 
209  template <>
210  struct size_type<Eigen::VectorXd>
211  {
212  typedef vcl_size_t type;
213  };
214 
215  template <typename T, int options>
216  struct size_type<Eigen::SparseMatrix<T, options> >
217  {
218  typedef vcl_size_t type;
219  };
220  #endif
221 
223  //
224  // Retrieve value_type:
225  //
227  template <typename T>
228  struct value_type
229  {
230  typedef typename T::value_type type;
231  };
232 
234 #ifdef VIENNACL_WITH_EIGEN
235  template <>
236  struct value_type<Eigen::MatrixXf>
237  {
238  typedef Eigen::MatrixXf::RealScalar type;
239  };
240 
241  template <>
242  struct value_type<Eigen::MatrixXd>
243  {
244  typedef Eigen::MatrixXd::RealScalar type;
245  };
246 
247  template <typename ScalarType, int option>
248  struct value_type<Eigen::SparseMatrix<ScalarType, option> >
249  {
250  typedef ScalarType type;
251  };
252 
253  template <>
254  struct value_type<Eigen::VectorXf>
255  {
256  typedef Eigen::VectorXf::RealScalar type;
257  };
258 
259  template <>
260  struct value_type<Eigen::VectorXd>
261  {
262  typedef Eigen::VectorXd::RealScalar type;
263  };
264 
265 #endif
266 
269  //
270  // Retrieve cpu value_type:
271  //
273  template <typename T>
275  {
276  typedef typename T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type;
277  };
278 
280  template <typename T>
281  struct cpu_value_type<const T>
282  {
283  typedef typename cpu_value_type<T>::type type;
284  };
285 
286  template <>
287  struct cpu_value_type<char>
288  {
289  typedef char type;
290  };
291 
292  template <>
293  struct cpu_value_type<unsigned char>
294  {
295  typedef unsigned char type;
296  };
297 
298  template <>
299  struct cpu_value_type<short>
300  {
301  typedef short type;
302  };
303 
304  template <>
305  struct cpu_value_type<unsigned short>
306  {
307  typedef unsigned short type;
308  };
309 
310  template <>
311  struct cpu_value_type<int>
312  {
313  typedef int type;
314  };
315 
316  template <>
317  struct cpu_value_type<unsigned int>
318  {
319  typedef unsigned int type;
320  };
321 
322  template <>
323  struct cpu_value_type<long>
324  {
325  typedef int type;
326  };
327 
328  template <>
329  struct cpu_value_type<unsigned long>
330  {
331  typedef unsigned long type;
332  };
333 
334 
335  template <>
336  struct cpu_value_type<float>
337  {
338  typedef float type;
339  };
340 
341  template <>
342  struct cpu_value_type<double>
343  {
344  typedef double type;
345  };
346 
347  template <typename T>
348  struct cpu_value_type<viennacl::scalar<T> >
349  {
350  typedef T type;
351  };
352 
353  template <typename T>
354  struct cpu_value_type<viennacl::vector_base<T> >
355  {
356  typedef T type;
357  };
358 
359  template <typename T>
360  struct cpu_value_type<viennacl::implicit_vector_base<T> >
361  {
362  typedef T type;
363  };
364 
365 
366  template <typename T, unsigned int ALIGNMENT>
367  struct cpu_value_type<viennacl::vector<T, ALIGNMENT> >
368  {
369  typedef T type;
370  };
371 
372  template <typename T>
373  struct cpu_value_type<viennacl::vector_range<T> >
374  {
375  typedef typename cpu_value_type<T>::type type;
376  };
377 
378  template <typename T>
379  struct cpu_value_type<viennacl::vector_slice<T> >
380  {
381  typedef typename cpu_value_type<T>::type type;
382  };
383 
384  template <typename T1, typename T2, typename OP>
385  struct cpu_value_type<viennacl::vector_expression<const T1, const T2, OP> >
386  {
387  typedef typename cpu_value_type<T1>::type type;
388  };
389 
390  template <typename T1, typename T2, typename OP>
391  struct cpu_value_type<const viennacl::vector_expression<const T1, const T2, OP> >
392  {
393  typedef typename cpu_value_type<T1>::type type;
394  };
395 
396 
397  template <typename T, typename F>
398  struct cpu_value_type<viennacl::matrix_base<T, F> >
399  {
400  typedef T type;
401  };
402 
403  template <typename T>
404  struct cpu_value_type<viennacl::implicit_matrix_base<T> >
405  {
406  typedef T type;
407  };
408 
409 
410  template <typename T, typename F, unsigned int ALIGNMENT>
411  struct cpu_value_type<viennacl::matrix<T, F, ALIGNMENT> >
412  {
413  typedef T type;
414  };
415 
416  template <typename T>
417  struct cpu_value_type<viennacl::matrix_range<T> >
418  {
419  typedef typename cpu_value_type<T>::type type;
420  };
421 
422  template <typename T>
423  struct cpu_value_type<viennacl::matrix_slice<T> >
424  {
425  typedef typename cpu_value_type<T>::type type;
426  };
427 
428  template <typename T, unsigned int ALIGNMENT>
429  struct cpu_value_type<viennacl::compressed_matrix<T, ALIGNMENT> >
430  {
431  typedef typename cpu_value_type<T>::type type;
432  };
433 
434  template <typename T>
435  struct cpu_value_type<viennacl::compressed_compressed_matrix<T> >
436  {
437  typedef typename cpu_value_type<T>::type type;
438  };
439 
440  template <typename T, unsigned int ALIGNMENT>
441  struct cpu_value_type<viennacl::coordinate_matrix<T, ALIGNMENT> >
442  {
443  typedef typename cpu_value_type<T>::type type;
444  };
445 
446  template <typename T, unsigned int ALIGNMENT>
447  struct cpu_value_type<viennacl::ell_matrix<T, ALIGNMENT> >
448  {
449  typedef typename cpu_value_type<T>::type type;
450  };
451 
452  template <typename T, unsigned int ALIGNMENT>
453  struct cpu_value_type<viennacl::hyb_matrix<T, ALIGNMENT> >
454  {
455  typedef typename cpu_value_type<T>::type type;
456  };
457 
458  template <typename T, unsigned int ALIGNMENT>
459  struct cpu_value_type<viennacl::circulant_matrix<T, ALIGNMENT> >
460  {
461  typedef typename cpu_value_type<T>::type type;
462  };
463 
464  template <typename T, unsigned int ALIGNMENT>
465  struct cpu_value_type<viennacl::hankel_matrix<T, ALIGNMENT> >
466  {
467  typedef typename cpu_value_type<T>::type type;
468  };
469 
470  template <typename T, unsigned int ALIGNMENT>
471  struct cpu_value_type<viennacl::toeplitz_matrix<T, ALIGNMENT> >
472  {
473  typedef typename cpu_value_type<T>::type type;
474  };
475 
476  template <typename T, unsigned int ALIGNMENT>
477  struct cpu_value_type<viennacl::vandermonde_matrix<T, ALIGNMENT> >
478  {
479  typedef typename cpu_value_type<T>::type type;
480  };
481 
482  template <typename T1, typename T2, typename OP>
483  struct cpu_value_type<viennacl::matrix_expression<T1, T2, OP> >
484  {
485  typedef typename cpu_value_type<T1>::type type;
486  };
487 
488 
489  //
490  // Deduce compatible vector type for a matrix type
491  //
492 
493  template <typename T>
494  struct vector_for_matrix
495  {
496  typedef typename T::ERROR_CANNOT_DEDUCE_VECTOR_FOR_MATRIX_TYPE type;
497  };
498 
499  //ViennaCL
500  template <typename T, typename F, unsigned int A>
501  struct vector_for_matrix< viennacl::matrix<T, F, A> >
502  {
503  typedef viennacl::vector<T,A> type;
504  };
505 
506  template <typename T, unsigned int A>
507  struct vector_for_matrix< viennacl::compressed_matrix<T, A> >
508  {
509  typedef viennacl::vector<T,A> type;
510  };
511 
512  template <typename T, unsigned int A>
513  struct vector_for_matrix< viennacl::coordinate_matrix<T, A> >
514  {
515  typedef viennacl::vector<T,A> type;
516  };
517 
518  #ifdef VIENNACL_WITH_UBLAS
519  //Boost:
520  template <typename T, typename F, typename A>
521  struct vector_for_matrix< boost::numeric::ublas::matrix<T, F, A> >
522  {
523  typedef boost::numeric::ublas::vector<T> type;
524  };
525 
526  template <typename T, typename U, vcl_size_t A, typename B, typename C>
527  struct vector_for_matrix< boost::numeric::ublas::compressed_matrix<T, U, A, B, C> >
528  {
529  typedef boost::numeric::ublas::vector<T> type;
530  };
531 
532  template <typename T, typename U, vcl_size_t A, typename B, typename C>
533  struct vector_for_matrix< boost::numeric::ublas::coordinate_matrix<T, U, A, B, C> >
534  {
535  typedef boost::numeric::ublas::vector<T> type;
536  };
537  #endif
538 
539 
540  template <typename T>
541  struct reference_if_nonscalar
542  {
543  typedef T & type;
544  };
545 
546 #define VIENNACL_REFERENCE_IF_NONSCALAR_INT(TNAME) \
547  template <> struct reference_if_nonscalar<TNAME> { typedef TNAME type; }; \
548  template <> struct reference_if_nonscalar<const TNAME> { typedef const TNAME type; }; \
549  template <> struct reference_if_nonscalar<unsigned TNAME> { typedef unsigned TNAME type; }; \
550  template <> struct reference_if_nonscalar<const unsigned TNAME> { typedef const unsigned TNAME type; };
551 
552  VIENNACL_REFERENCE_IF_NONSCALAR_INT(char)
553  VIENNACL_REFERENCE_IF_NONSCALAR_INT(short)
554  VIENNACL_REFERENCE_IF_NONSCALAR_INT(int)
555  VIENNACL_REFERENCE_IF_NONSCALAR_INT(long)
556 
557 #undef VIENNACL_REFERENCE_IF_NONSCALAR_INT
558 
559  template <>
560  struct reference_if_nonscalar<float>
561  {
562  typedef float type;
563  };
564 
565  template <>
566  struct reference_if_nonscalar<const float>
567  {
568  typedef const float type;
569  };
570 
571  template <>
572  struct reference_if_nonscalar<double>
573  {
574  typedef double type;
575  };
576 
577  template <>
578  struct reference_if_nonscalar<const double>
579  {
580  typedef const double type;
581  };
582 
585  //OpenCL equivalent type
587  template<typename T>
588  struct cl_type
589  {
590  typedef T type;
591  };
592 
594 #ifdef VIENNACL_WITH_OPENCL
595  template<>
596  struct cl_type<float>{ typedef cl_float type; };
597 
598  template<>
599  struct cl_type<double>{ typedef cl_double type; };
600 
601  template<>
602  struct cl_type<int>{ typedef cl_int type; };
603 
604  template<>
605  struct cl_type<unsigned int>{ typedef cl_uint type; };
606 
607  template<>
608  struct cl_type<long>{ typedef cl_long type; };
609 
610  template<>
611  struct cl_type<unsigned long>{ typedef cl_ulong type; };
612 
613  template<>
614  struct cl_type<short>{ typedef cl_short type; };
615 
616  template<>
617  struct cl_type<unsigned short>{ typedef cl_ushort type; };
618 
619  template<>
620  struct cl_type<char>{ typedef cl_char type; };
621 
622  template<>
623  struct cl_type<unsigned char>{ typedef cl_uchar type; };
624 #endif
625 
627  } //namespace result_of
628 } //namespace viennacl
629 
630 
631 #endif
std::size_t vcl_size_t
Definition: forwards.h:58
Metafunction for deducing the OpenCL type for a numeric type, e.g. float -> cl_float.
Definition: result_of.hpp:588
This file provides the forward declarations for the main types used within ViennaCL.
A dense matrix class.
Definition: forwards.h:293
Retrieves the alignment from a vector. Deprecated - will be replaced by a pure runtime facility in th...
Definition: result_of.hpp:65
Definition: result_of.hpp:68
T::ERROR_ARGUMENT_PROVIDED_IS_NOT_A_VECTOR_OR_A_MATRIX error_type
Definition: result_of.hpp:67
Generic helper function for retrieving the value_type associated with type T.
Definition: result_of.hpp:228
Common base class for dense vectors, vector ranges, and vector slices.
Definition: forwards.h:205
T::value_type type
Definition: result_of.hpp:230
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
Returns the orientation functor tag (either row_major or column_major) of a matrix.
Definition: result_of.hpp:135
T type
Definition: result_of.hpp:590
T::ERROR_ARGUMENT_PROVIDED_IS_NOT_A_MATRIX type
Definition: result_of.hpp:137
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:276
Helper meta function for retrieving the main RAM-based value type. Particularly important to obtain T...
Definition: result_of.hpp:274
T::size_type type
Definition: result_of.hpp:186
Generic meta-function for retrieving the size_type associated with type T.
Definition: result_of.hpp:184