perfmodel_history.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <unistd.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <common/config.h>
  21. #include <core/perfmodel/perfmodel.h>
  22. #include <core/jobs.h>
  23. #include <core/workers.h>
  24. #include <starpu-mutex.h>
  25. #include <datawizard/datawizard.h>
  26. #include <core/perfmodel/regression.h>
  27. #include <common/config.h>
  28. /*
  29. * History based model
  30. */
  31. static void insert_history_entry(struct starpu_history_entry_t *entry, struct starpu_history_list_t **list, struct starpu_htbl32_node_s **history_ptr)
  32. {
  33. struct starpu_history_list_t *link;
  34. struct starpu_history_entry_t *old;
  35. link = malloc(sizeof(struct starpu_history_list_t));
  36. link->next = *list;
  37. link->entry = entry;
  38. *list = link;
  39. old = htbl_insert_32(history_ptr, entry->footprint, entry);
  40. /* that may fail in case there is some concurrency issue */
  41. STARPU_ASSERT(old == NULL);
  42. }
  43. static void dump_reg_model(FILE *f, struct starpu_regression_model_t *reg_model)
  44. {
  45. fprintf(f, "%le\t%le\t%le\t%le\t%le\t%le\t%d\n", reg_model->sumlnx, reg_model->sumlnx2, reg_model->sumlny, reg_model->sumlnxlny, reg_model->alpha, reg_model->beta, reg_model->nsample);
  46. }
  47. static void scan_reg_model(FILE *f, struct starpu_regression_model_t *reg_model)
  48. {
  49. int res;
  50. res = fscanf(f, "%le\t%le\t%le\t%le\t%le\t%le\t%d\n", &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny, &reg_model->sumlnxlny, &reg_model->alpha, &reg_model->beta, &reg_model->nsample);
  51. STARPU_ASSERT(res == 7);
  52. }
  53. static void dump_history_entry(FILE *f, struct starpu_history_entry_t *entry)
  54. {
  55. fprintf(f, "%x\t%zu\t%le\t%le\t%le\t%le\t%d\n", entry->footprint, entry->size, entry->mean, entry->deviation, entry->sum, entry->sum2, entry->nsample);
  56. }
  57. static void scan_history_entry(FILE *f, struct starpu_history_entry_t *entry)
  58. {
  59. int res;
  60. res = fscanf(f, "%x\t%zu\t%le\t%le\t%le\t%le\t%d\n", &entry->footprint, &entry->size, &entry->mean, &entry->deviation, &entry->sum, &entry->sum2, &entry->nsample);
  61. STARPU_ASSERT(res == 7);
  62. }
  63. static void parse_per_arch_model_file(FILE *f, struct starpu_per_arch_perfmodel_t *per_arch_model, unsigned scan_history)
  64. {
  65. unsigned nentries;
  66. int res = fscanf(f, "%d\n", &nentries);
  67. STARPU_ASSERT(res == 1);
  68. scan_reg_model(f, &per_arch_model->regression);
  69. res = fscanf(f, "%le\t%le\t%le\n",
  70. &per_arch_model->regression.a,
  71. &per_arch_model->regression.b,
  72. &per_arch_model->regression.c);
  73. STARPU_ASSERT(res == 3);
  74. if (isnan(per_arch_model->regression.a)||isnan(per_arch_model->regression.b)||isnan(per_arch_model->regression.c))
  75. {
  76. per_arch_model->regression.valid = 0;
  77. }
  78. else {
  79. per_arch_model->regression.valid = 1;
  80. }
  81. if (!scan_history)
  82. return;
  83. /* parse core entries */
  84. unsigned i;
  85. for (i = 0; i < nentries; i++) {
  86. struct starpu_history_entry_t *entry = malloc(sizeof(struct starpu_history_entry_t));
  87. STARPU_ASSERT(entry);
  88. scan_history_entry(f, entry);
  89. /* insert the entry in the hashtable and the list structures */
  90. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  91. }
  92. }
  93. static void parse_model_file(FILE *f, struct starpu_perfmodel_t *model, unsigned scan_history)
  94. {
  95. parse_per_arch_model_file(f, &model->per_arch[STARPU_CORE_DEFAULT], scan_history);
  96. parse_per_arch_model_file(f, &model->per_arch[STARPU_CUDA_DEFAULT], scan_history);
  97. }
  98. static void dump_per_arch_model_file(FILE *f, struct starpu_per_arch_perfmodel_t *per_arch_model)
  99. {
  100. /* count the number of elements in the lists */
  101. struct starpu_history_list_t *ptr;
  102. unsigned nentries = 0;
  103. ptr = per_arch_model->list;
  104. while(ptr) {
  105. nentries++;
  106. ptr = ptr->next;
  107. }
  108. /* header */
  109. fprintf(f, "%d\n", nentries);
  110. dump_reg_model(f, &per_arch_model->regression);
  111. double a,b,c;
  112. regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  113. fprintf(f, "%le\t%le\t%le\n", a, b, c);
  114. ptr = per_arch_model->list;
  115. while (ptr) {
  116. //memcpy(&entries_array[i++], ptr->entry, sizeof(struct starpu_history_entry_t));
  117. dump_history_entry(f, ptr->entry);
  118. ptr = ptr->next;
  119. }
  120. }
  121. static void dump_model_file(FILE *f, struct starpu_perfmodel_t *model)
  122. {
  123. dump_per_arch_model_file(f, &model->per_arch[STARPU_CORE_DEFAULT]);
  124. dump_per_arch_model_file(f, &model->per_arch[STARPU_CUDA_DEFAULT]);
  125. }
  126. static void initialize_per_arch_model(struct starpu_per_arch_perfmodel_t *per_arch_model)
  127. {
  128. per_arch_model->history = NULL;
  129. per_arch_model->list = NULL;
  130. }
  131. static void initialize_model(struct starpu_perfmodel_t *model)
  132. {
  133. initialize_per_arch_model(&model->per_arch[STARPU_CORE_DEFAULT]);
  134. initialize_per_arch_model(&model->per_arch[STARPU_CUDA_DEFAULT]);
  135. }
  136. static struct starpu_model_list_t *registered_models = NULL;
  137. //static unsigned debug_modelid = 0;
  138. #ifdef MODEL_DEBUG
  139. static void get_model_debug_path(struct starpu_perfmodel_t *model, const char *arch, char *path, size_t maxlen)
  140. {
  141. strncpy(path, PERF_MODEL_DIR, maxlen);
  142. strncat(path, model->symbol, maxlen);
  143. char hostname[32];
  144. gethostname(hostname, 32);
  145. strncat(path, ".", maxlen);
  146. strncat(path, hostname, maxlen);
  147. strncat(path, ".", maxlen);
  148. strncat(path, arch, maxlen);
  149. strncat(path, ".debug", maxlen);
  150. }
  151. #endif
  152. void register_model(struct starpu_perfmodel_t *model)
  153. {
  154. /* add the model to a linked list */
  155. struct starpu_model_list_t *node = malloc(sizeof(struct starpu_model_list_t));
  156. node->model = model;
  157. //model->debug_modelid = debug_modelid++;
  158. /* put this model at the beginning of the list */
  159. node->next = registered_models;
  160. registered_models = node;
  161. #ifdef MODEL_DEBUG
  162. char debugpath[256];
  163. get_model_debug_path(model, "cuda", debugpath, 256);
  164. model->per_arch[STARPU_CUDA_DEFAULT].debug_file = fopen(debugpath, "a+");
  165. STARPU_ASSERT(model->per_arch[STARPU_CUDA_DEFAULT].debug_file);
  166. get_model_debug_path(model, "core", debugpath, 256);
  167. model->per_arch[STARPU_CORE_DEFAULT].debug_file = fopen(debugpath, "a+");
  168. STARPU_ASSERT(model->per_arch[STARPU_CORE_DEFAULT].debug_file);
  169. #endif
  170. return;
  171. }
  172. static void get_model_path(struct starpu_perfmodel_t *model, char *path, size_t maxlen)
  173. {
  174. strncpy(path, PERF_MODEL_DIR, maxlen);
  175. strncat(path, model->symbol, maxlen);
  176. char hostname[32];
  177. gethostname(hostname, 32);
  178. strncat(path, ".", maxlen);
  179. strncat(path, hostname, maxlen);
  180. }
  181. void save_history_based_model(struct starpu_perfmodel_t *model)
  182. {
  183. STARPU_ASSERT(model);
  184. STARPU_ASSERT(model->symbol);
  185. /* TODO checks */
  186. /* filename = $PERF_MODEL_DIR/symbol.hostname */
  187. char path[256];
  188. get_model_path(model, path, 256);
  189. #ifdef VERBOSE
  190. fprintf(stderr, "Opening performance model file %s for model %s\n", path, model->symbol);
  191. #endif
  192. /* overwrite existing file, or create it */
  193. FILE *f;
  194. f = fopen(path, "w+");
  195. STARPU_ASSERT(f);
  196. dump_model_file(f, model);
  197. fclose(f);
  198. #ifdef DEBUG_MODEL
  199. fclose(model->cuda_debug_file);
  200. fclose(model->core_debug_file);
  201. #endif
  202. }
  203. void dump_registered_models(void)
  204. {
  205. struct starpu_model_list_t *node;
  206. node = registered_models;
  207. #ifdef VERBOSE
  208. fprintf(stderr, "DUMP MODELS !\n");
  209. #endif
  210. while (node) {
  211. save_history_based_model(node->model);
  212. node = node->next;
  213. /* XXX free node */
  214. }
  215. }
  216. static int directory_existence_was_tested = 0;
  217. static void create_sampling_directory_if_needed(void)
  218. {
  219. /* Testing if a directory exists and creating it otherwise
  220. may not be safe: it is possible that the permission are
  221. changed in between. Instead, we create it and check if
  222. it already existed before */
  223. int ret;
  224. ret = mkdir(PERF_MODEL_DIR, S_IRWXU);
  225. if (ret == -1)
  226. {
  227. STARPU_ASSERT(errno == EEXIST);
  228. /* make sure that it is actually a directory */
  229. struct stat sb;
  230. stat(PERF_MODEL_DIR, &sb);
  231. STARPU_ASSERT(S_ISDIR(sb.st_mode));
  232. }
  233. }
  234. void load_history_based_model(struct starpu_perfmodel_t *model, unsigned scan_history)
  235. {
  236. STARPU_ASSERT(model);
  237. STARPU_ASSERT(model->symbol);
  238. /* XXX we assume the lock is implicitely initialized (taken = 0) */
  239. //init_mutex(&model->model_mutex);
  240. take_mutex(&model->model_mutex);
  241. /* perhaps some other thread got in before ... */
  242. if (!model->is_loaded)
  243. {
  244. /* make sure the performance model directory exists (or create it) */
  245. if (!directory_existence_was_tested)
  246. {
  247. create_sampling_directory_if_needed();
  248. directory_existence_was_tested = 1;
  249. }
  250. /*
  251. * We need to keep track of all the model that were opened so that we can
  252. * possibly update them at runtime termination ...
  253. */
  254. register_model(model);
  255. char path[256];
  256. get_model_path(model, path, 256);
  257. #ifdef VERBOSE
  258. fprintf(stderr, "Opening performance model file %s for model %s\n", path, model->symbol);
  259. #endif
  260. /* try to open an existing file and load it */
  261. int res;
  262. res = access(path, F_OK);
  263. if (res == 0) {
  264. // fprintf(stderr, "File exists !\n");
  265. FILE *f;
  266. f = fopen(path, "r");
  267. STARPU_ASSERT(f);
  268. parse_model_file(f, model, scan_history);
  269. fclose(f);
  270. }
  271. else {
  272. //fprintf(stderr, "File does not exists !\n");
  273. initialize_model(model);
  274. }
  275. if (starpu_get_env_number("CALIBRATE") != -1)
  276. {
  277. fprintf(stderr, "CALIBRATE model %s\n", model->symbol);
  278. model->benchmarking = 1;
  279. }
  280. else {
  281. model->benchmarking = 0;
  282. }
  283. model->is_loaded = 1;
  284. }
  285. release_mutex(&model->model_mutex);
  286. }
  287. double regression_based_job_expected_length(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct job_s *j)
  288. {
  289. double exp = -1.0;
  290. size_t size = job_get_data_size(j);
  291. struct starpu_regression_model_t *regmodel;
  292. if (!model->is_loaded)
  293. load_history_based_model(model, 0);
  294. regmodel = &model->per_arch[arch].regression;
  295. if (regmodel->valid)
  296. exp = regmodel->a*pow(size, regmodel->b) + regmodel->c;
  297. return exp;
  298. }
  299. double history_based_job_expected_length(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct job_s *j)
  300. {
  301. double exp;
  302. struct starpu_per_arch_perfmodel_t *per_arch_model;
  303. struct starpu_history_entry_t *entry;
  304. struct starpu_htbl32_node_s *history;
  305. if (!model->is_loaded)
  306. load_history_based_model(model, 1);
  307. if (!j->footprint_is_computed)
  308. compute_buffers_footprint(j);
  309. uint32_t key = j->footprint;
  310. per_arch_model = &model->per_arch[arch];
  311. history = per_arch_model->history;
  312. if (!history)
  313. return -1.0;
  314. take_mutex(&model->model_mutex);
  315. entry = htbl_search_32(history, key);
  316. release_mutex(&model->model_mutex);
  317. exp = entry?entry->mean:-1.0;
  318. return exp;
  319. }
  320. void update_perfmodel_history(job_t j, enum starpu_perf_archtype arch, double measured)
  321. {
  322. struct starpu_perfmodel_t *model = j->task->cl->model;
  323. if (model)
  324. {
  325. struct starpu_per_arch_perfmodel_t *per_arch_model = &model->per_arch[arch];
  326. if (model->type == HISTORY_BASED || model->type == REGRESSION_BASED)
  327. {
  328. uint32_t key = j->footprint;
  329. struct starpu_history_entry_t *entry;
  330. struct starpu_htbl32_node_s *history;
  331. struct starpu_htbl32_node_s **history_ptr;
  332. struct starpu_regression_model_t *reg_model;
  333. struct starpu_history_list_t **list;
  334. history = per_arch_model->history;
  335. history_ptr = &per_arch_model->history;
  336. reg_model = &per_arch_model->regression;
  337. list = &per_arch_model->list;
  338. take_mutex(&model->model_mutex);
  339. entry = htbl_search_32(history, key);
  340. if (!entry)
  341. {
  342. /* this is the first entry with such a footprint */
  343. entry = malloc(sizeof(struct starpu_history_entry_t));
  344. STARPU_ASSERT(entry);
  345. entry->mean = measured;
  346. entry->sum = measured;
  347. entry->deviation = 0.0;
  348. entry->sum2 = measured*measured;
  349. entry->size = job_get_data_size(j);
  350. entry->footprint = key;
  351. entry->nsample = 1;
  352. insert_history_entry(entry, list, history_ptr);
  353. }
  354. else {
  355. /* there is already some entry with the same footprint */
  356. entry->sum += measured;
  357. entry->sum2 += measured*measured;
  358. entry->nsample++;
  359. unsigned n = entry->nsample;
  360. entry->mean = entry->sum / n;
  361. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  362. }
  363. STARPU_ASSERT(entry);
  364. /* update the regression model as well */
  365. double logy, logx;
  366. logx = logl(entry->size);
  367. logy = logl(measured);
  368. reg_model->sumlnx += logx;
  369. reg_model->sumlnx2 += logx*logx;
  370. reg_model->sumlny += logy;
  371. reg_model->sumlnxlny += logx*logy;
  372. reg_model->nsample++;
  373. unsigned n = reg_model->nsample;
  374. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  375. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  376. reg_model->beta = num/denom;
  377. reg_model->alpha = expl((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  378. release_mutex(&model->model_mutex);
  379. }
  380. #ifdef MODEL_DEBUG
  381. FILE * debug_file = per_arch_model->debug_file;
  382. take_mutex(&model->model_mutex);
  383. fprintf(debug_file, "%lf\t", measured);
  384. unsigned i;
  385. struct starpu_task *task = j->task;
  386. for (i = 0; i < task->cl->nbuffers; i++)
  387. {
  388. data_state *state = task->buffers[i].state;
  389. STARPU_ASSERT(state->ops);
  390. STARPU_ASSERT(state->ops->display);
  391. state->ops->display(state, debug_file);
  392. }
  393. fprintf(debug_file, "\n");
  394. release_mutex(&model->model_mutex);
  395. #endif
  396. }
  397. }