Background:
Inadvertently see Cuda solution FFT has a Cufft function library, a general view of the relevant knowledge of Cufft, wrote a solution to a one-dimensional cuda code, according to the investigation know Cufft in the resolution of the situation 1d,2d,3d the complexity of the time is O (Nlogn), attached to solve One-dimensional code, ready to find some detailed information to learn about Cuda's library of functions.
#include"stdio.h"#include"cuda_runtime.h"#include"cufft.h"#include"Device_launch_parameters.h"#defineLENGTH 4intMain () {
floatData[length] = {1,2,3,4}; Cufftcomplex*compdata= (cufftcomplex*)malloc(length*sizeof(Cufftcomplex)); inti; for(i=0; i<length;i++) {compdata[i].x=Data[i]; Compdata[i].y=0; }Cufftcomplex*D_fftdata; Cudamalloc ((void* *) &d_fftdata,length*sizeof(Cufftcomplex)); cudamemcpy (D_fftdata,compdata,length*sizeof(Cufftcomplex), cudamemcpyhosttodevice); Cuffthandle plan; CUFFTPLAN1D (&PLAN,LENGTH,CUFFT_C2C,1); CUFFTEXECC2C (Plan, (Cufftcomplex*) D_fftdata, (cufftcomplex*) D_fftdata,cufft_forward); Cudadevicesynchronize (); cudamemcpy (Compdata,d_fftdata,length*sizeof(Cufftcomplex), cudamemcpydevicetohost); for(i=0; i<length;i++) { if(compdata[i].x! =0) {printf ("%3.1f", compdata[i].x); } if(Compdata[i].y! =0) {printf ("+%3.1fi", COMPDATA[I].Y); } printf ("\ n"); } Cufftdestroy (plan); Free(Compdata); Cudafree (D_fftdata);}
This code that runs under Linux:
Compile command: Nvcc-o fftcu fft.cu-i/usr/local/cuda/include-l/usr/local/cuda/lib64-lcufft
Run command:./FFTCU
Note: There are cufft.h header files in/usr/local/cuda/include, libcufft.so library files in/usr/local/cuda/lib64
Fourier transform-fft (Cuda implementation)