Fill.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2024 Charles Schlosser <cs.schlosser@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_FILL_H
11 #define EIGEN_FILL_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename Xpr>
21 struct eigen_fill_helper : std::false_type {};
22 
23 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
24 struct eigen_fill_helper<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::true_type {};
25 
26 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
27 struct eigen_fill_helper<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::true_type {};
28 
29 template <typename Xpr, int BlockRows, int BlockCols>
30 struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ true>> : eigen_fill_helper<Xpr> {};
31 
32 template <typename Xpr, int BlockRows, int BlockCols>
33 struct eigen_fill_helper<Block<Xpr, BlockRows, BlockCols, /*InnerPanel*/ false>>
34  : std::integral_constant<bool, eigen_fill_helper<Xpr>::value &&
35  (Xpr::IsRowMajor ? (BlockRows == 1) : (BlockCols == 1))> {};
36 
37 template <typename Xpr, int Options>
38 struct eigen_fill_helper<Map<Xpr, Options, Stride<0, 0>>> : eigen_fill_helper<Xpr> {};
39 
40 template <typename Xpr, int Options, int OuterStride_>
41 struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>>
42  : std::integral_constant<bool, eigen_fill_helper<Xpr>::value &&
43  enum_eq_not_dynamic(OuterStride_, Xpr::InnerSizeAtCompileTime)> {};
44 
45 template <typename Xpr, int Options, int OuterStride_>
46 struct eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 1>>>
47  : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
48 
49 template <typename Xpr, int Options, int InnerStride_>
50 struct eigen_fill_helper<Map<Xpr, Options, InnerStride<InnerStride_>>>
51  : eigen_fill_helper<Map<Xpr, Options, Stride<0, InnerStride_>>> {};
52 
53 template <typename Xpr, int Options, int OuterStride_>
54 struct eigen_fill_helper<Map<Xpr, Options, OuterStride<OuterStride_>>>
55  : eigen_fill_helper<Map<Xpr, Options, Stride<OuterStride_, 0>>> {};
56 
57 template <typename Xpr>
58 struct eigen_fill_impl<Xpr, /*use_fill*/ false> {
59  using Scalar = typename Xpr::Scalar;
61  using PlainObject = typename Xpr::PlainObject;
62  using Constant = typename PlainObject::ConstantReturnType;
63  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const Scalar& val) {
64  const Constant src(dst.rows(), dst.cols(), val);
65  run(dst, src);
66  }
67  template <typename SrcXpr>
68  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
70  }
71 };
72 
73 #if EIGEN_COMP_MSVC || defined(EIGEN_GPU_COMPILE_PHASE)
74 template <typename Xpr>
75 struct eigen_fill_impl<Xpr, /*use_fill*/ true> : eigen_fill_impl<Xpr, /*use_fill*/ false> {};
76 #else
77 template <typename Xpr>
78 struct eigen_fill_impl<Xpr, /*use_fill*/ true> {
79  using Scalar = typename Xpr::Scalar;
80  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const Scalar& val) {
81  using std::fill_n;
82  fill_n(dst.data(), dst.size(), val);
83  }
84  template <typename SrcXpr>
85  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
87  const Scalar& val = src.functor()();
88  run(dst, val);
89  }
90 };
91 #endif
92 
93 template <typename Xpr>
96 };
97 
98 template <typename Xpr>
99 struct eigen_zero_impl<Xpr, /*use_memset*/ false> {
100  using Scalar = typename Xpr::Scalar;
101  using PlainObject = typename Xpr::PlainObject;
102  using Zero = typename PlainObject::ZeroReturnType;
103  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst) {
104  const Zero src(dst.rows(), dst.cols());
105  run(dst, src);
106  }
107  template <typename SrcXpr>
108  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
110  }
111 };
112 
113 template <typename Xpr>
114 struct eigen_zero_impl<Xpr, /*use_memset*/ true> {
115  using Scalar = typename Xpr::Scalar;
116  static constexpr size_t max_bytes = (std::numeric_limits<std::ptrdiff_t>::max)();
117  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst) {
118  const size_t num_bytes = dst.size() * sizeof(Scalar);
119  if (num_bytes == 0) return;
120  void* dst_ptr = static_cast<void*>(dst.data());
121 #ifndef EIGEN_NO_DEBUG
122  if (num_bytes > max_bytes) throw_std_bad_alloc();
123  eigen_assert((dst_ptr != nullptr) && "null pointer dereference error!");
124 #endif
125  EIGEN_USING_STD(memset);
126  memset(dst_ptr, 0, num_bytes);
127  }
128  template <typename SrcXpr>
129  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr& dst, const SrcXpr& src) {
131  run(dst);
132  }
133 };
134 
135 } // namespace internal
136 } // namespace Eigen
137 
138 #endif // EIGEN_FILL_H
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1090
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
SCALAR Scalar
Definition: bench_gemm.cpp:45
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:48
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:110
Convenience specialization of Stride to specify only an inner stride See class Map for some examples.
Definition: Stride.h:93
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Convenience specialization of Stride to specify only an outer stride See class Map for some examples.
Definition: Stride.h:104
Holds strides information for Map.
Definition: Stride.h:55
#define max(a, b)
Definition: datatypes.h:23
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize_if_allowed(DstXprType &dst, const SrcXprType &src, const Functor &)
Definition: AssignEvaluator.h:703
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR void call_dense_assignment_loop(DstXprType &dst, const SrcXprType &src, const Functor &func)
Definition: AssignEvaluator.h:720
EIGEN_DEVICE_FUNC void throw_std_bad_alloc()
Definition: Memory.h:110
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
squared absolute value
Definition: GlobalFunctions.h:87
val
Definition: calibrate.py:119
Definition: Eigen_Colamd.h:49
Template functor for scalar/packet assignment.
Definition: AssignmentFunctors.h:25
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const Scalar &val)
Definition: Fill.h:63
typename Xpr::PlainObject PlainObject
Definition: Fill.h:61
typename PlainObject::ConstantReturnType Constant
Definition: Fill.h:62
typename Xpr::Scalar Scalar
Definition: Fill.h:59
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const SrcXpr &src)
Definition: Fill.h:68
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const SrcXpr &src)
Definition: Fill.h:85
typename Xpr::Scalar Scalar
Definition: Fill.h:79
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const Scalar &val)
Definition: Fill.h:80
Definition: ForwardDeclarations.h:512
static constexpr bool value
Definition: Fill.h:95
typename PlainObject::ZeroReturnType Zero
Definition: Fill.h:102
typename Xpr::PlainObject PlainObject
Definition: Fill.h:101
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const SrcXpr &src)
Definition: Fill.h:108
typename Xpr::Scalar Scalar
Definition: Fill.h:100
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst)
Definition: Fill.h:103
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst, const SrcXpr &src)
Definition: Fill.h:129
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(Xpr &dst)
Definition: Fill.h:117
typename Xpr::Scalar Scalar
Definition: Fill.h:115
Definition: ForwardDeclarations.h:516
Definition: NullaryFunctors.h:21