【问题标题】:CUFFT: How to calculate fft of pitched pointer?CUFFT:如何计算倾斜指针的 fft?
【发布时间】:2012-12-11 04:53:18
【问题描述】:

我正在尝试使用 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?

【问题讨论】:

标签: cuda fft cufft


【解决方案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);
}

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

【讨论】:

  • 我试过上面的代码,但还是不行。输出包含垃圾值。
  • @sgar91 srcPitch 和 dstPitch 应该是元素数,而不是字节数(对于 cudamemcpy2d)
  • 是的,我通过srcPitch/sizeof(float)dstPitch/sizeof(cufftComplex) 将间距指定为元素数。仍然得到不正确的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多