我目前正在调试我的代码,在那里我使用CUDA FFT例程。
我有这样的东西(请查看评论,了解我对自己工作的看法):
#include <cufft.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuComplex.h>
void foo(double* real, double* imag, size_t size)
{
cufftHandle plan;
cufftDoubleComplex* inputData;
cufftDoubleReal* outputReal;
//Allocation of arrays:
size_t allocSizeInput = sizeof(cufftDoubleComplex) * size;
size_t allocSizeOutput = sizeof(cufftDoubleReal) * (size - 1) * 2;
cudaMalloc((void**)&outputReal, allocSizeOutput);
cudaMalloc((void**)&inputData, allocSizeInput);
//Now I put the data in the arrays real and imag into input data by
//interleaving it
cudaMemcpy2D(static_cast<void*>(inputData),
2 * sizeof (double),
static_cast<const void*>(real),
sizeof(double),
sizeof(double),
size,
cudaMemcpyHostToDevice);
cudaMemcpy2D(static_cast<void*>(inputData) + sizeof(double),
2 * sizeof (double),
static_cast<const void*>(imag),
sizeof(double),
sizeof(double),
size,
cudaMemcpyHostToDevice);
//I checked inputData at this point and it does indeed look like i expect it to.
//Now I create the plan
cufftPlan1d(&plan, size, CUFFT_Z2D, 1);
//Now I execute the plan
cufftExecZ2D(plan, inputData, outputReal);
//Now I wait for device sync
cudaDeviceSynchronize();
//Now I fetch up the data from device
double* outDbl = new double[(size-1)*2]
cudaMemcpy(static_cast<void*>(outDbl),
static_cast<void*>(outputReal),
allocSizeOutput,
cudaMemcpyDeviceToHost);
//Here I am doing other fancy stuff which is not important
}
所以我现在的问题是,outDbl中的结果不是我所期望的。例如,如果我给这个函数给定以下值:
实数=[0]-5.567702511594111-5.595068807897317-5.595068807897317-5.567702511594111]
imag=[0]9.6786042248705352.280007038673738-2.280007038673738-9.678604224870535]
我希望得到:
结果=[4-46511-3.09563-0.29805 2.51837 5.34042]
但我得到了完全不同的东西。
我做错了什么?我是否误解了FFT函数?它基本上不是从复数到实数的逆FFT吗?我的数据复制例程有问题吗?
我必须承认我对这件事有点不知所措。
是的。。很抱歉在我问了这个问题之后,我在stackoverflow上找到了答案。
参见此处
基本上:cuda-fft不是标准化的,所以我必须将得到的值除以元素的数量才能得到标准化的值。