3

我正在尝试使用 CUFFT 计算图像的 fft。似乎 CUFFT 只提供 fft 分配的普通设备指针cudaMalloc

我的输入图像是使用分配的,cudaMallocPitch但没有处理图像指针间距的选项。

目前,我必须删除行的对齐方式,然后执行 fft,并将结果复制回倾斜的指针。我目前的代码如下:

void fft_device(float* src, cufftComplex* dst, int width, int height, int srcPitch, int dstPitch)
{
    //src and dst are device pointers allocated with cudaMallocPitch

    //Convert them to plain pointers. No padding of rows.
    float *plainSrc;
    cufftComplex *plainDst;

    cudaMalloc<float>(&plainSrc,width * height * sizeof(float));
    cudaMalloc<cufftComplex>(&plainDst,width * height * sizeof(cufftComplex));

    cudaMemcpy2D(plainSrc,width * sizeof(float),src,srcPitch,width * sizeof(float),height,cudaMemcpyDeviceToDevice);

    cufftHandle handle;
    cufftPlan2d(&handle,width,height,CUFFT_R2C);

    cufftSetCompatibilityMode(handle,CUFFT_COMPATIBILITY_NATIVE);

    cufftExecR2C(handle,plainSrc,plainDst);

    cufftDestroy(handle);

    cudaMemcpy2D(dst,dstPitch,plainDst,width * sizeof(cufftComplex),width * sizeof(cufftComplex),height,cudaMemcpyDeviceToDevice);

    cudaFree(plainSrc);
    cudaFree(plainDst);
} 

它给出了正确的结果,但我不想在函数内部进行 2 次额外的内存分配和复制。我想做这样的事情:

void fft_device(float* src, cufftComplex* dst, int width, int height, int srcPitch, int dstPitch)
{
    //src and dst are device pointers allocated with cudaMallocPitch
    //Don't know how to handle pitch here???
    cufftHandle handle;
    cufftPlan2d(&handle,width,height,CUFFT_R2C);

    cufftSetCompatibilityMode(handle,CUFFT_COMPATIBILITY_NATIVE);

    cufftExecR2C(handle,src,dst);

    cufftDestroy(handle);
}

问题:

如何直接使用CUFFT计算倾斜指针的fft?

4

1 回答 1

1

我想您可能对cufftPlanMany哪个可以让您使用音高进行 1D、2D 和 3D ffts 感兴趣。这里的关键是 inembed 和 oneembed 参数。

您可以查看 CUDA_CUFFT_Users_Guide.pdf(第 23-24 页)了解更多信息。但是对于您的示例,您将执行以下操作。

void fft_device(float* src, cufftComplex* dst,
                int width, int height,
                int srcPitch, int dstPitch)
{
    cufftHandle handle;
    int rank = 2; // 2D fft
    int n[] = {width, height};    // Size of the Fourier transform
    int istride = 1, ostride = 1; // Stride lengths
    int idist = 1, odist = 1;     // Distance between batches
    int inembed[] = {srcPitch, height}; // Input size with pitch
    int onembed[] = {dstPitch, height}; // Output size with pitch
    int batch = 1;
    cufftPlanMany(&handle, rank, n, 
                  inembed, istride, idist,
                  onembed, ostride, odist, CUFFT_R2C, batch);

    cufftSetCompatibilityMode(handle,CUFFT_COMPATIBILITY_NATIVE);
    cufftExecR2C(handle,src,dst);
    cufftDestroy(handle);
}

PS为了举例,我没有在这里添加退货支票。始终检查代码中的返回值。

于 2012-12-27T09:47:48.323 回答