11#ifndef EIGEN_TENSOR_TENSOR_PATCH_H
12#define EIGEN_TENSOR_TENSOR_PATCH_H
15#include "./InternalHeaderCheck.h"
20template <
typename PatchDim,
typename XprType>
21struct traits<TensorPatchOp<PatchDim, XprType> > : traits<XprType> {
22 using Scalar =
typename XprType::Scalar;
23 using XprTraits = traits<XprType>;
24 using StorageKind =
typename XprTraits::StorageKind;
25 using Index =
typename XprTraits::Index;
26 static constexpr int NumDimensions = XprTraits::NumDimensions + 1;
27 static constexpr int Layout = XprTraits::Layout;
28 using PointerType =
typename XprTraits::PointerType;
31template <
typename PatchDim,
typename XprType>
32struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense> {
33 using type =
const TensorPatchOp<PatchDim, XprType>&;
43template <
typename PatchDim,
typename XprType>
44class TensorPatchOp :
public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors> {
46 using Scalar =
typename Eigen::internal::traits<TensorPatchOp>::Scalar;
48 using CoeffReturnType =
typename XprType::CoeffReturnType;
49 using Nested =
typename Eigen::internal::ref_selector<TensorPatchOp>::type;
50 using StorageKind =
typename Eigen::internal::traits<TensorPatchOp>::StorageKind;
51 using Index =
typename Eigen::internal::traits<TensorPatchOp>::Index;
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(
const XprType& expr,
const PatchDim& patch_dims)
54 : m_xpr(expr), m_patch_dims(patch_dims) {}
56 EIGEN_DEVICE_FUNC
const PatchDim& patch_dims()
const {
return m_patch_dims; }
58 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
61 typename XprType::Nested m_xpr;
62 const PatchDim m_patch_dims;
66template <
typename PatchDim,
typename ArgType,
typename Device>
69 using Index =
typename XprType::Index;
70 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
71 using Dimensions = DSizes<Index, NumDims>;
72 using Scalar =
typename XprType::Scalar;
73 using CoeffReturnType =
typename XprType::CoeffReturnType;
74 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
75 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
76 using Storage = StorageMemory<CoeffReturnType, Device>;
77 using EvaluatorPointerType =
typename Storage::Type;
79 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
82 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
87 BlockAccess = NumDims > 1,
90 PreferBlockAccess =
true,
96 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
97 using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
98 using TensorBlock =
typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index>;
101 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
102 : m_impl(op.expression(), device), m_device(device) {
103 Index num_patches = 1;
104 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
105 const PatchDim& patch_dims = op.patch_dims();
106 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
107 for (
int i = 0; i < NumDims - 1; ++i) {
108 m_dimensions[i] = patch_dims[i];
109 num_patches *= (input_dims[i] - patch_dims[i] + 1);
111 m_dimensions[NumDims - 1] = num_patches;
113 m_inputStrides[0] = 1;
114 m_patchStrides[0] = 1;
115 for (
int i = 1; i < NumDims - 1; ++i) {
116 m_inputStrides[i] = m_inputStrides[i - 1] * input_dims[i - 1];
117 m_patchStrides[i] = m_patchStrides[i - 1] * (input_dims[i - 1] - patch_dims[i - 1] + 1);
119 m_outputStrides[0] = 1;
120 for (
int i = 1; i < NumDims; ++i) {
121 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
124 for (
int i = 0; i < NumDims - 1; ++i) {
125 m_dimensions[i + 1] = patch_dims[i];
126 num_patches *= (input_dims[i] - patch_dims[i] + 1);
128 m_dimensions[0] = num_patches;
130 m_inputStrides[NumDims - 2] = 1;
131 m_patchStrides[NumDims - 2] = 1;
132 for (
int i = NumDims - 3; i >= 0; --i) {
133 m_inputStrides[i] = m_inputStrides[i + 1] * input_dims[i + 1];
134 m_patchStrides[i] = m_patchStrides[i + 1] * (input_dims[i + 1] - patch_dims[i + 1] + 1);
136 m_outputStrides[NumDims - 1] = 1;
137 for (
int i = NumDims - 2; i >= 0; --i) {
138 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
145#ifdef EIGEN_USE_THREADS
146 template <
typename EvalSubExprsCallback>
147 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
148 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
152 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
153 m_impl.evalSubExprsIfNeeded(
nullptr);
157 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
160 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
162 Index patchIndex = index / m_outputStrides[output_stride_index];
164 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
165 Index inputIndex = 0;
166 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
168 for (
int i = NumDims - 2; i > 0; --i) {
169 const Index patchIdx = patchIndex / m_patchStrides[i];
170 patchIndex -= patchIdx * m_patchStrides[i];
171 const Index offsetIdx = patchOffset / m_outputStrides[i];
172 patchOffset -= offsetIdx * m_outputStrides[i];
173 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
177 for (
int i = 0; i < NumDims - 2; ++i) {
178 const Index patchIdx = patchIndex / m_patchStrides[i];
179 patchIndex -= patchIdx * m_patchStrides[i];
180 const Index offsetIdx = patchOffset / m_outputStrides[i + 1];
181 patchOffset -= offsetIdx * m_outputStrides[i + 1];
182 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
185 inputIndex += (patchIndex + patchOffset);
186 return m_impl.coeff(inputIndex);
189 template <
int LoadMode>
190 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
191 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
196 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
197 Index patchIndex = index / m_outputStrides[output_stride_index];
198 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
199 Index inputIndex = 0;
200 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
202 for (
int i = NumDims - 2; i > 0; --i) {
203 const Index patchIdx = patchIndex / m_patchStrides[i];
204 patchIndex -= patchIdx * m_patchStrides[i];
205 const Index offsetIdx = patchOffset / m_outputStrides[i];
206 patchOffset -= offsetIdx * m_outputStrides[i];
207 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
211 for (
int i = 0; i < NumDims - 2; ++i) {
212 const Index patchIdx = patchIndex / m_patchStrides[i];
213 patchIndex -= patchIdx * m_patchStrides[i];
214 const Index offsetIdx = patchOffset / m_outputStrides[i + 1];
215 patchOffset -= offsetIdx * m_outputStrides[i + 1];
216 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
219 inputIndex += (patchIndex + patchOffset);
223 const Index inner_size = m_dimensions[
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor) ? 0 : NumDims - 1];
224 if (patchOffset + PacketSize <= inner_size) {
225 return m_impl.template packet<Unaligned>(inputIndex);
227 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
228 CoeffReturnType values[PacketSize];
229 values[0] = m_impl.coeff(inputIndex);
231 for (
int i = 1; i < PacketSize; ++i) {
232 values[i] = coeff(index + i);
234 return internal::pload<PacketReturnType>(values);
238 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
239 const size_t target_size = m_device.firstLevelCacheSize();
244 const TensorOpCost cost_per_coeff =
245 m_impl.costPerCoeff(
false) + TensorOpCost(0,
sizeof(CoeffReturnType), 0);
246 return internal::TensorBlockResourceRequirements::withShapeAndSize<Scalar>(
247 internal::TensorBlockShapeType::kSkewedInnerDims, target_size, cost_per_coeff);
250 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
251 bool =
false)
const {
252 constexpr bool is_col_major =
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor);
254 if (desc.size() == 0) {
255 return TensorBlock(internal::TensorBlockKind::kView,
nullptr, desc.dimensions());
258 typename TensorBlock::Storage block_storage = TensorBlock::prepareStorage(desc, scratch);
259 CoeffReturnType* block_buffer = block_storage.data();
262 array<Index, NumDims> coords;
263 Index remaining = desc.offset();
264 EIGEN_IF_CONSTEXPR (is_col_major) {
265 for (
int i = NumDims - 1; i > 0; --i) {
266 coords[i] = remaining / m_outputStrides[i];
267 remaining -= coords[i] * m_outputStrides[i];
269 coords[0] = remaining;
271 for (
int i = 0; i < NumDims - 1; ++i) {
272 coords[i] = remaining / m_outputStrides[i];
273 remaining -= coords[i] * m_outputStrides[i];
275 coords[NumDims - 1] = remaining;
278 const int patch_dim = is_col_major ? NumDims - 1 : 0;
279 const int inner_dim = is_col_major ? 0 : NumDims - 1;
281 const Index num_patches_in_block = desc.dimension(patch_dim);
284 const Index inner_size = desc.dimension(inner_dim);
289 array<Index, NumDims> mid_sizes;
290 array<Index, NumDims> mid_src_stride;
291 array<Index, NumDims> mid_count;
293 Index src_corner = 0;
294 for (
int k = 0; k < NumDims - 1; ++k) {
295 const int d = is_col_major ? k : NumDims - 1 - k;
296 const int in_d = is_col_major ? d : d - 1;
297 src_corner += coords[d] * m_inputStrides[in_d];
299 mid_sizes[num_mid] = desc.dimension(d);
300 mid_src_stride[num_mid] = m_inputStrides[in_d];
309 for (Index p = 0; p < num_patches_in_block; ++p) {
311 Index patch_index = coords[patch_dim] + p;
313 EIGEN_IF_CONSTEXPR (is_col_major) {
314 for (
int i = NumDims - 2; i > 0; --i) {
315 const Index idx = patch_index / m_patchStrides[i];
316 patch_index -= idx * m_patchStrides[i];
317 src_patch += idx * m_inputStrides[i];
320 for (
int i = 0; i < NumDims - 2; ++i) {
321 const Index idx = patch_index / m_patchStrides[i];
322 patch_index -= idx * m_patchStrides[i];
323 src_patch += idx * m_inputStrides[i];
326 src_patch += patch_index;
328 Index src = src_patch + src_corner;
329 for (
int k = 0; k < num_mid; ++k) mid_count[k] = 0;
331 for (Index j = 0; j < inner_size; ++j) {
332 block_buffer[dst + j] = m_impl.coeff(src + j);
336 for (; k < num_mid; ++k) {
337 if (++mid_count[k] < mid_sizes[k]) {
338 src += mid_src_stride[k];
342 src -= mid_src_stride[k] * (mid_sizes[k] - 1);
344 if (k == num_mid)
break;
347 eigen_assert(dst == desc.size());
349 return block_storage.AsTensorMaterializedBlock();
352 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
353 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() + TensorOpCost::MulCost<Index>() +
354 2 * TensorOpCost::AddCost<Index>());
355 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
358 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return nullptr; }
361 Dimensions m_dimensions;
362 array<Index, NumDims> m_outputStrides;
363 array<Index, NumDims - 1> m_inputStrides;
364 array<Index, NumDims - 1> m_patchStrides;
366 TensorEvaluator<ArgType, Device> m_impl;
367 const Device EIGEN_DEVICE_REF m_device;
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor patch class.
Definition TensorPatch.h:44
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47