perfmodel_history.c 14 KB

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