10#ifndef EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
11#define EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
14#include "./InternalHeaderCheck.h"
20template <
typename Op,
typename XprType>
21struct traits<TensorScanOp<Op, XprType> > :
public traits<XprType> {
22 typedef typename XprType::Scalar Scalar;
23 typedef traits<XprType> XprTraits;
24 typedef typename XprTraits::StorageKind StorageKind;
25 typedef typename XprType::Nested Nested;
26 typedef std::remove_reference_t<Nested> Nested_;
27 static constexpr int NumDimensions = XprTraits::NumDimensions;
28 static constexpr int Layout = XprTraits::Layout;
29 typedef typename XprTraits::PointerType PointerType;
32template <
typename Op,
typename XprType>
33struct eval<TensorScanOp<Op, XprType>, Eigen::Dense> {
34 typedef const TensorScanOp<Op, XprType>& type;
37template <
typename Op,
typename XprType>
38struct nested<TensorScanOp<Op, XprType>, 1, typename eval<TensorScanOp<Op, XprType> >::type> {
39 typedef TensorScanOp<Op, XprType> type;
48template <
typename Op,
typename XprType>
49class TensorScanOp :
public TensorBase<TensorScanOp<Op, XprType>, ReadOnlyAccessors> {
51 typedef typename Eigen::internal::traits<TensorScanOp>::Scalar Scalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorScanOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorScanOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorScanOp>::Index Index;
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorScanOp(
const XprType& expr,
const Index& axis,
bool exclusive =
false,
60 : m_expr(expr), m_axis(axis), m_accumulator(op), m_exclusive(exclusive) {}
62 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Index axis()
const {
return m_axis; }
63 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const XprType& expression()
const {
return m_expr; }
64 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Op accumulator()
const {
return m_accumulator; }
65 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool exclusive()
const {
return m_exclusive; }
68 typename XprType::Nested m_expr;
70 const Op m_accumulator;
71 const bool m_exclusive;
76template <
typename Self>
77EIGEN_STRONG_INLINE
void ReduceScalar(Self& self,
Index offset,
typename Self::CoeffReturnType* data) {
79 typename Self::CoeffReturnType accum = self.accumulator().initialize();
80 if (self.stride() == 1) {
81 if (self.exclusive()) {
82 for (
Index curr = offset; curr < offset + self.size(); ++curr) {
83 data[curr] = self.accumulator().finalize(accum);
84 self.accumulator().reduce(self.inner().coeff(curr), &accum);
87 for (
Index curr = offset; curr < offset + self.size(); ++curr) {
88 self.accumulator().reduce(self.inner().coeff(curr), &accum);
89 data[curr] = self.accumulator().finalize(accum);
93 if (self.exclusive()) {
94 for (
Index idx3 = 0; idx3 < self.size(); idx3++) {
95 Index curr = offset + idx3 * self.stride();
96 data[curr] = self.accumulator().finalize(accum);
97 self.accumulator().reduce(self.inner().coeff(curr), &accum);
100 for (
Index idx3 = 0; idx3 < self.size(); idx3++) {
101 Index curr = offset + idx3 * self.stride();
102 self.accumulator().reduce(self.inner().coeff(curr), &accum);
103 data[curr] = self.accumulator().finalize(accum);
109template <
typename Self>
110EIGEN_STRONG_INLINE
void ReducePacket(Self& self,
Index offset,
typename Self::CoeffReturnType* data) {
111 using Scalar =
typename Self::CoeffReturnType;
112 using Packet =
typename Self::PacketReturnType;
114 Packet accum = self.accumulator().template initializePacket<Packet>();
115 if (self.stride() == 1) {
116 if (self.exclusive()) {
117 for (
Index curr = offset; curr < offset + self.size(); ++curr) {
118 internal::pstoreu<Scalar, Packet>(data + curr, self.accumulator().finalizePacket(accum));
119 self.accumulator().reducePacket(self.inner().template packet<Unaligned>(curr), &accum);
122 for (
Index curr = offset; curr < offset + self.size(); ++curr) {
123 self.accumulator().reducePacket(self.inner().template packet<Unaligned>(curr), &accum);
124 internal::pstoreu<Scalar, Packet>(data + curr, self.accumulator().finalizePacket(accum));
128 if (self.exclusive()) {
129 for (
Index idx3 = 0; idx3 < self.size(); idx3++) {
130 const Index curr = offset + idx3 * self.stride();
131 internal::pstoreu<Scalar, Packet>(data + curr, self.accumulator().finalizePacket(accum));
132 self.accumulator().reducePacket(self.inner().template packet<Unaligned>(curr), &accum);
135 for (
Index idx3 = 0; idx3 < self.size(); idx3++) {
136 const Index curr = offset + idx3 * self.stride();
137 self.accumulator().reducePacket(self.inner().template packet<Unaligned>(curr), &accum);
138 internal::pstoreu<Scalar, Packet>(data + curr, self.accumulator().finalizePacket(accum));
144template <
typename Self,
bool Vectorize,
bool Parallel>
146 EIGEN_STRONG_INLINE
void operator()(Self& self,
Index idx1,
typename Self::CoeffReturnType* data) {
147 for (
Index idx2 = 0; idx2 < self.stride(); idx2++) {
149 Index offset = idx1 + idx2;
150 ReduceScalar(self, offset, data);
156template <
typename Self>
157struct ReduceBlock<Self, true, false> {
158 EIGEN_STRONG_INLINE
void operator()(Self& self,
Index idx1,
typename Self::CoeffReturnType* data) {
159 using Packet =
typename Self::PacketReturnType;
160 const int PacketSize = internal::unpacket_traits<Packet>::size;
162 for (; idx2 + PacketSize <= self.stride(); idx2 += PacketSize) {
164 Index offset = idx1 + idx2;
165 ReducePacket(self, offset, data);
167 for (; idx2 < self.stride(); idx2++) {
169 Index offset = idx1 + idx2;
170 ReduceScalar(self, offset, data);
176template <
typename Self,
typename Reducer,
typename Device,
177 bool Vectorize = (TensorEvaluator<typename Self::ChildTypeNoConst, Device>::PacketAccess &&
178 internal::reducer_traits<Reducer, Device>::PacketAccess)>
180 void operator()(Self& self,
typename Self::CoeffReturnType* data)
const {
181 Index total_size = internal::array_prod(self.dimensions());
187 for (
Index idx1 = 0; idx1 < total_size; idx1 += self.stride() * self.size()) {
188 ReduceBlock<Self, Vectorize,
false> block_reducer;
189 block_reducer(self, idx1, data);
194#ifdef EIGEN_USE_THREADS
199EIGEN_STRONG_INLINE
Index AdjustBlockSize(
Index item_size,
Index block_size) {
200 constexpr Index kBlockAlignment = 128;
201 const Index items_per_cacheline = numext::maxi<Index>(1, kBlockAlignment / item_size);
202 return items_per_cacheline * numext::div_ceil(block_size, items_per_cacheline);
205template <
typename Self>
206struct ReduceBlock<Self,
true,
true> {
207 EIGEN_STRONG_INLINE
void operator()(Self& self,
Index idx1,
typename Self::CoeffReturnType* data) {
208 using Scalar =
typename Self::CoeffReturnType;
209 using Packet =
typename Self::PacketReturnType;
210 const int PacketSize = internal::unpacket_traits<Packet>::size;
211 Index num_scalars = self.stride();
212 Index num_packets = 0;
213 if (self.stride() >= PacketSize) {
214 num_packets = self.stride() / PacketSize;
215 self.device().parallelFor(
217 TensorOpCost(PacketSize * self.size(), PacketSize * self.size(), 16 * PacketSize * self.size(),
true,
221 [=](
Index blk_size) { return AdjustBlockSize(PacketSize * sizeof(Scalar), blk_size); },
223 for (Index packet = first; packet < last; ++packet) {
224 const Index idx2 = packet * PacketSize;
225 ReducePacket(self, idx1 + idx2, data);
228 num_scalars -= num_packets * PacketSize;
230 self.device().parallelFor(
231 num_scalars, TensorOpCost(self.size(), self.size(), 16 * self.size()),
234 [=](
Index blk_size) { return AdjustBlockSize(sizeof(Scalar), blk_size); },
236 for (Index scalar = first; scalar < last; ++scalar) {
237 const Index idx2 = num_packets * PacketSize + scalar;
238 ReduceScalar(self, idx1 + idx2, data);
244template <
typename Self>
245struct ReduceBlock<Self, false, true> {
246 EIGEN_STRONG_INLINE
void operator()(Self& self, Index idx1,
typename Self::CoeffReturnType* data) {
247 using Scalar =
typename Self::CoeffReturnType;
248 self.device().parallelFor(
249 self.stride(), TensorOpCost(self.size(), self.size(), 16 * self.size()),
252 [=](Index blk_size) { return AdjustBlockSize(sizeof(Scalar), blk_size); },
253 [&](Index first, Index last) {
254 for (Index idx2 = first; idx2 < last; ++idx2) {
255 ReduceScalar(self, idx1 + idx2, data);
262template <
typename Self,
typename Reducer,
bool Vectorize>
263struct ScanLauncher<Self, Reducer, ThreadPoolDevice, Vectorize> {
264 void operator()(Self& self,
typename Self::CoeffReturnType* data) {
265 using Scalar =
typename Self::CoeffReturnType;
266 using Packet =
typename Self::PacketReturnType;
267 const int PacketSize = internal::unpacket_traits<Packet>::size;
268 const Index total_size = internal::array_prod(self.dimensions());
269 const Index inner_block_size = self.stride() * self.size();
270 bool parallelize_by_outer_blocks = (total_size >= (self.stride() * inner_block_size));
272 if ((parallelize_by_outer_blocks && total_size <= 4096) ||
273 (!parallelize_by_outer_blocks && self.stride() < PacketSize)) {
274 ScanLauncher<Self, Reducer, DefaultDevice, Vectorize> launcher;
275 launcher(self, data);
279 if (parallelize_by_outer_blocks) {
281 const Index num_outer_blocks = total_size / inner_block_size;
282 self.device().parallelFor(
284 TensorOpCost(inner_block_size, inner_block_size, 16 * PacketSize * inner_block_size, Vectorize, PacketSize),
285 [=](Index blk_size) {
return AdjustBlockSize(inner_block_size *
sizeof(Scalar), blk_size); },
287 for (Index idx1 = first; idx1 < last; ++idx1) {
288 ReduceBlock<Self, Vectorize,
false> block_reducer;
289 block_reducer(self, idx1 * inner_block_size, data);
295 ReduceBlock<Self, Vectorize,
true> block_reducer;
296 for (Index idx1 = 0; idx1 < total_size; idx1 += self.stride() * self.size()) {
297 block_reducer(self, idx1, data);
304#if defined(EIGEN_USE_GPU) && (defined(EIGEN_GPUCC))
310template <
typename Self,
typename Reducer>
311__global__ EIGEN_HIP_LAUNCH_BOUNDS_1024
void ScanKernel(Self self, Index total_size,
312 typename Self::CoeffReturnType* data) {
314 Index val = threadIdx.x + blockIdx.x * blockDim.x;
315 Index offset = (val / self.stride()) * self.stride() * self.size() + val % self.stride();
317 if (offset + (self.size() - 1) * self.stride() < total_size) {
319 typename Self::CoeffReturnType accum = self.accumulator().initialize();
320 for (Index idx = 0; idx < self.size(); idx++) {
321 Index curr = offset + idx * self.stride();
322 if (self.exclusive()) {
323 data[curr] = self.accumulator().finalize(accum);
324 self.accumulator().reduce(self.inner().coeff(curr), &accum);
326 self.accumulator().reduce(self.inner().coeff(curr), &accum);
327 data[curr] = self.accumulator().finalize(accum);
334template <
typename Self,
typename Reducer,
bool Vectorize>
335struct ScanLauncher<Self, Reducer, GpuDevice, Vectorize> {
336 void operator()(
const Self& self,
typename Self::CoeffReturnType* data) {
337 Index total_size = internal::array_prod(self.dimensions());
338 Index num_blocks = (total_size / self.size() + 63) / 64;
339 Index block_size = 64;
341 LAUNCH_GPU_KERNEL((ScanKernel<Self, Reducer>), num_blocks, block_size, 0, self.device(), self, total_size, data);
349template <
typename Op,
typename ArgType,
typename Device>
350struct TensorEvaluator<const TensorScanOp<Op, ArgType>, Device> {
351 typedef TensorScanOp<Op, ArgType> XprType;
352 typedef typename XprType::Index
Index;
353 typedef const ArgType ChildTypeNoConst;
354 typedef const ArgType ChildType;
355 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
356 typedef DSizes<Index, NumDims> Dimensions;
357 typedef std::remove_const_t<typename XprType::Scalar> Scalar;
358 typedef typename XprType::CoeffReturnType CoeffReturnType;
359 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
360 typedef TensorEvaluator<const TensorScanOp<Op, ArgType>, Device> Self;
361 typedef StorageMemory<Scalar, Device> Storage;
362 typedef typename Storage::Type EvaluatorPointerType;
364 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
367 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
369 PreferBlockAccess =
false,
375 typedef internal::TensorBlockNotImplemented TensorBlock;
378 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
379 : m_impl(op.expression(), device),
381 m_exclusive(op.exclusive()),
382 m_accumulator(op.accumulator()),
383 m_size(m_impl.dimensions()[op.axis()]),
385 m_consume_dim(op.axis()),
388 EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
389 eigen_assert(op.axis() >= 0 && op.axis() < NumDims);
392 const Dimensions& dims = m_impl.dimensions();
393 if (
static_cast<int>(Layout) ==
static_cast<int>(ColMajor)) {
394 for (
int i = 0; i < op.axis(); ++i) {
395 m_stride = m_stride * dims[i];
402 unsigned int axis = internal::convert_index<unsigned int>(op.axis());
403 for (
unsigned int i = NumDims - 1; i > axis; --i) {
404 m_stride = m_stride * dims[i];
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_impl.dimensions(); }
411 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Index& stride()
const {
return m_stride; }
413 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Index& consume_dim()
const {
return m_consume_dim; }
415 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Index& size()
const {
return m_size; }
417 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Op& accumulator()
const {
return m_accumulator; }
419 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool exclusive()
const {
return m_exclusive; }
421 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const TensorEvaluator<ArgType, Device>& inner()
const {
return m_impl; }
423 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Device& device()
const {
return m_device; }
425 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
426 m_impl.evalSubExprsIfNeeded(NULL);
427 internal::ScanLauncher<Self, Op, Device> launcher;
429 launcher(*
this, data);
433 const Index total_size = internal::array_prod(dimensions());
435 static_cast<EvaluatorPointerType
>(m_device.get((Scalar*)m_device.allocate_temp(total_size *
sizeof(Scalar))));
436 launcher(*
this, m_output);
440 template <
int LoadMode>
441 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index)
const {
442 return internal::ploadt<PacketReturnType, LoadMode>(m_output + index);
445 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EvaluatorPointerType data()
const {
return m_output; }
447 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_output[index]; }
449 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool)
const {
450 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0);
453 EIGEN_STRONG_INLINE
void cleanup() {
455 m_device.deallocate_temp(m_output);
462 TensorEvaluator<ArgType, Device> m_impl;
463 const Device EIGEN_DEVICE_REF m_device;
464 const bool m_exclusive;
469 EvaluatorPointerType m_output;
The tensor base class.
Definition TensorForwardDeclarations.h:68
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index