CUDA Replaces FFT Part in Library

Asked 2 years ago, Updated 2 years ago, 101 views

We have created a blurring image processing program in C language based on reference. (CPU)
I am thinking of processing the FFT part using the cufft library. (Replace the FFT part with GPU)

 unsigned character imageIN [number of pixels] [number of pixels]

↓Include image data and convert to float type

for(i=0;i<width;i++){
  for(j=0;j<width;j++){
    data[i][j]=(float)imageIN[i][j];
    jdata[i][j] = 0.0;
  }
}

I put the image data in this way and compared the FFT part with the one made by the cufft library only in the c language.
The CUFFT library did not seem to run well.

Subject
How can I run the cufft library (abstract, but I summarized the questions below)
(The program below is the part where you want to skip the others to some extent and ask questions (the reverse FFT is comment out)

int main(intargc, char**argv){
  inti,k,d;
  int n = 512; // Size of one side of the image
  float*v = NULL;
  cufftComplex*dv;
  cufftHandle plan;

  cudaMalloc(void**)&dv,n*n*sizeof(float2));// Securing device memory
  cudaMallocHost(void**)&v,n*n*sizeof(float2)); // Host memory availability
  for(i=0;i<n*n*2;i++)v[i]=(float)(i%(n-1)); // Initialize data

  load_image_data(); // Load original image
  make_original_data(); // Create original data

  // Do you want to put the image data in v here?
  cudaMemcpy(dv,v,n*n*sizeof(double2),cudaMemcpyHostToDevice); // Host to device transfer
  Preparing for 2D FFT for cufftPlan2d(&plan,n,n,CUFFT_R2C);//n×n
  cufftExecC2C (plan, dv, dv, CUFFT_FORWARD); // Forward Conversion

  cudaMemcpy(data,dv,n*n*sizeof(double2),cudaMemcpyDeviceToHost);
                                  // Transfer from device to host
  // cuftDestroy(plan); // Free up resources
  // (I want blur to be done by CPU)
  Blur(); // Blur filtering in frequency domain

  /*
  cudaMemcpy(dv,v,n*n*sizeof(double2),cudaMemcpyHostToDevice); // Host to device transfer
  Preparing for 2D FFT for cufftPlan2d(&plan,n,n,CUFFT_C2R);//n×n
  cufftExecC2C(plan, dv, dv, CUFFT_INVERSE); // Reverse Conversion
  cuffTestroy(plan);// Free up resources
  cudaMemcpy(v,dv,n*n*sizeof(double2),cudaMemcpyDeviceToHost);
                          // Transfer from device to host
  */

  cudaFree(v);
  save_image_data(); // Save output image
  return 0;
}
  • Is 2D FFT OK for this kind of process?
  • Can I consider cufftComplex as a structure?
  • Also, how do I substitute image data?
  • Please let me know if there is anything else wrong.

I've tried many things, but I'm worried because I don't have enough knowledge.
Thank you for your cooperation.

src=

on VS2010

There were some expressions that might cause misunderstanding, so I added them in part ( ).

c cuda

2022-09-30 14:51

1 Answers

I'm a questioner.
Thanks to everyone's cooperation, it has been solved!

Please allow me to write changes for people like myself in the future.

float*v=NULL;→float2*v;
In this way, you can create a structure for v.

for(i=0;i<n*n*2;i++)v[i]=(float)(i%(n-1));// Initialize data
                        //↓ changed to
for(j=0;j<n;j++){for(i=0;i<n;i++){v[j*n+i]=make_cuComplex(data[j][i],jdata[j][i])}}};// data substitution

By doing so, the value can be easily substituted into the structure (x, y) of v.
Also, instead of taking n*n*2, take it with float2 to prevent bugs.

blur();→blur(v);
  save_image_data(); → save_image_data(v);

I changed it to an address because I used a lot of global functions.

I thought that once I use it before moving to CPU→GPU→CPU and then to GPU again, I need to free up space.
It was okay to release the space at the end.

The rest is
in response to your own questions. - You can definitely use 2D FFT for the image
- CufftComplex is a float structure.
- Image data can be substituted with make_cuComplex.


Enter a description of the image here

The contents of the device memory can also be seen directly in the debugger.

For those who made it easier to understand by editing
I am very grateful to the people who kindly taught me.
I will study so that I can reach the respondents as much as possible.


2022-09-30 14:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.