[英]CUDA fft different results from MATLAB fft
我試圖做一個簡單的fft並比較MATLAB和CUDA之間的結果。
MATLAB:9個數字1-9的向量
I = [1 2 3 4 5 6 7 8 9];
並使用此代碼:
fft(I)
給出結果:
45.0000 + 0.0000i
-4.5000 +12.3636i
-4.5000 + 5.3629i
-4.5000 + 2.5981i
-4.5000 + 0.7935i
-4.5000 - 0.7935i
-4.5000 - 2.5981i
-4.5000 - 5.3629i
-4.5000 -12.3636i
和CUDA代碼:
int FFT_Test_Function() {
int n = 9;
double* in = new double[n];
Complex* out = new Complex[n];
for (int i = 0; i<n; i++)
{
in[i] = i + 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 = cufftPlan1d(&plan, n, CUFFT_D2Z, 1);
if (res != CUFFT_SUCCESS) { cout << "cufft plan error: " << res << endl; return 1; }
cudaCheckErrors("cuda malloc fail");
cudaMemcpy(d_in, in, in_mem_size, cudaMemcpyHostToDevice);
cudaCheckErrors("cuda memcpy H2D fail");
res = cufftExecD2Z(plan, d_in, d_out);
if (res != CUFFT_SUCCESS) { cout << "cufft exec error: " << res << endl; return 1; }
cudaMemcpy(out, d_out, out_mem_size, cudaMemcpyDeviceToHost);
cudaCheckErrors("cuda memcpy D2H fail");
milliseconds timeEnd = getCurrentTimeStamp();
milliseconds totalTime = timeEnd - timeStart;
std::cout << "Total time: " << totalTime.count() << std::endl;
return 0;
}
在這個CUDA代碼中我得到了結果:
你可以看到CUDA給出4個零(單元格5-9)。
我錯過了什么?
非常感謝您的關注!
CUFFT_D2Z
是一個實數到復數的FFT,因此輸出數據中的前N/2 - 1
CUFFT_D2Z
N/2 - 1
點是冗余的 - 它們只是變換下半部分的復共軛(如果你在MATLAB輸出中可以看到這一點)比較關於中點的鏡像對)。
如果需要,您可以填寫這些“缺失”的術語,只需采用每個相應術語的復數共軛,但通常沒有太多意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.