10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_SCAN_H
20 template <
typename Op,
typename XprType>
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;
32 template <
typename Op,
typename XprType>
37 template <
typename Op,
typename XprType>
48 template <
typename Op,
typename XprType>
76 template <
typename Self>
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);
109 template <
typename Self>
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));
144 template <
typename Self,
bool Vectorize,
bool Parallel>
147 for (
Index idx2 = 0; idx2 <
self.stride(); idx2++) {
149 Index offset = idx1 + idx2;
156 template <
typename Self>
159 using Packet =
typename Self::PacketReturnType;
162 for (; idx2 + PacketSize <=
self.stride(); idx2 += PacketSize) {
164 Index offset = idx1 + idx2;
167 for (; idx2 <
self.stride(); idx2++) {
169 Index offset = idx1 + idx2;
176 template <
typename Self,
typename Reducer,
typename Device,
187 for (
Index idx1 = 0; idx1 < total_size; idx1 +=
self.stride() *
self.
size()) {
189 block_reducer(
self, idx1,
data);
194 #ifdef EIGEN_USE_THREADS
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);
205 template <
typename Self>
206 struct ReduceBlock<Self,
true,
true> {
208 using Scalar =
typename Self::CoeffReturnType;
209 using Packet =
typename Self::PacketReturnType;
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;
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;
244 template <
typename Self>
245 struct ReduceBlock<Self, false, true> {
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); },
254 for (
Index idx2 = first; idx2 <
last; ++idx2) {
262 template <
typename Self,
typename Reducer,
bool Vectorize>
263 struct 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;
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))
310 template <
typename Self,
typename Reducer>
312 typename Self::CoeffReturnType*
data) {
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);
334 template <
typename Self,
typename Reducer,
bool Vectorize>
335 struct ScanLauncher<Self, Reducer, GpuDevice, Vectorize> {
336 void operator()(
const Self&
self,
typename Self::CoeffReturnType*
data) {
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);
349 template <
typename Op,
typename ArgType,
typename Device>
357 typedef std::remove_const_t<typename XprType::Scalar>
Scalar;
369 PreferBlockAccess =
false,
379 : m_impl(
op.expression(), device),
381 m_exclusive(
op.exclusive()),
382 m_accumulator(
op.accumulator()),
385 m_consume_dim(
op.axis()),
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];
426 m_impl.evalSubExprsIfNeeded(NULL);
429 launcher(*
this,
data);
436 launcher(*
this, m_output);
440 template <
int LoadMode>
442 return internal::ploadt<PacketReturnType, LoadMode>(m_output + index);
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_HIP_LAUNCH_BOUNDS_1024
Definition: Macros.h:576
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
int data[]
Definition: Map_placement_new.cpp:1
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:34
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
The tensor base class.
Definition: TensorBase.h:1026
Definition: TensorCostModel.h:28
Definition: TensorScan.h:49
XprType::CoeffReturnType CoeffReturnType
Definition: TensorScan.h:53
Eigen::internal::nested< TensorScanOp >::type Nested
Definition: TensorScan.h:54
const bool m_exclusive
Definition: TensorScan.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Index axis() const
Definition: TensorScan.h:62
Eigen::internal::traits< TensorScanOp >::StorageKind StorageKind
Definition: TensorScan.h:55
Eigen::internal::traits< TensorScanOp >::Scalar Scalar
Definition: TensorScan.h:51
XprType::Nested m_expr
Definition: TensorScan.h:68
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Op accumulator() const
Definition: TensorScan.h:64
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorScan.h:52
const Op m_accumulator
Definition: TensorScan.h:70
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool exclusive() const
Definition: TensorScan.h:65
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorScanOp(const XprType &expr, const Index &axis, bool exclusive=false, const Op &op=Op())
Definition: TensorScan.h:58
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const XprType & expression() const
Definition: TensorScan.h:63
Eigen::internal::traits< TensorScanOp >::Index Index
Definition: TensorScan.h:56
const Index m_axis
Definition: TensorScan.h:69
Definition: TensorBlock.h:566
dim3 threadIdx
Definition: gpu_common.h:16
dim3 blockDim
Definition: gpu_common.h:16
dim3 blockIdx
Definition: gpu_common.h:16
static constexpr const last_t last
Definition: IndexedViewHelper.h:48
@ ColMajor
Definition: Constants.h:318
char char * op
Definition: level2_impl.h:374
EIGEN_STRONG_INLINE void ReduceScalar(Self &self, Index offset, typename Self::CoeffReturnType *data)
Definition: TensorScan.h:77
constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
Definition: MoreMeta.h:497
EIGEN_STRONG_INLINE void ReducePacket(Self &self, Index offset, typename Self::CoeffReturnType *data)
Definition: TensorScan.h:110
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE EIGEN_CONSTEXPR T div_ceil(T a, T b)
Definition: MathFunctions.h:1251
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
val
Definition: calibrate.py:119
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
T Real
Definition: NumTraits.h:183
Definition: TensorMeta.h:47
Definition: TensorForwardDeclarations.h:42
Storage::Type EvaluatorPointerType
Definition: TensorScan.h:362
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorScan.h:447
const ArgType ChildType
Definition: TensorScan.h:354
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EvaluatorPointerType data() const
Definition: TensorScan.h:445
XprType::CoeffReturnType CoeffReturnType
Definition: TensorScan.h:358
Index m_stride
Definition: TensorScan.h:467
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorEvaluator< ArgType, Device > & inner() const
Definition: TensorScan.h:421
TensorScanOp< Op, ArgType > XprType
Definition: TensorScan.h:351
DSizes< Index, NumDims > Dimensions
Definition: TensorScan.h:356
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Index & size() const
Definition: TensorScan.h:415
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorScan.h:375
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorScan.h:359
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorScan.h:409
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool exclusive() const
Definition: TensorScan.h:419
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorScan.h:378
Index m_consume_dim
Definition: TensorScan.h:468
const Index m_size
Definition: TensorScan.h:466
const ArgType ChildTypeNoConst
Definition: TensorScan.h:353
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data)
Definition: TensorScan.h:425
const Device EIGEN_DEVICE_REF m_device
Definition: TensorScan.h:463
const bool m_exclusive
Definition: TensorScan.h:464
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorScan.h:453
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Index & stride() const
Definition: TensorScan.h:411
std::remove_const_t< typename XprType::Scalar > Scalar
Definition: TensorScan.h:357
EvaluatorPointerType m_output
Definition: TensorScan.h:469
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Device & device() const
Definition: TensorScan.h:423
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition: TensorScan.h:441
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorScan.h:462
StorageMemory< Scalar, Device > Storage
Definition: TensorScan.h:361
TensorEvaluator< const TensorScanOp< Op, ArgType >, Device > Self
Definition: TensorScan.h:360
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Index & consume_dim() const
Definition: TensorScan.h:413
Op m_accumulator
Definition: TensorScan.h:465
XprType::Index Index
Definition: TensorScan.h:352
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Op & accumulator() const
Definition: TensorScan.h:417
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool) const
Definition: TensorScan.h:449
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31
static constexpr int Layout
Definition: TensorEvaluator.h:46
const Device EIGEN_DEVICE_REF m_device
Definition: TensorEvaluator.h:170
@ PacketAccess
Definition: TensorEvaluator.h:50
@ IsAligned
Definition: TensorEvaluator.h:49
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorEvaluator.h:165
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorEvaluator.h:69
EIGEN_STRONG_INLINE void operator()(Self &self, Index idx1, typename Self::CoeffReturnType *data)
Definition: TensorScan.h:158
Definition: TensorScan.h:145
EIGEN_STRONG_INLINE void operator()(Self &self, Index idx1, typename Self::CoeffReturnType *data)
Definition: TensorScan.h:146
Definition: TensorScan.h:179
void operator()(Self &self, typename Self::CoeffReturnType *data) const
Definition: TensorScan.h:180
const TensorScanOp< Op, XprType > & type
Definition: TensorScan.h:34
Definition: XprHelper.h:427
TensorScanOp< Op, XprType > type
Definition: TensorScan.h:39
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: TensorFunctors.h:60
traits< XprType > XprTraits
Definition: TensorScan.h:23
XprTraits::StorageKind StorageKind
Definition: TensorScan.h:24
XprTraits::PointerType PointerType
Definition: TensorScan.h:29
std::remove_reference_t< Nested > Nested_
Definition: TensorScan.h:26
XprType::Nested Nested
Definition: TensorScan.h:25
XprType::Scalar Scalar
Definition: TensorScan.h:22
Definition: ForwardDeclarations.h:21
Definition: GenericPacketMath.h:134
@ size
Definition: GenericPacketMath.h:139
Definition: ZVector/PacketMath.h:50