starpu_perfmodel_recdump.c 11 KB

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