CUDA - How to work with complex numbers?

前端 未结 1 515
攒了一身酷
攒了一身酷 2021-02-06 00:49

What CUDA headers should I include in my programme if I want to work with complex numbers and do simple maths operations (addition and multiplication) to these complex double nu

1条回答
  •  臣服心动
    2021-02-06 01:48

    The header to include is:

    #include 
    

    On a standard linux CUDA install, it is located in:

    /usr/local/cuda/include
    

    You will need to inspect that header file and use the functions defined in it to manipulate complex numbers on the device.

    To multiply a (double) complex number by a real number, I would:

    #include 
    ...
    double cr = 1;
    double ci = 2;
    double r = 3;
    cuDoubleComplex c = make_cuDoubleComplex(cr, ci);
    cuDoubleComplex result = cuCmul(c, make_cuDoubleComplex(r, 0));
    

    EDIT: With the recently released Thrust v1.8 in CUDA 7 RC, it is possible to use thrust::complex in either thrust code or CUDA device code. This makes it possible to write more natural-looking operations such as:

    #include 
    ...
    thrust::complex c = thrust::complex(2.0f, 5.0f);
    thrust::complex c2 = c*c;
    float r = c2.real();
    

    0 讨论(0)
提交回复
热议问题