Guest User

Untitled

a guest
May 27th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #include <cuda_runtime.h>
  6. #include <cufft.h>
  7.  
  8. typedef enum signaltype {REAL, COMPLEX} signal;
  9.  
  10. typedef float2 Complex;
  11.  
  12. void
  13. printData(Complex *a, int size, char *msg) {
  14.  
  15.   if (msg == "") printf("\n");
  16.   else printf("%s\n", msg);
  17.  
  18.   for (int i = 0; i < size; i++)
  19.     printf("%f %f\n", a[i].x, a[i].y);
  20. }
  21.  
  22. void
  23. normData(Complex *a, int size, float norm) {
  24.  
  25.   for (int i = 0; i < size; i++) {
  26.     a[i].x /= norm;
  27.     a[i].y /= norm;
  28.   }
  29. }
  30.  
  31. void
  32. randomFill(Complex *h_signal, int size, int flag) {
  33.  
  34.   // Real signal.
  35.   if (flag == REAL) {
  36.     for (int i = 0; i < size; i++) {
  37.       h_signal[i].x = rand() / (float) RAND_MAX;
  38.       h_signal[i].y = 0;
  39.     }
  40.   }
  41. }
  42.  
  43. // FFT a signal that's on the _DEVICE_.
  44. void
  45. signalFFT(Complex *d_signal, int signal_size) {
  46.  
  47.   cufftHandle plan;
  48.   if (cufftPlan1d(&plan, signal_size, CUFFT_C2C, 1) != CUFFT_SUCCESS) {
  49.     printf("Failed to plan FFT\n");
  50.     exit(0);
  51.   }
  52.  
  53.   // Execute the plan.
  54.   if (cufftExecC2C(plan, (cufftComplex *) d_signal, (cufftComplex *) d_signal, CUFFT_FORWARD) != CUFFT_SUCCESS) {
  55.     printf ("Failed Executing FFT\n");
  56.     exit(0);
  57.   }
  58.  
  59. }
  60.  
  61. void
  62. signalIFFT(Complex *d_signal, int signal_size) {
  63.  
  64.   cufftHandle plan;
  65.   if (cufftPlan1d(&plan, signal_size, CUFFT_C2C, 1) != CUFFT_SUCCESS) {
  66.     printf("Failed to plan IFFT\n");
  67.     exit(0);
  68.   }
  69.  
  70.   // Execute the plan.
  71.   if (cufftExecC2C(plan, (cufftComplex *) d_signal, (cufftComplex *) d_signal, CUFFT_INVERSE) != CUFFT_SUCCESS) {
  72.     printf ("Failed Executing IFFT\n");
  73.     exit(0);
  74.   }
  75.  
  76. }
  77.  
  78. int main()
  79. {
  80.  
  81.   Complex *h_signal, *d_signal1;
  82.  
  83.   int alloc_size, i;
  84.  
  85.   alloc_size = 16;
  86.  
  87.   // Kernel Block and Grid Size.
  88.   const dim3 blockSize(16, 16, 1);
  89.   const dim3 gridSize(alloc_size / 16 + 1, alloc_size / 16 + 1, 1);
  90.  
  91.   h_signal = (Complex *) malloc(sizeof(Complex) * alloc_size);
  92.  
  93.   cudaMalloc(&d_signal1, sizeof(Complex) * alloc_size);
  94.   if (cudaGetLastError() != cudaSuccess){
  95.     printf("Cuda error: Failed to allocate\n");
  96.     exit(0);
  97.   }
  98.   //cudaMalloc(&d_signal2, sizeof(Complex) * alloc_size);
  99.  
  100.   // Add random data to signal.
  101.   randomFill(h_signal, alloc_size, REAL);
  102.   printData(h_signal, alloc_size, "Random H1");
  103.  
  104.   cudaMemcpy(d_signal1, h_signal, sizeof(Complex) * alloc_size, cudaMemcpyHostToDevice);
  105.  
  106.   signalFFT(d_signal1, alloc_size);
  107.  
  108.   signalIFFT(d_signal1, alloc_size);
  109.  
  110.   cudaDeviceSynchronize();
  111.  
  112.   cudaMemcpy(h_signal, d_signal1, sizeof(Complex) * alloc_size, cudaMemcpyDeviceToHost);
  113.  
  114.   printData(h_signal, alloc_size, "IFFT");
  115.  
  116.   return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment