This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Complex.cuh
170 lines (133 loc) · 3.9 KB
/
Complex.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include "cuda/ComputeCapabilities.cuh"
#include <cuda_runtime.h>
#include <cuComplex.h>
#include "cuda/ShuffleTypes.cuh"
namespace facebook { namespace cuda {
/**
`cuComplex` wrapper.
*/
struct Complex {
__host__ __device__ __forceinline__
Complex() {}
__host__ __device__ __forceinline__
Complex(float re) : cplx_(make_cuComplex(re, 0.0f)) {}
__host__ __device__ __forceinline__
Complex(float re, float im) : cplx_(make_cuComplex(re, im)) {}
__host__ __device__ __forceinline__
Complex(const Complex& c) : cplx_(c.cplx_) {}
__host__ __device__ __forceinline__
Complex(const cuComplex& c) : cplx_(c) {}
__host__ __device__ __forceinline__
Complex& operator=(const Complex& c) {
// No need for swap
cplx_ = c.cplx_;
return *this;
}
__host__ __device__ __forceinline__
bool operator==(const Complex& c) const {
return cplx_.x == c.cplx_.x && cplx_.y == c.cplx_.y;
}
__host__ __device__ __forceinline__
bool operator!=(const Complex& c) const {
return !operator==(c);
}
__host__ __device__ __forceinline__
Complex operator-() const {
return Complex(make_cuComplex(-cplx_.x, -cplx_.y));
}
__host__ __device__ __forceinline__
Complex operator-(const Complex& c) const {
return Complex(cuCsubf(cplx_, c.cplx_));
}
__host__ __device__ __forceinline__
Complex operator+(const Complex& c) const {
return Complex(cuCaddf(cplx_, c.cplx_));
}
__host__ __device__ __forceinline__
Complex operator*(const Complex& c) const {
return Complex(cuCmulf(cplx_, c.cplx_));
}
__host__ __device__ __forceinline__
Complex operator/(const Complex& c) const {
return Complex(cuCdivf(cplx_, c.cplx_));
}
__host__ __device__ __forceinline__
Complex& operator+=(const Complex& c) {
cplx_ = cuCaddf(cplx_, c.cplx_);
return *this;
}
__host__ __device__ __forceinline__
Complex& operator-=(const Complex& c) {
cplx_ = cuCsubf(cplx_, c.cplx_);
return *this;
}
__host__ __device__ __forceinline__
Complex& operator*=(const Complex& c) {
cplx_ = cuCmulf(cplx_, c.cplx_);
return *this;
}
__host__ __device__ __forceinline__
Complex& operator/=(const Complex& c) {
cplx_ = cuCdivf(cplx_, c.cplx_);
return *this;
}
__host__ __device__ __forceinline__
Complex transpose() const {
return Complex(make_cuComplex(cplx_.y, cplx_.x));
}
__host__ __device__ __forceinline__
Complex conjugate() const {
return Complex(make_cuComplex(cplx_.x, -cplx_.y));
}
__host__ __device__ __forceinline__
void cexp(float angle) {
sincosf(angle, &cplx_.y, &cplx_.x);
}
__host__ __device__ __forceinline__
float& re() {
return cplx_.x;
}
__host__ __device__ __forceinline__
float& im() {
return cplx_.y;
}
__host__ __device__ __forceinline__
const float& re() const {
return cplx_.x;
}
__host__ __device__ __forceinline__
const float& im() const {
return cplx_.y;
}
__host__ __device__ __forceinline__
operator float2() const {
return static_cast<float2>(cplx_);
}
private:
cuComplex cplx_;
};
#ifdef __CUDA_ARCH__
__device__ __forceinline__ Complex
shfl(const Complex& c, int lane, int bound = WARP_SIZE) {
return Complex(shfl(c.re(), lane, bound),
shfl(c.im(), lane, bound));
}
__device__ __forceinline__ Complex
shfl_up(const Complex& c, int lane, int bound = WARP_SIZE) {
return Complex(shfl_up(c.re(), lane, bound),
shfl_up(c.im(), lane, bound));
}
__device__ __forceinline__ Complex
shfl_down(const Complex& c, int lane, int bound = WARP_SIZE) {
return Complex(shfl_down(c.re(), lane, bound),
shfl_down(c.im(), lane, bound));
}
__device__ __forceinline__ Complex
shfl_xor(const Complex& c, int lane, int bound = WARP_SIZE) {
return Complex(shfl_xor(c.re(), lane, bound),
shfl_xor(c.im(), lane, bound));
}
#endif
}}