使用Thrust库时使用cufft

3

我想在我的项目中结合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 > cuDoubleComplex * 之间是否存在转换?如果没有,该如何组合它们?

使用自定义的UnaryOperationthrust::tabulate代替thrust::sequence怎么样? - m.s.
1个回答

4
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;
}

1
CUFFT_C2C -> CUFFT_Z2Z - kangshiyin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接