使用推力库操作时使用袖套

Using cufft when operating with the thrust library

我想在我的项目中合并 thrust 库和 cufft。因此为了测试我写了

    int length = 5;
    thrust::device_vector<thrust::complex<double> > V1(length);
    thrust::device_vector<cuDoubleComplex> V2(length);
    thrust::device_vector<thrust::complex<double> > V3(length);
    thrust::sequence(V1.begin(), V1.end(), 1);
    thrust::sequence(V2.begin(), V2.end(), 2);
    thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), thrust::multiplies<thrust::complex<double> >());
    cufftHandle plan;
    cufftPlan1d(&plan, length, thrust::complex<double>, 1);
    cufftExecZ2Z(plan, &V1, &V2, CUFFT_FORWARD);
    for (int i = 0; i < length; i++)
        std::cout << V1[i] << ' ' << V2[i] << ' ' << V3[i] << '\n';
    std::cout << '\n';
    return  EXIT_SUCCESS;

不幸的是,cufft 只接受数组,例如 cuDoubleComplex *a,而 thrust::sequence 只能与 thrust::complex<double>-向量一起正常工作。编译上面的代码时,出现两个错误:

error : no operator "=" matches these operands
error : no operator "<<" matches these operands

第一个是指thrust::sequence(V2.begin(), V2.end(), 2);,第二个是指std::cout << V1[i] << ' ' << V2[i] << ' ' << V3[i] << '\n';。如果我评论 V2,一切正常。
thrust::device_vector<thrust::complex<double>>cuDoubleComplex * 之间是否有转换?如果没有,我该如何组合它们?

thrust::complex<double>std::complex<double>cuDoubleComplex 共享相同的 数据布局 。因此,要使您的上述示例正常工作,所需要做的就是将 device_vector 中的数据转换为原始指针并将它们传递给 cuFFT。在大多数操作中,Thrust 本身不能与 cuDoubleComplex 一起工作,因为该类型是一个简单的容器,它没有定义执行 Thrust 期望的 POD 类型的任何操作所需的任何运算符.

这应该有效:

#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/complex.h>
#include <iostream>
#include <cufft.h>

int main()
{
    int length = 5;
    thrust::device_vector<thrust::complex<double> > V1(length);
    thrust::device_vector<thrust::complex<double> > V2(length);
    thrust::device_vector<thrust::complex<double> > V3(length);
    thrust::sequence(V1.begin(), V1.end(), 1);
    thrust::sequence(V2.begin(), V2.end(), 2);
    thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), 
                         thrust::multiplies<thrust::complex<double> >());
    cufftHandle plan;
    cufftPlan1d(&plan, length, CUFFT_Z2Z, 1);
    cuDoubleComplex* _V1 = (cuDoubleComplex*)thrust::raw_pointer_cast(V1.data());
    cuDoubleComplex* _V2 = (cuDoubleComplex*)thrust::raw_pointer_cast(V2.data());

    cufftExecZ2Z(plan, _V1, _V2, CUFFT_FORWARD);
    for (int i = 0; i < length; i++)
        std::cout << V1[i] << ' ' << V2[i] << ' ' << V3[i] << '\n';
    std::cout << '\n';
    return  EXIT_SUCCESS;
}