Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Complex.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5// Copyright (C) 2021 C. Antonio Sanchez <cantonios@google.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_COMPLEX_GPU_H
13#define EIGEN_COMPLEX_GPU_H
14
15// Many std::complex methods such as operator+, operator-, operator* and
16// operator/ are not constexpr. Due to this, GCC and older versions of clang do
17// not treat them as device functions and thus Eigen functors making use of
18// these operators fail to compile. Here, we manually specialize these
19// operators for complex types when building for CUDA to enable their use
20// on-device.
21//
22// Eigen/Core includes this header ahead of the Core headers (Meta.h's
23// equal_strict, MathFunctions.h, GenericPacketMath.h, the functors), which
24// apply these operators to a dependent Scalar: two-phase
25// lookup finds non-ADL candidates in the definition context only, and ADL for
26// std::complex<T> reaches namespace std alone, so an overload declared later
27// leaves those templates bound to the host-only std:: operators. nvcc before
28// CUDA 13.3 resolved them at the instantiation point, which masked the ordering.
29//
30// NOTES:
31// - Compound assignment operators +=,-=,*=,/=(Scalar) will not work on device,
32// since they are already specialized in the standard. Using them will result
33// in silent kernel failures.
34// - Compiling with MSVC and using +=,-=,*=,/=(std::complex<Scalar>) will lead
35// to duplicate definition errors, since these are already specialized in
36// Visual Studio's <complex> header (contrary to the standard). This is
37// preferable to removing such definitions, which will lead to silent kernel
38// failures.
39// - Compiling with ICC requires defining _USE_COMPLEX_SPECIALIZATION_ prior
40// to the first inclusion of <complex>.
41// - Device code outside namespace Eigen that applies these operators to a
42// dependent Scalar reaches them only through `EIGEN_USING_STD_COMPLEX_OPERATORS`,
43// `using namespace Eigen;`, or using-declarations in scope (see
44// test/gpu_basic.cu); ADL alone finds the host-only std:: operators.
45
46#if defined(EIGEN_GPUCC) && defined(EIGEN_GPU_COMPILE_PHASE)
47
48// ICC already specializes std::complex<float> and std::complex<double>
49// operators, preventing us from making them device functions here.
50// This will lead to silent runtime errors if the operators are used on device.
51//
52// To allow std::complex operator use on device, define _OVERRIDE_COMPLEX_SPECIALIZATION_
53// prior to first inclusion of <complex>. This prevents ICC from adding
54// its own specializations, so our custom ones below can be used instead.
55#if !(EIGEN_COMP_ICC && defined(_USE_COMPLEX_SPECIALIZATION_))
56
57// Import Eigen's internal operator specializations. Downstream GPU code (e.g.,
58// TensorFlow) relies on this macro remaining defined after including Eigen
59// headers to bring the device std::complex operators into non-Eigen namespaces.
60#define EIGEN_USING_STD_COMPLEX_OPERATORS \
61 using Eigen::complex_operator_detail::operator+; \
62 using Eigen::complex_operator_detail::operator-; \
63 using Eigen::complex_operator_detail::operator*; \
64 using Eigen::complex_operator_detail::operator/; \
65 using Eigen::complex_operator_detail::operator+=; \
66 using Eigen::complex_operator_detail::operator-=; \
67 using Eigen::complex_operator_detail::operator*=; \
68 using Eigen::complex_operator_detail::operator/=; \
69 using Eigen::complex_operator_detail::operator==; \
70 using Eigen::complex_operator_detail::operator!=;
71
72// IWYU pragma: private
73#include "../../InternalHeaderCheck.h"
74
75namespace Eigen {
76
77namespace internal {
78// Defined in MathFunctions.h, which follows this header.
79template <typename T>
80EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_multiply(const std::complex<T>& a,
81 const std::complex<T>& b);
82template <typename T>
83EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> complex_divide(const std::complex<T>& a,
84 const std::complex<T>& b);
85} // namespace internal
86
87// Specialized std::complex overloads.
88namespace complex_operator_detail {
89
90// NOTE: We cannot specialize compound assignment operators with Scalar T,
91// (i.e. operator@=(const T&), for @=+,-,*,/)
92// since they are already specialized for float/double/long double within
93// the standard <complex> header. We also do not specialize the stream
94// operators.
95// numext is not declared yet; the std::complex members used below are
96// constexpr, hence device-callable under EIGEN_CONSTEXPR_ARE_DEVICE_FUNC
97// (nvcc: --expt-relaxed-constexpr), as real_impl<std::complex<T>>
98// already assumes.
99#define EIGEN_CREATE_STD_COMPLEX_OPERATOR_SPECIALIZATIONS(T) \
100 \
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator+(const std::complex<T>& a) { return a; } \
102 \
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator-(const std::complex<T>& a) { \
104 return std::complex<T>(-a.real(), -a.imag()); \
105 } \
106 \
107 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator+(const std::complex<T>& a, \
108 const std::complex<T>& b) { \
109 return std::complex<T>(a.real() + b.real(), a.imag() + b.imag()); \
110 } \
111 \
112 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator+(const std::complex<T>& a, const T& b) { \
113 return std::complex<T>(a.real() + b, a.imag()); \
114 } \
115 \
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator+(const T& a, const std::complex<T>& b) { \
117 return std::complex<T>(a + b.real(), b.imag()); \
118 } \
119 \
120 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator-(const std::complex<T>& a, \
121 const std::complex<T>& b) { \
122 return std::complex<T>(a.real() - b.real(), a.imag() - b.imag()); \
123 } \
124 \
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator-(const std::complex<T>& a, const T& b) { \
126 return std::complex<T>(a.real() - b, a.imag()); \
127 } \
128 \
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator-(const T& a, const std::complex<T>& b) { \
130 return std::complex<T>(a - b.real(), -b.imag()); \
131 } \
132 \
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator*(const std::complex<T>& a, \
134 const std::complex<T>& b) { \
135 return internal::complex_multiply(a, b); \
136 } \
137 \
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator*(const std::complex<T>& a, const T& b) { \
139 return std::complex<T>(a.real() * b, a.imag() * b); \
140 } \
141 \
142 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator*(const T& a, const std::complex<T>& b) { \
143 return std::complex<T>(a * b.real(), a * b.imag()); \
144 } \
145 \
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator/(const std::complex<T>& a, \
147 const std::complex<T>& b) { \
148 return internal::complex_divide(a, b); \
149 } \
150 \
151 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator/(const std::complex<T>& a, const T& b) { \
152 return std::complex<T>(a.real() / b, a.imag() / b); \
153 } \
154 \
155 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator/(const T& a, const std::complex<T>& b) { \
156 return internal::complex_divide(std::complex<T>(a, 0), b); \
157 } \
158 \
159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T>& operator+=(std::complex<T>& a, const std::complex<T>& b) { \
160 a = std::complex<T>(a.real() + b.real(), a.imag() + b.imag()); \
161 return a; \
162 } \
163 \
164 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T>& operator-=(std::complex<T>& a, const std::complex<T>& b) { \
165 a = std::complex<T>(a.real() - b.real(), a.imag() - b.imag()); \
166 return a; \
167 } \
168 \
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T>& operator*=(std::complex<T>& a, const std::complex<T>& b) { \
170 a = internal::complex_multiply(a, b); \
171 return a; \
172 } \
173 \
174 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T>& operator/=(std::complex<T>& a, const std::complex<T>& b) { \
175 a = internal::complex_divide(a, b); \
176 return a; \
177 } \
178 \
179 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator==(const std::complex<T>& a, const std::complex<T>& b) { \
180 return a.real() == b.real() && a.imag() == b.imag(); \
181 } \
182 \
183 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator==(const std::complex<T>& a, const T& b) { \
184 return a.real() == b && a.imag() == 0; \
185 } \
186 \
187 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator==(const T& a, const std::complex<T>& b) { \
188 return a == b.real() && 0 == b.imag(); \
189 } \
190 \
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator!=(const std::complex<T>& a, const std::complex<T>& b) { \
192 return !(a == b); \
193 } \
194 \
195 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator!=(const std::complex<T>& a, const T& b) { return !(a == b); } \
196 \
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool operator!=(const T& a, const std::complex<T>& b) { return !(a == b); }
198
199// Do not specialize for long double, since that reduces to double on device.
200EIGEN_CREATE_STD_COMPLEX_OPERATOR_SPECIALIZATIONS(float)
201EIGEN_CREATE_STD_COMPLEX_OPERATOR_SPECIALIZATIONS(double)
202
203#undef EIGEN_CREATE_STD_COMPLEX_OPERATOR_SPECIALIZATIONS
204
205} // namespace complex_operator_detail
206
207EIGEN_USING_STD_COMPLEX_OPERATORS
208
209namespace numext {
210EIGEN_USING_STD_COMPLEX_OPERATORS
211} // namespace numext
212
213namespace internal {
214EIGEN_USING_STD_COMPLEX_OPERATORS
215
216} // namespace internal
217} // namespace Eigen
218
219// NOTE: Do not #undef EIGEN_USING_STD_COMPLEX_OPERATORS. It is intentionally
220// kept defined so downstream GPU code outside namespace Eigen can import these
221// device operator specializations into their own namespaces.
222
223#endif // !(EIGEN_COMP_ICC && _USE_COMPLEX_SPECIALIZATION_)
224
225#endif // EIGEN_GPUCC && EIGEN_GPU_COMPILE_PHASE
226
227#endif // EIGEN_COMPLEX_GPU_H