cpu_fft.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "cpu_fft.h"
  4. void loop_data_fftw (OutputType outputType, TimeEntry *time_entry)
  5. {
  6. Timer *timer;
  7. timer = timer_new ();
  8. time_entry->dim_entries = (DimEntry *)malloc(N_DIMS * sizeof(DimEntry));
  9. time_entry->lib_name = "CPU (FFTW)";
  10. printf ("Device: %s\n", time_entry->lib_name);
  11. fflush (stdout);
  12. for (int k = 0; k < N_DIMS; k++) {
  13. int dim = DIMS[k];
  14. int power_min = N_POWERS_INTERVALS[k][0];
  15. int power_max = N_POWERS_INTERVALS[k][1];
  16. int num_entries = power_max - power_min + 1;
  17. time_entry->dim_entries[k].n_dims = dim;
  18. time_entry->dim_entries[k].sizes = (unsigned int **)malloc(sizeof(unsigned int *) * num_entries);
  19. time_entry->dim_entries[k].times = (double *)malloc(sizeof(double) * num_entries);
  20. time_entry->dim_entries[k].errors = (double *)malloc(sizeof(double) * num_entries);
  21. PRINT_DIM (dim);
  22. fflush (stdout);
  23. for (int m = power_min, i = 0; m <= power_max; m++, i++) {
  24. fftw_complex *host_orig_mem;
  25. fftw_complex *host_result_mem;
  26. fftw_complex *host_immediate_mem;
  27. fftw_plan plan;
  28. fftw_plan inverse_plan;
  29. double time_sec;
  30. double sum = 0.0;
  31. size_t side_size = pow(2,m);
  32. size_t size = pow(side_size,dim);
  33. size_t size_bytes = sizeof (fftw_complex) * size;
  34. host_orig_mem = fftw_malloc (size_bytes);
  35. host_immediate_mem = fftw_malloc (size_bytes);
  36. host_result_mem = fftw_malloc (size_bytes);
  37. switch (dim) {
  38. case 1:
  39. plan = fftw_plan_dft_1d (side_size,
  40. host_orig_mem, host_immediate_mem,
  41. FFTW_FORWARD, FFTW_ESTIMATE);
  42. inverse_plan = fftw_plan_dft_1d (side_size,
  43. host_immediate_mem,
  44. host_result_mem,
  45. FFTW_BACKWARD, FFTW_ESTIMATE);
  46. break;
  47. case 2:
  48. plan = fftw_plan_dft_2d (side_size, side_size,
  49. host_orig_mem, host_immediate_mem,
  50. FFTW_FORWARD, FFTW_ESTIMATE);
  51. inverse_plan = fftw_plan_dft_2d (side_size, side_size,
  52. host_immediate_mem, host_result_mem,
  53. FFTW_BACKWARD, FFTW_ESTIMATE);
  54. break;
  55. case 3:
  56. plan = fftw_plan_dft_3d (side_size, side_size, side_size,
  57. host_orig_mem, host_immediate_mem,
  58. FFTW_FORWARD, FFTW_ESTIMATE);
  59. inverse_plan = fftw_plan_dft_3d (side_size, side_size, side_size,
  60. host_immediate_mem, host_result_mem,
  61. FFTW_BACKWARD, FFTW_ESTIMATE);
  62. break;
  63. default:
  64. fprintf (stderr, "Unknown FFT dimensions\n");
  65. return;
  66. }
  67. for (int j = 0; j < size; j++) {
  68. host_orig_mem[j][0] = rand() / ((double) RAND_MAX);
  69. host_orig_mem[j][1] = rand() / ((double) RAND_MAX);
  70. }
  71. PRINT_DIMS(dim, side_size);
  72. fflush (stdout);
  73. printf (".");
  74. fflush (stdout);
  75. timer_start (timer);
  76. for (int j = 0; j < N_RUNS; j++) {
  77. fftw_execute (plan);
  78. }
  79. timer_stop (timer);
  80. fftw_execute (inverse_plan);
  81. for (int j = 0; j < size; j++) {
  82. sum += fabs (host_result_mem[j][0] / size - host_orig_mem[j][0]);
  83. sum += fabs (host_result_mem[j][1] / size - host_orig_mem[j][1]);
  84. }
  85. time_sec = timer_get_seconds (timer) / N_RUNS;
  86. time_entry->dim_entries[k].sizes[i] = (unsigned int *)malloc(sizeof(unsigned int) * dim);
  87. for (int j = 0; j < dim; j++) {
  88. time_entry->dim_entries[k].sizes[i][j] = side_size;
  89. }
  90. time_entry->dim_entries[k].times[i] = get_measurements_with_format(outputType, size_bytes, time_sec);
  91. time_entry->dim_entries[k].errors[i] = sum / size;
  92. fftw_destroy_plan (inverse_plan);
  93. fftw_destroy_plan (plan);
  94. fftw_free (host_orig_mem);
  95. fftw_free (host_immediate_mem);
  96. fftw_free (host_result_mem);
  97. }
  98. printf ("\n");
  99. fflush (stdout);
  100. }
  101. printf ("\n");
  102. timer_destroy (timer);
  103. }