DP3
Complex.h
Go to the documentation of this file.
1 // Copyright (C) 2023 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #ifndef DP3_DDECAL_GAIN_SOLVERS_KERNELS_COMPLEX_H_
5 #define DP3_DDECAL_GAIN_SOLVERS_KERNELS_COMPLEX_H_
6 
7 #include <cuComplex.h>
8 
9 __host__ __device__ static __inline__ cuDoubleComplex make_cuDoubleComplex(
10  const cuFloatComplex& a) {
11  return make_cuDoubleComplex(a.x, a.y);
12 }
13 
14 /*
15  * Taken the below utility functions from
16  * https://forums.developer.nvidia.com/t/additional-cucomplex-functions-cucnorm-cucsqrt-cucexp-and-some-complex-double-functions/36892
17  */
18 __host__ __device__ static __inline__ cuDoubleComplex cuCadd(cuDoubleComplex x,
19  double y) {
20  return make_cuDoubleComplex(cuCreal(x) + y, cuCimag(x));
21 }
22 __host__ __device__ static __inline__ cuDoubleComplex cuCdiv(cuDoubleComplex x,
23  double y) {
24  return make_cuDoubleComplex(cuCreal(x) / y, cuCimag(x) / y);
25 }
26 __host__ __device__ static __inline__ cuDoubleComplex cuCmul(cuDoubleComplex x,
27  double y) {
28  return make_cuDoubleComplex(cuCreal(x) * y, cuCimag(x) * y);
29 }
30 __host__ __device__ static __inline__ cuDoubleComplex cuCsub(cuDoubleComplex x,
31  double y) {
32  return make_cuDoubleComplex(cuCreal(x) - y, cuCimag(x));
33 }
34 
35 __host__ __device__ static __inline__ cuDoubleComplex cuCexp(
36  cuDoubleComplex x) {
37  double factor = exp(x.x);
38  return make_cuDoubleComplex(factor * cos(x.y), factor * sin(x.y));
39 }
40 
41 /*
42  * Cuda complex implementation of std::arg
43  * https://en.cppreference.com/w/cpp/numeric/complex/arg
44  */
45 __device__ static __inline__ float cuCarg(const cuDoubleComplex& z) {
46  return atan2(cuCimag(z), cuCreal(z));
47 }
48 
49 /*
50  * Cuda complex implementation of std::polar
51  * https://en.cppreference.com/w/cpp/numeric/complex/polar
52  */
53 __device__ static __inline__ cuDoubleComplex cuCpolar(const double r,
54  const double z) {
55  return make_cuDoubleComplex(r * cos(z), r * sin(z));
56 }
57 
58 template <typename T>
59 __device__ static __inline__ double cuNorm(const T& a) {
60  return a.x * a.x + a.y * a.y;
61 }
62 
63 #endif // DP3_DDECAL_GAIN_SOLVERS_KERNELS_COMPLEX_H_