CUFFT 2D source code

Hi everyone,

If somebody haas a source code about CUFFT 2D, please post it.


if you want 2-D in-place transform, you can use following code.

// For in-place FFTs, the input stride is assumed to be 2*(N/2+1) cufftReal elements or N/2+1 cufftComplex 

// elements. For out-of-place transforms, the input and output strides match the logical transform size (N) 

// and the non-redundant size (N/2+1), respectively.

	#ifdef DO_DOUBLE

		typedef cufftDoubleComplex Complex; 


		typedef cufftComplex  Complex; 



// forward FFT (inplace)

// real data are put in contiguous data array, h_idata[1:Nx, 1:Ny] 

// but size of h_idata is bigger, say  Nx * 2*(Ny>>1 +1) doublereal 


// output:

//		h_idata is a complex array with size Nx*(Ny>>1 + 1)

void  lsc_2DFFTF_R2C( const unsigned int Nx, const unsigned int Ny, 

					 doublereal *h_idata )


	cufftHandle plan;

	doublereal  *d_idata;

//	size_t   N	  = Nx * Ny;

	size_t   Ny_pad = ((Ny >> 1) + 1);

	size_t   N_pad  = Nx * Ny_pad;

	size_t   stride = 2*Ny_pad; // stride on real data

	cufftResult  flag;

	unsigned int i;

// step 1: transfer data to device, sequence by sequence 

	cutilSafeCall( cudaMalloc((void**)&d_idata, sizeof(Complex)*N_pad ) );

	doublereal *d_idata_ptr = d_idata;

	doublereal *h_idata_ptr = h_idata;

	for ( i = 1; i <= Nx; i++){

		CUDA_SAFE_CALL(cudaMemcpy(d_idata_ptr, h_idata_ptr, sizeof(doublereal)*Ny, cudaMemcpyHostToDevice) );

		d_idata_ptr += stride;

		h_idata_ptr += Ny;


// step 2: Create a 2D FFT plan. 

#if defined (DO_DOUBLE)

	cufftPlan2d(&plan, Nx, Ny, CUFFT_D2Z );


	cufftPlan2d(&plan, Nx, Ny, CUFFT_R2C );


// step 3: Use the CUFFT plan to transform the signal in-place.

#if defined (DO_DOUBLE)

	flag = cufftExecD2Z( plan, d_idata, (cufftDoubleComplex *)d_idata );


	flag = cufftExecR2C( plan, d_idata, (cufftComplex *) d_idata );


	if ( CUFFT_SUCCESS != flag ){

		printf("2D: cufftExecR2C fails\n");


// make sure that all threads are done


// step 4: copy data to host

	CUDA_SAFE_CALL(cudaMemcpy(h_idata, d_idata, sizeof(Complex)*N_pad, cudaMemcpyDeviceToHost) );

// Destroy the CUFFT plan.  




Thank you so much LSChien, but I have another question, if I have an image, how can i turn an image into a data matrix?

If you know, please tell me. Thanks again

what kind of image do you have? uint8, unit16 or double?

you can use some tools to convert image to double array, for example, MATLAB.

I’m using 8-bit grayscale images, but I need to load the image in the program, if I use Matlab, i have to copy the matrix (corresponding to the image) and put it in the program, but I need to load the image in the program from some location in the hard disk. Could you please tell me How to do that?

Thanks Again.