Eigen  5.0.1
 
Loading...
Searching...
No Matches
DeviceWrapper.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2023 Charlie 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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_DEVICEWRAPPER_H
12#define EIGEN_DEVICEWRAPPER_H
13
14namespace Eigen {
15template <typename Derived, typename Device>
16struct DeviceWrapper {
17 using Base = EigenBase<internal::remove_all_t<Derived>>;
18 using Scalar = typename Derived::Scalar;
19
20 EIGEN_DEVICE_FUNC DeviceWrapper(Base& xpr, Device& device) : m_xpr(xpr.derived()), m_device(device) {}
21 EIGEN_DEVICE_FUNC DeviceWrapper(const Base& xpr, Device& device) : m_xpr(xpr.derived()), m_device(device) {}
22
23 template <typename OtherDerived>
24 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator=(const EigenBase<OtherDerived>& other) {
25 using AssignOp = internal::assign_op<Scalar, typename OtherDerived::Scalar>;
26 internal::call_assignment(*this, other.derived(), AssignOp());
27 return m_xpr;
28 }
29 template <typename OtherDerived>
30 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator+=(const EigenBase<OtherDerived>& other) {
31 using AddAssignOp = internal::add_assign_op<Scalar, typename OtherDerived::Scalar>;
32 internal::call_assignment(*this, other.derived(), AddAssignOp());
33 return m_xpr;
34 }
35 template <typename OtherDerived>
36 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator-=(const EigenBase<OtherDerived>& other) {
37 using SubAssignOp = internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>;
38 internal::call_assignment(*this, other.derived(), SubAssignOp());
39 return m_xpr;
40 }
41
42 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& derived() { return m_xpr; }
43 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Device& device() { return m_device; }
44 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoAlias<DeviceWrapper, EigenBase> noalias() {
45 return NoAlias<DeviceWrapper, EigenBase>(*this);
46 }
47
48 Derived& m_xpr;
49 Device& m_device;
50};
51
52namespace internal {
53
54// this is where we differentiate between lazy assignment and specialized kernels (e.g. matrix products)
55template <typename DstXprType, typename SrcXprType, typename Functor, typename Device,
56 typename Kind = typename AssignmentKind<typename evaluator_traits<DstXprType>::Shape,
57 typename evaluator_traits<SrcXprType>::Shape>::Kind,
58 typename EnableIf = void>
59struct AssignmentWithDevice;
60
61// unless otherwise specified, use the default product implementation
62template <typename DstXprType, typename Lhs, typename Rhs, int Options, typename Functor, typename Device,
63 typename Weak>
64struct AssignmentWithDevice<DstXprType, Product<Lhs, Rhs, Options>, Functor, Device, Dense2Dense, Weak> {
65 using SrcXprType = Product<Lhs, Rhs, Options>;
66 using Base = Assignment<DstXprType, SrcXprType, Functor>;
67 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src, const Functor& func,
68 Device&) {
69 Base::run(dst, src, func);
70 }
71};
72
73// specialization for coefficient-wise assignment
74template <typename DstXprType, typename SrcXprType, typename Functor, typename Device, typename Weak>
75struct AssignmentWithDevice<DstXprType, SrcXprType, Functor, Device, Dense2Dense, Weak> {
76 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src, const Functor& func,
77 Device& device) {
78#ifndef EIGEN_NO_DEBUG
79 internal::check_for_aliasing(dst, src);
80#endif
81
82 call_dense_assignment_loop(dst, src, func, device);
83 }
84};
85
86// this allows us to use the default evaluation scheme if it is not specialized for the device
87template <typename Kernel, typename Device, int Traversal = Kernel::AssignmentTraits::Traversal,
88 int Unrolling = Kernel::AssignmentTraits::Unrolling>
89struct dense_assignment_loop_with_device {
90 using Base = dense_assignment_loop<Kernel, Traversal, Unrolling>;
91 // Always inlined so the loop lands in call_dense_assignment_loop(), which owns the evaluators.
92 static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE constexpr void run(Kernel& kernel, Device&) { Base::run(kernel); }
93};
94
95// entry point for a generic expression with device
96template <typename Dst, typename Src, typename Func, typename Device>
97EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void call_assignment_no_alias(DeviceWrapper<Dst, Device> dst,
98 const Src& src, const Func& func) {
99 enum {
100 NeedToTranspose = ((int(Dst::RowsAtCompileTime) == 1 && int(Src::ColsAtCompileTime) == 1) ||
101 (int(Dst::ColsAtCompileTime) == 1 && int(Src::RowsAtCompileTime) == 1)) &&
102 int(Dst::SizeAtCompileTime) != 1
103 };
104
105 using ActualDstTypeCleaned = std::conditional_t<NeedToTranspose, Transpose<Dst>, Dst>;
106 using ActualDstType = std::conditional_t<NeedToTranspose, Transpose<Dst>, Dst&>;
107 ActualDstType actualDst(dst.derived());
108
109 // TODO: check whether this is the right place to perform these checks:
110 EIGEN_STATIC_ASSERT_LVALUE(Dst)
111 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(ActualDstTypeCleaned, Src)
112 EIGEN_CHECK_BINARY_COMPATIBILITY(Func, typename ActualDstTypeCleaned::Scalar, typename Src::Scalar);
113
114 // this provides a mechanism for specializing simple assignments, matrix products, etc
115 AssignmentWithDevice<ActualDstTypeCleaned, Src, Func, Device>::run(actualDst, src, func, dst.device());
116}
117
118// copy and pasted from AssignEvaluator except forward device to kernel
119template <typename DstXprType, typename SrcXprType, typename Functor, typename Device>
120EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void call_dense_assignment_loop(DstXprType& dst, const SrcXprType& src,
121 const Functor& func, Device& device) {
122 using DstEvaluatorType = evaluator<DstXprType>;
123 using SrcEvaluatorType = evaluator<SrcXprType>;
124
125 SrcEvaluatorType srcEvaluator(src);
126
127 // NOTE To properly handle A = (A*A.transpose())/s with A rectangular,
128 // we need to resize the destination after the source evaluator has been created.
129 resize_if_allowed(dst, src, func);
130
131 DstEvaluatorType dstEvaluator(dst);
132
133 using Kernel = generic_dense_assignment_kernel<DstEvaluatorType, SrcEvaluatorType, Functor>;
134
135 Kernel kernel(dstEvaluator, srcEvaluator, func, dst.const_cast_derived());
136
137 dense_assignment_loop_with_device<Kernel, Device>::run(kernel, device);
138}
139
140} // namespace internal
141
142template <typename Derived>
143template <typename Device>
144EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DeviceWrapper<Derived, Device> EigenBase<Derived>::device(Device& device) {
145 return DeviceWrapper<Derived, Device>(derived(), device);
146}
147
148template <typename Derived>
149template <typename Device>
150EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DeviceWrapper<const Derived, Device> EigenBase<Derived>::device(
151 Device& device) const {
152 return DeviceWrapper<const Derived, Device>(derived(), device);
153}
154} // namespace Eigen
155#endif