starpu_tasks_rec_complete.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014, 2016 Universite de Bordeaux
  4. * Copyright (C) 2012-2015 CNRS
  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. #include <config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <common/uthash.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. UT_hash_handle hh;
  32. char *name;
  33. struct starpu_perfmodel model;
  34. } *models;
  35. int main(int argc, char *argv[]) {
  36. FILE *input;
  37. FILE *output;
  38. char s[1024], *c;
  39. uint32_t footprint = 0;
  40. int already_there = 0;
  41. char *model_name = NULL;
  42. struct model *model, *tmp;
  43. int ret;
  44. if (argc >= 2) {
  45. if (!strcmp(argv[1], "-h") ||
  46. !strcmp(argv[1], "--help"))
  47. {
  48. fprintf(stderr, "Complete a tasks.rec file with additional information, notably estimated termination times.\n");
  49. fprintf(stderr, "\n");
  50. fprintf(stderr, "Usage: %s [input-file [output-file]]\n", PROGNAME);
  51. fprintf(stderr, "\n");
  52. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  53. fprintf(stderr, "\n");
  54. fprintf(stderr, "Report bugs to <" PACKAGE_BUGREPORT ">.\n");
  55. exit(EXIT_SUCCESS);
  56. }
  57. }
  58. #ifdef STARPU_HAVE_SETENV
  59. setenv("STARPU_FXT_TRACE", "0", 1);
  60. #endif
  61. if (starpu_init(NULL) != 0)
  62. {
  63. fprintf(stderr, "StarPU initialization failure\n");
  64. exit(EXIT_FAILURE);
  65. }
  66. starpu_pause();
  67. if (argc >= 2)
  68. {
  69. input = fopen(argv[1], "r");
  70. if (!input)
  71. {
  72. fprintf(stderr, "couldn't open %s for read: %s\n", argv[1], strerror(errno));
  73. exit(EXIT_FAILURE);
  74. }
  75. }
  76. else
  77. input = stdin;
  78. if (argc >= 3)
  79. {
  80. output = fopen(argv[2], "w+");
  81. if (!output)
  82. {
  83. fprintf(stderr, "couldn't open %s for write: %s\n", argv[1], strerror(errno));
  84. exit(EXIT_FAILURE);
  85. }
  86. }
  87. else
  88. output = stdout;
  89. while (fgets(s, sizeof(s), input))
  90. {
  91. if (strlen(s) == sizeof(s) - 1)
  92. {
  93. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  94. exit(EXIT_FAILURE);
  95. }
  96. if (s[0] == '\n')
  97. {
  98. /* empty line, end of task */
  99. if (model_name)
  100. {
  101. if (already_there)
  102. {
  103. free(model_name);
  104. }
  105. else
  106. {
  107. /* Try to get already-loaded model */
  108. HASH_FIND_STR(models, model_name, model);
  109. if (model == NULL)
  110. {
  111. model = malloc(sizeof(*model));
  112. model->name = model_name;
  113. memset(&model->model, 0, sizeof(model->model));
  114. model->model.type = STARPU_PERFMODEL_INVALID;
  115. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  116. if (ret == 1)
  117. {
  118. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  119. exit(EXIT_FAILURE);
  120. }
  121. HASH_ADD_STR(models, name, model);
  122. }
  123. else
  124. free(model_name);
  125. fprintf(output, "EstimatedTime: ");
  126. starpu_perfmodel_print_estimations(&model->model, footprint, output);
  127. fprintf(output, "\n");
  128. }
  129. model_name = NULL;
  130. }
  131. already_there = 0;
  132. fprintf(output, "\n");
  133. continue;
  134. }
  135. /* Get rec field name */
  136. c = strchr(s, ':');
  137. if (!c)
  138. {
  139. fprintf(stderr, "odd line '%s'\n", s);
  140. exit(EXIT_FAILURE);
  141. }
  142. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  143. if (!STRHEADCMP(s, "Footprint: "))
  144. {
  145. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  146. }
  147. else if (!STRHEADCMP(s, "Model: "))
  148. {
  149. model_name = strdup(s + strlen("Model: "));
  150. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  151. }
  152. else if (!STRHEADCMP(s, "EstimatedTime: "))
  153. {
  154. already_there = 1;
  155. }
  156. fprintf(output, "%s", s);
  157. }
  158. if (fclose(output))
  159. {
  160. fprintf(stderr, "couldn't close output: %s\n", strerror(errno));
  161. exit(EXIT_FAILURE);
  162. }
  163. starpu_resume();
  164. starpu_shutdown();
  165. HASH_ITER(hh, models, model, tmp)
  166. {
  167. free(model->name);
  168. HASH_DEL(models, model);
  169. }
  170. return 0;
  171. }