CUFFT:如何计算俯仰指针的fft? [英] CUFFT: How to calculate fft of pitched pointer?

查看:122
本文介绍了CUFFT:如何计算俯仰指针的fft?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to calculate the fft of an image using CUFFT. It seems like CUFFT only offers fft of plain device pointers allocated with cudaMalloc.

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

My input images are allocated using cudaMallocPitch but there is no option for handling pitch of the image pointer.

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

Currently, I have to remove the alignment of rows, then execute the fft, and copy back the results to the pitched pointer. My current code is as follows:

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个额外的内存分配和复制.我想做这样的事情:

It gives correct result, but I don't want to do 2 extra memory allocations and copies inside the function. I want to do something like this:

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?

Question:

How to calculate the fft of pitched pointer directly using CUFFT?

推荐答案

我认为您可能对cufftPlanMany感兴趣,它可以让您以音高进行1D,2D和3D fft.这里的关键是嵌入参数和嵌入参数.

I think you may be interested in cufftPlanMany which would let you do 1D, 2D, and 3D ffts with pitches. The key here is inembed and onembed parameters.

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

You can look up CUDA_CUFFT_Users_Guide.pdf (Pages 23-24) for more information. But for your example, you'd be doing something like the follows.

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

P.S.为了此处的示例,我没有添加退货支票.始终检查代码中的返回值.

P.S. I did not add return checks for the sake of example here. Always check for return values in your code.

这篇关于CUFFT:如何计算俯仰指针的fft?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆