10#ifndef EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H
11#define EIGEN_CXX11_TENSOR_TENSOR_SHUFFLING_H
16template<
typename Shuffle,
typename XprType>
17struct traits<TensorShufflingOp<Shuffle, XprType> > :
public traits<XprType>
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index
Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions;
26 static const int Layout = XprTraits::Layout;
29template<
typename Shuffle,
typename XprType>
30struct eval<TensorShufflingOp<Shuffle, XprType>, Eigen::Dense>
32 typedef const TensorShufflingOp<Shuffle, XprType>& type;
35template<
typename Shuffle,
typename XprType>
36struct nested<TensorShufflingOp<Shuffle, XprType>, 1, typename eval<TensorShufflingOp<Shuffle, XprType> >::type>
38 typedef TensorShufflingOp<Shuffle, XprType> type;
48template <
typename Shuffle,
typename XprType>
49class TensorShufflingOp :
public TensorBase<TensorShufflingOp<Shuffle, XprType> > {
51 typedef typename Eigen::internal::traits<TensorShufflingOp>::Scalar Scalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorShufflingOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorShufflingOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorShufflingOp>::Index Index;
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorShufflingOp(
const XprType& expr,
const Shuffle& shuffle)
59 : m_xpr(expr), m_shuffle(shuffle) {}
62 const Shuffle& shufflePermutation()
const {
return m_shuffle; }
65 const typename internal::remove_all<typename XprType::Nested>::type&
66 expression()
const {
return m_xpr; }
69 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (
const TensorShufflingOp& other)
72 Assign assign(*
this, other);
73 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
77 template<
typename OtherDerived>
79 EIGEN_STRONG_INLINE TensorShufflingOp& operator = (
const OtherDerived& other)
82 Assign assign(*
this, other);
83 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
88 typename XprType::Nested m_xpr;
89 const Shuffle m_shuffle;
94template<
typename Shuffle,
typename ArgType,
typename Device>
98 typedef typename XprType::Index
Index;
99 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
101 typedef typename XprType::Scalar
Scalar;
102 typedef typename XprType::CoeffReturnType CoeffReturnType;
103 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
104 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
108 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
109 Layout = TensorEvaluator<ArgType, Device>::Layout,
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device&
device)
115 : m_impl(op.expression(),
device)
117 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
118 const Shuffle& shuffle = op.shufflePermutation();
119 for (
int i = 0; i < NumDims; ++i) {
120 m_dimensions[i] = input_dims[shuffle[i]];
123 array<Index, NumDims> inputStrides;
125 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
127 m_outputStrides[0] = 1;
128 for (
int i = 1; i < NumDims; ++i) {
129 inputStrides[i] = inputStrides[i - 1] * input_dims[i - 1];
130 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
133 inputStrides[NumDims - 1] = 1;
134 m_outputStrides[NumDims - 1] = 1;
135 for (
int i = NumDims - 2; i >= 0; --i) {
136 inputStrides[i] = inputStrides[i + 1] * input_dims[i + 1];
137 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
141 for (
int i = 0; i < NumDims; ++i) {
142 m_inputStrides[i] = inputStrides[shuffle[i]];
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(Scalar* ) {
149 m_impl.evalSubExprsIfNeeded(NULL);
152 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void cleanup() {
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const
158 return m_impl.coeff(srcCoeff(index));
161 template<
int LoadMode>
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const
164 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
165 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
167 EIGEN_ALIGN_MAX
typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
168 for (
int i = 0; i < PacketSize; ++i) {
169 values[i] = coeff(index+i);
171 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
175 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
176 const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() +
177 2 * TensorOpCost::MulCost<Index>() +
178 TensorOpCost::DivCost<Index>());
179 return m_impl.costPerCoeff(vectorized) +
180 TensorOpCost(0, 0, compute_cost,
false , PacketSize);
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;
213template<
typename Shuffle,
typename ArgType,
typename 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;
224 typedef typename XprType::CoeffReturnType CoeffReturnType;
225 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
226 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
230 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
234 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device&
device)
238 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
240 return this->m_impl.coeffRef(this->srcCoeff(index));
243 template <
int StoreMode> EIGEN_STRONG_INLINE
244 void writePacket(Index index,
const PacketReturnType& x)
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];
Definition TensorAssign.h:56
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor shuffling class.
Definition TensorShuffling.h:49
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112