starpu_perfmodel_recdump.c 12 KB

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