我目前正在调试我的代码,我在其中使用 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]
图像 = [0 9.678604224870535 2.280007038673738 -2.280007038673738 -9.678604224870535]
我希望得到:
结果 = [-4.46511 -3.09563 -0.29805 2.51837 5.34042]
但我得到了完全不同的东西。
我做错了什么?我误解了 FFT 函数吗?基本上不是从复数到实数的逆FFT吗?我的数据复制例程有问题吗?
我必须承认我对这个有点迷茫。