benchmark.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <math.h>
  6. #include <unistd.h>
  7. #include <getopt.h>
  8. #if defined HAVE_AMD_FFT || defined HAVE_APPLE_FFT
  9. #define HAVE_OPENCL 1
  10. #endif
  11. #ifdef HAVE_OPENCL
  12. #include "opencl_fft.h"
  13. #endif
  14. #ifdef HAVE_FFTW
  15. #include "cpu_fft.h"
  16. #endif
  17. #ifdef HAVE_CUDA_FFT
  18. #include "cuda_fft.h"
  19. #endif
  20. #include "utilities.h"
  21. int
  22. main (int argc, char **argv)
  23. {
  24. static OutputType outputType = OUT_NONE;
  25. static int only_time = 1;
  26. static bool new_line = true;
  27. char *timestamp;
  28. int ofn_len;
  29. char *output_filename;
  30. char fn_buffer[150];
  31. get_timestamp (&timestamp);
  32. ofn_len = sprintf(fn_buffer, "results_%s.txt", timestamp);
  33. output_filename = (char *)malloc(sizeof(char) * (ofn_len + 1));
  34. strcpy(output_filename, fn_buffer);
  35. free(timestamp);
  36. #ifdef HAVE_OPENCL
  37. cl_platform_id opencl_platform;
  38. cl_uint opencl_n_devices;
  39. cl_device_id *opencl_devices;
  40. cl_context opencl_context;
  41. cl_command_queue *opencl_queues;
  42. cl_int err;
  43. #endif
  44. #ifdef HAVE_CUDA_FFT
  45. int cuda_n_devices;
  46. #endif
  47. #ifdef HAVE_AMD_FFT
  48. static int with_amd_fft = 1;
  49. TimeEntry *amd_time_entries;
  50. #endif
  51. #ifdef HAVE_CUDA_FFT
  52. static int with_cuda_fft = 1;
  53. TimeEntry *cuda_time_entries;
  54. #endif
  55. #ifdef HAVE_APPLE_FFT
  56. static int with_apple_fft = 1;
  57. TimeEntry *apple_time_entries;
  58. #endif
  59. #ifdef HAVE_FFTW
  60. static int with_fftw = 1;
  61. TimeEntry *fftw_time_entries;
  62. #endif
  63. static char *options_descritions[] = {
  64. "Set the required output format [ms(default),sec,mflops,gflops,GBs,MBs] of benchmark results.",
  65. "Set the range of one side sizes [N M] (N < M) of 1D FFT in form of powers of two (e.g. form 2^N to 2^M).",
  66. "Set the range of one side sizes [N M] (N < M) of 2D FFT in form of powers of two (e.g. form 2^N to 2^M).",
  67. "Set the range of one side sizes [N M] (N < M) of 3D FFT in form of powers of two (e.g. form 2^N to 2^M).",
  68. "Set the number of runs for each size of each kind of FFT.",
  69. #ifdef HAVE_AMD_FFT
  70. "Disable AMD FFT library from benchmarking.",
  71. #endif
  72. #ifdef HAVE_CUDA_FFT
  73. "Disable CUDA FFT library from benchmarking.",
  74. #endif
  75. #ifdef HAVE_APPLE_FFT
  76. "Disable Apple FFT library from benchmarking.",
  77. #endif
  78. #ifdef HAVE_FFTW
  79. "Disable FFTW library from benchmarking",
  80. #endif
  81. "Add calculation errors to the results.",
  82. "Name of the output file (default name is result.txt).",
  83. "Display this usage information."
  84. };
  85. static struct option long_options[] =
  86. {
  87. {"measure-format", required_argument, NULL, 'm'},
  88. {"1d-pow2-range", required_argument, NULL, '1'},
  89. {"2d-pow2-range", required_argument, NULL, '2'},
  90. {"3d-pow2-range", required_argument, NULL, '3'},
  91. {"number-of-runs", required_argument, NULL, 'r'},
  92. #ifdef HAVE_AMD_FFT
  93. {"disable-amd", no_argument, &with_amd_fft, 0},
  94. #endif
  95. #ifdef HAVE_CUDA_FFT
  96. {"disable-cuda", no_argument, &with_cuda_fft, 0},
  97. #endif
  98. #ifdef HAVE_APPLE_FFT
  99. {"disable-apple", no_argument, &with_apple_fft, 0},
  100. #endif
  101. #ifdef HAVE_FFTW
  102. {"disable-fftw", no_argument, &with_fftw, 0},
  103. #endif
  104. {"print-errors", no_argument, NULL, 'p'},
  105. {"output-file", no_argument, NULL, 'o'},
  106. {"help", no_argument, NULL, 'h'},
  107. {NULL, 0, NULL, 0}
  108. };
  109. static char *short_options = "hm:1:2:3:r:o:p";
  110. /* Parse options */
  111. int next_option;
  112. int out_indx;
  113. do {
  114. next_option = getopt_long (argc, argv, short_options, long_options, &out_indx);
  115. switch (next_option) {
  116. case 'm':
  117. outputType = get_output_type_by_measure(optarg);
  118. break;
  119. case '1':
  120. if (!get_fft_range(argv[optind - 1], argv[optind], N_POWERS_INTERVALS[0])) {
  121. print_usage(argv[0], long_options, options_descritions, 1);
  122. }
  123. break;
  124. case '2':
  125. if (!get_fft_range(argv[optind - 1], argv[optind], N_POWERS_INTERVALS[1])) {
  126. print_usage(argv[0], long_options, options_descritions, 1);
  127. }
  128. break;
  129. case '3':
  130. if (!get_fft_range(argv[optind - 1], argv[optind], N_POWERS_INTERVALS[2])) {
  131. print_usage(argv[0], long_options, options_descritions, 1);
  132. }
  133. break;
  134. case 'r':
  135. if (!get_number_of_runs(optarg, &N_RUNS)) {
  136. print_usage(argv[0], long_options, options_descritions, 1);
  137. }
  138. break;
  139. case 'o':
  140. strcpy(output_filename, (const char*)optarg);
  141. break;
  142. case 'p':
  143. only_time = 0;
  144. break;
  145. case 'h':
  146. print_usage(argv[0], long_options, options_descritions, 0);
  147. break;
  148. case '?':
  149. print_usage(argv[0], long_options, options_descritions, 1);
  150. }
  151. }
  152. while (next_option != -1);
  153. if (outputType == OUT_NONE) {
  154. outputType = get_output_type_by_measure(NULL);
  155. }
  156. /* Open output file */
  157. FILE *fp;
  158. fp = fopen (output_filename, "w");
  159. #ifdef HAVE_OPENCL
  160. OCL_CHECK_ERROR (clGetPlatformIDs (1, &opencl_platform, NULL));
  161. OCL_CHECK_ERROR (clGetDeviceIDs (opencl_platform, CL_DEVICE_TYPE_ALL, 0, NULL, &opencl_n_devices));
  162. opencl_devices = malloc (opencl_n_devices * sizeof (cl_device_id));
  163. OCL_CHECK_ERROR (clGetDeviceIDs (opencl_platform, CL_DEVICE_TYPE_ALL, opencl_n_devices, opencl_devices, NULL));
  164. opencl_context = clCreateContext (NULL, opencl_n_devices, opencl_devices, NULL, NULL, &err);
  165. OCL_CHECK_ERROR (err);
  166. opencl_queues = malloc (opencl_n_devices * sizeof (cl_command_queue));
  167. for (int i = 0; i < opencl_n_devices; i++) {
  168. opencl_queues[i] = clCreateCommandQueue (opencl_context, opencl_devices[i], 0, &err);
  169. OCL_CHECK_ERROR (err);
  170. }
  171. #endif
  172. #ifdef HAVE_CUDA_FFT
  173. cudaGetDeviceCount(&cuda_n_devices);
  174. #endif
  175. #ifdef HAVE_AMD_FFT
  176. amd_time_entries = (TimeEntry *)malloc(sizeof(TimeEntry) * opencl_n_devices);
  177. if (with_amd_fft) {
  178. printf ("Testing AMD FFT ...\n");
  179. loop_data_opencl ("AMD", compute_amd_fft, opencl_context, opencl_queues, opencl_n_devices, outputType, amd_time_entries);
  180. }
  181. #endif
  182. #ifdef HAVE_APPLE_FFT
  183. apple_time_entries = (TimeEntry *)malloc(sizeof(TimeEntry) * opencl_n_devices);
  184. if (with_apple_fft) {
  185. printf ("Testing Apple FFT ...\n");
  186. loop_data_opencl ("APP", compute_apple_fft, opencl_context, opencl_queues, opencl_n_devices, outputType, apple_time_entries);
  187. }
  188. #endif
  189. #ifdef HAVE_CUDA_FFT
  190. cuda_time_entries = (TimeEntry *)malloc(sizeof(TimeEntry) * cuda_n_devices);
  191. if (with_cuda_fft) {
  192. printf ("Testing CUDA FFT ...\n");
  193. loop_data_cuda ("CUDA", compute_cuda_fft, cuda_n_devices, outputType, cuda_time_entries);
  194. }
  195. #endif
  196. #ifdef HAVE_FFTW
  197. fftw_time_entries = (TimeEntry *)malloc(sizeof(TimeEntry));
  198. if (with_fftw) {
  199. printf ("Testing FFTW3 ...\n");
  200. loop_data_fftw (outputType, &(fftw_time_entries[0]));
  201. }
  202. #endif
  203. /* Write headers */
  204. write_headers_in_file (N_DIMS, only_time, fp);
  205. #ifdef HAVE_AMD_FFT
  206. if (with_amd_fft) {
  207. write_time_entries_in_file (amd_time_entries, opencl_n_devices, N_DIMS, only_time, new_line, fp);
  208. }
  209. #endif
  210. #ifdef HAVE_APPLE_FFT
  211. if (with_apple_fft) {
  212. write_time_entries_in_file (apple_time_entries, opencl_n_devices, N_DIMS, only_time, new_line, fp);
  213. }
  214. #endif
  215. #ifdef HAVE_CUDA_FFT
  216. if (with_cuda_fft) {
  217. write_time_entries_in_file (cuda_time_entries, cuda_n_devices, N_DIMS, only_time, new_line, fp);
  218. }
  219. #endif
  220. #ifdef HAVE_FFTW
  221. if (with_fftw) {
  222. write_time_entries_in_file (fftw_time_entries, 1, N_DIMS, only_time, new_line, fp);
  223. }
  224. #endif
  225. #ifdef HAVE_OPENCL
  226. for (int i = 0; i < opencl_n_devices; i++) {
  227. clReleaseCommandQueue (opencl_queues[i]);
  228. }
  229. clReleaseContext (opencl_context);
  230. free (opencl_queues);
  231. free (opencl_devices);
  232. #endif
  233. fclose (fp);
  234. free(output_filename);
  235. return 0;
  236. }