10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H 11 #define EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H 23 template<
typename Str
ides,
typename XprType>
26 typedef typename XprType::Scalar
Scalar;
30 typedef typename XprType::Nested
Nested;
32 static const int NumDimensions = XprTraits::NumDimensions;
33 static const int Layout = XprTraits::Layout;
36 template<
typename Str
ides,
typename XprType>
42 template<
typename Str
ides,
typename XprType>
52 template<
typename Str
ides,
typename XprType>
64 : m_xpr(expr), m_dims(dims) {}
67 const Strides&
strides()
const {
return m_dims; }
77 Assign assign(*
this, other);
82 template<
typename OtherDerived>
87 Assign assign(*
this, other);
99 template<
typename Str
ides,
typename ArgType,
typename Device>
120 : m_impl(op.expression(), device)
122 m_dimensions = m_impl.dimensions();
123 for (
int i = 0; i < NumDims; ++i) {
124 m_dimensions[i] = ceilf(static_cast<float>(m_dimensions[i]) / op.
strides()[i]);
128 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
129 m_outputStrides[0] = 1;
130 m_inputStrides[0] = 1;
131 for (
int i = 1; i < NumDims; ++i) {
132 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
133 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
134 m_inputStrides[i-1] *= op.
strides()[i-1];
136 m_inputStrides[NumDims-1] *= op.
strides()[NumDims-1];
138 m_outputStrides[NumDims-1] = 1;
139 m_inputStrides[NumDims-1] = 1;
140 for (
int i = NumDims - 2; i >= 0; --i) {
141 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
142 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
143 m_inputStrides[i+1] *= op.
strides()[i+1];
145 m_inputStrides[0] *= op.
strides()[0];
152 m_impl.evalSubExprsIfNeeded(NULL);
161 return m_impl.coeff(srcCoeff(index));
164 template<
int LoadMode>
168 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
170 Index inputIndices[] = {0, 0};
171 Index indices[] = {index, index + PacketSize - 1};
172 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
173 for (
int i = NumDims - 1; i > 0; --i) {
174 const Index idx0 = indices[0] / m_outputStrides[i];
175 const Index idx1 = indices[1] / m_outputStrides[i];
176 inputIndices[0] += idx0 * m_inputStrides[i];
177 inputIndices[1] += idx1 * m_inputStrides[i];
178 indices[0] -= idx0 * m_outputStrides[i];
179 indices[1] -= idx1 * m_outputStrides[i];
181 inputIndices[0] += indices[0] * m_inputStrides[0];
182 inputIndices[1] += indices[1] * m_inputStrides[0];
184 for (
int i = 0; i < NumDims - 1; ++i) {
185 const Index idx0 = indices[0] / m_outputStrides[i];
186 const Index idx1 = indices[1] / m_outputStrides[i];
187 inputIndices[0] += idx0 * m_inputStrides[i];
188 inputIndices[1] += idx1 * m_inputStrides[i];
189 indices[0] -= idx0 * m_outputStrides[i];
190 indices[1] -= idx1 * m_outputStrides[i];
192 inputIndices[0] += indices[0] * m_inputStrides[NumDims-1];
193 inputIndices[1] += indices[1] * m_inputStrides[NumDims-1];
195 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
196 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
201 values[0] = m_impl.coeff(inputIndices[0]);
202 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
203 for (
int i = 1; i < PacketSize-1; ++i) {
204 values[i] = coeff(index+i);
206 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
212 double compute_cost = (NumDims - 1) * (TensorOpCost::AddCost<Index>() +
213 TensorOpCost::MulCost<Index>() +
214 TensorOpCost::DivCost<Index>()) +
215 TensorOpCost::MulCost<Index>();
219 const int innerDim = (
static_cast<int>(Layout) == static_cast<int>(
ColMajor)) ? 0 : (NumDims - 1);
220 return m_impl.costPerCoeff(vectorized && m_inputStrides[innerDim] == 1) +
222 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
225 EIGEN_DEVICE_FUNC Scalar*
data()
const {
return NULL; }
230 Index inputIndex = 0;
231 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
232 for (
int i = NumDims - 1; i > 0; --i) {
233 const Index idx = index / m_outputStrides[i];
234 inputIndex += idx * m_inputStrides[i];
235 index -= idx * m_outputStrides[i];
237 inputIndex += index * m_inputStrides[0];
239 for (
int i = 0; i < NumDims - 1; ++i) {
240 const Index idx = index / m_outputStrides[i];
241 inputIndex += idx * m_inputStrides[i];
242 index -= idx * m_outputStrides[i];
244 inputIndex += index * m_inputStrides[NumDims-1];
257 template<
typename Str
ides,
typename ArgType,
typename Device>
259 :
public TensorEvaluator<const TensorStridingOp<Strides, ArgType>, Device>
276 : Base(op, device) { }
286 return this->m_impl.coeffRef(this->srcCoeff(index));
293 eigen_assert(index+PacketSize-1 < this->dimensions().TotalSize());
295 Index inputIndices[] = {0, 0};
296 Index indices[] = {index, index + PacketSize - 1};
297 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
298 for (
int i = NumDims - 1; i > 0; --i) {
299 const Index idx0 = indices[0] / this->m_outputStrides[i];
300 const Index idx1 = indices[1] / this->m_outputStrides[i];
301 inputIndices[0] += idx0 * this->m_inputStrides[i];
302 inputIndices[1] += idx1 * this->m_inputStrides[i];
303 indices[0] -= idx0 * this->m_outputStrides[i];
304 indices[1] -= idx1 * this->m_outputStrides[i];
306 inputIndices[0] += indices[0] * this->m_inputStrides[0];
307 inputIndices[1] += indices[1] * this->m_inputStrides[0];
309 for (
int i = 0; i < NumDims - 1; ++i) {
310 const Index idx0 = indices[0] / this->m_outputStrides[i];
311 const Index idx1 = indices[1] / this->m_outputStrides[i];
312 inputIndices[0] += idx0 * this->m_inputStrides[i];
313 inputIndices[1] += idx1 * this->m_inputStrides[i];
314 indices[0] -= idx0 * this->m_outputStrides[i];
315 indices[1] -= idx1 * this->m_outputStrides[i];
317 inputIndices[0] += indices[0] * this->m_inputStrides[NumDims-1];
318 inputIndices[1] += indices[1] * this->m_inputStrides[NumDims-1];
320 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
321 this->m_impl.template writePacket<Unaligned>(inputIndices[0], x);
325 internal::pstore<Scalar, PacketReturnType>(values, x);
326 this->m_impl.coeffRef(inputIndices[0]) = values[0];
327 this->m_impl.coeffRef(inputIndices[1]) = values[PacketSize-1];
328 for (
int i = 1; i < PacketSize-1; ++i) {
329 this->coeffRef(index+i) = values[i];
338 #endif // EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStridingOp(const XprType &expr, const Strides &dims)
EIGEN_DEVICE_FUNC Scalar * data() const
TensorStridingOp< Strides, ArgType > XprType
#define EIGEN_STRONG_INLINE
TensorEvaluator< ArgType, Device > m_impl
const TensorStridingOp< Strides, XprType > & type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
array< Index, NumDims > m_outputStrides
std::vector< double > values
XprType::CoeffReturnType CoeffReturnType
TensorEvaluator< const XprType, Device > Base
Eigen::internal::traits< TensorStridingOp >::Index Index
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
PacketType< CoeffReturnType, Device >::type PacketReturnType
EIGEN_DEVICE_FUNC const Strides & strides() const
XprTraits::StorageKind StorageKind
array< Index, NumDims > m_inputStrides
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
TensorStridingOp< Strides, XprType > type
TensorStridingOp< Strides, ArgType > XprType
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Eigen::internal::nested< TensorStridingOp >::type Nested
DSizes< Index, NumDims > Dimensions
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index index, const PacketReturnType &x)
static EIGEN_DEVICE_FUNC void run(const Expression &expr, const Device &device=Device())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Eigen::internal::traits< TensorStridingOp >::Scalar Scalar
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
XprType::CoeffReturnType CoeffReturnType
Eigen::internal::traits< TensorStridingOp >::StorageKind StorageKind
PacketType< CoeffReturnType, Device >::type PacketReturnType
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
traits< XprType > XprTraits
XprType::CoeffReturnType CoeffReturnType
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
remove_reference< Nested >::type _Nested
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
internal::packet_traits< Scalar >::type type
Eigen::NumTraits< Scalar >::Real RealScalar