starpu_tasks_rec_complete.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. setenv("STARPU_FXT_TRACE", "0", 1);
  59. if (starpu_init(NULL) != 0)
  60. {
  61. fprintf(stderr, "StarPU initialization failure\n");
  62. exit(EXIT_FAILURE);
  63. }
  64. starpu_pause();
  65. if (argc >= 2)
  66. {
  67. input = fopen(argv[1], "r");
  68. if (!input)
  69. {
  70. fprintf(stderr, "couldn't open %s for read: %s\n", argv[1], strerror(errno));
  71. exit(EXIT_FAILURE);
  72. }
  73. }
  74. else
  75. input = stdin;
  76. if (argc >= 3)
  77. {
  78. output = fopen(argv[2], "w+");
  79. if (!output)
  80. {
  81. fprintf(stderr, "couldn't open %s for write: %s\n", argv[1], strerror(errno));
  82. exit(EXIT_FAILURE);
  83. }
  84. }
  85. else
  86. output = stdout;
  87. while (fgets(s, sizeof(s), input))
  88. {
  89. if (strlen(s) == sizeof(s) - 1)
  90. {
  91. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  92. exit(EXIT_FAILURE);
  93. }
  94. if (s[0] == '\n')
  95. {
  96. /* empty line, end of task */
  97. if (model_name)
  98. {
  99. if (already_there)
  100. {
  101. free(model_name);
  102. }
  103. else
  104. {
  105. /* Try to get already-loaded model */
  106. HASH_FIND_STR(models, model_name, model);
  107. if (model == NULL)
  108. {
  109. model = malloc(sizeof(*model));
  110. model->name = model_name;
  111. memset(&model->model, 0, sizeof(model->model));
  112. model->model.type = STARPU_PERFMODEL_INVALID;
  113. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  114. if (ret == 1)
  115. {
  116. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  117. exit(EXIT_FAILURE);
  118. }
  119. HASH_ADD_STR(models, name, model);
  120. }
  121. else
  122. free(model_name);
  123. fprintf(output, "EstimatedTime: ");
  124. starpu_perfmodel_print_estimations(&model->model, footprint, output);
  125. fprintf(output, "\n");
  126. }
  127. model_name = NULL;
  128. }
  129. already_there = 0;
  130. fprintf(output, "\n");
  131. continue;
  132. }
  133. /* Get rec field name */
  134. c = strchr(s, ':');
  135. if (!c)
  136. {
  137. fprintf(stderr, "odd line '%s'\n", s);
  138. exit(EXIT_FAILURE);
  139. }
  140. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  141. if (!STRHEADCMP(s, "Footprint: "))
  142. {
  143. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  144. }
  145. else if (!STRHEADCMP(s, "Model: "))
  146. {
  147. model_name = strdup(s + strlen("Model: "));
  148. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  149. }
  150. else if (!STRHEADCMP(s, "EstimatedTime: "))
  151. {
  152. already_there = 1;
  153. }
  154. fprintf(output, "%s", s);
  155. }
  156. if (fclose(output))
  157. {
  158. fprintf(stderr, "couldn't close output: %s\n", strerror(errno));
  159. exit(EXIT_FAILURE);
  160. }
  161. starpu_resume();
  162. starpu_shutdown();
  163. HASH_ITER(hh, models, model, tmp)
  164. {
  165. free(model->name);
  166. HASH_DEL(models, model);
  167. }
  168. return 0;
  169. }