utilities.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "utilities.h"
  2. int N_RUNS = 4;
  3. const int DIMS[N_DIMS] = {1, 2, 3};
  4. int N_POWERS_INTERVALS[N_DIMS][2] = {{5, 11}, {8, 11}, {7, 7}};
  5. void write_headers_in_file (int n_dims, int only_time, FILE *fp)
  6. {
  7. fprintf (fp, "# ");
  8. for (int i = 0; i < n_dims; i++) {
  9. int min_power = N_POWERS_INTERVALS[i][0];
  10. int max_power = N_POWERS_INTERVALS[i][1];
  11. for (int j = min_power; j <= max_power; j++) {
  12. int side_size = pow(2,j);
  13. switch (DIMS[i]) {
  14. case 1:
  15. fprintf (fp, ";");
  16. fprintf (fp, "%d ", side_size);
  17. if (!only_time) {
  18. fprintf (fp, ";");
  19. fprintf (fp, "%d(Error) ", side_size);
  20. }
  21. break;
  22. case 2:
  23. fprintf (fp, ";");
  24. fprintf (fp, "%dx%d ", side_size, side_size);
  25. if (!only_time) {
  26. fprintf (fp, ";");
  27. fprintf (fp, "%dx%d(Error) ", side_size, side_size);
  28. }
  29. break;
  30. case 3:
  31. fprintf (fp, ";");
  32. fprintf (fp, "%dx%dx%d ", side_size, side_size, side_size);
  33. if (!only_time) {
  34. fprintf (fp, ";");
  35. fprintf (fp, "%dx%dx%d(Error) ", side_size, side_size, side_size);
  36. }
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. void write_time_entries_in_file (TimeEntry* time_entries, int num_entries, int n_dims, int only_time, bool new_line, FILE *fp)
  43. {
  44. if (new_line) {
  45. fprintf (fp, "\n");
  46. }
  47. for (int i = 0; i < num_entries; i++) {
  48. fprintf (fp, "%s ", time_entries[i].lib_name);
  49. DimEntry *dim_entries = time_entries[i].dim_entries;
  50. for (int dim = 0; dim < n_dims; dim++) {
  51. DimEntry dim_entry = dim_entries[dim];
  52. for (int j = 0; j < (N_POWERS_INTERVALS[dim][1] - N_POWERS_INTERVALS[dim][0] + 1); j++) {
  53. if (only_time) {
  54. fprintf (fp, ";");
  55. fprintf (fp, "%f", dim_entry.times[j]);
  56. }
  57. else {
  58. fprintf (fp, ";%f;%f ", dim_entry.times[j], dim_entry.errors[j]);
  59. }
  60. }
  61. }
  62. if (i != num_entries -1) {
  63. fprintf (fp, "\n");
  64. }
  65. }
  66. }
  67. OutputType get_output_type_by_measure(char *measure) {
  68. OutputType outputType = OUT_MILLISECONDS;
  69. bool invalid_format = false;
  70. if (measure != NULL) {
  71. if (!strcmp(optarg, "ms"))
  72. outputType = OUT_MILLISECONDS;
  73. else if (!strcmp(optarg, "sec"))
  74. outputType = OUT_SECONDS;
  75. else if (!strcmp(optarg, "gflops"))
  76. outputType = OUT_GFLOPS;
  77. else if (!strcmp(optarg, "mflops"))
  78. outputType = OUT_MFLOPS;
  79. else if (!strcmp(optarg, "MBs") || !strcmp(optarg, "mbs"))
  80. outputType = OUT_THROUGHTPUT_MBS;
  81. else if (!strcmp(optarg, "GBs") || !strcmp(optarg, "gbs"))
  82. outputType = OUT_THROUGHTPUT_GBS;
  83. else {
  84. invalid_format = true;
  85. fprintf (stderr, "Incorrect measure format [%s], [ms] will be used.\nUse the following formats: ms,sec,mflops,gflops,GBs,MBs.\n\n", measure);
  86. }
  87. }
  88. if (!invalid_format || (measure == NULL)) {
  89. fprintf (stdout, "Output will be use measure [%s].\n\n", measure == NULL ? "ms" : measure);
  90. }
  91. return outputType;
  92. }
  93. bool get_fft_range(char *val1, char *val2, int *out_range) {
  94. int out_val1;
  95. int out_val2;
  96. bool range_is_valid = true;
  97. out_val1 = (int) strtol (val1, NULL, 10);
  98. out_val2 = (int) strtol (val2, NULL, 10);
  99. if (out_val1 != 0L &&
  100. out_val2 != 0L &&
  101. (out_val1 >= MIN_POW2 && out_val1 <= MAX_POW2) &&
  102. (out_val2 >= MIN_POW2 && out_val2 <= MAX_POW2) &&
  103. (out_val1 < out_val2)) {
  104. out_range[0] = out_val1;
  105. out_range[1] = out_val2;
  106. }
  107. else {
  108. fprintf (stderr, "Incorrect the range of powers, it should be in range [%d %d].\n\n", MIN_POW2, MAX_POW2);
  109. range_is_valid = false;
  110. }
  111. return range_is_valid;
  112. }
  113. bool get_number_of_runs(char *val, int *out) {
  114. bool value_valid = true;
  115. int out_val;
  116. out_val = (int) strtol (val, NULL, 10);
  117. if (out_val != 0L && (out_val >= MIN_RUNS && out_val <= MAX_RUNS)) {
  118. *out = out_val;
  119. }
  120. else {
  121. fprintf (stderr, "Incorrect the number of runs, the value should be from %d to %d.\n\n", MIN_RUNS, MAX_RUNS);
  122. value_valid = false;
  123. }
  124. return value_valid;
  125. }
  126. void print_usage(char *app_name, struct option long_options[], char **options_descritions, int exit_code)
  127. {
  128. printf ("Usage: %s [OPTIONS]\n", app_name);
  129. printf ("Options:\n");
  130. for (int i = 0; long_options[i].name != 0; i++) {
  131. printf(" --%-20s %s\n", long_options[i].name, options_descritions[i]);
  132. }
  133. exit (exit_code);
  134. }
  135. double get_measurements_with_format (OutputType outputType, size_t size_bytes, double time_sec)
  136. {
  137. double out_result = -1;
  138. if (outputType == OUT_MFLOPS) {
  139. size_t size = size_bytes / 2 / sizeof (float);
  140. out_result = 5 * size * log (size) / log (2) / (time_sec / 1000.0);
  141. }
  142. else if (outputType == OUT_GFLOPS) {
  143. out_result = -1;
  144. }
  145. else if (outputType == OUT_THROUGHTPUT_MBS) {
  146. out_result = ((double)size_bytes) / time_sec / 1000.0 / 1000.0;
  147. }
  148. else if (outputType == OUT_THROUGHTPUT_GBS) {
  149. out_result = ((double)size_bytes) / time_sec / 1000.0 / 1000.0 / 1000.0;
  150. }
  151. else if (outputType == OUT_MILLISECONDS) {
  152. out_result = time_sec * 1000.0;
  153. }
  154. else if (outputType == OUT_SECONDS) {
  155. out_result = time_sec * 1000.0;
  156. }
  157. else {
  158. fprintf (stderr, "Unknown output type of OpenCL routines!\n");
  159. }
  160. return out_result;
  161. }
  162. void get_timestamp (char **ts) {
  163. time_t rawtime;
  164. struct tm *timeinfo;
  165. time (&rawtime);
  166. timeinfo = localtime (&rawtime);
  167. *ts = (char *) malloc(sizeof(char) * 30);
  168. strftime (*ts, 30, "%d%m%Y%H%M%S", timeinfo);
  169. }