10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_STRIDING_H
23 template<
typename Str
ides,
typename XprType>
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>
77 Assign assign(*
this, other);
82 template<
typename OtherDerived>
87 Assign assign(*
this, other);
99 template<
typename Str
ides,
typename ArgType,
typename 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]);
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>
170 Index inputIndices[] = {0, 0};
171 Index indices[] = {index, index + PacketSize - 1};
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) {
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);
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);
230 Index inputIndex = 0;
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>
286 return this->
m_impl.coeffRef(this->srcCoeff(index));
295 Index inputIndices[] = {0, 0};
296 Index indices[] = {index, index + PacketSize - 1};
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