我正在尝试使用“FFT + point_wise_product + iFFT”方法执行2D卷积。使用NxN矩阵,该方法运行良好,但是,对于非正方形矩阵,结果不正确。我已经阅读了整个cuFFT文档,寻找有关这种矩阵的行为的任何注释,这些函数已经在现场和现场FFT上进行了测试,但我忘记了一些东西。

我已经在MATLAB中用相同的矩阵测试了相同的算法,一切都正确。
我为您展示了一个非常简化的代码,带有一个非常基本的过滤器,以提高清晰度,输出和预期输出。我究竟做错了什么?
我也阅读了其他相关的问题/答案,但没有一个能解决问题。提前非常感谢您。

const int W = 5;
const int H = 4;

//Signal, with just one value, for simplicity.
float A[H][W] = {{0,0,0,0,0},
        {0,1,0,0,0},
        {0,0,0,0,0},
        {0,0,0,0,0}};

//Central element of the kernel in the (0,0) position of the array.
float B[H][W] = {{0.5, 0.1,  0,    0,  0.2},
        {0  ,  0  ,  0,    0,  0},
        {0  ,  0  ,  0,    0,  0},
        {0  ,  0  ,  0,    0,  0}};


cufftReal* d_inA, *d_inB;
cufftComplex* d_outA, *d_outB;

size_t real_size = W * H * sizeof(cufftReal);
size_t complex_size = W * (H/2+1) * sizeof(cufftComplex);

cudaMalloc((void**)&d_inA, real_size);
cudaMalloc((void**)&d_inB, real_size);

cudaMalloc((void**)&d_outA, complex_size);
cudaMalloc((void**)&d_outB, complex_size);

cudaMemset(d_inA,0,real_size);
cudaMemset(d_inB,0,real_size);

cudaMemcpy(d_inA, A, real_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_inB, B, real_size, cudaMemcpyHostToDevice);


cufftHandle fwplanA, fwplanB, bwplan;
cufftPlan2d(&fwplanA, W, H, CUFFT_R2C);
cufftPlan2d(&fwplanB, W, H, CUFFT_R2C);
cufftPlan2d(&bwplan, W, H, CUFFT_C2R);

cufftSetCompatibilityMode(fwplanA,CUFFT_COMPATIBILITY_NATIVE);
cufftSetCompatibilityMode(fwplanB,CUFFT_COMPATIBILITY_NATIVE);
cufftSetCompatibilityMode(bwplan,CUFFT_COMPATIBILITY_NATIVE);

cufftExecR2C(fwplanA, d_inA, d_outA);
cufftExecR2C(fwplanB, d_inB, d_outB);

int blocksx = ceil((W*(H/2+1 )) / 256.0f);
dim3 threads(256);
dim3 grid(blocksx);
// One complex product for each thread, scaled by the inverse of the
// number of elements involved in the FFT
pointwise_product<<<grid, threads>>>(d_outA, d_outB, (W*(H/2+1)), 1.0f/(W*H));

cufftExecC2R(bwplan, d_outA, d_inA);


cufftReal* result = new cufftReal[W*2*(H/2+1)];
cudaMemcpy(result, d_inA, real_size,cudaMemcpyDeviceToHost);

// Print result...
// Free memory...

输出。注意位移值
-0.0  0.0   -0.0   -0.0   0.0
0.0   0.5    0.1    0.0   -0.0
0.2   0.0    0.0   -0.0   -0.0
-0.0  0.0   -0.0   -0.0   -0.0

预期输出(MATLAB)
0         0         0         0         0
0.2000    0.5000    0.1000    0.0000    0.0000
0         0         0         0         0
0         0         0         0         0

相关文件:
http://developer.download.nvidia.com/compute/cuda/2_2/sdk/website/projects/convolutionFFT2D/doc/convolutionFFT2D.pdf

最佳答案

您正在将cufft计划中的行与列交换。

相关的原型(prototype)是

 cufftPlan2d(cufftHandle *plan, int nx, int ny, cufftType type)

其中nx是行数,ny是列数,因此应为
 cufftPlan2d(&fwplanA, H, W, CUFFT_R2C);

并不是
 cufftPlan2d(&fwplanA, W, H, CUFFT_R2C);

09-28 05:16