10#ifndef EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
11#define EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
14#include "./InternalHeaderCheck.h"
19template <
typename PatchDim,
typename XprType>
20struct traits<TensorPatchOp<PatchDim, XprType> > :
public traits<XprType> {
21 typedef typename XprType::Scalar Scalar;
22 typedef traits<XprType> XprTraits;
23 typedef typename XprTraits::StorageKind StorageKind;
24 typedef typename XprTraits::Index
Index;
25 typedef typename XprType::Nested Nested;
26 typedef std::remove_reference_t<Nested> Nested_;
27 static constexpr int NumDimensions = XprTraits::NumDimensions + 1;
28 static constexpr int Layout = XprTraits::Layout;
29 typedef typename XprTraits::PointerType PointerType;
32template <
typename PatchDim,
typename XprType>
33struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense> {
34 typedef const TensorPatchOp<PatchDim, XprType>& type;
37template <
typename PatchDim,
typename XprType>
38struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type> {
39 typedef TensorPatchOp<PatchDim, XprType> type;
49template <
typename PatchDim,
typename XprType>
50class TensorPatchOp :
public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors> {
52 typedef typename Eigen::internal::traits<TensorPatchOp>::Scalar Scalar;
54 typedef typename XprType::CoeffReturnType CoeffReturnType;
55 typedef typename Eigen::internal::nested<TensorPatchOp>::type Nested;
56 typedef typename Eigen::internal::traits<TensorPatchOp>::StorageKind StorageKind;
57 typedef typename Eigen::internal::traits<TensorPatchOp>::Index Index;
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(
const XprType& expr,
const PatchDim& patch_dims)
60 : m_xpr(expr), m_patch_dims(patch_dims) {}
62 EIGEN_DEVICE_FUNC
const PatchDim& patch_dims()
const {
return m_patch_dims; }
64 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
67 typename XprType::Nested m_xpr;
68 const PatchDim m_patch_dims;
72template <
typename PatchDim,
typename ArgType,
typename Device>
75 typedef typename XprType::Index
Index;
76 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
78 typedef typename XprType::Scalar
Scalar;
80 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
81 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
82 typedef StorageMemory<CoeffReturnType, Device> Storage;
83 typedef typename Storage::Type EvaluatorPointerType;
85 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
88 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
96 typedef internal::TensorBlockNotImplemented TensorBlock;
99 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device) : m_impl(op.expression(), device) {
100 Index num_patches = 1;
101 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
102 const PatchDim& patch_dims = op.patch_dims();
103 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
104 for (
int i = 0; i < NumDims - 1; ++i) {
105 m_dimensions[i] = patch_dims[i];
106 num_patches *= (input_dims[i] - patch_dims[i] + 1);
108 m_dimensions[NumDims - 1] = num_patches;
110 m_inputStrides[0] = 1;
111 m_patchStrides[0] = 1;
112 for (
int i = 1; i < NumDims - 1; ++i) {
113 m_inputStrides[i] = m_inputStrides[i - 1] * input_dims[i - 1];
114 m_patchStrides[i] = m_patchStrides[i - 1] * (input_dims[i - 1] - patch_dims[i - 1] + 1);
116 m_outputStrides[0] = 1;
117 for (
int i = 1; i < NumDims; ++i) {
118 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
121 for (
int i = 0; i < NumDims - 1; ++i) {
122 m_dimensions[i + 1] = patch_dims[i];
123 num_patches *= (input_dims[i] - patch_dims[i] + 1);
125 m_dimensions[0] = num_patches;
127 m_inputStrides[NumDims - 2] = 1;
128 m_patchStrides[NumDims - 2] = 1;
129 for (
int i = NumDims - 3; i >= 0; --i) {
130 m_inputStrides[i] = m_inputStrides[i + 1] * input_dims[i + 1];
131 m_patchStrides[i] = m_patchStrides[i + 1] * (input_dims[i + 1] - patch_dims[i + 1] + 1);
133 m_outputStrides[NumDims - 1] = 1;
134 for (
int i = NumDims - 2; i >= 0; --i) {
135 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
142 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
143 m_impl.evalSubExprsIfNeeded(NULL);
147 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
150 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
152 Index patchIndex = index / m_outputStrides[output_stride_index];
154 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
155 Index inputIndex = 0;
156 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
158 for (
int i = NumDims - 2; i > 0; --i) {
159 const Index patchIdx = patchIndex / m_patchStrides[i];
160 patchIndex -= patchIdx * m_patchStrides[i];
161 const Index offsetIdx = patchOffset / m_outputStrides[i];
162 patchOffset -= offsetIdx * m_outputStrides[i];
163 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
167 for (
int i = 0; i < NumDims - 2; ++i) {
168 const Index patchIdx = patchIndex / m_patchStrides[i];
169 patchIndex -= patchIdx * m_patchStrides[i];
170 const Index offsetIdx = patchOffset / m_outputStrides[i + 1];
171 patchOffset -= offsetIdx * m_outputStrides[i + 1];
172 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
175 inputIndex += (patchIndex + patchOffset);
176 return m_impl.coeff(inputIndex);
179 template <
int LoadMode>
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
181 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
183 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
184 Index indices[2] = {index, index + PacketSize - 1};
185 Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
186 indices[1] / m_outputStrides[output_stride_index]};
187 Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
188 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
190 Index inputIndices[2] = {0, 0};
191 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
193 for (
int i = NumDims - 2; i > 0; --i) {
194 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i], patchIndices[1] / m_patchStrides[i]};
195 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
196 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
198 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i], patchOffsets[1] / m_outputStrides[i]};
199 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i];
200 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i];
202 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
203 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
207 for (
int i = 0; i < NumDims - 2; ++i) {
208 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i], patchIndices[1] / m_patchStrides[i]};
209 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
210 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
212 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i + 1], patchOffsets[1] / m_outputStrides[i + 1]};
213 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i + 1];
214 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i + 1];
216 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
217 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
220 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
221 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
223 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
224 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
227 EIGEN_ALIGN_MAX CoeffReturnType values[PacketSize];
228 values[0] = m_impl.coeff(inputIndices[0]);
229 values[PacketSize - 1] = m_impl.coeff(inputIndices[1]);
231 for (
int i = 1; i < PacketSize - 1; ++i) {
232 values[i] = coeff(index + i);
234 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
239 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
240 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() + TensorOpCost::MulCost<Index>() +
241 2 * TensorOpCost::AddCost<Index>());
242 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
245 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return NULL; }
248 Dimensions m_dimensions;
249 array<Index, NumDims> m_outputStrides;
250 array<Index, NumDims - 1> m_inputStrides;
251 array<Index, NumDims - 1> m_patchStrides;
253 TensorEvaluator<ArgType, Device> m_impl;
The tensor base class.
Definition TensorForwardDeclarations.h:68
Tensor patch class.
Definition TensorPatch.h:50
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:30