DUNE-ACFEM (unstable)

transpose.hh
1 #ifndef __DUNE_ACFEM_TENSORS_OPERATIONS_TRANSPOSE_HH__
2 #define __DUNE_ACFEM_TENSORS_OPERATIONS_TRANSPOSE_HH__
3 
4 #include "../../expressions/storage.hh"
5 #include "../../mpl/permutation.hh"
6 #include "../../mpl/sort.hh"
7 #include "../tensorbase.hh"
8 #include "../modules.hh"
9 
10 namespace Dune {
11 
12  namespace ACFem {
13 
14  // forward
15  template<class Perm>
16  struct TransposeOperation;
17 
26  namespace Tensor {
27 
28  template<class Tensor, class Perm>
29  class Transposition;
30 
50  template<class Tensor, std::size_t... Perm>
51  class Transposition<Tensor, Seq<Perm...> >
52  : public TensorBase<typename TensorTraits<Tensor>::FieldType,
53  Seq<Get<Perm, typename TensorTraits<Tensor>::Signature>::value...>,
54  Transposition<Tensor, Seq<Perm...> > >
55  , public Expressions::Storage<OperationTraits<TransposeOperation<Seq<Perm...> > >, Tensor>
56  {
57  static_assert(sizeof...(Perm) == TensorTraits<Tensor>::rank,
58  "A transposition must be provided for all tensor dimensions.");
59 
60  using ArgType = Tensor;
61  using HostType = std::decay_t<Tensor>;
62  using BaseType = TensorBase<typename HostType::FieldType,
63  Seq<Get<Perm, typename TensorTraits<Tensor>::Signature>::value...>,
64  Transposition<Tensor, Seq<Perm...> > >;
65  using StorageType = Expressions::Storage<OperationTraits<TransposeOperation<Seq<Perm...> > >, Tensor>;
66  public:
67  using Permutation = Seq<Perm...>;
68  using StorageType::operation;
69  using StorageType::operand;
70  using BaseType::rank;
71  using typename BaseType::Signature;
72  using typename BaseType::FieldType;
73 
74  public:
77  template<class Arg, std::enable_if_t<std::is_constructible<ArgType, Arg>::value, int> = 0>
78  Transposition(Arg&& host)
79  : StorageType(std::forward<Arg>(host))
80  {}
81 
83  template<
84  class... Dummy,
85  std::enable_if_t<(sizeof...(Dummy) == 0
86  && IsTypedValue<ArgType>::value), int> = 0>
87  Transposition(Dummy&&...)
88  : StorageType(ArgType{})
89  {}
90 
94  template<class... Dims,
95  std::enable_if_t<(sizeof...(Dims) == rank
96  &&
98  , int> = 0>
99  decltype(auto) operator()(Dims... indices)
100  {
101  return operand(0_c)(std::get<Perm>(std::forward_as_tuple(indices...))...);
102  }
103 
107  template<class... Dims,
108  std::enable_if_t<(sizeof...(Dims) == rank
109  &&
111  , int> = 0>
112  decltype(auto) operator()(Dims... indices) const
113  {
114  return operand(0_c)(std::get<Perm>(std::forward_as_tuple(indices...))...);
115  }
116 
117  template<std::size_t... Indices, std::enable_if_t<sizeof...(Indices) == rank, int> = 0>
118  decltype(auto) constexpr operator()(Seq<Indices...>) const
119  {
120  return operand(0_c)(Seq<Get<Perm, Seq<Indices...> >::value...>{});
121  }
122 
123  template<std::size_t... Indices, class Pos = MakeIndexSequence<sizeof...(Indices)> >
124  static bool constexpr isZero(Seq<Indices...> = Seq<Indices...>{}, Pos = Pos{})
125  {
126  // Truncate Pos to size of Indices...
127  using RealPos = HeadPart<sizeof...(Indices), Pos>;
128 
129  // Map to the transposed positions.
130  using TransposedPos = PermuteSequenceValues<RealPos, Seq<Perm...> >;
131 
132  using SortedPos = typename SortSequence<TransposedPos>::Result;
133  using Permutation = typename SortSequence<TransposedPos>::Permutation;
134  using SortedIndices = PermuteSequence<Seq<Indices...>, Permutation>;
135 
136  return HostType::isZero(SortedIndices{}, SortedPos{});
137  }
138 
139  std::string name() const
140  {
141  using T = Tensor;
142  std::string pfx = std::is_reference<T>::value ? (RefersConst<T>::value ? "cref" : "ref") : "";
143  return operationName(operation(), pfx+operand(0_c).name());
144  }
145 
146  };
147 
149  template<class T>
151  : FalseType
152  {};
153 
154  template<class T, std::size_t... Perm>
155  struct IsTransposition<Transposition<T, Seq<Perm...> > >
156  : TrueType
157  {};
158 
160  template<std::size_t... Perm, class T,
161  std::enable_if_t<(IsProperTensor<T>::value
162  && isPermutation(ResizedPermutation<Seq<Perm...>, TensorTraits<T>::rank>{})
163  && sizeof...(Perm) <= TensorTraits<T>::rank
164  ), int> = 0>
165  constexpr decltype(auto) transpose(T&& t, Seq<Perm...> = Seq<Perm...>{})
166  {
167  using Operation = TransposeOperation<ResizedPermutation<Seq<Perm...>, TensorTraits<T>::rank> >;
168 
169  return Expressions::finalize<Operation>(std::forward<T>(t));
170  }
171 
174  template<class Perm, class T, std::enable_if_t<Perm::size() == TensorTraits<T>::rank, int> = 0>
175  constexpr auto operate(Expressions::DontOptimize, OperationTraits<TransposeOperation<Perm> >, T&& t)
176  {
177  DUNE_ACFEM_RECORD_OPTIMIZATION;
178 
179  return Transposition<T, Perm>(std::forward<T>(t));
180  }
181 
182  } // NS Tensor
183 
184  //using Tensor::transpose;
185 
186  } // NS ACFem
187 
188  template<class T, class P>
189  struct FieldTraits<ACFem::Tensor::Transposition<T, P> >
190  : FieldTraits<std::decay_t<T> >
191  {};
192 
194 
196 
197 } // NS Dune
198 
199 #endif // __DUNE_ACFEM_TENSORS_OPERATIONS_TRANSPOSE_HH__
OptimizeTag< 0 > DontOptimize
Bottom level is overloaded to do nothing.
Definition: optimizationbase.hh:74
std::string operationName(F &&f, const std::string &arg)
Verbose print of an operation, helper function to produce noise.
Definition: operationtraits.hh:601
BoolConstant< ExpressionTraits< T >::isTypedValue > IsTypedValue
Compile-time true if T is a "typed value", e.g. a std::integral_constant.
Definition: expressiontraits.hh:90
typename GetHeadPartHelper< Cnt, Seq >::Type HeadPart
Extract Cnt many consecutive elements from the front of Seq.
Definition: access.hh:217
MakeSequence< std::size_t, N, Offset, Stride, Repeat > MakeIndexSequence
Make a sequence of std::size_t elements.
Definition: generators.hh:34
constexpr auto operate(Expressions::DontOptimize, OperationTraits< TransposeOperation< Perm > >, T &&t)
Definition: transpose.hh:175
Transposition(Arg &&host)
Constructor from a given host-tensor.
Definition: transpose.hh:78
Transposition(Dummy &&...)
Allow default construction if contained types fulfill IsTypedValue.
Definition: transpose.hh:87
constexpr decltype(auto) transpose(T &&t, Seq< Perm... >=Seq< Perm... >{})
Promote operands to tensor transposition operation.
Definition: operandpromotion.hh:309
typename ResizedPermutationHelper< Permutation, N >::Type ResizedPermutation
Pad or truncate the permuation at the given size.
Definition: permutation.hh:83
typename TranspositionHelper< I0, I1, N >::Type Transposition
Generate the transposition of I0 and I1 as permutation.
Definition: permutation.hh:206
constexpr bool isPermutation(IndexSequence< Ind... >=IndexSequence< Ind... >{})
Definition: compare.hh:362
decltype(isIntegralPack(std::declval< T >()...)) IsIntegralPack
Decide whether the given parameter pack contains only integral types.
Definition: compare.hh:377
typename PermuteSequenceValuesHelper< Perm, Sequence >::Type PermuteSequenceValues
Apply the given permutation to the values of the given sequence.
Definition: permutation.hh:101
std::decay_t< decltype(permute(Sequence{}, Perm{}))> PermuteSequence
Apply the given permutation to the positions of the given sequence.
Definition: permutation.hh:137
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
Gets the type of the n-th element of a tuple-like or the std::integral_constant corresponding to the ...
Definition: access.hh:42
Traits in order to identify a TensorView class.
Definition: transpose.hh:152
Base class for all tensors.
Definition: tensorbase.hh:144
Permutation of index positions of tensors.
Definition: expressionoperations.hh:167
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)