complex.h
Go to the documentation of this file.
/***************************************************************************************************
* Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************************************/
#pragma once
#include <cuComplex.h>
#include <cstdint>
#include "cutlass/cutlass.h"
#include "cutlass/half.h"
#include "cutlass/real.h"
#if !defined(__CUDACC_RTC__)
#include <iosfwd>
#endif
namespace cutlass {
enum class ComplexTransform {
kNone,
kConjugate
};
//
// Accessors for CUDA complex types
//
CUTLASS_HOST_DEVICE
float const &real(cuFloatComplex const &z) { return z.x; }
CUTLASS_HOST_DEVICE
float &real(cuFloatComplex &z) { return z.x; }
CUTLASS_HOST_DEVICE
double const &real(cuDoubleComplex const &z) { return z.x; }
CUTLASS_HOST_DEVICE
double &real(cuDoubleComplex &z) { return z.x; }
CUTLASS_HOST_DEVICE
float const &imag(cuFloatComplex const &z) { return z.y; }
CUTLASS_HOST_DEVICE
float &imag(cuFloatComplex &z) { return z.y; }
CUTLASS_HOST_DEVICE
double const &imag(cuDoubleComplex const &z) { return z.y; }
CUTLASS_HOST_DEVICE
double &imag(cuDoubleComplex &z) { return z.y; }
template <typename T>
class complex
{
public:
private:
//
// Data members
//
T _real;
T _imag;
public:
//
// Methods
//
CUTLASS_HOST_DEVICE
complex(T r = T(0)) : _real(r), _imag(T(0)) {}
CUTLASS_HOST_DEVICE
complex(T r, T i) : _real(r), _imag(i) {}
//
template<typename A>
CUTLASS_HOST_DEVICE
complex(complex<A> const &z) : _real(static_cast<T>(z.real())), _imag(static_cast<T>(z.imag())) {}
CUTLASS_HOST_DEVICE
complex(cuFloatComplex const &z) : _real(static_cast<T>(cuCrealf(z))), _imag(static_cast<T>(cuCimagf(z))) {}
CUTLASS_HOST_DEVICE
complex(cuDoubleComplex const &z) : _real(static_cast<T>(cuCreal(z))), _imag(static_cast<T>(cuCimag(z))) {}
template<typename A>
CUTLASS_HOST_DEVICE
complex<T>& operator=(complex<A> const &z)
{
_real = static_cast<T>(z.real());
_imag = static_cast<T>(z.imag());
return *this;
}
CUTLASS_HOST_DEVICE bool operator==(complex<T> const &rhs) const {
return this->real() == rhs.real() && this->imag() == rhs.imag();
}
CUTLASS_HOST_DEVICE bool operator!=(complex<T> const &rhs) const {
return !(*this == rhs);
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator+(complex<A> const &rhs) const {
return complex<T>(this->real() + rhs.real(), this->imag() + rhs.imag());
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator-(complex<A> const &rhs) const {
return complex<T>(this->real() - rhs.real(), this->imag() - rhs.imag());
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator*(complex<A> const &rhs) const {
return complex<T>(this->real() * rhs.real() - this->imag() * rhs.imag(),
this->real() * rhs.imag() + this->imag() * rhs.real());
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator*(A const &s) const {
return complex<T>(this->real() * s, this->imag() * s);
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator/(complex<A> const &rhs) const {
T d = (rhs.real() * (rhs) + rhs.imag() * rhs.imag());
return complex<T>((this->real() * (rhs) + this->imag() * rhs.imag()) / d,
(this->imag() * (rhs)-this->real() * rhs.imag()) / d);
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> operator/(A const &s) const {
return complex<T>(this->real() / s, this->imag() / s);
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> &operator+=(complex<A> const &rhs) {
*this = *this + rhs;
return *this;
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> &operator-=(complex<A> const &rhs) {
*this = *this - rhs;
return *this;
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> &operator*=(complex<A> const &rhs) {
*this = *this * rhs;
return *this;
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> &operator*=(A s) {
*this = *this * s;
return *this;
}
template <typename A>
CUTLASS_HOST_DEVICE complex<T> &operator/=(complex<A> const &rhs) {
*this = *this / rhs;
return *this;
}
CUTLASS_HOST_DEVICE
T const &real() const { return _real; }
CUTLASS_HOST_DEVICE
T &real() { return _real; }
CUTLASS_HOST_DEVICE
T const &imag() const { return _imag; }
CUTLASS_HOST_DEVICE
T &imag() { return _imag; }
CUTLASS_HOST_DEVICE
explicit operator cuFloatComplex() const { return make_cuFloatComplex(float(real()), float(imag())); }
CUTLASS_HOST_DEVICE
explicit operator cuDoubleComplex() const { return make_cuDoubleComplex(real(), imag()); }
};
//
// Accessors for complex template
//
template <typename T>
CUTLASS_HOST_DEVICE T const &real(complex<T> const &z) {
return z.real();
}
template <typename T>
CUTLASS_HOST_DEVICE T &real(complex<T> &z) {
return z.real();
}
template <typename T>
CUTLASS_HOST_DEVICE T const &imag(complex<T> const &z) {
return z.imag();
}
template <typename T>
CUTLASS_HOST_DEVICE T &imag(complex<T> &z) {
return z.imag();
}
//
// Output operators
//
#if !defined(__CUDACC_RTC__)
template <typename T>
std::ostream &operator<<(std::ostream &out, complex<T> const &z) {
T _r = real(z);
T _i = imag(z);
if (bool(_i)) {
return out << _r << "+i" << _i;
}
return out << _r;
}
#endif
//
// Non-member operators defined for complex types
//
//
// Non-member functions defined for complex numbers
//
template <typename T>
CUTLASS_HOST_DEVICE T abs(complex<T> const &z) {
return sqrt(norm(z));
}
template <typename T>
CUTLASS_HOST_DEVICE T arg(complex<T> const &z) {
return atan2(imag(z), real(z));
}
template <typename T>
CUTLASS_HOST_DEVICE T norm(T const &z) {
return z * z;
}
template <>
CUTLASS_HOST_DEVICE int8_t norm(int8_t const &z) {
return static_cast<int8_t>(z * z);
}
template <typename T>
CUTLASS_HOST_DEVICE double norm(complex<T> const &z) {
return real(z) * real(z) + imag(z) * imag(z);
}
template <typename T, typename R>
CUTLASS_HOST_DEVICE R norm_accumulate(T const &x, R const & accumulator) {
return accumulator + static_cast<R>(x) * static_cast<R>(x);
}
template <typename T, typename R>
CUTLASS_HOST_DEVICE R norm_accumulate(complex<T> const &z, R const &accumulator) {
return accumulator + static_cast<R>(real(z)) * static_cast<R>(real(z)) +
static_cast<R>(imag(z)) * static_cast<R>(imag(z));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> conj(complex<T> const &z) {
return complex<T>(real(z), -imag(z));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> proj(complex<T> const &z) {
T d = real(z) * real(z) + imag(z) * imag(z) + T(1);
return complex<T>((T(2) * real(z)) / d, (T(2) * imag(z)) / d);
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> polar(T const &r, T const &theta = T()) {
return complex<T>(r * cos(theta), r * sin(theta));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> exp(complex<T> const &z) {
return complex<T>(real(z) * cos(imag(z)), real(z) * sin(imag(z)));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> log(complex<T> const &z) {
return complex<T>(log(abs(z)), arg(z));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> log10(complex<T> const &z) {
return log(z) / T(log(T(10)));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> sqrt(complex<T> const &z) {
return sqrt(T(2)) / T(2) *
complex<T>(sqrt(sqrt(norm(z)) + real(z)),
(imag(z) < 0 ? T(-1) : T(1)) * sqrt(sqrt(norm(z)) - real(z)));
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> cos(complex<T> const &z) {
return (exp(z) + exp(-z)) / T(2);
}
template <typename T>
CUTLASS_HOST_DEVICE complex<T> sin(complex<T> const &z) {
return (exp(-z) - exp(z)) * complex<T>(T(0), T(1) / T(2));
}
template <typename T>
struct RealType< complex<T> > {
using Type = T;
};
template <>
CUTLASS_HOST_DEVICE
cutlass::complex<half_t> from_real<cutlass::complex<half_t> >(double r) {
return cutlass::complex<half_t>(half_t(r));
}
template <>
CUTLASS_HOST_DEVICE
cutlass::complex<float> from_real<cutlass::complex<float> >(double r) {
return cutlass::complex<float>(float(r));
}
template <>
CUTLASS_HOST_DEVICE
cutlass::complex<double> from_real<cutlass::complex<double> >(double r) {
return cutlass::complex<double>(r);
}
} // namespace cutlass
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