perfmodel_history.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. unsigned have_to_load;
  246. have_to_load = __sync_bool_compare_and_swap (&model->is_loaded,
  247. STARPU_PERFMODEL_NOT_LOADED,
  248. STARPU_PERFMODEL_LOADING);
  249. if (!have_to_load)
  250. {
  251. /* someone is already loading the model, we wait until it's finished */
  252. while (model->is_loaded != STARPU_PERFMODEL_LOADED)
  253. {
  254. __sync_synchronize();
  255. }
  256. return;
  257. }
  258. //init_mutex(&model->model_mutex);
  259. pthread_spin_init(&model->model_mutex, 0);
  260. pthread_spin_lock(&model->model_mutex);
  261. /* make sure the performance model directory exists (or create it) */
  262. if (!directory_existence_was_tested)
  263. {
  264. create_sampling_directory_if_needed();
  265. directory_existence_was_tested = 1;
  266. }
  267. /*
  268. * We need to keep track of all the model that were opened so that we can
  269. * possibly update them at runtime termination ...
  270. */
  271. register_model(model);
  272. char path[256];
  273. get_model_path(model, path, 256);
  274. #ifdef VERBOSE
  275. fprintf(stderr, "Opening performance model file %s for model %s\n", path, model->symbol);
  276. #endif
  277. /* try to open an existing file and load it */
  278. int res;
  279. res = access(path, F_OK);
  280. if (res == 0) {
  281. // fprintf(stderr, "File exists !\n");
  282. FILE *f;
  283. f = fopen(path, "r");
  284. STARPU_ASSERT(f);
  285. parse_model_file(f, model, scan_history);
  286. fclose(f);
  287. }
  288. else {
  289. //fprintf(stderr, "File does not exists !\n");
  290. initialize_model(model);
  291. }
  292. if (starpu_get_env_number("CALIBRATE") != -1)
  293. {
  294. fprintf(stderr, "CALIBRATE model %s\n", model->symbol);
  295. model->benchmarking = 1;
  296. }
  297. else {
  298. model->benchmarking = 0;
  299. }
  300. model->is_loaded = STARPU_PERFMODEL_LOADED;
  301. pthread_spin_unlock(&model->model_mutex);
  302. }
  303. /* This function is intended to be used by external tools that should read the
  304. * performance model files */
  305. int starpu_load_history_debug(const char *symbol, struct starpu_perfmodel_t *model)
  306. {
  307. model->symbol = symbol;
  308. /* where is the file if it exists ? */
  309. char path[256];
  310. get_model_path(model, path, 256);
  311. // fprintf(stderr, "get_model_path -> %s\n", path);
  312. /* does it exist ? */
  313. int res;
  314. res = access(path, F_OK);
  315. if (res) {
  316. fprintf(stderr, "There is no performance model for symbol %s\n", symbol);
  317. return 1;
  318. }
  319. FILE *f = fopen(path, "r");
  320. STARPU_ASSERT(f);
  321. parse_model_file(f, model, 1);
  322. return 0;
  323. }
  324. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel_t *model,
  325. enum starpu_perf_archtype arch, char **path, size_t maxlen)
  326. {
  327. char *archname;
  328. STARPU_ASSERT(path);
  329. switch(arch) {
  330. case STARPU_CORE_DEFAULT:
  331. archname = "core";
  332. break;
  333. case STARPU_CUDA_DEFAULT:
  334. archname = "cuda";
  335. break;
  336. case STARPU_GORDON_DEFAULT:
  337. archname = "gordon";
  338. break;
  339. default:
  340. /* unknown architecture */
  341. *path = NULL;
  342. return;
  343. }
  344. get_model_debug_path(model, archname, *path, maxlen);
  345. }
  346. double regression_based_job_expected_length(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct job_s *j)
  347. {
  348. double exp = -1.0;
  349. size_t size = job_get_data_size(j);
  350. struct starpu_regression_model_t *regmodel;
  351. if (STARPU_UNLIKELY(model->is_loaded != STARPU_PERFMODEL_LOADED))
  352. load_history_based_model(model, 0);
  353. regmodel = &model->per_arch[arch].regression;
  354. if (regmodel->valid)
  355. exp = regmodel->a*pow(size, regmodel->b) + regmodel->c;
  356. return exp;
  357. }
  358. double history_based_job_expected_length(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct job_s *j)
  359. {
  360. double exp;
  361. struct starpu_per_arch_perfmodel_t *per_arch_model;
  362. struct starpu_history_entry_t *entry;
  363. struct starpu_htbl32_node_s *history;
  364. if (STARPU_UNLIKELY(model->is_loaded != STARPU_PERFMODEL_LOADED))
  365. load_history_based_model(model, 1);
  366. if (STARPU_UNLIKELY(!j->footprint_is_computed))
  367. compute_buffers_footprint(j);
  368. uint32_t key = j->footprint;
  369. per_arch_model = &model->per_arch[arch];
  370. history = per_arch_model->history;
  371. if (!history)
  372. return -1.0;
  373. pthread_spin_lock(&model->model_mutex);
  374. entry = htbl_search_32(history, key);
  375. pthread_spin_unlock(&model->model_mutex);
  376. exp = entry?entry->mean:-1.0;
  377. return exp;
  378. }
  379. void update_perfmodel_history(job_t j, enum starpu_perf_archtype arch, unsigned cpuid, double measured)
  380. {
  381. struct starpu_perfmodel_t *model = j->task->cl->model;
  382. if (model)
  383. {
  384. struct starpu_per_arch_perfmodel_t *per_arch_model = &model->per_arch[arch];
  385. if (model->type == HISTORY_BASED || model->type == REGRESSION_BASED)
  386. {
  387. uint32_t key = j->footprint;
  388. struct starpu_history_entry_t *entry;
  389. struct starpu_htbl32_node_s *history;
  390. struct starpu_htbl32_node_s **history_ptr;
  391. struct starpu_regression_model_t *reg_model;
  392. struct starpu_history_list_t **list;
  393. history = per_arch_model->history;
  394. history_ptr = &per_arch_model->history;
  395. reg_model = &per_arch_model->regression;
  396. list = &per_arch_model->list;
  397. pthread_spin_lock(&model->model_mutex);
  398. entry = htbl_search_32(history, key);
  399. if (!entry)
  400. {
  401. /* this is the first entry with such a footprint */
  402. entry = malloc(sizeof(struct starpu_history_entry_t));
  403. STARPU_ASSERT(entry);
  404. entry->mean = measured;
  405. entry->sum = measured;
  406. entry->deviation = 0.0;
  407. entry->sum2 = measured*measured;
  408. entry->size = job_get_data_size(j);
  409. entry->footprint = key;
  410. entry->nsample = 1;
  411. insert_history_entry(entry, list, history_ptr);
  412. }
  413. else {
  414. /* there is already some entry with the same footprint */
  415. entry->sum += measured;
  416. entry->sum2 += measured*measured;
  417. entry->nsample++;
  418. unsigned n = entry->nsample;
  419. entry->mean = entry->sum / n;
  420. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  421. }
  422. STARPU_ASSERT(entry);
  423. /* update the regression model as well */
  424. double logy, logx;
  425. logx = logl(entry->size);
  426. logy = logl(measured);
  427. reg_model->sumlnx += logx;
  428. reg_model->sumlnx2 += logx*logx;
  429. reg_model->sumlny += logy;
  430. reg_model->sumlnxlny += logx*logy;
  431. reg_model->nsample++;
  432. unsigned n = reg_model->nsample;
  433. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  434. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  435. reg_model->beta = num/denom;
  436. reg_model->alpha = expl((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  437. pthread_spin_unlock(&model->model_mutex);
  438. }
  439. #ifdef MODEL_DEBUG
  440. FILE * debug_file = per_arch_model->debug_file;
  441. pthread_spin_lock(&model->model_mutex);
  442. STARPU_ASSERT(j->footprint_is_computed);
  443. fprintf(debug_file, "%x\t%d\t%lf\t%lf\t%d\t\t", j->footprint, job_get_data_size(j), measured, j->predicted, cpuid);
  444. unsigned i;
  445. struct starpu_task *task = j->task;
  446. for (i = 0; i < task->cl->nbuffers; i++)
  447. {
  448. data_state *state = task->buffers[i].handle;
  449. STARPU_ASSERT(state->ops);
  450. STARPU_ASSERT(state->ops->display);
  451. state->ops->display(state, debug_file);
  452. }
  453. fprintf(debug_file, "\n");
  454. pthread_spin_unlock(&model->model_mutex);
  455. #endif
  456. }
  457. }