10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_FUNCTORS_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_FUNCTORS_H
22 template <
typename Scalar>
23 struct scalar_mod_op {
25 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator() (
const Scalar& a)
const {
return a % m_divisor; }
26 const Scalar m_divisor;
28 template <
typename Scalar>
29 struct functor_traits<scalar_mod_op<Scalar> >
30 {
enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess =
false }; };
36 template <
typename Scalar>
37 struct scalar_mod2_op {
38 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator() (
const Scalar& a,
const Scalar&
b)
const {
return a %
b; }
40 template <
typename Scalar>
41 struct functor_traits<scalar_mod2_op<Scalar> >
42 {
enum { Cost = scalar_div_cost<Scalar,false>::value, PacketAccess =
false }; };
44 template <
typename Scalar>
45 struct scalar_fmod_op {
47 operator()(
const Scalar& a,
const Scalar&
b)
const {
51 template <
typename Scalar>
52 struct functor_traits<scalar_fmod_op<Scalar> > {
54 PacketAccess =
false };
57 template<
typename Reducer,
typename Device>
58 struct reducer_traits {
63 IsExactlyAssociative =
true
68 template <
typename T>
struct SumReducer
71 internal::scalar_sum_op<T> sum_op;
72 *accum = sum_op(*accum, t);
74 template <
typename Packet>
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void reducePacket(
const Packet& p, Packet* accum)
const {
76 (*accum) = padd<Packet>(*accum, p);
80 internal::scalar_cast_op<int, T> conv;
83 template <
typename Packet>
85 return pset1<Packet>(initialize());
90 template <
typename Packet>
91 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(
const Packet& vaccum)
const {
94 template <
typename Packet>
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
T finalizeBoth(
const T saccum,
const Packet& vaccum)
const {
96 internal::scalar_sum_op<T> sum_op;
97 return sum_op(saccum,
predux(vaccum));
101 template <
typename T,
typename Device>
102 struct reducer_traits<SumReducer<
T>, Device> {
105 PacketAccess = PacketType<T, Device>::HasAdd,
111 template <
typename T>
struct MeanReducer
114 MeanReducer() : scalarCount_(0), packetCount_(0) { }
117 internal::scalar_sum_op<T> sum_op;
118 *accum = sum_op(*accum, t);
121 template <
typename Packet>
122 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void reducePacket(
const Packet& p, Packet* accum) {
123 (*accum) = padd<Packet>(*accum, p);
128 internal::scalar_cast_op<int, T> conv;
131 template <
typename Packet>
133 return pset1<Packet>(initialize());
136 internal::scalar_quotient_op<T> quotient_op;
137 return quotient_op(accum,
T(scalarCount_));
139 template <
typename Packet>
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(
const Packet& vaccum)
const {
141 return pdiv(vaccum, pset1<Packet>(
T(packetCount_)));
143 template <
typename Packet>
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
T finalizeBoth(
const T saccum,
const Packet& vaccum)
const {
145 internal::scalar_sum_op<T> sum_op;
146 internal::scalar_quotient_op<T> quotient_op;
148 sum_op(saccum,
predux(vaccum)),
149 T(scalarCount_ + packetCount_ * unpacket_traits<Packet>::size));
157 template <
typename T,
typename Device>
158 struct reducer_traits<MeanReducer<
T>, Device> {
161 PacketAccess = PacketType<T, Device>::HasAdd &&
169 template <
typename T,
bool IsMax = true,
bool IsInteger = true>
170 struct MinMaxBottomValue {
175 template <
typename T>
176 struct MinMaxBottomValue<
T, true, false> {
181 template <
typename T>
182 struct MinMaxBottomValue<
T, false, true> {
187 template <
typename T>
188 struct MinMaxBottomValue<
T, false, false> {
195 template <
typename T,
int NaNPropagation=PropagateFast>
struct MaxReducer
198 scalar_max_op<T, T, NaNPropagation> op;
199 *accum = op(t, *accum);
201 template <
typename Packet>
202 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void reducePacket(
const Packet& p, Packet* accum)
const {
203 scalar_max_op<T, T, NaNPropagation> op;
204 (*accum) = op.packetOp(*accum, p);
209 template <
typename Packet>
211 return pset1<Packet>(initialize());
216 template <
typename Packet>
217 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(
const Packet& vaccum)
const {
220 template <
typename Packet>
221 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
T finalizeBoth(
const T saccum,
const Packet& vaccum)
const {
222 scalar_max_op<T, T, NaNPropagation> op;
223 return op(saccum, op.predux(vaccum));
227 template <
typename T,
typename Device,
int NaNPropagation>
228 struct reducer_traits<MaxReducer<
T, NaNPropagation>, Device> {
231 PacketAccess = PacketType<T, Device>::HasMax,
237 template <
typename T,
int NaNPropagation=PropagateFast>
struct MinReducer
240 scalar_min_op<T, T, NaNPropagation> op;
241 *accum = op(t, *accum);
243 template <
typename Packet>
244 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void reducePacket(
const Packet& p, Packet* accum)
const {
245 scalar_min_op<T, T, NaNPropagation> op;
246 (*accum) = op.packetOp(*accum, p);
251 template <
typename Packet>
253 return pset1<Packet>(initialize());
258 template <
typename Packet>
259 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(
const Packet& vaccum)
const {
262 template <
typename Packet>
263 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
T finalizeBoth(
const T saccum,
const Packet& vaccum)
const {
264 scalar_min_op<T, T, NaNPropagation> op;
265 return op(saccum, op.predux(vaccum));
269 template <
typename T,
typename Device,
int NaNPropagation>
270 struct reducer_traits<MinReducer<
T, NaNPropagation>, Device> {
273 PacketAccess = PacketType<T, Device>::HasMin,
279 template <
typename T>
struct ProdReducer
282 internal::scalar_product_op<T> prod_op;
283 (*accum) = prod_op(*accum, t);
285 template <
typename Packet>
286 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void reducePacket(
const Packet& p, Packet* accum)
const {
287 (*accum) = pmul<Packet>(*accum, p);
290 internal::scalar_cast_op<int, T> conv;
293 template <
typename Packet>
295 return pset1<Packet>(initialize());
300 template <
typename Packet>
301 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(
const Packet& vaccum)
const {
304 template <
typename Packet>
305 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
T finalizeBoth(
const T saccum,
const Packet& vaccum)
const {
306 internal::scalar_product_op<T> prod_op;
311 template <
typename T,
typename Device>
312 struct reducer_traits<ProdReducer<
T>, Device> {
315 PacketAccess = PacketType<T, Device>::HasMul,
317 IsExactlyAssociative =
true
325 *accum = *accum && t;
335 template <
typename Device>
336 struct reducer_traits<AndReducer, Device> {
339 PacketAccess =
false,
341 IsExactlyAssociative =
true
348 *accum = *accum || t;
358 template <
typename Device>
359 struct reducer_traits<OrReducer, Device> {
362 PacketAccess =
false,
364 IsExactlyAssociative =
true
370 template <
typename T>
struct ArgMaxPairReducer
373 if (t.second < accum->second) {
375 }
else if (t.second > accum->second || accum->first > t.first ) {
380 return T(0, NumTraits<typename T::second_type>::lowest());
387 template <
typename T,
typename Device>
388 struct reducer_traits<ArgMaxPairReducer<
T>, Device> {
391 PacketAccess =
false,
393 IsExactlyAssociative =
true
398 template <
typename T>
struct ArgMinPairReducer
401 if (t.second > accum->second) {
403 }
else if (t.second < accum->second || accum->first > t.first) {
408 return T(0, NumTraits<typename T::second_type>::highest());
415 template <
typename T,
typename Device>
416 struct reducer_traits<ArgMinPairReducer<
T>, Device> {
419 PacketAccess =
false,
421 IsExactlyAssociative =
true
426 template <
typename T,
typename Index,
size_t NumDims>
427 class GaussianGenerator {
429 static constexpr
bool PacketAccess =
false;
436 for (
size_t i = 0;
i < NumDims; ++
i) {
437 m_two_sigmas[
i] = std_devs[
i] * std_devs[
i] * 2;
444 for (
size_t i = 0;
i < NumDims; ++
i) {
445 T offset = coordinates[
i] - m_means[
i];
446 tmp += offset * offset / m_two_sigmas[
i];
456 template <
typename T,
typename Index,
size_t NumDims>
457 struct functor_traits<GaussianGenerator<
T,
Index, NumDims> > {
460 functor_traits<scalar_quotient_op<T, T> >::Cost) +
461 functor_traits<scalar_exp_op<T> >::Cost,
462 PacketAccess = GaussianGenerator<T, Index, NumDims>::PacketAccess
466 template <
typename Scalar>
467 struct scalar_clamp_op {
468 EIGEN_DEVICE_FUNC inline scalar_clamp_op(
const Scalar& _min,
const Scalar& _max) : m_min(_min), m_max(_max) {}
473 template <
typename Packet>
475 packetOp(
const Packet&
x)
const {
481 template<
typename Scalar>
482 struct functor_traits<scalar_clamp_op<Scalar> >
483 {
enum { Cost = 2 *
NumTraits<Scalar>::AddCost, PacketAccess = (packet_traits<Scalar>::HasMin && packet_traits<Scalar>::HasMax)}; };
IndexedView_or_VectorBlock operator()(const Indices &indices)
#define EIGEN_UNROLL_LOOP
#define EIGEN_DEVICE_FUNC
Packet pmin(const Packet &a, const Packet &b)
unpacket_traits< Packet >::type predux(const Packet &a)
Packet pmax(const Packet &a, const Packet &b)
unpacket_traits< Packet >::type predux_mul(const Packet &a)
Packet pdiv(const Packet &a, const Packet &b)
EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
EIGEN_ALWAYS_INLINE T exp(const T &x)
EIGEN_ALWAYS_INLINE T fmod(const T &a, const T &b)
EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex