starpu_perfmodel_recdump.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013-2014 Université de Bordeaux
  4. * Copyright (C) 2011, 2012, 2013, 2014, 2016 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  19. #include <dirent.h>
  20. #include <sys/stat.h>
  21. #endif
  22. #include <config.h>
  23. #include <assert.h>
  24. #include <getopt.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <starpu.h>
  28. #include <common/utils.h>
  29. #include <common/uthash.h>
  30. #include <core/perfmodel/perfmodel.h> // we need to browse the list associated to history-based models
  31. // just like in starpu_perfmodel_plot
  32. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  33. #if defined(_WIN32) && !defined(__CYGWIN__)
  34. #include <windows.h>
  35. #endif
  36. #define PROGNAME "starpu_perfmodel_recdump"
  37. typedef struct _footprint_list {
  38. struct _footprint_list* next;
  39. uint32_t footprint;
  40. } footprint_list;
  41. footprint_list* add_footprint(footprint_list* list, uint32_t footprint) {
  42. footprint_list * l = list;
  43. while(l) {
  44. if(l->footprint == footprint) break;
  45. l = l->next;
  46. }
  47. if(l) return list;
  48. else {
  49. footprint_list * res = malloc(sizeof(footprint_list));
  50. res->footprint = footprint;
  51. res->next = list;
  52. return res;
  53. }
  54. }
  55. static struct model {
  56. UT_hash_handle hh;
  57. char *name;
  58. struct starpu_perfmodel model;
  59. footprint_list* footprints;
  60. } *models;
  61. void get_comb_name(int comb, char* name, int name_size) {
  62. char* space;
  63. struct starpu_perfmodel_arch *arch_comb = starpu_perfmodel_arch_comb_fetch(comb);
  64. STARPU_ASSERT_MSG(arch_comb->ndevices == 1, "Cannot work with multi-device workers\n");
  65. snprintf(name, name_size, "%s%u", starpu_perfmodel_get_archtype_name(arch_comb->devices[0].type), arch_comb->devices[0].devid);
  66. }
  67. void print_archs(FILE* output) {
  68. int nb_combs = starpu_perfmodel_get_narch_combs();
  69. int nb_workers_per_comb[nb_combs];
  70. nb_combs = starpu_perfmodel_get_narch_combs();
  71. unsigned workerid; int comb;
  72. for(comb = 0; comb < nb_combs; comb++) nb_workers_per_comb[comb] = 0;
  73. fprintf(output, "%%rec: worker_count\n\n");
  74. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  75. {
  76. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(workerid, STARPU_NMAX_SCHED_CTXS);
  77. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  78. nb_workers_per_comb[comb] += 1;
  79. }
  80. for(comb = 0; comb < nb_combs; comb++) {
  81. if(nb_workers_per_comb > 0 ) {
  82. char name[32];
  83. get_comb_name(comb, name, 32);
  84. fprintf(output, "Architecture: %s\n", name);
  85. fprintf(output, "NbWorkers: %d\n\n", nb_workers_per_comb[comb]);
  86. }
  87. }
  88. }
  89. /* output file name */
  90. static char* poutput = NULL;
  91. static char* pinput = NULL;
  92. static void usage()
  93. {
  94. fprintf(stderr, "Dumps perfmodels to a rec file\n\n");
  95. fprintf(stderr, "Usage: %s [ output-file ]\n", PROGNAME);
  96. fprintf(stderr, "\n");
  97. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  98. fprintf(stderr, "\n");
  99. fprintf(stderr, "Report bugs to <"PACKAGE_BUGREPORT">.");
  100. fprintf(stderr, "\n");
  101. }
  102. static void parse_args(int argc, char **argv)
  103. {
  104. int c;
  105. static struct option long_options[] =
  106. {
  107. {"help", no_argument, NULL, 'h'},
  108. {"output", required_argument, NULL, 'o'},
  109. {0, 0, 0, 0}
  110. };
  111. int option_index;
  112. while ((c = getopt_long(argc, argv, "ho:", long_options, &option_index)) != -1)
  113. {
  114. switch (c)
  115. {
  116. case 'h': /* display help */
  117. usage();
  118. exit(EXIT_SUCCESS);
  119. break;
  120. case 'o':
  121. poutput = optarg;
  122. break;
  123. case '?':
  124. default:
  125. fprintf(stderr, "Unrecognized option: -%c\n", optopt);
  126. }
  127. }
  128. if(optind < argc) {
  129. pinput = argv[optind++];
  130. if(optind < argc) {
  131. fprintf(stderr, "Unrecognized argument: %s\n", argv[optind]);
  132. exit(EXIT_FAILURE);
  133. }
  134. }
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. FILE* output;
  139. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  140. WSADATA wsadata;
  141. WSAStartup(MAKEWORD(1,0), &wsadata);
  142. _STARPU_MSG("Listing perfmodels is not implemented on pure Windows yet\n");
  143. return 1;
  144. #else
  145. parse_args(argc, argv);
  146. if(poutput != NULL) {
  147. output = fopen(poutput, "w+");
  148. if (!output)
  149. {
  150. fprintf(stderr, "couldn't open %s for write: %s\n", poutput, strerror(errno));
  151. exit(EXIT_FAILURE);
  152. }
  153. } else {
  154. output = stdout;
  155. }
  156. if (starpu_init(NULL) != 0)
  157. {
  158. fprintf(stderr, "StarPU initialization failure\n");
  159. exit(EXIT_FAILURE);
  160. }
  161. starpu_pause();
  162. if(pinput) {
  163. FILE* input = fopen(pinput, "r");
  164. char s[1024], *c;
  165. struct model *model, *tmp;
  166. uint32_t footprint = 0;
  167. char *model_name = NULL;
  168. int ret;
  169. if (!input) {
  170. fprintf(stderr, "couldn't open %s for read: %s\n", pinput, strerror(errno));
  171. exit(EXIT_FAILURE);
  172. }
  173. while (fgets(s, sizeof(s), input)) {
  174. if (strlen(s) == sizeof(s) - 1)
  175. {
  176. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  177. exit(EXIT_FAILURE);
  178. }
  179. if (s[0] == '\n')
  180. {
  181. /* empty line, end of task */
  182. if (model_name)
  183. {
  184. /* Try to get already-loaded model */
  185. HASH_FIND_STR(models, model_name, model);
  186. if (model == NULL)
  187. {
  188. model = malloc(sizeof(*model));
  189. model->name = model_name;
  190. model->footprints = NULL;
  191. memset(&model->model, 0, sizeof(model->model));
  192. model->model.type = STARPU_PERFMODEL_INVALID;
  193. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  194. if (ret == 1)
  195. {
  196. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  197. exit(EXIT_FAILURE);
  198. }
  199. HASH_ADD_STR(models, name, model);
  200. }
  201. else
  202. free(model_name);
  203. model->footprints = add_footprint(model->footprints, footprint);
  204. model_name = NULL;
  205. }
  206. continue;
  207. }
  208. /* Get rec field name */
  209. c = strchr(s, ':');
  210. if (!c)
  211. {
  212. fprintf(stderr, "odd line '%s'\n", s);
  213. exit(EXIT_FAILURE);
  214. }
  215. if (!STRHEADCMP(s, "Footprint: "))
  216. {
  217. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  218. }
  219. else if (!STRHEADCMP(s, "Model: "))
  220. {
  221. model_name = strdup(s + strlen("Model: "));
  222. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  223. }
  224. }
  225. /* All models loaded */
  226. {
  227. print_archs(output);
  228. fprintf(output, "%%rec: timing\n\n");
  229. int nb_combs = starpu_perfmodel_get_narch_combs();
  230. HASH_ITER(hh, models, model, tmp) {
  231. footprint_list* l = model->footprints, *ltmp;
  232. int comb;
  233. while(l) {
  234. for(comb = 0; comb < nb_combs; comb++) {
  235. char archname[32];
  236. get_comb_name(comb, archname, 32);
  237. if(!model->model.state || model->model.state->nimpls[comb] == 0){
  238. _STARPU_DISP("Symbol %s does not have any implementation on comb %d, not dumping\n", model->name, comb);
  239. continue;
  240. }
  241. if(model->model.state->nimpls[comb] > 1)
  242. _STARPU_DISP("Warning, more than one implementations in comb %d of symbol %s, using only the first one\n", comb, model->name);
  243. struct starpu_perfmodel_per_arch *arch_model = &model->model.state->per_arch[comb][0];
  244. struct starpu_perfmodel_history_list *ptr;
  245. ptr = arch_model->list;
  246. if(!ptr)
  247. _STARPU_DISP("Implementation %d of symbol %s does not have history based model, not dumping\n", comb, model->name);
  248. else while(ptr) {
  249. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  250. if(entry->footprint == l->footprint) {
  251. fprintf(output, "Name: %s\n", model->name);
  252. fprintf(output, "Architecture: %s\n", archname);
  253. fprintf(output, "Footprint: %08x\n", l->footprint);
  254. fprintf(output, "Mean: %-15e\nStddev: %-15e\n",
  255. entry->mean, entry->deviation);
  256. fprintf(output, "\n");
  257. break;
  258. }
  259. ptr=ptr->next;
  260. }
  261. }
  262. ltmp = l->next;
  263. free(l);
  264. l = ltmp;
  265. }
  266. free(model->name);
  267. HASH_DEL(models, model);
  268. }
  269. }
  270. }
  271. else
  272. {
  273. char *path;
  274. DIR *dp;
  275. struct dirent *ep;
  276. path = _starpu_get_perf_model_dir_codelet();
  277. dp = opendir(path);
  278. if (dp != NULL)
  279. {
  280. while ((ep = readdir(dp)))
  281. {
  282. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
  283. int comb, nb_combs;
  284. char* symbol = strdup(ep->d_name);
  285. char *dot = strchr(symbol, '.');
  286. struct starpu_perfmodel model = {.type = STARPU_PERFMODEL_INVALID };
  287. if(dot) *dot = '\0';
  288. STARPU_ASSERT(starpu_perfmodel_load_symbol(symbol, &model) == 0);
  289. if(model.state == NULL)
  290. continue;
  291. nb_combs = starpu_perfmodel_get_narch_combs();
  292. for(comb = 0; comb < nb_combs; ++comb) {
  293. char name[32];
  294. get_comb_name(comb, name, 32);
  295. if(!model.state || model.state->nimpls[comb] == 0) {
  296. _STARPU_DISP("Symbol %s does not have any implementation on comb %d, not dumping\n", symbol, comb);
  297. fprintf(output, "\n");
  298. continue;
  299. }
  300. struct starpu_perfmodel_per_arch *arch_model = &model.state->per_arch[comb][0];
  301. struct starpu_perfmodel_history_list *ptr;
  302. ptr = arch_model->list;
  303. if(!ptr)
  304. _STARPU_DISP("Symbol %s for comb %d does not have history based model, not dumping\n", symbol, comb);
  305. else while(ptr) {
  306. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  307. fprintf(output, "Name: %s\n", symbol);
  308. fprintf(output, "Architecture: %s\n", name);
  309. fprintf(output, "Footprint: %08x\nMean: %-15e\nStddev: %-15e\n",
  310. entry->footprint, entry->mean, entry->deviation);
  311. fprintf(output, "\n");
  312. ptr=ptr->next;
  313. }
  314. }
  315. }
  316. }
  317. closedir (dp);
  318. }
  319. else
  320. {
  321. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  322. }
  323. print_archs(output);
  324. }
  325. starpu_resume();
  326. starpu_shutdown();
  327. return 0;
  328. #endif
  329. }