perfmodel_history.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <dirent.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <common/config.h>
  23. #include <core/perfmodel/perfmodel.h>
  24. #include <core/jobs.h>
  25. #include <core/workers.h>
  26. #include <pthread.h>
  27. #include <datawizard/datawizard.h>
  28. #include <core/perfmodel/regression.h>
  29. #include <common/config.h>
  30. #include <starpu_parameters.h>
  31. #ifdef STARPU_HAVE_WINDOWS
  32. #include <windows.h>
  33. #endif
  34. static pthread_rwlock_t registered_models_rwlock;
  35. static struct starpu_model_list_t *registered_models = NULL;
  36. /*
  37. * History based model
  38. */
  39. static void insert_history_entry(struct starpu_history_entry_t *entry, struct starpu_history_list_t **list, struct starpu_htbl32_node_s **history_ptr)
  40. {
  41. struct starpu_history_list_t *link;
  42. struct starpu_history_entry_t *old;
  43. link = (struct starpu_history_list_t *) malloc(sizeof(struct starpu_history_list_t));
  44. link->next = *list;
  45. link->entry = entry;
  46. *list = link;
  47. old = (struct starpu_history_entry_t *) _starpu_htbl_insert_32(history_ptr, entry->footprint, entry);
  48. /* that may fail in case there is some concurrency issue */
  49. STARPU_ASSERT(old == NULL);
  50. }
  51. static void dump_reg_model(FILE *f, struct starpu_perfmodel_t *model, unsigned arch, unsigned nimpl)
  52. {
  53. struct starpu_per_arch_perfmodel_t *per_arch_model;
  54. per_arch_model = &model->per_arch[arch][nimpl];
  55. struct starpu_regression_model_t *reg_model;
  56. reg_model = &per_arch_model->regression;
  57. /*
  58. * Linear Regression model
  59. */
  60. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  61. double alpha = nan(""), beta = nan("");
  62. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  63. {
  64. if (reg_model->nsample > 1)
  65. {
  66. alpha = reg_model->alpha;
  67. beta = reg_model->beta;
  68. }
  69. }
  70. fprintf(f, "# sumlnx\tsumlnx2\t\tsumlny\t\tsumlnxlny\talpha\t\tbeta\t\tn\n");
  71. fprintf(f, "%-15le\t%-15le\t%-15le\t%-15le\t%-15le\t%-15le\t%u\n", reg_model->sumlnx, reg_model->sumlnx2, reg_model->sumlny, reg_model->sumlnxlny, alpha, beta, reg_model->nsample);
  72. /*
  73. * Non-Linear Regression model
  74. */
  75. double a = nan(""), b = nan(""), c = nan("");
  76. if (model->type == STARPU_NL_REGRESSION_BASED)
  77. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  78. fprintf(f, "# a\t\tb\t\tc\n");
  79. fprintf(f, "%-15le\t%-15le\t%-15le\n", a, b, c);
  80. }
  81. static void scan_reg_model(FILE *f, struct starpu_regression_model_t *reg_model)
  82. {
  83. int res;
  84. /*
  85. * Linear Regression model
  86. */
  87. _starpu_drop_comments(f);
  88. res = fscanf(f, "%le\t%le\t%le\t%le\t%le\t%le\t%u\n",
  89. &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny,
  90. &reg_model->sumlnxlny, &reg_model->alpha, &reg_model->beta,
  91. &reg_model->nsample);
  92. STARPU_ASSERT(res == 7);
  93. /* If any of the parameters describing the linear regression model is NaN, the model is invalid */
  94. unsigned invalid = (isnan(reg_model->alpha)||isnan(reg_model->beta));
  95. reg_model->valid = !invalid && reg_model->nsample >= STARPU_CALIBRATION_MINIMUM;
  96. /*
  97. * Non-Linear Regression model
  98. */
  99. _starpu_drop_comments(f);
  100. res = fscanf(f, "%le\t%le\t%le\n", &reg_model->a, &reg_model->b, &reg_model->c);
  101. STARPU_ASSERT(res == 3);
  102. /* If any of the parameters describing the non-linear regression model is NaN, the model is invalid */
  103. unsigned nl_invalid = (isnan(reg_model->a)||isnan(reg_model->b)||isnan(reg_model->c));
  104. reg_model->nl_valid = !nl_invalid && reg_model->nsample >= STARPU_CALIBRATION_MINIMUM;
  105. }
  106. static void dump_history_entry(FILE *f, struct starpu_history_entry_t *entry)
  107. {
  108. fprintf(f, "%08x\t%-15lu\t%-15le\t%-15le\t%-15le\t%-15le\t%u\n", entry->footprint, (unsigned long) entry->size, entry->mean, entry->deviation, entry->sum, entry->sum2, entry->nsample);
  109. }
  110. static void scan_history_entry(FILE *f, struct starpu_history_entry_t *entry)
  111. {
  112. int res;
  113. _starpu_drop_comments(f);
  114. /* In case entry is NULL, we just drop these values */
  115. unsigned nsample;
  116. uint32_t footprint;
  117. #ifdef STARPU_HAVE_WINDOWS
  118. unsigned size; /* in bytes */
  119. #else
  120. size_t size; /* in bytes */
  121. #endif
  122. double mean;
  123. double deviation;
  124. double sum;
  125. double sum2;
  126. /* Read the values from the file */
  127. res = fscanf(f, "%x\t%"
  128. #ifndef STARPU_HAVE_WINDOWS
  129. "z"
  130. #endif
  131. "u\t%le\t%le\t%le\t%le\t%u\n", &footprint, &size, &mean, &deviation, &sum, &sum2, &nsample);
  132. STARPU_ASSERT(res == 7);
  133. if (entry)
  134. {
  135. entry->footprint = footprint;
  136. entry->size = size;
  137. entry->mean = mean;
  138. entry->deviation = deviation;
  139. entry->sum = sum;
  140. entry->sum2 = sum2;
  141. entry->nsample = nsample;
  142. }
  143. }
  144. static void parse_per_arch_model_file(FILE *f, struct starpu_per_arch_perfmodel_t *per_arch_model, unsigned scan_history)
  145. {
  146. unsigned nentries;
  147. _starpu_drop_comments(f);
  148. int res = fscanf(f, "%u\n", &nentries);
  149. STARPU_ASSERT(res == 1);
  150. scan_reg_model(f, &per_arch_model->regression);
  151. /* parse cpu entries */
  152. unsigned i;
  153. for (i = 0; i < nentries; i++) {
  154. struct starpu_history_entry_t *entry = NULL;
  155. if (scan_history)
  156. {
  157. entry = (struct starpu_history_entry_t *) malloc(sizeof(struct starpu_history_entry_t));
  158. STARPU_ASSERT(entry);
  159. }
  160. scan_history_entry(f, entry);
  161. /* insert the entry in the hashtable and the list structures */
  162. if (scan_history)
  163. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  164. }
  165. }
  166. static void parse_arch(FILE *f, struct starpu_perfmodel_t *model, unsigned scan_history, unsigned archmin, unsigned archmax, int skiparch)
  167. {
  168. unsigned arch, impl;
  169. struct starpu_per_arch_perfmodel_t dummy;
  170. int nimpls, implmax, skipimpl;
  171. unsigned ret;
  172. for (arch = archmin; arch < archmax; arch++) {
  173. _starpu_drop_comments(f);
  174. ret = fscanf(f, "%d\n", &nimpls);
  175. STARPU_ASSERT(ret == 1);
  176. implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  177. skipimpl = nimpls - STARPU_MAXIMPLEMENTATIONS;
  178. for (impl = 0; impl < implmax; impl++) {
  179. parse_per_arch_model_file(f, &model->per_arch[arch][impl], scan_history);
  180. }
  181. if (skipimpl > 0) {
  182. for (impl = 0; impl < skipimpl; impl++) {
  183. parse_per_arch_model_file(f, &dummy, 0);
  184. }
  185. }
  186. }
  187. if (skiparch > 0) {
  188. _starpu_drop_comments(f);
  189. ret = fscanf(f, "%d\n", &nimpls);
  190. STARPU_ASSERT(ret == 1);
  191. implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  192. skipimpl = nimpls - STARPU_MAXIMPLEMENTATIONS;
  193. for (arch = 0; arch < skiparch; arch ++) {
  194. for (impl = 0; impl < implmax; impl++) {
  195. parse_per_arch_model_file(f, &dummy, 0);
  196. }
  197. if (skipimpl > 0) {
  198. for (impl = 0; impl < skipimpl; impl++) {
  199. parse_per_arch_model_file(f, &dummy, 0);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. static void parse_model_file(FILE *f, struct starpu_perfmodel_t *model, unsigned scan_history)
  206. {
  207. unsigned ret;
  208. unsigned archmin = 0;
  209. unsigned max_gordondevs = 1; /* XXX : we need a STARPU_MAXGORDONDEVS cst */
  210. unsigned narchs;
  211. /* We could probably write a clean loop here, but the code would not
  212. * really be easier to read. */
  213. /* Parsing CPUs */
  214. _starpu_drop_comments(f);
  215. ret = fscanf(f, "%u\n", &narchs);
  216. STARPU_ASSERT(ret == 1);
  217. _STARPU_DEBUG("Parsing %u CPUs\n", narchs);
  218. if (narchs > 0)
  219. {
  220. parse_arch(f, model, scan_history,
  221. archmin,
  222. STARPU_MIN(narchs, STARPU_MAXCPUS),
  223. narchs - STARPU_MAXCPUS);
  224. }
  225. /* Parsing CUDA devs */
  226. _starpu_drop_comments(f);
  227. ret = fscanf(f, "%u\n", &narchs);
  228. STARPU_ASSERT(ret == 1);
  229. _STARPU_DEBUG("Parsing %u CUDA devices\n", narchs);
  230. if (narchs > 0)
  231. {
  232. archmin += STARPU_MAXCPUS;
  233. parse_arch(f, model, scan_history,
  234. archmin,
  235. archmin + STARPU_MIN(narchs, STARPU_MAXCUDADEVS),
  236. narchs - STARPU_MAXCUDADEVS);
  237. }
  238. /* Parsing OpenCL devs */
  239. _starpu_drop_comments(f);
  240. ret = fscanf(f, "%u\n", &narchs);
  241. STARPU_ASSERT(ret == 1);
  242. _STARPU_DEBUG("Parsing %u OpenCL devices\n", narchs);
  243. if (narchs > 0)
  244. {
  245. archmin += STARPU_MAXCUDADEVS;
  246. parse_arch(f, model, scan_history,
  247. archmin,
  248. archmin + STARPU_MIN(narchs, STARPU_MAXOPENCLDEVS),
  249. narchs - STARPU_MAXOPENCLDEVS);
  250. }
  251. /* Parsing Gordon implementations */
  252. _starpu_drop_comments(f);
  253. ret = fscanf(f, "%u\n", &narchs);
  254. STARPU_ASSERT(ret == 1);
  255. _STARPU_DEBUG("Parsing %u Gordon devices\n", narchs);
  256. if (narchs > 0)
  257. {
  258. archmin += STARPU_MAXOPENCLDEVS;
  259. parse_arch(f, model, scan_history,
  260. archmin,
  261. archmin + max_gordondevs,
  262. narchs - max_gordondevs);
  263. }
  264. }
  265. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel_t *model, unsigned arch, unsigned nimpl)
  266. {
  267. struct starpu_per_arch_perfmodel_t *per_arch_model;
  268. per_arch_model = &model->per_arch[arch][nimpl];
  269. /* count the number of elements in the lists */
  270. struct starpu_history_list_t *ptr = NULL;
  271. unsigned nentries = 0;
  272. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  273. {
  274. /* Dump the list of all entries in the history */
  275. ptr = per_arch_model->list;
  276. while(ptr) {
  277. nentries++;
  278. ptr = ptr->next;
  279. }
  280. }
  281. if (nentries == 0)
  282. return;
  283. /* header */
  284. char archname[32];
  285. starpu_perfmodel_get_arch_name((enum starpu_perf_archtype) arch, archname, 32, nimpl);
  286. fprintf(f, "# Model for %s\n", archname);
  287. fprintf(f, "# number of entries\n%u\n", nentries);
  288. dump_reg_model(f, model, arch, nimpl);
  289. /* Dump the history into the model file in case it is necessary */
  290. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  291. {
  292. fprintf(f, "# hash\t\tsize\t\tmean\t\tdev\t\tsum\t\tsum2\t\tn\n");
  293. ptr = per_arch_model->list;
  294. while (ptr) {
  295. dump_history_entry(f, ptr->entry);
  296. ptr = ptr->next;
  297. }
  298. }
  299. fprintf(f, "\n##################\n");
  300. }
  301. static unsigned get_n_entries(struct starpu_perfmodel_t *model, unsigned arch, unsigned impl)
  302. {
  303. struct starpu_per_arch_perfmodel_t *per_arch_model;
  304. per_arch_model = &model->per_arch[arch][impl];
  305. /* count the number of elements in the lists */
  306. struct starpu_history_list_t *ptr = NULL;
  307. unsigned nentries = 0;
  308. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  309. {
  310. /* Dump the list of all entries in the history */
  311. ptr = per_arch_model->list;
  312. while(ptr) {
  313. nentries++;
  314. ptr = ptr->next;
  315. }
  316. }
  317. return nentries;
  318. }
  319. static void dump_model_file(FILE *f, struct starpu_perfmodel_t *model)
  320. {
  321. unsigned number_of_archs[4] = { 0, 0, 0, 0};
  322. unsigned arch;
  323. unsigned nimpl;
  324. unsigned idx = 0;
  325. /* Finding the number of archs to write for each kind of device */
  326. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  327. {
  328. switch (arch)
  329. {
  330. case STARPU_CUDA_DEFAULT:
  331. case STARPU_OPENCL_DEFAULT:
  332. case STARPU_GORDON_DEFAULT:
  333. idx++;
  334. break;
  335. default:
  336. break;
  337. }
  338. unsigned nentries = 0;
  339. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  340. {
  341. nentries = get_n_entries(model, arch, nimpl) != 0;
  342. if (nentries > 0)
  343. {
  344. number_of_archs[idx]++;
  345. break;
  346. }
  347. }
  348. }
  349. /* Writing stuff */
  350. char *name;
  351. unsigned substract_to_arch = 0;
  352. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  353. {
  354. switch (arch)
  355. {
  356. case STARPU_CPU_DEFAULT:
  357. name = "CPU";
  358. fprintf(f, "##################\n");
  359. fprintf(f, "# %ss\n", name);
  360. fprintf(f, "# number of %s architectures\n", name);
  361. fprintf(f, "%u\n", number_of_archs[0]);
  362. break;
  363. case STARPU_CUDA_DEFAULT:
  364. name = "CUDA";
  365. substract_to_arch = STARPU_MAXCPUS;
  366. fprintf(f, "##################\n");
  367. fprintf(f, "# %ss\n", name);
  368. fprintf(f, "# number of %s architectures\n", name);
  369. fprintf(f, "%u\n", number_of_archs[1]);
  370. break;
  371. case STARPU_OPENCL_DEFAULT:
  372. name = "OPENCL";
  373. substract_to_arch += STARPU_MAXCUDADEVS;
  374. fprintf(f, "##################\n");
  375. fprintf(f, "# %ss\n", name);
  376. fprintf(f, "# number of %s architectures\n", name);
  377. fprintf(f, "%u\n", number_of_archs[2]);
  378. break;
  379. case STARPU_GORDON_DEFAULT:
  380. name = "GORDON";
  381. substract_to_arch += STARPU_MAXOPENCLDEVS;
  382. fprintf(f, "##################\n");
  383. fprintf(f, "# %ss\n", name);
  384. fprintf(f, "# number of %s architectures\n", name);
  385. fprintf(f, "%u\n", number_of_archs[3]);
  386. break;
  387. default:
  388. STARPU_ASSERT(0);
  389. }
  390. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  391. {
  392. if (get_n_entries(model, arch, nimpl) == 0)
  393. break;
  394. }
  395. unsigned max_impl = nimpl;
  396. if (max_impl == 0)
  397. continue;
  398. fprintf(f, "###########\n");
  399. fprintf(f, "# %s_%u\n", name, arch - substract_to_arch);
  400. fprintf(f, "# number of implementations\n");
  401. fprintf(f, "%u\n", max_impl);
  402. for (nimpl = 0; nimpl < max_impl; nimpl++)
  403. {
  404. dump_per_arch_model_file(f, model, arch, nimpl);
  405. }
  406. }
  407. }
  408. static void initialize_per_arch_model(struct starpu_per_arch_perfmodel_t *per_arch_model)
  409. {
  410. per_arch_model->history = NULL;
  411. per_arch_model->list = NULL;
  412. }
  413. static void initialize_model(struct starpu_perfmodel_t *model)
  414. {
  415. unsigned arch;
  416. unsigned nimpl;
  417. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  418. {
  419. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  420. {
  421. initialize_per_arch_model(&model->per_arch[arch][nimpl]);
  422. }
  423. }
  424. }
  425. static void get_model_debug_path(struct starpu_perfmodel_t *model, const char *arch, char *path, size_t maxlen)
  426. {
  427. STARPU_ASSERT(path);
  428. _starpu_get_perf_model_dir_debug(path, maxlen);
  429. strncat(path, model->symbol, maxlen);
  430. char hostname[32];
  431. char *forced_hostname = getenv("STARPU_HOSTNAME");
  432. if (forced_hostname && forced_hostname[0])
  433. snprintf(hostname, sizeof(hostname), "%s", forced_hostname);
  434. else
  435. gethostname(hostname, sizeof(hostname));
  436. strncat(path, ".", maxlen);
  437. strncat(path, hostname, maxlen);
  438. strncat(path, ".", maxlen);
  439. strncat(path, arch, maxlen);
  440. strncat(path, ".debug", maxlen);
  441. }
  442. /* registered_models_rwlock must be taken in write mode before calling this
  443. * function */
  444. void _starpu_register_model(struct starpu_perfmodel_t *model)
  445. {
  446. /* add the model to a linked list */
  447. struct starpu_model_list_t *node = (struct starpu_model_list_t *) malloc(sizeof(struct starpu_model_list_t));
  448. node->model = model;
  449. //model->debug_modelid = debug_modelid++;
  450. /* put this model at the beginning of the list */
  451. node->next = registered_models;
  452. registered_models = node;
  453. #ifdef STARPU_MODEL_DEBUG
  454. _starpu_create_sampling_directory_if_needed();
  455. unsigned arch;
  456. unsigned nimpl;
  457. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++) {
  458. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++) {
  459. char debugpath[256];
  460. starpu_perfmodel_debugfilepath(model, arch, debugpath, 256, nimpl);
  461. model->per_arch[arch][nimpl].debug_file = fopen(debugpath, "a+");
  462. STARPU_ASSERT(model->per_arch[arch][nimpl].debug_file);
  463. }
  464. }
  465. #endif
  466. return;
  467. }
  468. static void get_model_path(struct starpu_perfmodel_t *model, char *path, size_t maxlen)
  469. {
  470. _starpu_get_perf_model_dir_codelets(path, maxlen);
  471. strncat(path, model->symbol, maxlen);
  472. char hostname[32];
  473. char *forced_hostname = getenv("STARPU_HOSTNAME");
  474. if (forced_hostname && forced_hostname[0])
  475. snprintf(hostname, sizeof(hostname), "%s", forced_hostname);
  476. else
  477. gethostname(hostname, sizeof(hostname));
  478. strncat(path, ".", maxlen);
  479. strncat(path, hostname, maxlen);
  480. }
  481. static void save_history_based_model(struct starpu_perfmodel_t *model)
  482. {
  483. STARPU_ASSERT(model);
  484. STARPU_ASSERT(model->symbol);
  485. /* TODO checks */
  486. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  487. char path[256];
  488. get_model_path(model, path, 256);
  489. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  490. /* overwrite existing file, or create it */
  491. FILE *f;
  492. f = fopen(path, "w+");
  493. STARPU_ASSERT(f);
  494. dump_model_file(f, model);
  495. fclose(f);
  496. }
  497. static void _starpu_dump_registered_models(void)
  498. {
  499. PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  500. struct starpu_model_list_t *node;
  501. node = registered_models;
  502. _STARPU_DEBUG("DUMP MODELS !\n");
  503. while (node) {
  504. save_history_based_model(node->model);
  505. node = node->next;
  506. /* XXX free node */
  507. }
  508. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  509. }
  510. void _starpu_initialize_registered_performance_models(void)
  511. {
  512. registered_models = NULL;
  513. PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  514. }
  515. void _starpu_deinitialize_registered_performance_models(void)
  516. {
  517. if (_starpu_get_calibrate_flag())
  518. _starpu_dump_registered_models();
  519. PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  520. }
  521. /* We first try to grab the global lock in read mode to check whether the model
  522. * was loaded or not (this is very likely to have been already loaded). If the
  523. * model was not loaded yet, we take the lock in write mode, and if the model
  524. * is still not loaded once we have the lock, we do load it. */
  525. void _starpu_load_history_based_model(struct starpu_perfmodel_t *model, unsigned scan_history)
  526. {
  527. STARPU_ASSERT(model);
  528. STARPU_ASSERT(model->symbol);
  529. int already_loaded;
  530. PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  531. already_loaded = model->is_loaded;
  532. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  533. if (already_loaded)
  534. return;
  535. /* The model is still not loaded so we grab the lock in write mode, and
  536. * if it's not loaded once we have the lock, we do load it. */
  537. PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  538. /* Was the model initialized since the previous test ? */
  539. if (model->is_loaded)
  540. {
  541. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  542. return;
  543. }
  544. PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  545. PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  546. /* make sure the performance model directory exists (or create it) */
  547. _starpu_create_sampling_directory_if_needed();
  548. /*
  549. * We need to keep track of all the model that were opened so that we can
  550. * possibly update them at runtime termination ...
  551. */
  552. _starpu_register_model(model);
  553. char path[256];
  554. get_model_path(model, path, 256);
  555. _STARPU_DEBUG("Opening performance model file %s for model %s ... ", path, model->symbol);
  556. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  557. model->benchmarking = calibrate_flag;
  558. /* try to open an existing file and load it */
  559. int res;
  560. res = access(path, F_OK);
  561. if (res == 0) {
  562. if (calibrate_flag == 2)
  563. {
  564. /* The user specified that the performance model should
  565. * be overwritten, so we don't load the existing file !
  566. * */
  567. _STARPU_DEBUG("Overwrite existing file\n");
  568. initialize_model(model);
  569. }
  570. else {
  571. /* We load the available file */
  572. _STARPU_DEBUG("File exists\n");
  573. FILE *f;
  574. f = fopen(path, "r");
  575. STARPU_ASSERT(f);
  576. parse_model_file(f, model, scan_history);
  577. fclose(f);
  578. }
  579. }
  580. else {
  581. _STARPU_DEBUG("File does not exists\n");
  582. if (!calibrate_flag) {
  583. _STARPU_DISP("Warning: model %s is not calibrated, forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol);
  584. _starpu_set_calibrate_flag(1);
  585. model->benchmarking = 1;
  586. }
  587. initialize_model(model);
  588. }
  589. model->is_loaded = 1;
  590. PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  591. PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  592. }
  593. /* This function is intended to be used by external tools that should read
  594. * the performance model files */
  595. int starpu_list_models(void)
  596. {
  597. char path[256];
  598. DIR *dp;
  599. struct dirent *ep;
  600. char perf_model_dir_codelets[256];
  601. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  602. strncpy(path, perf_model_dir_codelets, 256);
  603. dp = opendir(path);
  604. if (dp != NULL) {
  605. while ((ep = readdir(dp))) {
  606. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  607. fprintf(stdout, "file: <%s>\n", ep->d_name);
  608. }
  609. closedir (dp);
  610. return 0;
  611. }
  612. else {
  613. perror ("Couldn't open the directory");
  614. return 1;
  615. }
  616. }
  617. /* This function is intended to be used by external tools that should read the
  618. * performance model files */
  619. int starpu_load_history_debug(const char *symbol, struct starpu_perfmodel_t *model)
  620. {
  621. model->symbol = strdup(symbol);
  622. /* where is the file if it exists ? */
  623. char path[256];
  624. get_model_path(model, path, 256);
  625. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  626. /* does it exist ? */
  627. int res;
  628. res = access(path, F_OK);
  629. if (res) {
  630. char *dot = strrchr(symbol, '.');
  631. if (dot) {
  632. char *symbol2 = strdup(symbol);
  633. symbol2[dot-symbol] = '\0';
  634. int ret;
  635. fprintf(stderr,"note: loading history from %s instead of %s\n", symbol2, symbol);
  636. ret = starpu_load_history_debug(symbol2,model);
  637. free(symbol2);
  638. return ret;
  639. }
  640. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  641. return 1;
  642. }
  643. FILE *f = fopen(path, "r");
  644. STARPU_ASSERT(f);
  645. parse_model_file(f, model, 1);
  646. return 0;
  647. }
  648. void starpu_perfmodel_get_arch_name(enum starpu_perf_archtype arch, char *archname, size_t maxlen,unsigned nimpl)
  649. {
  650. if (arch < STARPU_CUDA_DEFAULT)
  651. {
  652. if (arch == STARPU_CPU_DEFAULT)
  653. {
  654. /* NB: We could just use cpu_1 as well ... */
  655. snprintf(archname, maxlen, "cpu_impl_%u",nimpl);
  656. }
  657. else
  658. {
  659. /* For combined CPU workers */
  660. int cpu_count = arch - STARPU_CPU_DEFAULT + 1;
  661. snprintf(archname, maxlen, "cpu_%d_impl_%u", cpu_count,nimpl);
  662. }
  663. }
  664. else if ((STARPU_CUDA_DEFAULT <= arch)
  665. && (arch < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS))
  666. {
  667. int devid = arch - STARPU_CUDA_DEFAULT;
  668. snprintf(archname, maxlen, "cuda_%d_impl_%u", devid,nimpl);
  669. }
  670. else if ((STARPU_OPENCL_DEFAULT <= arch)
  671. && (arch < STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS))
  672. {
  673. int devid = arch - STARPU_OPENCL_DEFAULT;
  674. snprintf(archname, maxlen, "opencl_%d_impl_%u", devid,nimpl);
  675. }
  676. else if (arch == STARPU_GORDON_DEFAULT)
  677. {
  678. snprintf(archname, maxlen, "gordon_impl_%u",nimpl);
  679. }
  680. else
  681. {
  682. STARPU_ABORT();
  683. }
  684. }
  685. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel_t *model,
  686. enum starpu_perf_archtype arch, char *path, size_t maxlen, unsigned nimpl)
  687. {
  688. char archname[32];
  689. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  690. STARPU_ASSERT(path);
  691. get_model_debug_path(model, archname, path, maxlen);
  692. }
  693. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct starpu_job_s *j, unsigned nimpl)
  694. {
  695. double exp = -1.0;
  696. size_t size = _starpu_job_get_data_size(j);
  697. struct starpu_regression_model_t *regmodel;
  698. regmodel = &model->per_arch[arch][nimpl].regression;
  699. if (regmodel->valid)
  700. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  701. return exp;
  702. }
  703. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct starpu_job_s *j,unsigned nimpl)
  704. {
  705. double exp = -1.0;
  706. size_t size = _starpu_job_get_data_size(j);
  707. struct starpu_regression_model_t *regmodel;
  708. regmodel = &model->per_arch[arch][nimpl].regression;
  709. if (regmodel->nl_valid)
  710. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  711. return exp;
  712. }
  713. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, struct starpu_job_s *j,unsigned nimpl)
  714. {
  715. double exp;
  716. struct starpu_per_arch_perfmodel_t *per_arch_model;
  717. struct starpu_history_entry_t *entry;
  718. struct starpu_htbl32_node_s *history;
  719. uint32_t key = _starpu_compute_buffers_footprint(j);
  720. per_arch_model = &model->per_arch[arch][nimpl];
  721. history = per_arch_model->history;
  722. if (!history)
  723. return -1.0;
  724. PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  725. entry = (struct starpu_history_entry_t *) _starpu_htbl_search_32(history, key);
  726. PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  727. exp = entry?entry->mean:-1.0;
  728. if (entry && entry->nsample < STARPU_CALIBRATION_MINIMUM)
  729. /* TODO: report differently if we've scheduled really enough
  730. * of that task and the scheduler should perhaps put it aside */
  731. /* Not calibrated enough */
  732. return -1.0;
  733. return exp;
  734. }
  735. void _starpu_update_perfmodel_history(starpu_job_t j, struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, unsigned cpuid STARPU_ATTRIBUTE_UNUSED, double measured, unsigned nimpl)
  736. {
  737. if (model)
  738. {
  739. PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  740. struct starpu_per_arch_perfmodel_t *per_arch_model = &model->per_arch[arch][nimpl];
  741. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  742. {
  743. uint32_t key = _starpu_compute_buffers_footprint(j);
  744. struct starpu_history_entry_t *entry;
  745. struct starpu_htbl32_node_s *history;
  746. struct starpu_htbl32_node_s **history_ptr;
  747. struct starpu_history_list_t **list;
  748. history = per_arch_model->history;
  749. history_ptr = &per_arch_model->history;
  750. list = &per_arch_model->list;
  751. entry = (struct starpu_history_entry_t *) _starpu_htbl_search_32(history, key);
  752. if (!entry)
  753. {
  754. /* this is the first entry with such a footprint */
  755. entry = (struct starpu_history_entry_t *) malloc(sizeof(struct starpu_history_entry_t));
  756. STARPU_ASSERT(entry);
  757. entry->mean = measured;
  758. entry->sum = measured;
  759. entry->deviation = 0.0;
  760. entry->sum2 = measured*measured;
  761. entry->size = _starpu_job_get_data_size(j);
  762. entry->footprint = key;
  763. entry->nsample = 1;
  764. insert_history_entry(entry, list, history_ptr);
  765. }
  766. else {
  767. /* there is already some entry with the same footprint */
  768. entry->sum += measured;
  769. entry->sum2 += measured*measured;
  770. entry->nsample++;
  771. unsigned n = entry->nsample;
  772. entry->mean = entry->sum / n;
  773. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  774. }
  775. STARPU_ASSERT(entry);
  776. }
  777. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  778. {
  779. struct starpu_regression_model_t *reg_model;
  780. reg_model = &per_arch_model->regression;
  781. /* update the regression model */
  782. size_t job_size = _starpu_job_get_data_size(j);
  783. double logy, logx;
  784. logx = log((double)job_size);
  785. logy = log(measured);
  786. reg_model->sumlnx += logx;
  787. reg_model->sumlnx2 += logx*logx;
  788. reg_model->sumlny += logy;
  789. reg_model->sumlnxlny += logx*logy;
  790. reg_model->nsample++;
  791. unsigned n = reg_model->nsample;
  792. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  793. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  794. reg_model->beta = num/denom;
  795. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  796. if (reg_model->nsample >= STARPU_CALIBRATION_MINIMUM) {
  797. reg_model->valid = 1;
  798. reg_model->nl_valid = 1;
  799. }
  800. }
  801. #ifdef STARPU_MODEL_DEBUG
  802. struct starpu_task *task = j->task;
  803. FILE * debug_file = per_arch_model->debug_file;
  804. STARPU_ASSERT(j->footprint_is_computed);
  805. fprintf(debug_file, "0x%x\t%lu\t%f\t%f\t%f\t%d\t\t", j->footprint, (unsigned long) _starpu_job_get_data_size(j), measured, task->predicted, task->predicted_transfer, cpuid);
  806. unsigned i;
  807. for (i = 0; i < task->cl->nbuffers; i++)
  808. {
  809. struct starpu_data_handle_t *handle = task->buffers[i].handle;
  810. STARPU_ASSERT(handle->ops);
  811. STARPU_ASSERT(handle->ops->display);
  812. handle->ops->display(handle, debug_file);
  813. }
  814. fprintf(debug_file, "\n");
  815. #endif
  816. PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  817. }
  818. }