10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
23 template<
typename PatchDim,
typename XprType>
30 typedef typename XprType::Nested
Nested;
32 static const int NumDimensions = XprTraits::NumDimensions + 1;
33 static const int Layout = XprTraits::Layout;
37 template<
typename PatchDim,
typename XprType>
43 template<
typename PatchDim,
typename XprType>
53 template<
typename PatchDim,
typename XprType>
81 template<
typename PatchDim,
typename ArgType,
typename Device>
111 : m_impl(op.expression(), device)
113 Index num_patches = 1;
117 for (
int i = 0;
i < NumDims-1; ++
i) {
118 m_dimensions[
i] = patch_dims[
i];
119 num_patches *= (input_dims[
i] - patch_dims[
i] + 1);
121 m_dimensions[NumDims-1] = num_patches;
123 m_inputStrides[0] = 1;
124 m_patchStrides[0] = 1;
125 for (
int i = 1;
i < NumDims-1; ++
i) {
126 m_inputStrides[
i] = m_inputStrides[
i-1] * input_dims[
i-1];
127 m_patchStrides[
i] = m_patchStrides[
i-1] * (input_dims[
i-1] - patch_dims[
i-1] + 1);
129 m_outputStrides[0] = 1;
130 for (
int i = 1;
i < NumDims; ++
i) {
131 m_outputStrides[
i] = m_outputStrides[
i-1] * m_dimensions[
i-1];
134 for (
int i = 0;
i < NumDims-1; ++
i) {
135 m_dimensions[
i+1] = patch_dims[
i];
136 num_patches *= (input_dims[
i] - patch_dims[
i] + 1);
138 m_dimensions[0] = num_patches;
140 m_inputStrides[NumDims-2] = 1;
141 m_patchStrides[NumDims-2] = 1;
142 for (
int i = NumDims-3;
i >= 0; --
i) {
143 m_inputStrides[
i] = m_inputStrides[
i+1] * input_dims[
i+1];
144 m_patchStrides[
i] = m_patchStrides[
i+1] * (input_dims[
i+1] - patch_dims[
i+1] + 1);
146 m_outputStrides[NumDims-1] = 1;
147 for (
int i = NumDims-2;
i >= 0; --
i) {
148 m_outputStrides[
i] = m_outputStrides[
i+1] * m_dimensions[
i+1];
156 m_impl.evalSubExprsIfNeeded(
NULL);
166 Index output_stride_index = (
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
168 Index patchIndex = index / m_outputStrides[output_stride_index];
170 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
171 Index inputIndex = 0;
174 for (
int i = NumDims - 2;
i > 0; --
i) {
175 const Index patchIdx = patchIndex / m_patchStrides[
i];
176 patchIndex -= patchIdx * m_patchStrides[
i];
177 const Index offsetIdx = patchOffset / m_outputStrides[
i];
178 patchOffset -= offsetIdx * m_outputStrides[
i];
179 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[
i];
183 for (
int i = 0;
i < NumDims - 2; ++
i) {
184 const Index patchIdx = patchIndex / m_patchStrides[
i];
185 patchIndex -= patchIdx * m_patchStrides[
i];
186 const Index offsetIdx = patchOffset / m_outputStrides[
i+1];
187 patchOffset -= offsetIdx * m_outputStrides[
i+1];
188 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[
i];
191 inputIndex += (patchIndex + patchOffset);
192 return m_impl.coeff(inputIndex);
195 template<
int LoadMode>
201 Index output_stride_index = (
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
203 Index patchIndices[2] = {
indices[0] / m_outputStrides[output_stride_index],
204 indices[1] / m_outputStrides[output_stride_index]};
205 Index patchOffsets[2] = {
indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
206 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
208 Index inputIndices[2] = {0, 0};
211 for (
int i = NumDims - 2;
i > 0; --
i) {
212 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[
i],
213 patchIndices[1] / m_patchStrides[
i]};
214 patchIndices[0] -= patchIdx[0] * m_patchStrides[
i];
215 patchIndices[1] -= patchIdx[1] * m_patchStrides[
i];
217 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[
i],
218 patchOffsets[1] / m_outputStrides[
i]};
219 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[
i];
220 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[
i];
222 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[
i];
223 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[
i];
227 for (
int i = 0;
i < NumDims - 2; ++
i) {
228 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[
i],
229 patchIndices[1] / m_patchStrides[
i]};
230 patchIndices[0] -= patchIdx[0] * m_patchStrides[
i];
231 patchIndices[1] -= patchIdx[1] * m_patchStrides[
i];
233 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[
i+1],
234 patchOffsets[1] / m_outputStrides[
i+1]};
235 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[
i+1];
236 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[
i+1];
238 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[
i];
239 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[
i];
242 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
243 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
245 if (inputIndices[1] - inputIndices[0] ==
PacketSize - 1) {
251 values[0] = m_impl.coeff(inputIndices[0]);
263 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() +
264 TensorOpCost::MulCost<Index>() +
265 2 * TensorOpCost::AddCost<Index>());
266 return m_impl.costPerCoeff(vectorized) +
272 #ifdef EIGEN_USE_SYCL
291 #endif // EIGEN_CXX11_TENSOR_TENSOR_PATCH_H