| CUTLASS CUDA Templates for Linear Algebra Subroutines and Solvers

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
44 kNone,
45 kConjugate
46 };
47
49
50 //
51 // Accessors for CUDA complex types
52 //
53
55 CUTLASS_HOST_DEVICE
56 float const &real(cuFloatComplex const &z) { return z.x; }
57
59 CUTLASS_HOST_DEVICE
60 float &real(cuFloatComplex &z) { return z.x; }
61
63 CUTLASS_HOST_DEVICE
64 double const &real(cuDoubleComplex const &z) { return z.x; }
65
67 CUTLASS_HOST_DEVICE
68 double &real(cuDoubleComplex &z) { return z.x; }
69
71 CUTLASS_HOST_DEVICE
72 float const &imag(cuFloatComplex const &z) { return z.y; }
73
75 CUTLASS_HOST_DEVICE
76 float &imag(cuFloatComplex &z) { return z.y; }
77
79 CUTLASS_HOST_DEVICE
80 double const &imag(cuDoubleComplex const &z) { return z.y; }
81
83 CUTLASS_HOST_DEVICE
84 double &imag(cuDoubleComplex &z) { return z.y; }
85
87
90
91 template <typename T>
92 class complex
93 {
94 public:
96
97 private:
98 //
99 // Data members
100 //
101
103 T _real;
104
106 T _imag;
107
108 public:
109
110 //
111 // Methods
112 //
113
115 CUTLASS_HOST_DEVICE
116 complex(T r = T(0)) : _real(r), _imag(T(0)) {}
117
119 CUTLASS_HOST_DEVICE
120 complex(T r, T i) : _real(r), _imag(i) {}
121 //
123 template<typename A>
124 CUTLASS_HOST_DEVICE
125 complex(complex<A> const &z) : _real(static_cast<T>(z.real())), _imag(static_cast<T>(z.imag())) {}
126
128 CUTLASS_HOST_DEVICE
129 complex(cuFloatComplex const &z) : _real(static_cast<T>(cuCrealf(z))), _imag(static_cast<T>(cuCimagf(z))) {}
130
132 CUTLASS_HOST_DEVICE
133 complex(cuDoubleComplex const &z) : _real(static_cast<T>(cuCreal(z))), _imag(static_cast<T>(cuCimag(z))) {}
134
136 template<typename A>
137 CUTLASS_HOST_DEVICE
138 complex<T>& operator=(complex<A> const &z)
139 {
140 _real = static_cast<T>(z.real());
141 _imag = static_cast<T>(z.imag());
142 return *this;
143 }
144
146 CUTLASS_HOST_DEVICE bool operator==(complex<T> const &rhs) const {
147 return this->real() == rhs.real() && this->imag() == rhs.imag();
148 }
149
151 CUTLASS_HOST_DEVICE bool operator!=(complex<T> const &rhs) const {
152 return !(*this == rhs);
153 }
154
156 template <typename A>
157 CUTLASS_HOST_DEVICE complex<T> operator+(complex<A> const &rhs) const {
158 return complex<T>(this->real() + rhs.real(), this->imag() + rhs.imag());
159 }
160
162 template <typename A>
163 CUTLASS_HOST_DEVICE complex<T> operator-(complex<A> const &rhs) const {
164 return complex<T>(this->real() - rhs.real(), this->imag() - rhs.imag());
165 }
166
168 template <typename A>
169 CUTLASS_HOST_DEVICE complex<T> operator*(complex<A> const &rhs) const {
170 return complex<T>(this->real() * rhs.real() - this->imag() * rhs.imag(),
171 this->real() * rhs.imag() + this->imag() * rhs.real());
172 }
173
175 template <typename A>
176 CUTLASS_HOST_DEVICE complex<T> operator*(A const &s) const {
177 return complex<T>(this->real() * s, this->imag() * s);
178 }
179
181 template <typename A>
182 CUTLASS_HOST_DEVICE complex<T> operator/(complex<A> const &rhs) const {
183 T d = (rhs.real() * (rhs) + rhs.imag() * rhs.imag());
184
185 return complex<T>((this->real() * (rhs) + this->imag() * rhs.imag()) / d,
186 (this->imag() * (rhs)-this->real() * rhs.imag()) / d);
187 }
188
190 template <typename A>
191 CUTLASS_HOST_DEVICE complex<T> operator/(A const &s) const {
192 return complex<T>(this->real() / s, this->imag() / s);
193 }
194
196 template <typename A>
197 CUTLASS_HOST_DEVICE complex<T> &operator+=(complex<A> const &rhs) {
198 *this = *this + rhs;
199 return *this;
200 }
201
203 template <typename A>
204 CUTLASS_HOST_DEVICE complex<T> &operator-=(complex<A> const &rhs) {
205 *this = *this - rhs;
206 return *this;
207 }
208
210 template <typename A>
211 CUTLASS_HOST_DEVICE complex<T> &operator*=(complex<A> const &rhs) {
212 *this = *this * rhs;
213 return *this;
214 }
215
217 template <typename A>
218 CUTLASS_HOST_DEVICE complex<T> &operator*=(A s) {
219 *this = *this * s;
220 return *this;
221 }
222
224 template <typename A>
225 CUTLASS_HOST_DEVICE complex<T> &operator/=(complex<A> const &rhs) {
226 *this = *this / rhs;
227 return *this;
228 }
229
231 CUTLASS_HOST_DEVICE
232 T const &real() const { return _real; }
233
235 CUTLASS_HOST_DEVICE
236 T &real() { return _real; }
237
239 CUTLASS_HOST_DEVICE
240 T const &imag() const { return _imag; }
241
243 CUTLASS_HOST_DEVICE
244 T &imag() { return _imag; }
245
247 CUTLASS_HOST_DEVICE
248 explicit operator cuFloatComplex() const { return make_cuFloatComplex(float(real()), float(imag())); }
249
251 CUTLASS_HOST_DEVICE
252 explicit 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) {
264 return z.real();
265 }
266
268 template <typename T>
269 CUTLASS_HOST_DEVICE T &real(complex<T> &z) {
270 return z.real();
271 }
272
274 template <typename T>
275 CUTLASS_HOST_DEVICE T const &imag(complex<T> const &z) {
276 return z.imag();
277 }
278
280 template <typename T>
281 CUTLASS_HOST_DEVICE T &imag(complex<T> &z) {
282 return 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
295 if (bool(_i)) {
296 return out << _r << "+i" << _i;
297 }
298 return 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) {
314 return sqrt(norm(z));
315 }
316
318 template <typename T>
319 CUTLASS_HOST_DEVICE T arg(complex<T> const &z) {
320 return atan2(imag(z), real(z));
321 }
322
324 template <typename T>
325 CUTLASS_HOST_DEVICE T norm(T const &z) {
326 return z * z;
327 }
328
330 template <>
331 CUTLASS_HOST_DEVICE int8_t norm(int8_t const &z) {
332 return static_cast<int8_t>(z * z);
333 }
334
336 template <typename T>
337 CUTLASS_HOST_DEVICE double norm(complex<T> const &z) {
338 return 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) {
344 return 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) {
350 return 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) {
357 return 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);
364 return 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()) {
370 return 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) {
376 return 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) {
382 return complex<T>(log(abs(z)), arg(z));
383 }
384
386 template <typename T>
387 CUTLASS_HOST_DEVICE complex<T> log10(complex<T> const &z) {
388 return log(z) / T(log(T(10)));
389 }
390
392 template <typename T>
393 CUTLASS_HOST_DEVICE complex<T> sqrt(complex<T> const &z) {
394 return sqrt(T(2)) / T(2) *
395 complex<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) {
402 return (exp(z) + exp(-z)) / T(2);
403 }
404
406 template <typename T>
407 CUTLASS_HOST_DEVICE complex<T> sin(complex<T> const &z) {
408 return (exp(-z) - exp(z)) * complex<T>(T(0), T(1) / T(2));
409 }
410
412
414 template <typename T>
415 struct RealType< complex<T> > {
416 using Type = T;
417 };
418
420
421 template <>
422 CUTLASS_HOST_DEVICE
423 cutlass::complex<half_t> from_real<cutlass::complex<half_t> >(double r) {
424 return cutlass::complex<half_t>(half_t(r));
425 }
426
427 template <>
428 CUTLASS_HOST_DEVICE
429 cutlass::complex<float> from_real<cutlass::complex<float> >(double r) {
430 return cutlass::complex<float>(float(r));
431 }
432
433 template <>
434 CUTLASS_HOST_DEVICE
435 cutlass::complex<double> from_real<cutlass::complex<double> >(double r) {
436 return cutlass::complex<double>(r);
437 }
438
440
441 } // namespace cutlass
442
cutlass::cos
CUTLASS_HOST_DEVICE complex< T > cos(complex< T > const &z)
Computes the cosine of complex z.
Definition: complex.h:401
cutlass::complex::complex
CUTLASS_HOST_DEVICE complex(cuDoubleComplex const &z)
Conversion from cuDoubleComplex.
Definition: complex.h:133
cutlass::complex::operator-=
CUTLASS_HOST_DEVICE complex< T > & operator-=(complex< A > const &rhs)
Subtraction.
Definition: complex.h:204
cutlass::complex::operator-
CUTLASS_HOST_DEVICE complex< T > operator-(complex< A > const &rhs) const
Subtraction.
Definition: complex.h:163
cutlass
Definition: aligned_buffer.h:35
cutlass::ComplexTransform
ComplexTransform
Enumeraed type describing a transformation on a complex value.
Definition: complex.h:43
cutlass::complex::operator=
CUTLASS_HOST_DEVICE complex< T > & operator=(complex< A > const &z)
Assignment.
Definition: complex.h:138
cutlass::abs
CUTLASS_HOST_DEVICE T abs(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:313
cutlass::imag
CUTLASS_HOST_DEVICE float const & imag(cuFloatComplex const &z)
Returns the imaginary part of the complex number.
Definition: complex.h:72
cutlass::complex::complex
CUTLASS_HOST_DEVICE complex(T r=T(0))
Constructor.
Definition: complex.h:116
cutlass::norm_accumulate
CUTLASS_HOST_DEVICE R norm_accumulate(T const &x, R const &accumulator)
Norm-accumulate calculation.
Definition: complex.h:343
half.h
Defines a class for using IEEE half-precision floating-point types in host or device code...
cutlass::norm
CUTLASS_HOST_DEVICE T norm(T const &z)
Returns the squared magnitude of a real number.
Definition: complex.h:325
cutlass::polar
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
cutlass::half_t
IEEE half-precision floating-point type.
Definition: half.h:126
cutlass::complex::operator!=
CUTLASS_HOST_DEVICE bool operator!=(complex< T > const &rhs) const
Inequality operator.
Definition: complex.h:151
cutlass::real
CUTLASS_HOST_DEVICE float const & real(cuFloatComplex const &z)
Returns the real part of the complex number.
Definition: complex.h:56
cutlass::exp
CUTLASS_HOST_DEVICE complex< T > exp(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:375
cutlass::complex::imag
CUTLASS_HOST_DEVICE T const & imag() const
Accesses the imaginary part of the complex number.
Definition: complex.h:240
cutlass::ComplexTransform::kNone
cutlass::arg
CUTLASS_HOST_DEVICE T arg(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:319
cutlass::log
CUTLASS_HOST_DEVICE complex< T > log(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:381
real.h
cutlass::complex::complex
CUTLASS_HOST_DEVICE complex(T r, T i)
Constructor.
Definition: complex.h:120
cutlass::complex::operator==
CUTLASS_HOST_DEVICE bool operator==(complex< T > const &rhs) const
Equality operator.
Definition: complex.h:146
cutlass::conj
CUTLASS_HOST_DEVICE complex< T > conj(complex< T > const &z)
Returns the complex conjugate.
Definition: complex.h:356
cutlass::complex::operator*=
CUTLASS_HOST_DEVICE complex< T > & operator*=(complex< A > const &rhs)
Multiplication.
Definition: complex.h:211
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
cutlass::complex::imag
CUTLASS_HOST_DEVICE T & imag()
Accesses the imaginary part of the complex number.
Definition: complex.h:244
cutlass::sin
CUTLASS_HOST_DEVICE complex< T > sin(complex< T > const &z)
Computes the sin of complex z.
Definition: complex.h:407
cutlass::complex::complex
CUTLASS_HOST_DEVICE complex(cuFloatComplex const &z)
Conversion from cuFloatComplex.
Definition: complex.h:129
cutlass::complex::operator+
CUTLASS_HOST_DEVICE complex< T > operator+(complex< A > const &rhs) const
Addition.
Definition: complex.h:157
cutlass::complex::real
CUTLASS_HOST_DEVICE T const & real() const
Accesses the real part of the complex number.
Definition: complex.h:232
cutlass::proj
CUTLASS_HOST_DEVICE complex< T > proj(complex< T > const &z)
Projects the complex number z onto the Riemann sphere.
Definition: complex.h:362
cutlass::complex
Definition: complex.h:92
cutlass::complex::operator*
CUTLASS_HOST_DEVICE complex< T > operator*(complex< A > const &rhs) const
Multiplication.
Definition: complex.h:169
cutlass::complex::operator*=
CUTLASS_HOST_DEVICE complex< T > & operator*=(A s)
Scalar multiplication.
Definition: complex.h:218
cutlass::ComplexTransform::kConjugate
cutlass::complex::real
CUTLASS_HOST_DEVICE T & real()
Accesses the real part of the complex number.
Definition: complex.h:236
cutlass::complex::operator*
CUTLASS_HOST_DEVICE complex< T > operator*(A const &s) const
Scalar Multiplication.
Definition: complex.h:176
cutlass::complex::operator+=
CUTLASS_HOST_DEVICE complex< T > & operator+=(complex< A > const &rhs)
Addition.
Definition: complex.h:197
cutlass::complex::operator/
CUTLASS_HOST_DEVICE complex< T > operator/(complex< A > const &rhs) const
Division.
Definition: complex.h:182
cutlass::complex::operator/=
CUTLASS_HOST_DEVICE complex< T > & operator/=(complex< A > const &rhs)
Division.
Definition: complex.h:225
cutlass::complex::complex
CUTLASS_HOST_DEVICE complex(complex< A > const &z)
Constructor.
Definition: complex.h:125
cutlass::log10
CUTLASS_HOST_DEVICE complex< T > log10(complex< T > const &z)
Computes the complex exponential of z.
Definition: complex.h:387
cutlass::RealType
Used to determine the real-valued underlying type of a numeric type T.
Definition: real.h:31
cutlass.h
Basic include for CUTLASS.
cutlass::complex::operator/
CUTLASS_HOST_DEVICE complex< T > operator/(A const &s) const
Scalar Division.
Definition: complex.h:191
cutlass::sqrt
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