我知道之前有人问过类似的问题,但我遇到了麻烦。这是我写的代码:
void fft(const double *indata_real, const double *indata_imag, double *outdata_real, double *outdata_imag, int x, int y)
{
int size = sizeof(cufftDoubleComplex)*x*y;
// allocate data on host
cufftDoubleComplex* host_data = (cufftDoubleComplex*)malloc(size);
for (int i = 0; i < x*y; ++i) {
host_data[i].x = indata_real[i];
host_data[i].y = indata_imag[i];
}
// allocate data on device
cufftDoubleComplex* device_data;
cudaMalloc((void**)&device_data, size);
// copy data from host to device
cudaMemcpy(device_data, host_data, size, cudaMemcpyHostToDevice);
// create plan
cufftHandle plan;
cufftPlan2d(&plan, x, y, CUFFT_Z2Z);
// perform transform
cufftExecZ2Z(plan, (cufftDoubleComplex *)device_data, (cufftDoubleComplex *)device_data, CUFFT_FORWARD);
// copy data back from device to host
cudaMemcpy(host_data, device_data, size, cudaMemcpyDeviceToHost);
// copy transform to outdata
for (int i = 0; i < x*y; ++i) {
outdata_real[i] = host_data[i].x;
outdata_imag[i] = host_data[i].y;
}
// clean up
cufftDestroy(plan);
free(host_data);
cudaFree(device_data);
}
以上适用于单精度,即当我将所有“cufftDoubleComplex”替换为“cufftComplex”时,将“CUFFT_Z2Z”替换为“CUFFT_C2C”,并将“cufftExecZ2Z”替换为cufftExecC2C
根据我在另一页上找到的内容,我认为这可以在双精度下正常运行。但目前 outdata 数组与 indata 数组相同 - 它没有做任何事情。
因此,如果有人能发现我做错了什么,那就太好了!
小号