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

Using cufft when operating with the thrust library

本文关键字:操作      更新时间:2023-10-16

我想在我的项目中合并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。推力本身不能在大多数操作中与cuDoubleComplex一起使用,因为该类型是一个简单的容器,它没有定义任何需要执行推力期望的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;
}