10#ifndef EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
11#define EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
16template<
typename PatchDim,
typename XprType>
17struct traits<TensorPatchOp<PatchDim, XprType> > :
public traits<XprType>
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index
Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions + 1;
26 static const int Layout = XprTraits::Layout;
29template<
typename PatchDim,
typename XprType>
30struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense>
32 typedef const TensorPatchOp<PatchDim, XprType>& type;
35template<
typename PatchDim,
typename XprType>
36struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type>
38 typedef TensorPatchOp<PatchDim, XprType> type;
48template <
typename PatchDim,
typename XprType>
49class TensorPatchOp :
public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors> {
51 typedef typename Eigen::internal::traits<TensorPatchOp>::Scalar Scalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorPatchOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorPatchOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorPatchOp>::Index Index;
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(
const XprType& expr,
const PatchDim& patch_dims)
59 : m_xpr(expr), m_patch_dims(patch_dims) {}
62 const PatchDim& patch_dims()
const {
return m_patch_dims; }
65 const typename internal::remove_all<typename XprType::Nested>::type&
66 expression()
const {
return m_xpr; }
69 typename XprType::Nested m_xpr;
70 const PatchDim m_patch_dims;
75template<
typename PatchDim,
typename ArgType,
typename Device>
79 typedef typename XprType::Index
Index;
80 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
82 typedef typename XprType::Scalar
Scalar;
83 typedef typename XprType::CoeffReturnType CoeffReturnType;
84 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
85 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
90 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
91 Layout = TensorEvaluator<ArgType, Device>::Layout,
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device&
device)
97 : m_impl(op.expression(),
device)
99 Index num_patches = 1;
100 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
101 const PatchDim& patch_dims = op.patch_dims();
102 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
103 for (
int i = 0; i < NumDims-1; ++i) {
104 m_dimensions[i] = patch_dims[i];
105 num_patches *= (input_dims[i] - patch_dims[i] + 1);
107 m_dimensions[NumDims-1] = num_patches;
109 m_inputStrides[0] = 1;
110 m_patchStrides[0] = 1;
111 for (
int i = 1; i < NumDims-1; ++i) {
112 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
113 m_patchStrides[i] = m_patchStrides[i-1] * (input_dims[i-1] - patch_dims[i-1] + 1);
115 m_outputStrides[0] = 1;
116 for (
int i = 1; i < NumDims; ++i) {
117 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
120 for (
int i = 0; i < NumDims-1; ++i) {
121 m_dimensions[i+1] = patch_dims[i];
122 num_patches *= (input_dims[i] - patch_dims[i] + 1);
124 m_dimensions[0] = num_patches;
126 m_inputStrides[NumDims-2] = 1;
127 m_patchStrides[NumDims-2] = 1;
128 for (
int i = NumDims-3; i >= 0; --i) {
129 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
130 m_patchStrides[i] = m_patchStrides[i+1] * (input_dims[i+1] - patch_dims[i+1] + 1);
132 m_outputStrides[NumDims-1] = 1;
133 for (
int i = NumDims-2; i >= 0; --i) {
134 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(Scalar* ) {
142 m_impl.evalSubExprsIfNeeded(NULL);
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void cleanup() {
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const
152 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
154 Index patchIndex = index / m_outputStrides[output_stride_index];
156 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
157 Index inputIndex = 0;
158 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
159 for (
int i = NumDims - 2; i > 0; --i) {
160 const Index patchIdx = patchIndex / m_patchStrides[i];
161 patchIndex -= patchIdx * m_patchStrides[i];
162 const Index offsetIdx = patchOffset / m_outputStrides[i];
163 patchOffset -= offsetIdx * m_outputStrides[i];
164 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
182 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
183 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
185 Index output_stride_index = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? NumDims - 1 : 0;
186 Index indices[2] = {index, index + PacketSize - 1};
187 Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
188 indices[1] / m_outputStrides[output_stride_index]};
189 Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
190 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
192 Index inputIndices[2] = {0, 0};
193 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
194 for (
int i = NumDims - 2; i > 0; --i) {
195 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
196 patchIndices[1] / m_patchStrides[i]};
197 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
198 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
200 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i],
201 patchOffsets[1] / m_outputStrides[i]};
202 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i];
203 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i];
205 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
206 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
209 for (
int i = 0; i < NumDims - 2; ++i) {
210 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
211 patchIndices[1] / m_patchStrides[i]};
212 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
213 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
215 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i+1],
216 patchOffsets[1] / m_outputStrides[i+1]};
217 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i+1];
218 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i+1];
220 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
221 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
224 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
225 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
227 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
228 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
232 EIGEN_ALIGN_MAX CoeffReturnType values[PacketSize];
233 values[0] = m_impl.coeff(inputIndices[0]);
234 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
235 for (
int i = 1; i < PacketSize-1; ++i) {
236 values[i] = coeff(index+i);
238 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
243 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
244 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() +
245 TensorOpCost::MulCost<Index>() +
246 2 * TensorOpCost::AddCost<Index>());
247 return m_impl.costPerCoeff(vectorized) +
248 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
251 EIGEN_DEVICE_FUNC Scalar* data()
const {
return NULL; }
254 Dimensions m_dimensions;
255 array<Index, NumDims> m_outputStrides;
256 array<Index, NumDims-1> m_inputStrides;
257 array<Index, NumDims-1> m_patchStrides;
259 TensorEvaluator<ArgType, Device> m_impl;
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor patch class.
Definition TensorPatch.h:49
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112