TensorGenerator.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Generator, typename XprType>
24 struct traits<TensorGeneratorOp<Generator, XprType> > : public traits<XprType>
25 {
26  typedef typename XprType::Scalar Scalar;
27  typedef traits<XprType> XprTraits;
28  typedef typename packet_traits<Scalar>::type Packet;
29  typedef typename XprTraits::StorageKind StorageKind;
30  typedef typename XprTraits::Index Index;
31  typedef typename XprType::Nested Nested;
32  typedef typename remove_reference<Nested>::type _Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
35 };
36 
37 template<typename Generator, typename XprType>
38 struct eval<TensorGeneratorOp<Generator, XprType>, Eigen::Dense>
39 {
40  typedef const TensorGeneratorOp<Generator, XprType>& type;
41 };
42 
43 template<typename Generator, typename XprType>
44 struct nested<TensorGeneratorOp<Generator, XprType>, 1, typename eval<TensorGeneratorOp<Generator, XprType> >::type>
45 {
46  typedef TensorGeneratorOp<Generator, XprType> type;
47 };
48 
49 } // end namespace internal
50 
51 
52 
53 template<typename Generator, typename XprType>
54 class TensorGeneratorOp : public TensorBase<TensorGeneratorOp<Generator, XprType>, ReadOnlyAccessors>
55 {
56  public:
57  typedef typename Eigen::internal::traits<TensorGeneratorOp>::Scalar Scalar;
58  typedef typename Eigen::internal::traits<TensorGeneratorOp>::Packet Packet;
59  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
60  typedef typename XprType::CoeffReturnType CoeffReturnType;
61  typedef typename XprType::PacketReturnType PacketReturnType;
62  typedef typename Eigen::internal::nested<TensorGeneratorOp>::type Nested;
63  typedef typename Eigen::internal::traits<TensorGeneratorOp>::StorageKind StorageKind;
64  typedef typename Eigen::internal::traits<TensorGeneratorOp>::Index Index;
65 
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorGeneratorOp(const XprType& expr, const Generator& generator)
67  : m_xpr(expr), m_generator(generator) {}
68 
69  EIGEN_DEVICE_FUNC
70  const Generator& generator() const { return m_generator; }
71 
72  EIGEN_DEVICE_FUNC
73  const typename internal::remove_all<typename XprType::Nested>::type&
74  expression() const { return m_xpr; }
75 
76  protected:
77  typename XprType::Nested m_xpr;
78  const Generator m_generator;
79 };
80 
81 
82 // Eval as rvalue
83 template<typename Generator, typename ArgType, typename Device>
84 struct TensorEvaluator<const TensorGeneratorOp<Generator, ArgType>, Device>
85 {
86  typedef TensorGeneratorOp<Generator, ArgType> XprType;
87  typedef typename XprType::Index Index;
88  typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
89  static const int NumDims = internal::array_size<Dimensions>::value;
90  typedef typename XprType::Scalar Scalar;
91 
92  enum {
93  IsAligned = false,
94  PacketAccess = (internal::packet_traits<Scalar>::size > 1),
95  BlockAccess = false,
96  Layout = TensorEvaluator<ArgType, Device>::Layout,
97  CoordAccess = false, // to be implemented
98  };
99 
100  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
101  : m_generator(op.generator())
102  {
103  TensorEvaluator<ArgType, Device> impl(op.expression(), device);
104  m_dimensions = impl.dimensions();
105 
106  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
107  m_strides[0] = 1;
108  for (int i = 1; i < NumDims; ++i) {
109  m_strides[i] = m_strides[i - 1] * m_dimensions[i - 1];
110  }
111  } else {
112  m_strides[NumDims - 1] = 1;
113  for (int i = NumDims - 2; i >= 0; --i) {
114  m_strides[i] = m_strides[i + 1] * m_dimensions[i + 1];
115  }
116  }
117  }
118 
119  typedef typename XprType::CoeffReturnType CoeffReturnType;
120  typedef typename XprType::PacketReturnType PacketReturnType;
121 
122  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
123 
124  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
125  return true;
126  }
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
128  }
129 
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
131  {
132  array<Index, NumDims> coords;
133  extract_coordinates(index, coords);
134  return m_generator(coords);
135  }
136 
137  template<int LoadMode>
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
139  {
140  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
141  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
142  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
143 
144  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
145  for (int i = 0; i < packetSize; ++i) {
146  values[i] = coeff(index+i);
147  }
148  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
149  return rslt;
150  }
151 
152  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
153 
154  protected:
155  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
156  void extract_coordinates(Index index, array<Index, NumDims>& coords) const {
157  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
158  for (int i = NumDims - 1; i > 0; --i) {
159  const Index idx = index / m_strides[i];
160  index -= idx * m_strides[i];
161  coords[i] = idx;
162  }
163  coords[0] = index;
164  } else {
165  for (int i = 0; i < NumDims - 1; ++i) {
166  const Index idx = index / m_strides[i];
167  index -= idx * m_strides[i];
168  coords[i] = idx;
169  }
170  coords[NumDims-1] = index;
171  }
172  }
173 
174  Dimensions m_dimensions;
175  array<Index, NumDims> m_strides;
176  Generator m_generator;
177 };
178 
179 } // end namespace Eigen
180 
181 #endif // EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13