10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
13 #include "./InternalHeaderCheck.h"
25 template<
typename PatchDim,
typename XprType>
26 struct traits<TensorPatchOp<PatchDim, XprType> > :
public traits<XprType>
28 typedef typename XprType::Scalar Scalar;
29 typedef traits<XprType> XprTraits;
30 typedef typename XprTraits::StorageKind StorageKind;
31 typedef typename XprTraits::Index
Index;
32 typedef typename XprType::Nested Nested;
33 typedef std::remove_reference_t<Nested> Nested_;
34 static constexpr
int NumDimensions = XprTraits::NumDimensions + 1;
35 static constexpr
int Layout = XprTraits::Layout;
36 typedef typename XprTraits::PointerType PointerType;
39 template<
typename PatchDim,
typename XprType>
40 struct eval<TensorPatchOp<PatchDim, XprType>,
Eigen::Dense>
42 typedef const TensorPatchOp<PatchDim, XprType>& type;
45 template<
typename PatchDim,
typename XprType>
46 struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type>
48 typedef TensorPatchOp<PatchDim, XprType> type;
55 template<
typename PatchDim,
typename XprType>
56 class TensorPatchOp :
public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors>
59 typedef typename Eigen::internal::traits<TensorPatchOp>::Scalar Scalar;
61 typedef typename XprType::CoeffReturnType CoeffReturnType;
62 typedef typename Eigen::internal::nested<TensorPatchOp>::type Nested;
63 typedef typename Eigen::internal::traits<TensorPatchOp>::StorageKind StorageKind;
64 typedef typename Eigen::internal::traits<TensorPatchOp>::Index
Index;
66 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(
const XprType& expr,
const PatchDim& patch_dims)
67 : m_xpr(expr), m_patch_dims(patch_dims) {}
70 const PatchDim& patch_dims()
const {
return m_patch_dims; }
73 const internal::remove_all_t<typename XprType::Nested>&
74 expression()
const {
return m_xpr; }
77 typename XprType::Nested m_xpr;
78 const PatchDim m_patch_dims;
83 template<
typename PatchDim,
typename ArgType,
typename Device>
84 struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
86 typedef TensorPatchOp<PatchDim, ArgType> XprType;
87 typedef typename XprType::Index Index;
88 static constexpr
int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
89 typedef DSizes<Index, NumDims> Dimensions;
90 typedef typename XprType::Scalar Scalar;
91 typedef typename XprType::CoeffReturnType CoeffReturnType;
92 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
93 static constexpr
int PacketSize = PacketType<CoeffReturnType, Device>::size;
94 typedef StorageMemory<CoeffReturnType, Device> Storage;
95 typedef typename Storage::Type EvaluatorPointerType;
97 static constexpr
int Layout = TensorEvaluator<ArgType, Device>::Layout;
100 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
102 PreferBlockAccess = TensorEvaluator<ArgType, Device>::PreferBlockAccess,
108 typedef internal::TensorBlockNotImplemented TensorBlock;
111 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
112 : m_impl(op.expression(), device)
114 Index num_patches = 1;
115 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
116 const PatchDim& patch_dims = op.patch_dims();
117 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
118 for (
int i = 0; i < NumDims-1; ++i) {
119 m_dimensions[i] = patch_dims[i];
120 num_patches *= (input_dims[i] - patch_dims[i] + 1);
122 m_dimensions[NumDims-1] = num_patches;
124 m_inputStrides[0] = 1;
125 m_patchStrides[0] = 1;
126 for (
int i = 1; i < NumDims-1; ++i) {
127 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
128 m_patchStrides[i] = m_patchStrides[i-1] * (input_dims[i-1] - patch_dims[i-1] + 1);
130 m_outputStrides[0] = 1;
131 for (
int i = 1; i < NumDims; ++i) {
132 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
135 for (
int i = 0; i < NumDims-1; ++i) {
136 m_dimensions[i+1] = patch_dims[i];
137 num_patches *= (input_dims[i] - patch_dims[i] + 1);
139 m_dimensions[0] = num_patches;
141 m_inputStrides[NumDims-2] = 1;
142 m_patchStrides[NumDims-2] = 1;
143 for (
int i = NumDims-3; i >= 0; --i) {
144 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
145 m_patchStrides[i] = m_patchStrides[i+1] * (input_dims[i+1] - patch_dims[i+1] + 1);
147 m_outputStrides[NumDims-1] = 1;
148 for (
int i = NumDims-2; i >= 0; --i) {
149 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
156 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
157 m_impl.evalSubExprsIfNeeded(NULL);
161 EIGEN_STRONG_INLINE
void cleanup() {
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const
167 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
169 Index patchIndex = index / m_outputStrides[output_stride_index];
171 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
172 Index inputIndex = 0;
173 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
175 for (
int i = NumDims - 2; i > 0; --i) {
176 const Index patchIdx = patchIndex / m_patchStrides[i];
177 patchIndex -= patchIdx * m_patchStrides[i];
178 const Index offsetIdx = patchOffset / m_outputStrides[i];
179 patchOffset -= offsetIdx * m_outputStrides[i];
180 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
184 for (
int i = 0; i < NumDims - 2; ++i) {
185 const Index patchIdx = patchIndex / m_patchStrides[i];
186 patchIndex -= patchIdx * m_patchStrides[i];
187 const Index offsetIdx = patchOffset / m_outputStrides[i+1];
188 patchOffset -= offsetIdx * m_outputStrides[i+1];
189 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
192 inputIndex += (patchIndex + patchOffset);
193 return m_impl.coeff(inputIndex);
196 template<
int LoadMode>
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const
199 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
201 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
202 Index indices[2] = {index, index + PacketSize - 1};
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};
209 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
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) {
246 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
250 EIGEN_ALIGN_MAX CoeffReturnType values[PacketSize];
251 values[0] = m_impl.coeff(inputIndices[0]);
252 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
254 for (
int i = 1; i < PacketSize-1; ++i) {
255 values[i] = coeff(index+i);
257 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
262 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
263 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() +
264 TensorOpCost::MulCost<Index>() +
265 2 * TensorOpCost::AddCost<Index>());
266 return m_impl.costPerCoeff(vectorized) +
267 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
270 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return NULL; }
272 #ifdef EIGEN_USE_SYCL
274 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void bind(cl::sycl::handler &cgh)
const {
280 Dimensions m_dimensions;
281 array<Index, NumDims> m_outputStrides;
282 array<
Index, NumDims-1> m_inputStrides;
283 array<
Index, NumDims-1> m_patchStrides;
285 TensorEvaluator<ArgType, Device> m_impl;
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index