ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
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 NumericT, unsigned int AlignmentV>
79 struct alignment< vector<NumericT, AlignmentV> >
80 {
81  enum { value = AlignmentV };
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 NumericT, typename F, unsigned int AlignmentV>
106 struct alignment< matrix<NumericT, F, AlignmentV> >
107 {
108  enum { value = AlignmentV };
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 // Retrieve size_type
132 //
134 template<typename T>
135 struct size_type
136 {
137  typedef typename T::size_type type;
138 };
139 
141 template<typename T, typename SizeType>
142 struct size_type< vector_base<T, SizeType> >
143 {
144  typedef SizeType type;
145 };
146 
147 //
148 // Retrieve difference_type
149 //
151 template<typename T>
152 struct difference_type
153 {
154  typedef typename T::difference_type type;
155 };
156 
157 
158 #ifdef VIENNACL_WITH_EIGEN
159 template<class T, int a, int b, int c, int d, int e>
160 struct size_type< Eigen::Matrix<T, a, b, c, d, e> >
161 {
162  typedef vcl_size_t type;
163 };
164 
165 template<>
166 struct size_type<Eigen::VectorXf>
167 {
168  typedef vcl_size_t type;
169 };
170 
171 template<>
172 struct size_type<Eigen::VectorXd>
173 {
174  typedef vcl_size_t type;
175 };
176 
177 template<typename T, int options>
178 struct size_type<Eigen::SparseMatrix<T, options> >
179 {
180  typedef vcl_size_t type;
181 };
182 #endif
183 
185 //
186 // Retrieve value_type:
187 //
189 template<typename T>
191 {
192  typedef typename T::value_type type;
193 };
194 
196 #ifdef VIENNACL_WITH_EIGEN
197 template<>
198 struct value_type<Eigen::MatrixXf>
199 {
200  typedef Eigen::MatrixXf::RealScalar type;
201 };
202 
203 template<>
204 struct value_type<Eigen::MatrixXd>
205 {
206  typedef Eigen::MatrixXd::RealScalar type;
207 };
208 
209 template<typename ScalarType, int option>
210 struct value_type<Eigen::SparseMatrix<ScalarType, option> >
211 {
212  typedef ScalarType type;
213 };
214 
215 template<>
216 struct value_type<Eigen::VectorXf>
217 {
218  typedef Eigen::VectorXf::RealScalar type;
219 };
220 
221 template<>
222 struct value_type<Eigen::VectorXd>
223 {
224  typedef Eigen::VectorXd::RealScalar type;
225 };
226 
227 #endif
228 
231 //
232 // Retrieve cpu value_type:
233 //
235 template<typename T>
237 {
238  typedef typename T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type;
239 };
240 
242 template<typename T>
243 struct cpu_value_type<const T>
244 {
245  typedef typename cpu_value_type<T>::type type;
246 };
247 
248 template<>
249 struct cpu_value_type<char>
250 {
251  typedef char type;
252 };
253 
254 template<>
255 struct cpu_value_type<unsigned char>
256 {
257  typedef unsigned char type;
258 };
259 
260 template<>
261 struct cpu_value_type<short>
262 {
263  typedef short type;
264 };
265 
266 template<>
267 struct cpu_value_type<unsigned short>
268 {
269  typedef unsigned short type;
270 };
271 
272 template<>
273 struct cpu_value_type<int>
274 {
275  typedef int type;
276 };
277 
278 template<>
279 struct cpu_value_type<unsigned int>
280 {
281  typedef unsigned int type;
282 };
283 
284 template<>
285 struct cpu_value_type<long>
286 {
287  typedef int type;
288 };
289 
290 template<>
291 struct cpu_value_type<unsigned long>
292 {
293  typedef unsigned long type;
294 };
295 
296 
297 template<>
298 struct cpu_value_type<float>
299 {
300  typedef float type;
301 };
302 
303 template<>
304 struct cpu_value_type<double>
305 {
306  typedef double type;
307 };
308 
309 template<typename T>
310 struct cpu_value_type<viennacl::scalar<T> >
311 {
312  typedef T type;
313 };
314 
315 template<typename T>
316 struct cpu_value_type<viennacl::vector_base<T> >
317 {
318  typedef T type;
319 };
320 
321 template<typename T>
322 struct cpu_value_type<viennacl::implicit_vector_base<T> >
323 {
324  typedef T type;
325 };
326 
327 
328 template<typename T, unsigned int AlignmentV>
329 struct cpu_value_type<viennacl::vector<T, AlignmentV> >
330 {
331  typedef T type;
332 };
333 
334 template<typename T>
335 struct cpu_value_type<viennacl::vector_range<T> >
336 {
337  typedef typename cpu_value_type<T>::type type;
338 };
339 
340 template<typename T>
341 struct cpu_value_type<viennacl::vector_slice<T> >
342 {
343  typedef typename cpu_value_type<T>::type type;
344 };
345 
346 template<typename T1, typename T2, typename OP>
347 struct cpu_value_type<viennacl::vector_expression<const T1, const T2, OP> >
348 {
349  typedef typename cpu_value_type<T1>::type type;
350 };
351 
352 template<typename T1, typename T2, typename OP>
353 struct cpu_value_type<const viennacl::vector_expression<const T1, const T2, OP> >
354 {
355  typedef typename cpu_value_type<T1>::type type;
356 };
357 
358 
359 template<typename T>
360 struct cpu_value_type<viennacl::matrix_base<T> >
361 {
362  typedef T type;
363 };
364 
365 template<typename T>
366 struct cpu_value_type<viennacl::implicit_matrix_base<T> >
367 {
368  typedef T type;
369 };
370 
371 
372 template<typename T, typename F, unsigned int AlignmentV>
373 struct cpu_value_type<viennacl::matrix<T, F, AlignmentV> >
374 {
375  typedef T type;
376 };
377 
378 template<typename T>
379 struct cpu_value_type<viennacl::matrix_range<T> >
380 {
381  typedef typename cpu_value_type<T>::type type;
382 };
383 
384 template<typename T>
385 struct cpu_value_type<viennacl::matrix_slice<T> >
386 {
387  typedef typename cpu_value_type<T>::type type;
388 };
389 
390 template<typename T, unsigned int AlignmentV>
391 struct cpu_value_type<viennacl::compressed_matrix<T, AlignmentV> >
392 {
393  typedef typename cpu_value_type<T>::type type;
394 };
395 
396 template<typename T>
397 struct cpu_value_type<viennacl::compressed_compressed_matrix<T> >
398 {
399  typedef typename cpu_value_type<T>::type type;
400 };
401 
402 template<typename T, unsigned int AlignmentV>
403 struct cpu_value_type<viennacl::coordinate_matrix<T, AlignmentV> >
404 {
405  typedef typename cpu_value_type<T>::type type;
406 };
407 
408 template<typename T, unsigned int AlignmentV>
409 struct cpu_value_type<viennacl::ell_matrix<T, AlignmentV> >
410 {
411  typedef typename cpu_value_type<T>::type type;
412 };
413 
414 template<typename T, typename IndexT>
415 struct cpu_value_type<viennacl::sliced_ell_matrix<T, IndexT> >
416 {
417  typedef typename cpu_value_type<T>::type type;
418 };
419 
420 template<typename T, unsigned int AlignmentV>
421 struct cpu_value_type<viennacl::hyb_matrix<T, AlignmentV> >
422 {
423  typedef typename cpu_value_type<T>::type type;
424 };
425 
426 template<typename T, unsigned int AlignmentV>
427 struct cpu_value_type<viennacl::circulant_matrix<T, AlignmentV> >
428 {
429  typedef typename cpu_value_type<T>::type type;
430 };
431 
432 template<typename T, unsigned int AlignmentV>
433 struct cpu_value_type<viennacl::hankel_matrix<T, AlignmentV> >
434 {
435  typedef typename cpu_value_type<T>::type type;
436 };
437 
438 template<typename T, unsigned int AlignmentV>
439 struct cpu_value_type<viennacl::toeplitz_matrix<T, AlignmentV> >
440 {
441  typedef typename cpu_value_type<T>::type type;
442 };
443 
444 template<typename T, unsigned int AlignmentV>
445 struct cpu_value_type<viennacl::vandermonde_matrix<T, AlignmentV> >
446 {
447  typedef typename cpu_value_type<T>::type type;
448 };
449 
450 template<typename T1, typename T2, typename OP>
451 struct cpu_value_type<viennacl::matrix_expression<T1, T2, OP> >
452 {
453  typedef typename cpu_value_type<T1>::type type;
454 };
455 
456 
457 //
458 // Deduce compatible vector type for a matrix type
459 //
460 
461 template<typename T>
462 struct vector_for_matrix
463 {
464  typedef typename T::ERROR_CANNOT_DEDUCE_VECTOR_FOR_MATRIX_TYPE type;
465 };
466 
467 //ViennaCL
468 template<typename T, typename F, unsigned int A>
469 struct vector_for_matrix< viennacl::matrix<T, F, A> >
470 {
471  typedef viennacl::vector<T,A> type;
472 };
473 
474 template<typename T, unsigned int A>
475 struct vector_for_matrix< viennacl::compressed_matrix<T, A> >
476 {
477  typedef viennacl::vector<T,A> type;
478 };
479 
480 template<typename T, unsigned int A>
481 struct vector_for_matrix< viennacl::coordinate_matrix<T, A> >
482 {
483  typedef viennacl::vector<T,A> type;
484 };
485 
486 #ifdef VIENNACL_WITH_UBLAS
487 //Boost:
488 template<typename T, typename F, typename A>
489 struct vector_for_matrix< boost::numeric::ublas::matrix<T, F, A> >
490 {
491  typedef boost::numeric::ublas::vector<T> type;
492 };
493 
494 template<typename T, typename U, vcl_size_t A, typename B, typename C>
495 struct vector_for_matrix< boost::numeric::ublas::compressed_matrix<T, U, A, B, C> >
496 {
497  typedef boost::numeric::ublas::vector<T> type;
498 };
499 
500 template<typename T, typename U, vcl_size_t A, typename B, typename C>
501 struct vector_for_matrix< boost::numeric::ublas::coordinate_matrix<T, U, A, B, C> >
502 {
503  typedef boost::numeric::ublas::vector<T> type;
504 };
505 #endif
506 
507 template<typename T>
508 struct reference_if_nonscalar
509 {
510  typedef T & type;
511 };
512 
513 #define VIENNACL_REFERENCE_IF_NONSCALAR_INT(TNAME) \
514 template<> struct reference_if_nonscalar<TNAME> { typedef TNAME type; }; \
515 template<> struct reference_if_nonscalar<const TNAME> { typedef const TNAME type; }; \
516 template<> struct reference_if_nonscalar<unsigned TNAME> { typedef unsigned TNAME type; }; \
517 template<> struct reference_if_nonscalar<const unsigned TNAME> { typedef const unsigned TNAME type; };
518 
519  VIENNACL_REFERENCE_IF_NONSCALAR_INT(char)
520  VIENNACL_REFERENCE_IF_NONSCALAR_INT(short)
521  VIENNACL_REFERENCE_IF_NONSCALAR_INT(int)
522  VIENNACL_REFERENCE_IF_NONSCALAR_INT(long)
523 
524 #undef VIENNACL_REFERENCE_IF_NONSCALAR_INT
525 
526 template<>
527 struct reference_if_nonscalar<float>
528 {
529  typedef float type;
530 };
531 
532 template<>
533 struct reference_if_nonscalar<const float>
534 {
535  typedef const float type;
536 };
537 
538 template<>
539 struct reference_if_nonscalar<double>
540 {
541  typedef double type;
542 };
543 
544 template<>
545 struct reference_if_nonscalar<const double>
546 {
547  typedef const double type;
548 };
549 
552 //OpenCL equivalent type
554 template<typename T>
555 struct cl_type
556 {
557  typedef T type;
558 };
559 
561 #ifdef VIENNACL_WITH_OPENCL
562 template<>
563 struct cl_type<float>{ typedef cl_float type; };
564 
565 template<>
566 struct cl_type<double>{ typedef cl_double type; };
567 
568 template<>
569 struct cl_type<int>{ typedef cl_int type; };
570 
571 template<>
572 struct cl_type<unsigned int>{ typedef cl_uint type; };
573 
574 template<>
575 struct cl_type<long>{ typedef cl_long type; };
576 
577 template<>
578 struct cl_type<unsigned long>{ typedef cl_ulong type; };
579 
580 template<>
581 struct cl_type<short>{ typedef cl_short type; };
582 
583 template<>
584 struct cl_type<unsigned short>{ typedef cl_ushort type; };
585 
586 template<>
587 struct cl_type<char>{ typedef cl_char type; };
588 
589 template<>
590 struct cl_type<unsigned char>{ typedef cl_uchar type; };
591 #endif
592 
594 } //namespace result_of
595 } //namespace viennacl
596 
597 
598 #endif
Metafunction for deducing the OpenCL type for a numeric type, e.g. float -> cl_float.
Definition: result_of.hpp:555
This file provides the forward declarations for the main types used within ViennaCL.
Retrieves the alignment from a vector. Deprecated - will be replaced by a pure runtime facility in th...
Definition: result_of.hpp:65
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
Generic helper function for retrieving the value_type associated with type T.
Definition: result_of.hpp:190
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
std::size_t vcl_size_t
Definition: forwards.h:74
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:238
Helper meta function for retrieving the main RAM-based value type. Particularly important to obtain T...
Definition: result_of.hpp:236
float ScalarType
Definition: fft_1d.cpp:42
Generic meta-function for retrieving the size_type associated with type T.
Definition: result_of.hpp:135
T::ERROR_ARGUMENT_PROVIDED_IS_NOT_A_VECTOR_OR_A_MATRIX error_type
Definition: result_of.hpp:67