CoherentPadOp.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) 2020 The Eigen Team.
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_COHERENT_PAD_OP_H
11 #define EIGEN_COHERENT_PAD_OP_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 // Pads a vector with zeros to a given size.
20 template <typename XprType, int SizeAtCompileTime_>
21 struct CoherentPadOp;
22 
23 template <typename XprType, int SizeAtCompileTime_>
24 struct traits<CoherentPadOp<XprType, SizeAtCompileTime_>> : public traits<XprType> {
27  typedef typename std::remove_reference_t<XprNested> XprNested_;
28  enum : int {
30  SizeAtCompileTime = SizeAtCompileTime_,
31  RowsAtCompileTime = IsRowMajor ? 1 : SizeAtCompileTime,
32  ColsAtCompileTime = IsRowMajor ? SizeAtCompileTime : 1,
33  MaxRowsAtCompileTime = RowsAtCompileTime,
34  MaxColsAtCompileTime = ColsAtCompileTime,
36  };
37 };
38 
39 // Pads a vector with zeros to a given size.
40 template <typename XprType, int SizeAtCompileTime_>
41 struct CoherentPadOp : public dense_xpr_base<CoherentPadOp<XprType, SizeAtCompileTime_>>::type {
44 
48 
52 
54  static_assert(XprNested_::IsVectorAtCompileTime, "input type must be a vector");
55  }
56 
58 
60 
63  }
64 
67  }
68 
69  private:
72 };
73 
74 // Adapted from the Replicate evaluator.
75 template <typename ArgType, int SizeAtCompileTime>
76 struct unary_evaluator<CoherentPadOp<ArgType, SizeAtCompileTime>>
77  : evaluator_base<CoherentPadOp<ArgType, SizeAtCompileTime>> {
82 
83  enum {
85  LinearAccessMask = XprType::IsVectorAtCompileTime ? LinearAccessBit : 0,
88  };
89 
91  : m_arg(pad.nestedExpression()), m_argImpl(m_arg), m_size(pad.nestedExpression().size()) {}
92 
94  EIGEN_IF_CONSTEXPR(XprType::IsRowMajor) {
95  if (col < m_size.value()) {
96  return m_argImpl.coeff(1, col);
97  }
98  }
99  else {
100  if (row < m_size.value()) {
101  return m_argImpl.coeff(row, 1);
102  }
103  }
104  return CoeffReturnType(0);
105  }
106 
108  if (index < m_size.value()) {
109  return m_argImpl.coeff(index);
110  }
111  return CoeffReturnType(0);
112  }
113 
114  template <int LoadMode, typename PacketType>
116  // AutoDiff scalar's derivative must be a vector, which is enforced by static assert.
117  // Defer to linear access for simplicity.
118  EIGEN_IF_CONSTEXPR(XprType::IsRowMajor) { return packet(col); }
119  return packet(row);
120  }
121 
122  template <int LoadMode, typename PacketType>
124  constexpr int kPacketSize = unpacket_traits<PacketType>::size;
125  if (index + kPacketSize <= m_size.value()) {
126  return m_argImpl.template packet<LoadMode, PacketType>(index);
127  } else if (index < m_size.value()) {
128  // Partial packet.
129  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[kPacketSize];
130  const int partial = m_size.value() - index;
131  for (int i = 0; i < partial && i < kPacketSize; ++i) {
132  values[i] = m_argImpl.coeff(index + i);
133  }
134  for (int i = partial; i < kPacketSize; ++i) {
135  values[i] = CoeffReturnType(0);
136  }
137  return pload<PacketType>(values);
138  }
139  return pset1<PacketType>(CoeffReturnType(0));
140  }
141 
142  protected:
146 };
147 
148 } // namespace internal
149 
150 } // namespace Eigen
151 
152 #endif // EIGEN_CWISE_BINARY_OP_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:146
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_IF_CONSTEXPR(X)
Definition: Macros.h:1306
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
m row(1)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:161
const unsigned int LinearAccessBit
Definition: Constants.h:133
const unsigned int RowMajorBit
Definition: Constants.h:70
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
const unsigned int HereditaryBits
Definition: Constants.h:198
const unsigned int NestByRefBit
Definition: Constants.h:173
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
default
Definition: calibrate.py:45
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
CwiseBinaryOp< internal::scalar_sum_op< double, double >, const CpyMatrixXd, const CpyMatrixXd > XprType
Definition: nestbyvalue.cpp:15
Definition: TensorMeta.h:47
Definition: CoherentPadOp.h:41
const internal::variable_if_dynamic< Index, SizeAtCompileTime > size_
Definition: CoherentPadOp.h:71
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const
Definition: CoherentPadOp.h:61
XprNested xpr_
Definition: CoherentPadOp.h:70
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const XprNested_ & nestedExpression() const
Definition: CoherentPadOp.h:57
typename traits< CoherentPadOp >::XprNested_ XprNested_
Definition: CoherentPadOp.h:46
typename traits< CoherentPadOp >::XprNested XprNested
Definition: CoherentPadOp.h:45
internal::generic_xpr_base< CoherentPadOp< XprType, SizeAtCompileTime_ > >::type Base
Definition: CoherentPadOp.h:42
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const
Definition: CoherentPadOp.h:65
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: CoherentPadOp.h:59
XprNested_ NestedExpression
Definition: CoherentPadOp.h:47
Definition: XprHelper.h:558
Definition: CoreEvaluators.h:118
Definition: CoreEvaluators.h:104
Definition: XprHelper.h:575
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
std::conditional_t< bool(traits< T >::Flags &NestByRefBit), T const &, const T > type
Definition: XprHelper.h:507
T type
Definition: Meta.h:118
internal::remove_all< XprType >::type PlainXprType
Definition: CoherentPadOp.h:25
internal::ref_selector< XprType >::type XprNested
Definition: CoherentPadOp.h:26
std::remove_reference_t< XprNested > XprNested_
Definition: CoherentPadOp.h:27
Definition: ForwardDeclarations.h:21
const variable_if_dynamic< Index, ArgTypeNestedCleaned::SizeAtCompileTime > m_size
Definition: CoherentPadOp.h:145
EIGEN_STRONG_INLINE PacketType packet(Index index) const
Definition: CoherentPadOp.h:123
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE unary_evaluator(const XprType &pad)
Definition: CoherentPadOp.h:90
internal::remove_all_t< ArgTypeNested > ArgTypeNestedCleaned
Definition: CoherentPadOp.h:81
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
Definition: CoherentPadOp.h:93
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: CoherentPadOp.h:107
evaluator< ArgTypeNestedCleaned > m_argImpl
Definition: CoherentPadOp.h:144
internal::nested_eval< ArgType, 1 >::type ArgTypeNested
Definition: CoherentPadOp.h:80
EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const
Definition: CoherentPadOp.h:115
internal::remove_all_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: CoherentPadOp.h:79
CoherentPadOp< ArgType, SizeAtCompileTime > XprType
Definition: CoherentPadOp.h:78
Definition: CoreEvaluators.h:82
Definition: GenericPacketMath.h:134
Definition: Slicing_custom_padding_cxx11.cpp:1