starpu_tasks_rec_complete.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <common/config.h>
  22. #include <common/uthash.h>
  23. #include <common/utils.h>
  24. #include <starpu.h>
  25. #define PROGNAME "starpu_tasks_rec_complete"
  26. /*
  27. * This program takes a tasks.rec file, and emits a tasks.rec file with
  28. * additional information, notably estimated termination times.
  29. */
  30. static struct model
  31. {
  32. UT_hash_handle hh;
  33. char *name;
  34. struct starpu_perfmodel model;
  35. } *models;
  36. int main(int argc, char *argv[])
  37. {
  38. FILE *input;
  39. FILE *output;
  40. char s[1024], *c;
  41. uint32_t footprint = 0;
  42. int already_there = 0;
  43. char *model_name = NULL;
  44. struct model *model, *tmp=NULL;
  45. int ret;
  46. if (argc >= 2)
  47. {
  48. if (!strcmp(argv[1], "-h") ||
  49. !strcmp(argv[1], "--help"))
  50. {
  51. fprintf(stderr, "Complete a tasks.rec file with additional information, notably estimated termination times.\n");
  52. fprintf(stderr, "\n");
  53. fprintf(stderr, "Usage: %s [input-file [output-file]]\n", PROGNAME);
  54. fprintf(stderr, "\n");
  55. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  56. fprintf(stderr, "\n");
  57. fprintf(stderr, "Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
  58. exit(EXIT_SUCCESS);
  59. }
  60. else if (strncmp(argv[1], "--version", 9) == 0 || strncmp(argv[1], "-v", 2) == 0)
  61. {
  62. fputs(PROGNAME " (" PACKAGE_NAME ") " PACKAGE_VERSION "\n", stderr);
  63. exit(EXIT_FAILURE);
  64. }
  65. }
  66. #ifdef STARPU_HAVE_SETENV
  67. setenv("STARPU_FXT_TRACE", "0", 1);
  68. #endif
  69. if (starpu_init(NULL) != 0)
  70. {
  71. fprintf(stderr, "StarPU initialization failure\n");
  72. exit(EXIT_FAILURE);
  73. }
  74. starpu_pause();
  75. if (argc >= 2)
  76. {
  77. input = fopen(argv[1], "r");
  78. if (!input)
  79. {
  80. fprintf(stderr, "couldn't open %s for read: %s\n", argv[1], strerror(errno));
  81. exit(EXIT_FAILURE);
  82. }
  83. }
  84. else
  85. input = stdin;
  86. if (argc >= 3)
  87. {
  88. output = fopen(argv[2], "w+");
  89. if (!output)
  90. {
  91. fprintf(stderr, "couldn't open %s for write: %s\n", argv[1], strerror(errno));
  92. exit(EXIT_FAILURE);
  93. }
  94. }
  95. else
  96. output = stdout;
  97. while (fgets(s, sizeof(s), input))
  98. {
  99. if (strlen(s) == sizeof(s) - 1)
  100. {
  101. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  102. exit(EXIT_FAILURE);
  103. }
  104. if (s[0] == '\n')
  105. {
  106. /* empty line, end of task */
  107. if (model_name)
  108. {
  109. if (already_there)
  110. {
  111. free(model_name);
  112. }
  113. else
  114. {
  115. /* Try to get already-loaded model */
  116. HASH_FIND_STR(models, model_name, model);
  117. if (model == NULL)
  118. {
  119. _STARPU_MALLOC(model, sizeof(*model));
  120. model->name = model_name;
  121. memset(&model->model, 0, sizeof(model->model));
  122. model->model.type = STARPU_PERFMODEL_INVALID;
  123. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  124. if (ret == 1)
  125. {
  126. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  127. exit(EXIT_FAILURE);
  128. }
  129. HASH_ADD_STR(models, name, model);
  130. }
  131. else
  132. free(model_name);
  133. fprintf(output, "EstimatedTime: ");
  134. starpu_perfmodel_print_estimations(&model->model, footprint, output);
  135. fprintf(output, "\n");
  136. }
  137. model_name = NULL;
  138. }
  139. already_there = 0;
  140. fprintf(output, "\n");
  141. continue;
  142. }
  143. /* Get rec field name */
  144. c = strchr(s, ':');
  145. if (!c)
  146. {
  147. fprintf(stderr, "odd line '%s'\n", s);
  148. exit(EXIT_FAILURE);
  149. }
  150. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  151. if (!STRHEADCMP(s, "Footprint: "))
  152. {
  153. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  154. }
  155. else if (!STRHEADCMP(s, "Model: "))
  156. {
  157. model_name = strdup(s + strlen("Model: "));
  158. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  159. }
  160. else if (!STRHEADCMP(s, "EstimatedTime: "))
  161. {
  162. already_there = 1;
  163. }
  164. fprintf(output, "%s", s);
  165. }
  166. if (fclose(input))
  167. {
  168. fprintf(stderr, "couldn't close input: %s\n", strerror(errno));
  169. exit(EXIT_FAILURE);
  170. }
  171. if (fclose(output))
  172. {
  173. fprintf(stderr, "couldn't close output: %s\n", strerror(errno));
  174. exit(EXIT_FAILURE);
  175. }
  176. starpu_resume();
  177. HASH_ITER(hh, models, model, tmp)
  178. {
  179. HASH_DEL(models, model);
  180. starpu_perfmodel_unload_model(&model->model);
  181. free(model->name);
  182. free(model);
  183. }
  184. starpu_shutdown();
  185. return 0;
  186. }