10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H 11 #define EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H 23 template<
typename Shuffle,
typename XprType>
24 struct traits<TensorShufflingOp<Shuffle, XprType> > :
public traits<XprType>
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;
37 template<
typename Shuffle,
typename XprType>
38 struct eval<TensorShufflingOp<Shuffle, XprType>,
Eigen::Dense>
40 typedef const TensorShufflingOp<Shuffle, XprType>& type;
43 template<
typename Shuffle,
typename XprType>
44 struct nested<TensorShufflingOp<Shuffle, XprType>, 1, typename eval<TensorShufflingOp<Shuffle, XprType> >::type>
46 typedef TensorShufflingOp<Shuffle, XprType> type;
53 template<
typename Shuffle,
typename XprType>
54 class TensorShufflingOp :
public TensorBase<TensorShufflingOp<Shuffle, XprType> >
57 typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
58 typedef typename Eigen::internal::traits<TensorShufflingOp>::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<TensorShufflingOp>::type Nested;
63 typedef typename Eigen::internal::traits<TensorShufflingOp>::StorageKind StorageKind;
64 typedef typename Eigen::internal::traits<TensorShufflingOp>::Index Index;
66 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(
const XprType& expr,
const Shuffle& shuffle)
67 : m_xpr(expr), m_shuffle(shuffle) {}
70 const Shuffle& shufflePermutation()
const {
return m_shuffle; }
73 const typename internal::remove_all<typename XprType::Nested>::type&
74 expression()
const {
return m_xpr; }
77 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (
const TensorShufflingOp& other)
79 typedef TensorAssignOp<TensorShufflingOp, const TensorShufflingOp> Assign;
80 Assign assign(*
this, other);
81 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
85 template<
typename OtherDerived>
87 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (
const OtherDerived& other)
89 typedef TensorAssignOp<TensorShufflingOp, const OtherDerived> Assign;
90 Assign assign(*
this, other);
91 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
96 typename XprType::Nested m_xpr;
97 const Shuffle m_shuffle;
102 template<
typename Shuffle,
typename ArgType,
typename Device>
103 struct TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
105 typedef TensorShufflingOp<Shuffle, ArgType> XprType;
106 typedef typename XprType::Index Index;
107 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
108 typedef DSizes<Index, NumDims> Dimensions;
109 typedef typename XprType::Scalar Scalar;
113 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
114 Layout = TensorEvaluator<ArgType, Device>::Layout,
118 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
119 : m_impl(op.expression(), device)
121 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
122 const Shuffle& shuffle = op.shufflePermutation();
123 for (
int i = 0; i < NumDims; ++i) {
124 m_dimensions[i] = input_dims[shuffle[i]];
127 array<Index, NumDims> inputStrides;
129 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
131 m_outputStrides[0] = 1;
132 for (
int i = 1; i < NumDims; ++i) {
133 inputStrides[i] = inputStrides[i - 1] * input_dims[i - 1];
134 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
137 inputStrides[NumDims - 1] = 1;
138 m_outputStrides[NumDims - 1] = 1;
139 for (
int i = NumDims - 2; i >= 0; --i) {
140 inputStrides[i] = inputStrides[i + 1] * input_dims[i + 1];
141 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
145 for (
int i = 0; i < NumDims; ++i) {
146 m_inputStrides[i] = inputStrides[shuffle[i]];
150 typedef typename XprType::CoeffReturnType CoeffReturnType;
151 typedef typename XprType::PacketReturnType PacketReturnType;
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
155 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(Scalar* ) {
156 m_impl.evalSubExprsIfNeeded(NULL);
159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void cleanup() {
163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const 165 return m_impl.coeff(srcCoeff(index));
168 template<
int LoadMode>
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const 171 const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
172 EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
173 eigen_assert(index+packetSize-1 < dimensions().TotalSize());
175 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
176 for (
int i = 0; i < packetSize; ++i) {
177 values[i] = coeff(index+i);
179 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
183 EIGEN_DEVICE_FUNC Scalar* data()
const {
return NULL; }
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index)
const {
187 Index inputIndex = 0;
188 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
189 for (
int i = NumDims - 1; i > 0; --i) {
190 const Index idx = index / m_outputStrides[i];
191 inputIndex += idx * m_inputStrides[i];
192 index -= idx * m_outputStrides[i];
194 return inputIndex + index * m_inputStrides[0];
196 for (
int i = 0; i < NumDims - 1; ++i) {
197 const Index idx = index / m_outputStrides[i];
198 inputIndex += idx * m_inputStrides[i];
199 index -= idx * m_outputStrides[i];
201 return inputIndex + index * m_inputStrides[NumDims - 1];
205 Dimensions m_dimensions;
206 array<Index, NumDims> m_outputStrides;
207 array<Index, NumDims> m_inputStrides;
208 TensorEvaluator<ArgType, Device> m_impl;
213 template<
typename Shuffle,
typename ArgType,
typename Device>
214 struct TensorEvaluator<TensorShufflingOp<Shuffle, ArgType>, Device>
215 :
public TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device>
217 typedef TensorEvaluator<const TensorShufflingOp<Shuffle, ArgType>, Device> Base;
219 typedef TensorShufflingOp<Shuffle, ArgType> XprType;
220 typedef typename XprType::Index Index;
221 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
222 typedef DSizes<Index, NumDims> Dimensions;
223 typedef typename XprType::Scalar Scalar;
227 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
230 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
234 typedef typename XprType::CoeffReturnType CoeffReturnType;
235 typedef typename XprType::PacketReturnType PacketReturnType;
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
239 return this->m_impl.coeffRef(this->srcCoeff(index));
242 template <
int StoreMode> EIGEN_STRONG_INLINE
243 void writePacket(Index index,
const PacketReturnType& x)
245 static const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
246 EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
248 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
249 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
250 for (
int i = 0; i < packetSize; ++i) {
251 this->coeffRef(index+i) = values[i];
259 #endif // EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13