docs/complex_8h_source.html
| | CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers |
complex.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted
5 * provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright notice, this list of
7 * conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright notice, this list of
9 * conditions and the following disclaimer in the documentation and/or other materials
10 * provided with the distribution.
11 * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12 * to endorse or promote products derived from this software without specific prior written
13 * permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 **************************************************************************************************/
25 #pragma once
26
27 #include <cuComplex.h>
28 #include <cstdint>
29
30 #include "cutlass/cutlass.h"
31 #include "cutlass/half.h"
32 #include "cutlass/real.h"
33
34 #if !defined(__CUDACC_RTC__)
35 #include <iosfwd>
36 #endif
37
38 namespace cutlass {
39
41
43 enum class ComplexTransform {
44kNone,
46 };
47
49
50 //
51 // Accessors for CUDA complex types
52 //
53
56 float const &real(cuFloatComplex const &z) { return z.x; }
57
60 float &real(cuFloatComplex &z) { return z.x; }
61
64 double const &real(cuDoubleComplex const &z) { return z.x; }
65
68 double &real(cuDoubleComplex &z) { return z.x; }
69
72 float const &imag(cuFloatComplex const &z) { return z.y; }
73
76 float &imag(cuFloatComplex &z) { return z.y; }
77
80 double const &imag(cuDoubleComplex const &z) { return z.y; }
81
84 double &imag(cuDoubleComplex &z) { return z.y; }
85
87
90
91 template <typename T>
93 {
94public:
96
97private:
98//
99// Data members
100//
101
103 T _real;
104
106 T _imag;
107
108public:
109
110 //
111 // Methods
112 //
113
116complex(T r = T(0)) : _real(r), _imag(T(0)) {}
117
120complex(T r, T i) : _real(r), _imag(i) {}
121//
123 template<typename A>
125complex(complex<A> const &z) : _real(static_cast<T>(z.real())), _imag(static_cast<T>(z.imag())) {}
126
129complex(cuFloatComplex const &z) : _real(static_cast<T>(cuCrealf(z))), _imag(static_cast<T>(cuCimagf(z))) {}
130
133complex(cuDoubleComplex const &z) : _real(static_cast<T>(cuCreal(z))), _imag(static_cast<T>(cuCimag(z))) {}
134
136template<typename A>
138complex<T>& operator=(complex<A> const &z)
139 {
140 _real = static_cast<T>(z.real());
141 _imag = static_cast<T>(z.imag());
142return *this;
143 }
144
146CUTLASS_HOST_DEVICE bool operator==(complex<T> const &rhs) const {
147return this->real() == rhs.real() && this->imag() == rhs.imag();
148 }
149
151CUTLASS_HOST_DEVICE bool operator!=(complex<T> const &rhs) const {
152return !(*this == rhs);
153 }
154
156template <typename A>
157CUTLASS_HOST_DEVICE complex<T> operator+(complex<A> const &rhs) const {
158return complex<T>(this->real() + rhs.real(), this->imag() + rhs.imag());
159 }
160
162template <typename A>
163CUTLASS_HOST_DEVICE complex<T> operator-(complex<A> const &rhs) const {
164return complex<T>(this->real() - rhs.real(), this->imag() - rhs.imag());
165 }
166
168template <typename A>
169CUTLASS_HOST_DEVICE complex<T> operator*(complex<A> const &rhs) const {
170return complex<T>(this->real() * rhs.real() - this->imag() * rhs.imag(),
171 this->real() * rhs.imag() + this->imag() * rhs.real());
172 }
173
175template <typename A>
176CUTLASS_HOST_DEVICE complex<T> operator*(A const &s) const {
177return complex<T>(this->real() * s, this->imag() * s);
178 }
179
181template <typename A>
182CUTLASS_HOST_DEVICE complex<T> operator/(complex<A> const &rhs) const {
183 T d = (rhs.real() * (rhs) + rhs.imag() * rhs.imag());
184
185return complex<T>((this->real() * (rhs) + this->imag() * rhs.imag()) / d,
186 (this->imag() * (rhs)-this->real() * rhs.imag()) / d);
187 }
188
190template <typename A>
191CUTLASS_HOST_DEVICE complex<T> operator/(A const &s) const {
192return complex<T>(this->real() / s, this->imag() / s);
193 }
194
196template <typename A>
197CUTLASS_HOST_DEVICE complex<T> &operator+=(complex<A> const &rhs) {
198 *this = *this + rhs;
199return *this;
200 }
201
203template <typename A>
204CUTLASS_HOST_DEVICE complex<T> &operator-=(complex<A> const &rhs) {
205 *this = *this - rhs;
206return *this;
207 }
208
210template <typename A>
211CUTLASS_HOST_DEVICE complex<T> &operator*=(complex<A> const &rhs) {
212 *this = *this * rhs;
213return *this;
214 }
215
217template <typename A>
218CUTLASS_HOST_DEVICE complex<T> &operator*=(A s) {
219 *this = *this * s;
220return *this;
221 }
222
224template <typename A>
225CUTLASS_HOST_DEVICE complex<T> &operator/=(complex<A> const &rhs) {
226 *this = *this / rhs;
227return *this;
228 }
229
232 T const &real() const { return _real; }
233
236 T &real() { return _real; }
237
240 T const &imag() const { return _imag; }
241
244 T &imag() { return _imag; }
245
248explicit operator cuFloatComplex() const { return make_cuFloatComplex(float(real()), float(imag())); }
249
252explicit operator cuDoubleComplex() const { return make_cuDoubleComplex(real(), imag()); }
253 };
254
256
257 //
258 // Accessors for complex template
259 //
260
262 template <typename T>
263 CUTLASS_HOST_DEVICE T const &real(complex<T> const &z) {
264return z.real();
265 }
266
268 template <typename T>
269 CUTLASS_HOST_DEVICE T &real(complex<T> &z) {
270return z.real();
271 }
272
274 template <typename T>
275 CUTLASS_HOST_DEVICE T const &imag(complex<T> const &z) {
276return z.imag();
277 }
278
280 template <typename T>
281 CUTLASS_HOST_DEVICE T &imag(complex<T> &z) {
282return z.imag();
283 }
284
285 //
286 // Output operators
287 //
288
289 #if !defined(__CUDACC_RTC__)
290 template <typename T>
291 std::ostream &operator<<(std::ostream &out, complex<T> const &z) {
292 T _r = real(z);
293 T _i = imag(z);
294
295if (bool(_i)) {
296return out << _r << "+i" << _i;
297 }
298return out << _r;
299 }
300 #endif
301
302 //
303 // Non-member operators defined for complex types
304 //
305
306
307 //
308 // Non-member functions defined for complex numbers
309 //
310
312 template <typename T>
313 CUTLASS_HOST_DEVICE T abs(complex<T> const &z) {
315 }
316
318 template <typename T>
319 CUTLASS_HOST_DEVICE T arg(complex<T> const &z) {
320return atan2(imag(z), real(z));
321 }
322
324 template <typename T>
325 CUTLASS_HOST_DEVICE T norm(T const &z) {
326return z * z;
327 }
328
330 template <>
331 CUTLASS_HOST_DEVICE int8_t norm(int8_t const &z) {
332return static_cast<int8_t>(z * z);
333 }
334
336 template <typename T>
337 CUTLASS_HOST_DEVICE double norm(complex<T> const &z) {
338return real(z) * real(z) + imag(z) * imag(z);
339 }
340
342 template <typename T, typename R>
343 CUTLASS_HOST_DEVICE R norm_accumulate(T const &x, R const & accumulator) {
344return accumulator + static_cast<R>(x) * static_cast<R>(x);
345 }
346
348 template <typename T, typename R>
349 CUTLASS_HOST_DEVICE R norm_accumulate(complex<T> const &z, R const &accumulator) {
350return accumulator + static_cast<R>(real(z)) * static_cast<R>(real(z)) +
351 static_cast<R>(imag(z)) * static_cast<R>(imag(z));
352 }
353
355 template <typename T>
356 CUTLASS_HOST_DEVICE complex<T> conj(complex<T> const &z) {
357return complex<T>(real(z), -imag(z));
358 }
359
361 template <typename T>
362 CUTLASS_HOST_DEVICE complex<T> proj(complex<T> const &z) {
363 T d = real(z) * real(z) + imag(z) * imag(z) + T(1);
364return complex<T>((T(2) * real(z)) / d, (T(2) * imag(z)) / d);
365 }
366
368 template <typename T>
369 CUTLASS_HOST_DEVICE complex<T> polar(T const &r, T const &theta = T()) {
370return complex<T>(r * cos(theta), r * sin(theta));
371 }
372
374 template <typename T>
375 CUTLASS_HOST_DEVICE complex<T> exp(complex<T> const &z) {
376return complex<T>(real(z) * cos(imag(z)), real(z) * sin(imag(z)));
377 }
378
380 template <typename T>
381 CUTLASS_HOST_DEVICE complex<T> log(complex<T> const &z) {
382return complex<T>(log(abs(z)), arg(z));
383 }
384
386 template <typename T>
387 CUTLASS_HOST_DEVICE complex<T> log10(complex<T> const &z) {
388return log(z) / T(log(T(10)));
389 }
390
392 template <typename T>
393 CUTLASS_HOST_DEVICE complex<T> sqrt(complex<T> const &z) {
394return sqrt(T(2)) / T(2) *
395complex<T>(sqrt(sqrt(norm(z)) + real(z)),
396 (imag(z) < 0 ? T(-1) : T(1)) * sqrt(sqrt(norm(z)) - real(z)));
397 }
398
400 template <typename T>
401 CUTLASS_HOST_DEVICE complex<T> cos(complex<T> const &z) {
402return (exp(z) + exp(-z)) / T(2);
403 }
404
406 template <typename T>
407 CUTLASS_HOST_DEVICE complex<T> sin(complex<T> const &z) {
408return (exp(-z) - exp(z)) * complex<T>(T(0), T(1) / T(2));
409 }
410
412
414 template <typename T>
415 struct RealType< complex<T> > {
417 };
418
420
421 template <>
423 cutlass::complex<half_t> from_real<cutlass::complex<half_t> >(double r) {
424return cutlass::complex<half_t>(half_t(r));
425 }
426
427 template <>
429 cutlass::complex<float> from_real<cutlass::complex<float> >(double r) {
430return cutlass::complex<float>(float(r));
431 }
432
433 template <>
435 cutlass::complex<double> from_real<cutlass::complex<double> >(double r) {
436return cutlass::complex<double>(r);
437 }
438
440
441 } // namespace cutlass
442
CUTLASS_HOST_DEVICE complex< T > cos(complex< T > const &z)
Computes the cosine of complex z.
Definition: complex.h:401
CUTLASS_HOST_DEVICE complex(cuDoubleComplex const &z)
Conversion from cuDoubleComplex.
Definition: complex.h:133
CUTLASS_HOST_DEVICE complex< T > & operator-=(complex< A > const &rhs)
Subtraction.
Definition: complex.h:204
CUTLASS_HOST_DEVICE complex< T > operator-(complex< A > const &rhs) const
Subtraction.
Definition: complex.h:163
Definition: aligned_buffer.h:35
ComplexTransform
Enumeraed type describing a transformation on a complex value.
Definition: complex.h:43
CUTLASS_HOST_DEVICE complex< T > & operator=(complex< A > const &z)
Assignment.
Definition: complex.h:138
CUTLASS_HOST_DEVICE T abs(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:313
CUTLASS_HOST_DEVICE float const & imag(cuFloatComplex const &z)
Returns the imaginary part of the complex number.
Definition: complex.h:72
CUTLASS_HOST_DEVICE complex(T r=T(0))
Constructor.
Definition: complex.h:116
CUTLASS_HOST_DEVICE R norm_accumulate(T const &x, R const &accumulator)
Norm-accumulate calculation.
Definition: complex.h:343
Defines a class for using IEEE half-precision floating-point types in host or device code...
CUTLASS_HOST_DEVICE T norm(T const &z)
Returns the squared magnitude of a real number.
Definition: complex.h:325
CUTLASS_HOST_DEVICE complex< T > polar(T const &r, T const &theta=T())
Returns a complex number with magnitude r and phase theta.
Definition: complex.h:369
IEEE half-precision floating-point type.
Definition: half.h:126
CUTLASS_HOST_DEVICE bool operator!=(complex< T > const &rhs) const
Inequality operator.
Definition: complex.h:151
CUTLASS_HOST_DEVICE float const & real(cuFloatComplex const &z)
Returns the real part of the complex number.
Definition: complex.h:56
CUTLASS_HOST_DEVICE complex< T > exp(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:375
CUTLASS_HOST_DEVICE T const & imag() const
Accesses the imaginary part of the complex number.
Definition: complex.h:240
cutlass::ComplexTransform::kNone
CUTLASS_HOST_DEVICE T arg(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:319
CUTLASS_HOST_DEVICE complex< T > log(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:381
CUTLASS_HOST_DEVICE complex(T r, T i)
Constructor.
Definition: complex.h:120
CUTLASS_HOST_DEVICE bool operator==(complex< T > const &rhs) const
Equality operator.
Definition: complex.h:146
CUTLASS_HOST_DEVICE complex< T > conj(complex< T > const &z)
Returns the complex conjugate.
Definition: complex.h:356
CUTLASS_HOST_DEVICE complex< T > & operator*=(complex< A > const &rhs)
Multiplication.
Definition: complex.h:211
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
CUTLASS_HOST_DEVICE T & imag()
Accesses the imaginary part of the complex number.
Definition: complex.h:244
CUTLASS_HOST_DEVICE complex< T > sin(complex< T > const &z)
Computes the sin of complex z.
Definition: complex.h:407
CUTLASS_HOST_DEVICE complex(cuFloatComplex const &z)
Conversion from cuFloatComplex.
Definition: complex.h:129
CUTLASS_HOST_DEVICE complex< T > operator+(complex< A > const &rhs) const
Addition.
Definition: complex.h:157
CUTLASS_HOST_DEVICE T const & real() const
Accesses the real part of the complex number.
Definition: complex.h:232
CUTLASS_HOST_DEVICE complex< T > proj(complex< T > const &z)
Projects the complex number z onto the Riemann sphere.
Definition: complex.h:362
Definition: complex.h:92
CUTLASS_HOST_DEVICE complex< T > operator*(complex< A > const &rhs) const
Multiplication.
Definition: complex.h:169
CUTLASS_HOST_DEVICE complex< T > & operator*=(A s)
Scalar multiplication.
Definition: complex.h:218
cutlass::ComplexTransform::kConjugate
CUTLASS_HOST_DEVICE T & real()
Accesses the real part of the complex number.
Definition: complex.h:236
CUTLASS_HOST_DEVICE complex< T > operator*(A const &s) const
Scalar Multiplication.
Definition: complex.h:176
CUTLASS_HOST_DEVICE complex< T > & operator+=(complex< A > const &rhs)
Addition.
Definition: complex.h:197
CUTLASS_HOST_DEVICE complex< T > operator/(complex< A > const &rhs) const
Division.
Definition: complex.h:182
CUTLASS_HOST_DEVICE complex< T > & operator/=(complex< A > const &rhs)
Division.
Definition: complex.h:225
CUTLASS_HOST_DEVICE complex(complex< A > const &z)
Constructor.
Definition: complex.h:125
CUTLASS_HOST_DEVICE complex< T > log10(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:387
Used to determine the real-valued underlying type of a numeric type T.
Definition: real.h:31
Basic include for CUTLASS.
CUTLASS_HOST_DEVICE complex< T > operator/(A const &s) const
Scalar Division.
Definition: complex.h:191
CUTLASS_HOST_DEVICE complex< T > sqrt(complex< T > const &z)
Computes the square root of complex number z.
Definition: complex.h:393
cutlass::RealType< complex< T > >::Type
T Type
Definition: complex.h:416
Generated by 1.8.11