MiniTensor Version of the Day
Loading...
Searching...
No Matches
MiniTensor_Traits.h
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MiniTensor Package
4//
5// Copyright 2016 NTESS and the MiniTensor contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#if !defined(MiniTensor_Traits_h)
11#define MiniTensor_Traits_h
12
13#include <complex>
14#include <type_traits>
15
16#include "MiniTensor_config.h"
17#include "Sacado.hpp"
18
19#if !defined( KOKKOS_INLINE_FUNCTION )
20#define KOKKOS_INLINE_FUNCTION inline
21#endif
22
23#if defined(KOKKOS_ENABLE_CUDA)
24#define MT_ERROR_EXIT(...) \
25 Kokkos::abort(#__VA_ARGS__)
26#else
27#define MT_ERROR_EXIT(...) \
28 fprintf(stderr, "ERROR in: %s\n", __PRETTY_FUNCTION__); \
29 fprintf(stderr, __VA_ARGS__); \
30 fprintf(stderr, "\n"); \
31 exit(1)
32#endif // KOKKOS_ENABLE_CUDA
33
34#if defined(KOKKOS_ENABLE_CUDA)
35#define MT_WARNING(...) \
36 Kokkos::abort(#__VA_ARGS__)
37#else
38#define MT_WARNING(...) \
39 fprintf(stderr, "WARNING in: %s\n", __PRETTY_FUNCTION__); \
40 fprintf(stderr, __VA_ARGS__); \
41 fprintf(stderr, "\n")
42#endif // KOKKOS_ENABLE_CUDA
43
44namespace minitensor {
45
48
50using Index = uint32_t;
51
55constexpr Index
57
59using LongIndex = uint64_t;
60
64constexpr Index
66
68using Real = double;
69
71using Complex = std::complex<Real>;
72
74template <typename T, Index N> class Vector;
75template <typename T, Index N> class Tensor;
76template <typename T, Index N> class Tensor3;
77template <typename T, Index N> class Tensor4;
78template <typename T, Index M, Index N> class Matrix;
79
81constexpr Index
83
92template<Index N, Index D>
93inline constexpr bool
95
97using Sacado::Promote;
98using Sacado::mpl::lazy_disable_if;
99using Sacado::mpl::disable_if_c;
100
102template <typename T>
103struct is_vector {
105 static bool const value = false;
106};
107
109template <typename T, Index N>
110struct is_vector<Vector<T, N>> {
112 static bool const value = true;
113};
114
119template <typename T, Index N>
124
126template <typename T>
127struct is_tensor {
129 static bool const value = false;
130};
131
133template <typename T, Index N>
134struct is_tensor<Tensor<T, N>> {
136 static bool const value = true;
137};
138
143template <typename T, Index N>
148
150template <typename T>
153 static bool const value = false;
154};
155
157template <typename T, Index N>
158struct is_tensor3<Tensor3<T, N>> {
160 static bool const value = true;
161};
162
167template <typename T, Index N>
172
174template <typename T>
177 static bool const value = false;
178};
179
181template <typename T, Index N>
182struct is_tensor4<Tensor4<T, N>> {
184 static bool const value = true;
185};
186
191template <typename T, Index N>
196
198template <typename T>
199struct is_matrix {
201 static bool const value = false;
202};
203
205template <typename T, Index M, Index N>
206struct is_matrix<Matrix<T, M, N>> {
208 static bool const value = true;
209};
210
215template <typename T, Index M, Index N>
220
222template <typename T>
225 static bool const value = false;
226};
227
229template <typename T, Index N>
230struct order_1234<Vector<T, N>> {
232 static bool const value = true;
233};
234
236template <typename T, Index N>
237struct order_1234<Tensor<T, N>> {
239 static bool const value = true;
240};
241
243template <typename T, Index N>
244struct order_1234<Tensor3<T, N>> {
246 static bool const value = true;
247};
248
250template <typename T, Index N>
251struct order_1234<Tensor4<T, N>> {
253 static bool const value = true;
254 };
255
257template<typename T, Index M, Index N>
258struct order_1234<Matrix<T, M, N>>{
260 static bool const value = true;
261};
262
264
265using std::string;
266
271template<Index N>
274 static string eval() {return string("INVALID");}
275};
276
278template<>
281 static string eval() {return string("DYNAMIC");}
282};
283
285template<>
288 static string eval() {return string("1");}
289};
290
292template<>
295 static string eval() {return string("2");}
296};
297
299template<>
302 static string eval() {return string("3");}
303};
304
306template<>
309 static string eval() {return string("4");}
310};
311
313} // namespace minitensor
314
315namespace Sacado {
316
325using std::complex;
326using std::string;
327
329template<>
330struct Promote<double, Index> {
332 typedef double type;
333};
334
336template<>
337struct Promote<Index, double> {
339 typedef double type;
340};
341
343template<>
344struct Promote<float, Index> {
346 typedef float type;
347};
348
350template<>
351struct Promote<Index, float> {
353 typedef float type;
354};
355
357template<>
358struct Promote<complex<double>, Index> {
360 typedef complex<double> type;
361};
362
364template<>
365struct Promote<Index, complex<double>> {
367 typedef complex<double> type;
368};
369
371template<>
372struct Promote<complex<float>, Index> {
374 typedef complex<float> type;
375};
376
378template<>
379struct Promote<Index, complex<float>> {
381 typedef complex<float> type;
382};
383
385template <typename T, Index N>
386struct ScalarType<Vector<T, N>> {
388 typedef typename ScalarType<T>::type type;
389};
390
392template <typename T, Index N>
393struct ValueType<Vector<T, N>> {
395 typedef typename ValueType<T>::type type;
396};
397
399template <typename T, Index N>
400struct IsADType<Vector<T, N>> {
402 static bool const value = IsADType<T>::value;
403};
404
406template <typename T, Index N>
407struct IsScalarType<Vector<T, N>> {
409 static bool const value = IsScalarType<T>::value;
410};
411
413template <typename T, Index N>
414struct Value<Vector<T, N>> {
416 typedef typename ValueType<Vector<T, N>>::type value_type;
418 static const Vector<value_type, N>
420 {
422
423 for (Index i = 0; i < x.get_number_components(); ++i) {
424 v[i] = Value<T>::eval(x[i]);
425 }
426
427 return v;
428 }
429};
430
432template <typename T, Index N>
433struct ScalarValue<Vector<T, N>> {
435 typedef typename ScalarType<Vector<T, N>>::type scalar_type;
438 static const Vector<scalar_type, N>
440 {
442
443 for (Index i = 0; i < x.get_number_components(); ++i) {
444 v[i] = ScalarValue<T>::eval(x[i]);
445 }
446 return v;
447 }
448};
449
451template <typename T, Index N>
452struct StringName<Vector<T, N>> {
454 static string
456 {
457 return string("Vector<") + StringName<T>::eval() + string(", ") +
458 dimension_string<N>::eval() + string(">");
459 }
460};
461
463template <typename T, Index N>
464struct IsEqual<Vector<T, N>> {
466 static bool eval(T const & x, T const & y) { return x == y; }
467};
468
470template <typename T, Index N>
471struct IsStaticallySized<Vector<T, N>> {
473 static bool const value = true;
474};
475
477template <typename T>
478struct IsStaticallySized<Vector<T, DYNAMIC>>
479{
481 static bool const value = false;
482};
483
485template <typename T, Index N>
486struct ScalarType<Tensor<T, N>> {
488 typedef typename ScalarType<T>::type type;
489};
490
492template <typename T, Index N>
493struct ValueType<Tensor<T, N>> {
495 typedef typename ValueType<T>::type type;
496};
497
499template <typename T, Index N>
500struct IsADType<Tensor<T, N>> {
502 static bool const value = IsADType<T>::value;
503};
504
506template <typename T, Index N>
507struct IsScalarType<Tensor<T, N>> {
509 static bool const value = IsScalarType<T>::value;
510};
511
513template <typename T, Index N>
514struct Value<Tensor<T, N>> {
516 typedef typename ValueType<Tensor<T, N>>::type value_type;
518 static const Tensor<value_type, N>
520 {
522
523 for (Index i = 0; i < x.get_number_components(); ++i) {
524 v[i] = Value<T>::eval(x[i]);
525 }
526
527 return v;
528 }
529};
530
532template <typename T, Index N>
533struct ScalarValue<Tensor<T, N>> {
535 typedef typename ScalarType<Tensor<T, N>>::type scalar_type;
538 static const Tensor<scalar_type, N>
540 {
542
543 for (Index i = 0; i < x.get_number_components(); ++i) {
544 v[i] = ScalarValue<T>::eval(x[i]);
545 }
546
547 return v;
548 }
549};
550
552template <typename T, Index N>
553struct StringName<Tensor<T, N>> {
555 static string
557 {
558 return string("Tensor<") + StringName<T>::eval() + string(", ") +
559 dimension_string<N>::eval() + string(">");
560 }
561};
562
564template <typename T, Index N>
565struct IsEqual<Tensor<T, N>> {
567 static bool eval(T const & x, T const & y) { return x == y; }
568};
569
571template <typename T, Index N>
572struct IsStaticallySized<Tensor<T, N>> {
574 static bool const value = true;
575};
576
578template <typename T>
579struct IsStaticallySized<Tensor<T, DYNAMIC>>
580{
582 static bool const value = false;
583};
584
586template <typename T, Index N>
587struct ScalarType<Tensor3<T, N>> {
589 typedef typename ScalarType<T>::type type;
590};
591
593template <typename T, Index N>
594struct ValueType<Tensor3<T, N>> {
596 typedef typename ValueType<T>::type type;
597};
598
600template <typename T, Index N>
601struct IsADType<Tensor3<T, N>> {
603 static bool const value = IsADType<T>::value;
604};
605
607template <typename T, Index N>
608struct IsScalarType<Tensor3<T, N>> {
610 static bool const value = IsScalarType<T>::value;
611};
612
614template <typename T, Index N>
615struct Value<Tensor3<T, N>> {
617 typedef typename ValueType<Tensor3<T, N>>::type value_type;
619 static const Tensor3<value_type, N>
621 {
623
624 for (Index i = 0; i < x.get_number_components(); ++i) {
625 v[i] = Value<T>::eval(x[i]);
626 }
627
628 return v;
629 }
630};
631
633template <typename T, Index N>
634struct ScalarValue<Tensor3<T, N>> {
636 typedef typename ScalarType<Tensor3<T, N>>::type scalar_type;
639 static const Tensor3<scalar_type, N>
641 {
643
644 for (Index i = 0; i < x.get_number_components(); ++i) {
645 v[i] = ScalarValue<T>::eval(x[i]);
646 }
647
648 return v;
649 }
650};
651
653template <typename T, Index N>
654struct StringName<Tensor3<T, N>> {
656 static string
658 {
659 return string("Tensor3<") + StringName<T>::eval() + string(", ") +
660 dimension_string<N>::eval() + string(">");
661 }
662};
663
665template <typename T, Index N>
666struct IsEqual<Tensor3<T, N>> {
668 static bool eval(T const & x, T const & y) { return x == y; }
669};
670
672template <typename T, Index N>
673struct IsStaticallySized<Tensor3<T, N>>
674{
676 static bool const value = true;
677};
678
680template <typename T>
681struct IsStaticallySized<Tensor3<T, DYNAMIC>>
682{
684 static bool const value = false;
685};
686
688template <typename T, Index N>
689struct ScalarType<Tensor4<T, N>> {
691 typedef typename ScalarType<T>::type type;
692};
693
695template <typename T, Index N>
696struct ValueType<Tensor4<T, N>> {
698 typedef typename ValueType<T>::type type;
699};
700
702template <typename T, Index N>
703struct IsADType<Tensor4<T, N>> {
705 static bool const value = IsADType<T>::value;
706};
707
709template <typename T, Index N>
710struct IsScalarType<Tensor4<T, N>> {
712 static bool const value = IsScalarType<T>::value;
713};
714
716template <typename T, Index N>
717struct Value<Tensor4<T, N>> {
719 typedef typename ValueType<Tensor4<T, N>>::type value_type;
721 static const Tensor4<value_type, N>
723 {
725
726 for (Index i = 0; i < x.get_number_components(); ++i) {
727 v[i] = Value<T>::eval(x[i]);
728 }
729
730 return v;
731 }
732};
733
735template <typename T, Index N>
736struct ScalarValue<Tensor4<T, N>> {
738 typedef typename ScalarType<Tensor4<T, N>>::type scalar_type;
741 static const Tensor4<scalar_type, N>
743 {
745
746 for (Index i = 0; i < x.get_number_components(); ++i) {
747 v[i] = ScalarValue<T>::eval(x[i]);
748 }
749
750 return v;
751 }
752};
753
755template <typename T, Index N>
756struct StringName<Tensor4<T, N>> {
758 static string
760 {
761 return string("Tensor4<") + StringName<T>::eval() + string(", ") +
762 dimension_string<N>::eval() + string(">");
763 }
764};
765
767template <typename T, Index N>
768struct IsEqual<Tensor4<T, N>> {
770 static bool eval(T const & x, T const & y) { return x == y; }
771};
772
774template <typename T, Index N>
775struct IsStaticallySized<Tensor4<T, N>>
776{
778 static bool const value = true;
779};
780
782template <typename T>
783struct IsStaticallySized<Tensor4<T, DYNAMIC>>
784{
786 static bool const value= false;
787};
788
790template <typename T, Index M, Index N>
791struct ScalarType<Matrix<T, M, N>> {
793 typedef typename ScalarType<T>::type type;
794};
795
797template <typename T, Index M, Index N>
798struct ValueType<Matrix<T, M, N>> {
800 typedef typename ValueType<T>::type type;
801};
802
804template <typename T, Index M, Index N>
805struct IsADType<Matrix<T, M, N>> {
807 static bool const value = IsADType<T>::value;
808};
809
811template <typename T, Index M, Index N>
812struct IsScalarType<Matrix<T, M, N>> {
814 static bool const value = IsScalarType<T>::value;
815};
816
818template <typename T, Index M, Index N>
819struct Value<Matrix<T, M, N>> {
821 typedef typename ValueType<Matrix<T, M, N>>::type value_type;
823 static const Matrix<value_type, M, N>
825 {
827
828 for (Index i = 0; i < x.get_number_components(); ++i) {
829 v[i] = Value<T>::eval(x[i]);
830 }
831
832 return v;
833 }
834};
835
837template <typename T, Index M, Index N>
838struct ScalarValue<Matrix<T, M, N>> {
840 typedef typename ScalarType<Matrix<T, M, N>>::type scalar_type;
843 static const Matrix<scalar_type, M, N>
845 {
847
848 for (Index i = 0; i < x.get_number_components(); ++i) {
849 v[i] = ScalarValue<T>::eval(x[i]);
850 }
851
852 return v;
853 }
854};
855
857template <typename T, Index M, Index N>
858struct StringName<Matrix<T, M, N>> {
860 static string
862 {
863 return string("Matrix<") + StringName<T>::eval() + string(", ") +
864 dimension_string<M>::eval() + string(", ") +
865 dimension_string<N>::eval() + string(">");
866 }
867};
868
870template <typename T, Index M, Index N>
871struct IsEqual<Matrix<T, M, N>> {
873 static bool eval(T const & x, T const & y) { return x == y; }
874};
875
877template <typename T, Index M, Index N>
878struct IsStaticallySized<Matrix<T, M, N>> {
880 static bool const value = true;
881};
882
885template <typename T, Index M>
886struct IsStaticallySized<Matrix<T, M, DYNAMIC>>
887{
889 static bool const value = false;
890};
891
894template <typename T, Index N>
895struct IsStaticallySized<Matrix<T, DYNAMIC, N>>
896{
898 static bool const value = false;
899};
900
903template <typename T>
904struct IsStaticallySized<Matrix<T, DYNAMIC, DYNAMIC>>
905{
907 static bool const value = false;
908};
909
910} // namespace Sacado
911
912#endif // MiniTensor_Traits_h
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION Index get_num_cols() const
KOKKOS_INLINE_FUNCTION Index get_num_rows() const
KOKKOS_INLINE_FUNCTION Index get_number_components() const
double Real
Floating point type.
uint32_t Index
Indexing type.
constexpr bool dimension_reachable
constexpr Index INDEX_SIZE
constexpr Index DYNAMIC
Indicator for dynamic storage.
constexpr Index LONG_INDEX_SIZE
std::complex< Real > Complex
Complex type.
uint64_t LongIndex
High count type.
static bool eval(T const &x, T const &y)
Compare two components for equality.
static bool eval(T const &x, T const &y)
Compare two components for equality.
static bool eval(T const &x, T const &y)
Compare two components for equality.
static bool eval(T const &x, T const &y)
Compare two components for equality.
static bool eval(T const &x, T const &y)
Compare two components for equality.
ScalarType< T >::type type
Underlying scalar type of the Matrix components.
ScalarType< T >::type type
Underlying scalar type of the Tensor3 components.
ScalarType< T >::type type
Underlying scalar type of the Tensor4 components.
ScalarType< T >::type type
Underlying scalar type of the Tensor components.
ScalarType< T >::type type
Underlying scalar type of the Vector components.
static const Matrix< scalar_type, M, N > eval(Matrix< T, M, N > const &x)
ScalarType< Matrix< T, M, N > >::type scalar_type
Scalar type of the Matrix components.
ScalarType< Tensor3< T, N > >::type scalar_type
Scalar type of the Tensor3 components.
static const Tensor3< scalar_type, N > eval(Tensor3< T, N > const &x)
ScalarType< Tensor4< T, N > >::type scalar_type
Scalar type of the Tensor4 components.
static const Tensor4< scalar_type, N > eval(Tensor4< T, N > const &x)
static const Tensor< scalar_type, N > eval(Tensor< T, N > const &x)
ScalarType< Tensor< T, N > >::type scalar_type
Scalar type of the Tensor components.
ScalarType< Vector< T, N > >::type scalar_type
Scalar type of the Vector components.
static const Vector< scalar_type, N > eval(Vector< T, N > const &x)
static string eval()
Return the name of the Matrix type as a string.
static string eval()
Return the name of the Tensor3 type as a string.
static string eval()
Return the name of the Tensor4 type as a string.
static string eval()
Return the name of the Tensor type as a string.
static string eval()
Return the name of the Vector type as a string.
ValueType< Matrix< T, M, N > >::type value_type
Value type of the Matrix components.
static const Matrix< value_type, M, N > eval(Matrix< T, M, N > const &x)
Extract a Matrix of the values of the components of x.
static const Tensor3< value_type, N > eval(Tensor3< T, N > const &x)
Extract a Tensor3 of the values of the components of x.
ValueType< Tensor3< T, N > >::type value_type
Value type of the Tensor3 components.
static const Tensor4< value_type, N > eval(Tensor4< T, N > const &x)
Extract a Tensor4 of the values of the components of x.
ValueType< Tensor4< T, N > >::type value_type
Value type of the Tensor4 components.
static const Tensor< value_type, N > eval(Tensor< T, N > const &x)
Extract a Tensor of the values of the components of x.
ValueType< Tensor< T, N > >::type value_type
Value type of the Tensor components.
static const Vector< value_type, N > eval(Vector< T, N > const &x)
Extract a Vector of the values of the components of x.
ValueType< Vector< T, N > >::type value_type
Value type of the Vector components.
ValueType< T >::type type
Value type of the Matrix components.
ValueType< T >::type type
Value type of the Tensor3 components.
ValueType< T >::type type
Value type of the Tensor4 components.
ValueType< T >::type type
Value type of the Tensor components.
ValueType< T >::type type
Value type of the Vector components.
Matrix< typename T::type, M, N > type
Matrix with element type T::type.
Tensor3< typename T::type, N > type
Tensor3 with element type T::type.
Tensor4< typename T::type, N > type
Tensor4 with element type T::type.
Tensor< typename T::type, N > type
Tensor with element type T::type.
Vector< typename T::type, N > type
Vector with element type T::type.
static string eval()
Return "1".
static string eval()
Return "2".
static string eval()
Return "3".
static string eval()
Return "4".
static string eval()
Return "DYNAMIC".
static string eval()
Return "INVALID" for dimensions without a specialization.
static bool const value
Whether T is a Matrix.
static bool const value
Whether T is a Tensor3.
static bool const value
Whether T is a Tensor4.
static bool const value
Whether T is a Tensor.
static bool const value
Whether T is a Vector.
Tensors from 1st to 4th order and matrix.
static bool const value
Whether T is a tensor of order 1 to 4 or a matrix.