2d CUFFT problem how can I use cufft.h?

I’m the basic user about nvidia. Sorry to my short English.

These days I tried to 2d cufft using rawfile but failed to do the fft.

Raw file’s inpormation : data type : unsigned char
size:256*256

I followed CUDA CUFFT Library pdf file…

Is there anyone who can solve my problem?

Below is my code. I hope somebody help me. Thank you for reading this.

(When I checked a odata, that is not correct…)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cufft.h>
#include <cutil_inline.h>

void runTest(int argc, char** argv);

#define NY 256
#define NX 256

int main(int argc, char** argv)
{
runTest(argc, argv);

cutilExit(argc, argv);

}

void runTest(int argc, char** argv)
{
if( cutCheckCmdLineFlag(argc, (const char**)argv, “device”) )
cutilDeviceInit(argc, argv);
else
cudaSetDevice( cutGetMaxGflopsDeviceId() );

cufftComplex *idata=(cufftComplex*)malloc(sizeof(cufftComplex)*NX*NY);
cufftComplex *odata=(cufftComplex*)malloc(sizeof(cufftComplex)*NX*NY);

cufftComplex *d_idata;
cufftComplex *d_odata;
     
cutilSafeCall(cudaMalloc((void**)&d_odata,sizeof(cufftComplex)*NX*NY));
   
cufftHandle plan;
 
unsigned char *rawdata=  (unsigned char*)malloc(sizeof(unsigned char) * NX*NY);

FILE *infile;
infile=fopen("Lena_256.raw","rb");
if(infile==NULL){
   printf("File not found! \n");
}
fread(rawdata,sizeof(unsigned char),NX*NY,infile);
fclose(infile);
		
for(int i=0; i<NX*NY; i++)
{
     idata[i].x=float(rawdata[i]);
     idata[i].y=0.0f;
 }

unsigned int timer = 0;
cutilCheckError(cutCreateTimer(&timer));
cutilCheckError(cutStartTimer(timer));


cufftPlan2d(&plan,NX,NY,CUFFT_C2C);    
cufftExecC2C(plan,d_idata,odata,CUFFT_FORWARD);


cutilCheckError(cutStopTimer(timer));
printf("Processing time: %f (ms) \n", cutGetTimerValue(timer));
cutilCheckError(cutDeleteTimer(timer));

cufftDestroy(plan);
free(idata);
free(odata);

cudaThreadExit();

}

  1. you miss allocation of d_idata
    cutilSafeCall(cudaMalloc((void**)&d_idata,sizeof(cufftComplex)NXNY));

  2. you need to copy data from host to device before executing FFT
    CUDA_SAFE_CALL(cudaMemcpy( d_idata, idata, sizeof(cufftComplex)NXNY, cudaMemcpyHostToDevice) );

  3. after FFT, you need to copy data back, from device to host
    CUDA_SAFE_CALL(cudaMemcpy( odata, d_odata, sizeof(cufftComplex)NXNY , cudaMemcpyDeviceToHost) );

then you can access host data, odata.

By the way, your code should give you “invalid device memory” since d_idata is invlid.

LSChien, Thank you for your help. Today I changed my code to follow your advice.

However if I use a device memory, for example, about my code, idata array is about raw file.

        cutilsafeCall(cudaMalloc((void**)&d_idata,sizeof(cufftComplex)*NX*NY);
        CUDA_SAFE_CALL(cudaMemcpy(d_idata,idata,sizeof(cufftComplex)

NXNY,cudaMemcpyHostToDevice));

If I use upper 2 sentence, idata values move to d_idata, but when I checked the d_idata, every d_idata value is 0, not idata value…

Is there any method to move idata information to d_idata array?

Thank you for reading this.

how do you check the value of d_idata, inside kernel function or in host code?

you cannot use printf to show content of d_idata since it points to device memory but your host thread only

address host memory.

if you want to verify data transfer, I suggest

step 1: random generate idata

step 2: transfer idata (host) to d_idata (device)

step 3: write a kernel function to do “copy”, copy d_idata to d_odata

step 4: transfer d_odata (device) to odata (host)

step 5: compare idata and odata

Thank you for your help. I really appreciate your kind answer and help.

I solve all my problem. I appreciate again and have a nice day ^^