cuComplex.h and exp()

前端 未结 2 999
滥情空心
滥情空心 2021-01-21 11:02

Q0:

Is exp() supported by cuComplex.h?

Q1:

How to write A = B * exp(i * C), where A, B, C are same size arrays of real numbers? Is this right?

ma

相关标签:
2条回答
  • 2021-01-21 11:22

    cuComplex.h only offers some basic operations on cuComplex (principally those used inside the CUBLAS and CUFFT libraries), the exponential function is not supported.

    You can implement the exponential yourself using component-wise arithmetic. cuComplex stores the real part of a complex number in the x component and the imaginary part in the y component. Given a complex number z = x + i*y, the exponent can be computed as:

    exp(z) = exp(x) * (cos(y) + i * sin(y))

    This leads to the following CUDA code (untested):

    cuComplex my_complex_exp (cuComplex arg)
    {
       cuComplex res;
       float s, c;
       float e = expf(arg.x);
       sincosf(arg.y, &s, &c);
       res.x = c * e;
       res.y = s * e;
       return res;
    }
    
    0 讨论(0)
  • 2021-01-21 11:28

    Thrust also now has support for complex exponentials.

    It's not necessary to otherwise be using thrust for this capability. You can include the thrust complex header file, and use these constructs in ordinary CUDA code.

    $ cat t1793.cu
    #include <cuComplex.h>
    #include <thrust/complex.h>
    #include <stdio.h>
    
    __host__ __device__
    cuFloatComplex my_complex_exp (cuFloatComplex arg)
    {
       cuFloatComplex res;
       float s, c;
       float e = expf(arg.x);
       sincosf(arg.y, &s, &c);
       res.x = c * e;
       res.y = s * e;
       return res;
    }
    
    __global__ void demo(){
    
      cuFloatComplex a = make_cuFloatComplex(1.0f, 1.0f);
      thrust::complex<float> b(1.0f, 1.0f);
      printf("thrust: %f,%f, cuComplex: %f,%f\n", exp(b).real(), exp(b).imag(), cuCrealf( my_complex_exp(a)), cuCimagf(my_complex_exp(a)));
    }
    
    int main(){
    
      demo<<<1,1>>>();
      cudaDeviceSynchronize();
    }
    $ nvcc -o t1793 t1793.cu
    $ cuda-memcheck ./t1793
    ========= CUDA-MEMCHECK
    thrust: 1.468694,2.287355, cuComplex: 1.468694,2.287355
    ========= ERROR SUMMARY: 0 errors
    $
    
    0 讨论(0)
提交回复
热议问题