CUDA fft 2d与MATLAB fft 2d的结果不同 [英] CUDA fft 2d different results from MATLAB fft on 2d

查看:359
本文介绍了CUDA fft 2d与MATLAB fft 2d的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的fft并比较2D数组上MATLAB和CUDA之间的结果.

I have tried to do a simple fft and compare the results between MATLAB and CUDA on 2d arrays.

MATLAB: 9个数字1-9的数组

MATLAB: array of 9 numbers 1-9

I = [1 2 3
4 5 6
7 8 9];

并使用以下代码:

fft(I)

给出结果:

 12.0000 + 0.0000i  15.0000 + 0.0000i  18.0000 + 0.0000i
  -4.5000 + 2.5981i  -4.5000 + 2.5981i  -4.5000 + 2.5981i
  -4.5000 - 2.5981i  -4.5000 - 2.5981i  -4.5000 - 2.5981i

和CUDA代码:

int FFT_Test_Function() {

int width = 3;
int height = 3;
    int n = width * height;

double in[width][height];
Complex out[width][height];

for (int i = 0; i<width; i++)
{
    for (int j = 0; j < height; j++)
    {
        in[i][j] = (i * width) + j + 1;
    }
}

    // Allocate the buffer
    cufftDoubleReal *d_in;
    cufftDoubleComplex *d_out;
    unsigned int out_mem_size = sizeof(cufftDoubleComplex)*n;
    unsigned int in_mem_size = sizeof(cufftDoubleReal)*n;
    cudaMalloc((void **)&d_in, in_mem_size);
    cudaMalloc((void **)&d_out, out_mem_size);

    // Save time stamp
    milliseconds timeStart = getCurrentTimeStamp();

    cufftHandle plan;
    cufftResult res = cufftPlan2d(&plan, width, height, CUFFT_D2Z);
    if (res != CUFFT_SUCCESS) { cout << "cufft plan error: " << res << endl; return 1; }
    cudaCheckErrors("cuda malloc fail");

for (int i = 0; i < width; i++)
{
    cudaMemcpy(d_in + (i * width), &in[i], height * sizeof(double), cudaMemcpyHostToDevice);
    cudaCheckErrors("cuda memcpy H2D fail");
}
    cudaCheckErrors("cuda memcpy H2D fail");

    res = cufftExecD2Z(plan, d_in, d_out);
    if (res != CUFFT_SUCCESS) { cout << "cufft exec error: " << res << endl; return 1; }
for (int i = 0; i < width; i++)
{
    cudaMemcpy(&out[i], d_out + (i * width), height * sizeof(Complex), cudaMemcpyDeviceToHost);
    cudaCheckErrors("cuda memcpy H2D fail");
}
    cudaCheckErrors("cuda memcpy D2H fail");

    milliseconds timeEnd = getCurrentTimeStamp();
    milliseconds totalTime = timeEnd - timeStart;
    std::cout << "Total time: " << totalTime.count() << std::endl;

    return 0;
}

在此CUDA代码中,我得到了结果:

In this CUDA code i got the result:

您可以看到CUDA给出了不同的结果.

You can see that CUDA gives different results.

我错过了什么?

非常感谢您的关注!

推荐答案

cuFFT结果看起来正确,但是您的FFT代码错误-应该是:

The cuFFT result looks correct, but your FFT code is wrong - it should be:

octave:1> I = [ 1 2 3; 4 5 6; 7 8 9 ]
I =

   1   2   3
   4   5   6
   7   8   9

octave:2> fft2(I)
ans =

   45.00000 +  0.00000i   -4.50000 +  2.59808i   -4.50000 -  2.59808i
  -13.50000 +  7.79423i    0.00000 +  0.00000i    0.00000 +  0.00000i
  -13.50000 -  7.79423i    0.00000 -  0.00000i    0.00000 -  0.00000i

请注意使用 fft2 .

Note the use of fft2.

这篇关于CUDA fft 2d与MATLAB fft 2d的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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