10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
19 template <
typename Op,
typename XprType>
20 struct traits<TensorScanOp<Op, XprType> >
22 typedef typename XprType::Scalar Scalar;
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;
32 template<
typename Op,
typename XprType>
33 struct eval<TensorScanOp<Op, XprType>,
Eigen::Dense>
35 typedef const TensorScanOp<Op, XprType>& type;
38 template<
typename Op,
typename XprType>
39 struct nested<TensorScanOp<Op, XprType>, 1,
40 typename eval<TensorScanOp<Op, XprType> >::type>
42 typedef TensorScanOp<Op, XprType> type;
51 template <
typename Op,
typename XprType>
53 :
public TensorBase<TensorScanOp<Op, XprType>, ReadOnlyAccessors> {
55 typedef typename Eigen::internal::traits<TensorScanOp>::Scalar
Scalar;
58 typedef typename Eigen::internal::nested<TensorScanOp>::type
Nested;
59 typedef typename Eigen::internal::traits<TensorScanOp>::StorageKind
StorageKind;
60 typedef typename Eigen::internal::traits<TensorScanOp>::Index
Index;
85 template <
typename Self>
87 typename Self::CoeffReturnType* data) {
89 typename Self::CoeffReturnType accum =
self.accumulator().initialize();
90 if (
self.stride() == 1) {
91 if (
self.exclusive()) {
92 for (
Index curr = offset; curr < offset +
self.size(); ++curr) {
93 data[curr] =
self.accumulator().finalize(accum);
94 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
97 for (
Index curr = offset; curr < offset +
self.size(); ++curr) {
98 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
99 data[curr] =
self.accumulator().finalize(accum);
103 if (
self.exclusive()) {
104 for (
Index idx3 = 0; idx3 <
self.size(); idx3++) {
105 Index curr = offset + idx3 *
self.stride();
106 data[curr] =
self.accumulator().finalize(accum);
107 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
110 for (
Index idx3 = 0; idx3 <
self.size(); idx3++) {
111 Index curr = offset + idx3 *
self.stride();
112 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
113 data[curr] =
self.accumulator().finalize(accum);
119 template <
typename Self>
121 typename Self::CoeffReturnType* data) {
122 using Scalar =
typename Self::CoeffReturnType;
123 using Packet =
typename Self::PacketReturnType;
125 Packet accum =
self.accumulator().template initializePacket<Packet>();
126 if (
self.stride() == 1) {
127 if (
self.exclusive()) {
128 for (
Index curr = offset; curr < offset +
self.size(); ++curr) {
129 internal::pstoreu<Scalar, Packet>(
data + curr,
self.accumulator().finalizePacket(accum));
130 self.accumulator().reducePacket(
self.inner().
template packet<Unaligned>(curr), &accum);
133 for (
Index curr = offset; curr < offset +
self.size(); ++curr) {
134 self.accumulator().reducePacket(
self.inner().
template packet<Unaligned>(curr), &accum);
135 internal::pstoreu<Scalar, Packet>(
data + curr,
self.accumulator().finalizePacket(accum));
139 if (
self.exclusive()) {
140 for (
Index idx3 = 0; idx3 <
self.size(); idx3++) {
141 const Index curr = offset + idx3 *
self.stride();
142 internal::pstoreu<Scalar, Packet>(
data + curr,
self.accumulator().finalizePacket(accum));
143 self.accumulator().reducePacket(
self.inner().
template packet<Unaligned>(curr), &accum);
146 for (
Index idx3 = 0; idx3 <
self.size(); idx3++) {
147 const Index curr = offset + idx3 *
self.stride();
148 self.accumulator().reducePacket(
self.inner().
template packet<Unaligned>(curr), &accum);
149 internal::pstoreu<Scalar, Packet>(
data + curr,
self.accumulator().finalizePacket(accum));
155 template <
typename Self,
bool Vectorize,
bool Parallel>
158 typename Self::CoeffReturnType* data) {
159 for (
Index idx2 = 0; idx2 <
self.stride(); idx2++) {
161 Index offset = idx1 + idx2;
168 template <
typename Self>
169 struct ReduceBlock<Self, true, false> {
171 typename Self::CoeffReturnType* data) {
172 using Packet =
typename Self::PacketReturnType;
173 const int PacketSize = internal::unpacket_traits<Packet>::size;
175 for (; idx2 + PacketSize <=
self.stride(); idx2 += PacketSize) {
177 Index offset = idx1 + idx2;
180 for (; idx2 <
self.stride(); idx2++) {
182 Index offset = idx1 + idx2;
189 template <
typename Self,
typename Reducer,
typename Device,
192 internal::reducer_traits<Reducer, Device>::PacketAccess)>
193 struct ScanLauncher {
194 void operator()(Self&
self,
typename Self::CoeffReturnType* data)
const {
201 for (
Index idx1 = 0; idx1 < total_size; idx1 +=
self.stride() *
self.
size()) {
202 ReduceBlock<Self, Vectorize,
false> block_reducer;
203 block_reducer(
self, idx1, data);
208 #ifdef EIGEN_USE_THREADS
213 EIGEN_STRONG_INLINE
Index AdjustBlockSize(
Index item_size,
Index block_size) {
215 const Index items_per_cacheline =
216 numext::maxi<Index>(1, kBlockAlignment / item_size);
217 return items_per_cacheline *
divup(block_size, items_per_cacheline);
220 template <
typename Self>
221 struct ReduceBlock<Self,
true,
true> {
223 typename Self::CoeffReturnType* data) {
224 using Scalar =
typename Self::CoeffReturnType;
225 using Packet =
typename Self::PacketReturnType;
226 const int PacketSize = internal::unpacket_traits<Packet>::size;
227 Index num_scalars =
self.stride();
228 Index num_packets = 0;
229 if (
self.stride() >= PacketSize) {
230 num_packets =
self.stride() / PacketSize;
231 self.device().parallelFor(
233 TensorOpCost(PacketSize *
self.
size(), PacketSize *
self.
size(),
234 16 * PacketSize *
self.
size(),
true, PacketSize),
237 [=](
Index blk_size) {
238 return AdjustBlockSize(PacketSize *
sizeof(Scalar), blk_size);
242 const Index idx2 = packet * PacketSize;
246 num_scalars -= num_packets * PacketSize;
248 self.device().parallelFor(
249 num_scalars, TensorOpCost(
self.
size(),
self.
size(), 16 *
self.
size()),
252 [=](
Index blk_size) {
253 return AdjustBlockSize(
sizeof(Scalar), blk_size);
257 const Index idx2 = num_packets * PacketSize + scalar;
264 template <
typename Self>
265 struct ReduceBlock<Self, false, true> {
267 typename Self::CoeffReturnType* data) {
268 using Scalar =
typename Self::CoeffReturnType;
269 self.device().parallelFor(
270 self.stride(), TensorOpCost(
self.
size(),
self.
size(), 16 *
self.
size()),
273 [=](
Index blk_size) {
274 return AdjustBlockSize(
sizeof(Scalar), blk_size);
285 template <
typename Self,
typename Reducer,
bool Vectorize>
286 struct ScanLauncher<Self, Reducer, ThreadPoolDevice, Vectorize> {
287 void operator()(Self&
self,
typename Self::CoeffReturnType* data) {
288 using Scalar =
typename Self::CoeffReturnType;
289 using Packet =
typename Self::PacketReturnType;
290 const int PacketSize = internal::unpacket_traits<Packet>::size;
292 const Index inner_block_size =
self.stride() *
self.size();
293 bool parallelize_by_outer_blocks = (total_size >= (
self.stride() * inner_block_size));
295 if ((parallelize_by_outer_blocks && total_size <= 4096) ||
296 (!parallelize_by_outer_blocks &&
self.stride() < PacketSize)) {
297 ScanLauncher<Self, Reducer, DefaultDevice, Vectorize> launcher;
298 launcher(
self, data);
302 if (parallelize_by_outer_blocks) {
304 const Index num_outer_blocks = total_size / inner_block_size;
305 self.device().parallelFor(
307 TensorOpCost(inner_block_size, inner_block_size,
308 16 * PacketSize * inner_block_size, Vectorize,
310 [=](
Index blk_size) {
311 return AdjustBlockSize(inner_block_size *
sizeof(Scalar), blk_size);
315 ReduceBlock<Self, Vectorize,
false> block_reducer;
316 block_reducer(
self, idx1 * inner_block_size, data);
322 ReduceBlock<Self, Vectorize,
true> block_reducer;
323 for (
Index idx1 = 0; idx1 < total_size;
324 idx1 +=
self.stride() *
self.size()) {
325 block_reducer(
self, idx1, data);
332 #if defined(EIGEN_USE_GPU) && (defined(EIGEN_GPUCC))
338 template <
typename Self,
typename Reducer>
341 Index val = threadIdx.x + blockIdx.x * blockDim.x;
342 Index offset = (val /
self.stride()) *
self.stride() *
self.size() + val %
self.stride();
344 if (offset + (
self.
size() - 1) *
self.stride() < total_size) {
346 typename Self::CoeffReturnType accum =
self.accumulator().initialize();
347 for (
Index idx = 0; idx <
self.size(); idx++) {
348 Index curr = offset + idx *
self.stride();
349 if (
self.exclusive()) {
350 data[curr] =
self.accumulator().finalize(accum);
351 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
353 self.accumulator().reduce(
self.inner().coeff(curr), &accum);
354 data[curr] =
self.accumulator().finalize(accum);
362 template <
typename Self,
typename Reducer,
bool Vectorize>
363 struct ScanLauncher<Self, Reducer, GpuDevice, Vectorize> {
364 void operator()(
const Self&
self,
typename Self::CoeffReturnType* data) {
366 Index num_blocks = (total_size /
self.size() + 63) / 64;
367 Index block_size = 64;
369 LAUNCH_GPU_KERNEL((ScanKernel<Self, Reducer>), num_blocks, block_size, 0,
self.device(),
self, total_size, data);
377 template <
typename Op,
typename ArgType,
typename Device>
384 static constexpr
int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
386 typedef std::remove_const_t<typename XprType::Scalar>
Scalar;
398 PreferBlockAccess =
false,
408 : m_impl(op.expression(), device),
410 m_exclusive(op.exclusive()),
411 m_accumulator(op.accumulator()),
413 m_stride(1), m_consume_dim(op.axis()),
423 for (
int i = 0;
i < op.
axis(); ++
i) {
424 m_stride = m_stride * dims[
i];
430 unsigned int axis = internal::convert_index<unsigned int>(op.
axis());
431 for (
unsigned int i = NumDims - 1;
i > axis; --
i) {
432 m_stride = m_stride * dims[
i];
438 return m_impl.dimensions();
446 return m_consume_dim;
454 return m_accumulator;
470 m_impl.evalSubExprsIfNeeded(NULL);
471 internal::ScanLauncher<Self, Op, Device> launcher;
473 launcher(*
this,
data);
479 launcher(*
this, m_output);
483 template<
int LoadMode>
485 return internal::ploadt<PacketReturnType, LoadMode>(m_output + index);
495 return m_output[index];
IndexedView_or_VectorBlock operator()(const Indices &indices)
#define EIGEN_DEVICE_FUNC
#define EIGEN_HIP_LAUNCH_BOUNDS_1024
#define EIGEN_STATIC_ASSERT(X, MSG)
const Op accumulator() const
XprType::CoeffReturnType CoeffReturnType
Eigen::internal::nested< TensorScanOp >::type Nested
Eigen::internal::traits< TensorScanOp >::StorageKind StorageKind
TensorScanOp(const XprType &expr, const Index &axis, bool exclusive=false, const Op &op=Op())
Eigen::internal::traits< TensorScanOp >::Scalar Scalar
Eigen::NumTraits< Scalar >::Real RealScalar
const XprType & expression() const
Eigen::internal::traits< TensorScanOp >::Index Index
void ReducePacket(Self &self, Index offset, typename Self::CoeffReturnType *data)
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
constexpr auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
void ReduceScalar(Self &self, Index offset, typename Self::CoeffReturnType *data)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
EIGEN_ALWAYS_INLINE T divup(const X x, const Y y)
internal::packet_traits< Scalar >::type type
PacketReturnType packet(Index index) const
Storage::Type EvaluatorPointerType
bool evalSubExprsIfNeeded(EvaluatorPointerType data)
XprType::CoeffReturnType CoeffReturnType
const TensorEvaluator< ArgType, Device > & inner() const
const Dimensions & dimensions() const
TensorScanOp< Op, ArgType > XprType
DSizes< Index, NumDims > Dimensions
internal::TensorBlockNotImplemented TensorBlock
PacketType< CoeffReturnType, Device >::type PacketReturnType
CoeffReturnType coeff(Index index) const
const Op & accumulator() const
TensorOpCost costPerCoeff(bool) const
const ArgType ChildTypeNoConst
const Device & device() const
EvaluatorPointerType data() const
const Index & size() const
const Device EIGEN_DEVICE_REF m_device
const Index & consume_dim() const
std::remove_const_t< typename XprType::Scalar > Scalar
EvaluatorPointerType m_output
const Index & stride() const
TensorEvaluator< ArgType, Device > m_impl
StorageMemory< Scalar, Device > Storage
TensorEvaluator< const TensorScanOp< Op, ArgType >, Device > Self
TensorEvaluator(const XprType &op, const Device &device)
A cost model used to limit the number of threads used for evaluating tensor expression.
const Dimensions & dimensions() const
static constexpr int Layout
const Device EIGEN_DEVICE_REF m_device
EvaluatorPointerType data() const