TensorForcedEval.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 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_FORCED_EVAL_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename XprType>
24 struct traits<TensorForcedEvalOp<XprType> >
25 {
26  // Type promotion to handle the case where the types of the lhs and the rhs are different.
27  typedef typename XprType::Scalar Scalar;
28  typedef traits<XprType> XprTraits;
29  typedef typename traits<XprType>::StorageKind StorageKind;
30  typedef typename traits<XprType>::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  enum {
37  Flags = 0
38  };
39 };
40 
41 template<typename XprType>
42 struct eval<TensorForcedEvalOp<XprType>, Eigen::Dense>
43 {
44  typedef const TensorForcedEvalOp<XprType>& type;
45 };
46 
47 template<typename XprType>
48 struct nested<TensorForcedEvalOp<XprType>, 1, typename eval<TensorForcedEvalOp<XprType> >::type>
49 {
50  typedef TensorForcedEvalOp<XprType> type;
51 };
52 
53 } // end namespace internal
54 
55 
56 
57 template<typename XprType>
58 class TensorForcedEvalOp : public TensorBase<TensorForcedEvalOp<XprType>, ReadOnlyAccessors>
59 {
60  public:
61  typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Scalar Scalar;
62  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
63  typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
64  typedef typename Eigen::internal::nested<TensorForcedEvalOp>::type Nested;
65  typedef typename Eigen::internal::traits<TensorForcedEvalOp>::StorageKind StorageKind;
66  typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Index Index;
67 
68  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(const XprType& expr)
69  : m_xpr(expr) {}
70 
71  EIGEN_DEVICE_FUNC
72  const typename internal::remove_all<typename XprType::Nested>::type&
73  expression() const { return m_xpr; }
74 
75  protected:
76  typename XprType::Nested m_xpr;
77 };
78 
79 
80 template<typename ArgType, typename Device>
81 struct TensorEvaluator<const TensorForcedEvalOp<ArgType>, Device>
82 {
83  typedef TensorForcedEvalOp<ArgType> XprType;
84  typedef typename ArgType::Scalar Scalar;
85  typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
86  typedef typename XprType::Index Index;
87  typedef typename XprType::CoeffReturnType CoeffReturnType;
88  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
89  static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
90 
91  enum {
92  IsAligned = true,
93  PacketAccess = (PacketSize > 1),
94  Layout = TensorEvaluator<ArgType, Device>::Layout,
95  RawAccess = true
96  };
97 
98  EIGEN_DEVICE_FUNC TensorEvaluator(const XprType& op, const Device& device)
99  : m_impl(op.expression(), device), m_op(op.expression()), m_device(device), m_buffer(NULL)
100  { }
101 
102  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
103 
104  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(CoeffReturnType*) {
105  const Index numValues = m_impl.dimensions().TotalSize();
106  m_buffer = (CoeffReturnType*)m_device.allocate(numValues * sizeof(CoeffReturnType));
107  // Should initialize the memory in case we're dealing with non POD types.
108  if (NumTraits<CoeffReturnType>::RequireInitialization) {
109  for (Index i = 0; i < numValues; ++i) {
110  new(m_buffer+i) CoeffReturnType();
111  }
112  }
113  typedef TensorEvalToOp<const ArgType> EvalTo;
114  EvalTo evalToTmp(m_buffer, m_op);
115  const bool PacketAccess = internal::IsVectorizable<Device, const ArgType>::value;
116  internal::TensorExecutor<const EvalTo, Device, PacketAccess>::run(evalToTmp, m_device);
117  return true;
118  }
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
120  m_device.deallocate(m_buffer);
121  m_buffer = NULL;
122  }
123 
124  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
125  {
126  return m_buffer[index];
127  }
128 
129  template<int LoadMode>
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
131  {
132  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
133  }
134 
135  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
136  return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
137  }
138 
139  EIGEN_DEVICE_FUNC Scalar* data() const { return m_buffer; }
140 
141  private:
142  TensorEvaluator<ArgType, Device> m_impl;
143  const ArgType m_op;
144  const Device& m_device;
145  CoeffReturnType* m_buffer;
146 };
147 
148 
149 } // end namespace Eigen
150 
151 #endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45