perfmodel_history.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <common/utils.h>
  24. #include <core/perfmodel/perfmodel.h>
  25. #include <core/jobs.h>
  26. #include <core/workers.h>
  27. #include <datawizard/datawizard.h>
  28. #include <core/perfmodel/regression.h>
  29. #include <common/config.h>
  30. #include <starpu_parameters.h>
  31. #include <common/uthash.h>
  32. #ifdef STARPU_HAVE_WINDOWS
  33. #include <windows.h>
  34. #endif
  35. #define HASH_ADD_UINT32_T(head,field,add) HASH_ADD(hh,head,field,sizeof(uint32_t),add)
  36. #define HASH_FIND_UINT32_T(head,find,out) HASH_FIND(hh,head,find,sizeof(uint32_t),out)
  37. struct starpu_perfmodel_history_table
  38. {
  39. UT_hash_handle hh;
  40. uint32_t footprint;
  41. struct starpu_perfmodel_history_entry *history_entry;
  42. };
  43. /* We want more than 10% variance on X to trust regression */
  44. #define VALID_REGRESSION(reg_model) \
  45. ((reg_model)->minx < (9*(reg_model)->maxx)/10 && (reg_model)->nsample >= _STARPU_CALIBRATION_MINIMUM)
  46. static starpu_pthread_rwlock_t registered_models_rwlock;
  47. static struct _starpu_perfmodel_list *registered_models = NULL;
  48. size_t _starpu_job_get_data_size(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, unsigned nimpl, struct _starpu_job *j)
  49. {
  50. struct starpu_task *task = j->task;
  51. if (model && model->per_arch[arch][nimpl].size_base)
  52. {
  53. return model->per_arch[arch][nimpl].size_base(task, arch, nimpl);
  54. }
  55. else if (model && model->size_base)
  56. {
  57. return model->size_base(task, nimpl);
  58. }
  59. else
  60. {
  61. unsigned nbuffers = task->cl->nbuffers;
  62. size_t size = 0;
  63. unsigned buffer;
  64. for (buffer = 0; buffer < nbuffers; buffer++)
  65. {
  66. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  67. size += _starpu_data_get_size(handle);
  68. }
  69. return size;
  70. }
  71. }
  72. /*
  73. * History based model
  74. */
  75. static void insert_history_entry(struct starpu_perfmodel_history_entry *entry, struct starpu_perfmodel_history_list **list, struct starpu_perfmodel_history_table **history_ptr)
  76. {
  77. struct starpu_perfmodel_history_list *link;
  78. struct starpu_perfmodel_history_table *table;
  79. link = (struct starpu_perfmodel_history_list *) malloc(sizeof(struct starpu_perfmodel_history_list));
  80. link->next = *list;
  81. link->entry = entry;
  82. *list = link;
  83. /* detect concurrency issue */
  84. //HASH_FIND_UINT32_T(*history_ptr, &entry->footprint, table);
  85. //STARPU_ASSERT(table == NULL);
  86. table = (struct starpu_perfmodel_history_table*) malloc(sizeof(*table));
  87. STARPU_ASSERT(table != NULL);
  88. table->footprint = entry->footprint;
  89. table->history_entry = entry;
  90. HASH_ADD_UINT32_T(*history_ptr, footprint, table);
  91. }
  92. static void dump_reg_model(FILE *f, struct starpu_perfmodel *model, unsigned arch, unsigned nimpl)
  93. {
  94. struct starpu_perfmodel_per_arch *per_arch_model;
  95. per_arch_model = &model->per_arch[arch][nimpl];
  96. struct starpu_perfmodel_regression_model *reg_model;
  97. reg_model = &per_arch_model->regression;
  98. /*
  99. * Linear Regression model
  100. */
  101. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  102. double alpha = nan(""), beta = nan("");
  103. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  104. {
  105. if (reg_model->nsample > 1)
  106. {
  107. alpha = reg_model->alpha;
  108. beta = reg_model->beta;
  109. }
  110. }
  111. fprintf(f, "# sumlnx\tsumlnx2\t\tsumlny\t\tsumlnxlny\talpha\t\tbeta\t\tn\tminx\t\tmaxx\n");
  112. fprintf(f, "%-15le\t%-15le\t%-15le\t%-15le\t%-15le\t%-15le\t%u\t%-15lu\t%-15lu\n", reg_model->sumlnx, reg_model->sumlnx2, reg_model->sumlny, reg_model->sumlnxlny, alpha, beta, reg_model->nsample, reg_model->minx, reg_model->maxx);
  113. /*
  114. * Non-Linear Regression model
  115. */
  116. double a = nan(""), b = nan(""), c = nan("");
  117. if (model->type == STARPU_NL_REGRESSION_BASED)
  118. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  119. fprintf(f, "# a\t\tb\t\tc\n");
  120. fprintf(f, "%-15le\t%-15le\t%-15le\n", a, b, c);
  121. }
  122. static void scan_reg_model(FILE *f, struct starpu_perfmodel_regression_model *reg_model)
  123. {
  124. int res;
  125. /*
  126. * Linear Regression model
  127. */
  128. _starpu_drop_comments(f);
  129. res = fscanf(f, "%le\t%le\t%le\t%le\t%le\t%le\t%u\t%lu\t%lu\n",
  130. &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny,
  131. &reg_model->sumlnxlny, &reg_model->alpha, &reg_model->beta,
  132. &reg_model->nsample,
  133. &reg_model->minx, &reg_model->maxx);
  134. STARPU_ASSERT_MSG(res == 9, "Incorrect performance model file");
  135. /* If any of the parameters describing the linear regression model is NaN, the model is invalid */
  136. unsigned invalid = (isnan(reg_model->alpha)||isnan(reg_model->beta));
  137. reg_model->valid = !invalid && VALID_REGRESSION(reg_model);
  138. /*
  139. * Non-Linear Regression model
  140. */
  141. _starpu_drop_comments(f);
  142. res = fscanf(f, "%le\t%le\t%le\n", &reg_model->a, &reg_model->b, &reg_model->c);
  143. STARPU_ASSERT_MSG(res == 3, "Incorrect performance model file");
  144. /* If any of the parameters describing the non-linear regression model is NaN, the model is invalid */
  145. unsigned nl_invalid = (isnan(reg_model->a)||isnan(reg_model->b)||isnan(reg_model->c));
  146. reg_model->nl_valid = !nl_invalid && VALID_REGRESSION(reg_model);
  147. }
  148. static void dump_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  149. {
  150. fprintf(f, "%08x\t%-15lu\t%-15le\t%-15le\t%-15le\t%-15le\t%-15le\t%u\n", entry->footprint, (unsigned long) entry->size, entry->flops, entry->mean, entry->deviation, entry->sum, entry->sum2, entry->nsample);
  151. }
  152. static void scan_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  153. {
  154. int res;
  155. _starpu_drop_comments(f);
  156. /* In case entry is NULL, we just drop these values */
  157. unsigned nsample;
  158. uint32_t footprint;
  159. unsigned long size; /* in bytes */
  160. double flops;
  161. double mean;
  162. double deviation;
  163. double sum;
  164. double sum2;
  165. char line[256];
  166. char *ret;
  167. ret = fgets(line, sizeof(line), f);
  168. STARPU_ASSERT(ret);
  169. STARPU_ASSERT(strchr(line, '\n'));
  170. /* Read the values from the file */
  171. res = sscanf(line, "%x\t%lu\t%le\t%le\t%le\t%le\t%le\t%u", &footprint, &size, &flops, &mean, &deviation, &sum, &sum2, &nsample);
  172. if (res != 8)
  173. {
  174. flops = 0.;
  175. /* Read the values from the file */
  176. res = sscanf(line, "%x\t%lu\t%le\t%le\t%le\t%le\t%u", &footprint, &size, &mean, &deviation, &sum, &sum2, &nsample);
  177. STARPU_ASSERT_MSG(res == 7, "Incorrect performance model file");
  178. }
  179. if (entry)
  180. {
  181. entry->footprint = footprint;
  182. entry->size = size;
  183. entry->flops = flops;
  184. entry->mean = mean;
  185. entry->deviation = deviation;
  186. entry->sum = sum;
  187. entry->sum2 = sum2;
  188. entry->nsample = nsample;
  189. }
  190. }
  191. static void parse_per_arch_model_file(FILE *f, struct starpu_perfmodel_per_arch *per_arch_model, unsigned scan_history)
  192. {
  193. unsigned nentries;
  194. _starpu_drop_comments(f);
  195. int res = fscanf(f, "%u\n", &nentries);
  196. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  197. scan_reg_model(f, &per_arch_model->regression);
  198. /* parse cpu entries */
  199. unsigned i;
  200. for (i = 0; i < nentries; i++)
  201. {
  202. struct starpu_perfmodel_history_entry *entry = NULL;
  203. if (scan_history)
  204. {
  205. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  206. STARPU_ASSERT(entry);
  207. }
  208. scan_history_entry(f, entry);
  209. /* insert the entry in the hashtable and the list structures */
  210. /* TODO: Insert it at the end of the list, to avoid reversing
  211. * the order... But efficiently! We may have a lot of entries */
  212. if (scan_history)
  213. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  214. }
  215. }
  216. static void parse_arch(FILE *f, struct starpu_perfmodel *model, unsigned scan_history, unsigned archmin, unsigned archmax, unsigned skiparch)
  217. {
  218. struct starpu_perfmodel_per_arch dummy;
  219. int nimpls, implmax, skipimpl, impl;
  220. unsigned ret, arch;
  221. for (arch = archmin; arch < archmax; arch++)
  222. {
  223. _STARPU_DEBUG("Parsing arch %u\n", arch);
  224. _starpu_drop_comments(f);
  225. ret = fscanf(f, "%d\n", &nimpls);
  226. _STARPU_DEBUG("%d implementations\n", nimpls);
  227. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  228. implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  229. skipimpl = nimpls - STARPU_MAXIMPLEMENTATIONS;
  230. for (impl = 0; impl < implmax; impl++)
  231. {
  232. parse_per_arch_model_file(f, &model->per_arch[arch][impl], scan_history);
  233. }
  234. if (skipimpl > 0)
  235. {
  236. for (impl = 0; impl < skipimpl; impl++)
  237. {
  238. parse_per_arch_model_file(f, &dummy, 0);
  239. }
  240. }
  241. }
  242. if (skiparch > 0)
  243. {
  244. _starpu_drop_comments(f);
  245. for (arch = 0; arch < skiparch; arch ++)
  246. {
  247. _STARPU_DEBUG("skipping arch %u\n", arch);
  248. ret = fscanf(f, "%d\n", &nimpls);
  249. _STARPU_DEBUG("%d implementations\n", nimpls);
  250. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  251. implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  252. skipimpl = nimpls - STARPU_MAXIMPLEMENTATIONS;
  253. for (impl = 0; impl < implmax; impl++)
  254. {
  255. parse_per_arch_model_file(f, &dummy, 0);
  256. }
  257. if (skipimpl > 0)
  258. {
  259. for (impl = 0; impl < skipimpl; impl++)
  260. {
  261. parse_per_arch_model_file(f, &dummy, 0);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. static void parse_model_file(FILE *f, struct starpu_perfmodel *model, unsigned scan_history)
  268. {
  269. unsigned ret;
  270. unsigned archmin = 0;
  271. unsigned narchs;
  272. /* We could probably write a clean loop here, but the code would not
  273. * really be easier to read. */
  274. /* Parsing CPUs */
  275. _starpu_drop_comments(f);
  276. ret = fscanf(f, "%u\n", &narchs);
  277. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  278. _STARPU_DEBUG("Parsing %u CPUs\n", narchs);
  279. if (narchs > 0)
  280. {
  281. parse_arch(f, model, scan_history,
  282. archmin,
  283. STARPU_MIN(narchs, STARPU_MAXCPUS),
  284. narchs > STARPU_MAXCPUS ? narchs - STARPU_MAXCPUS : 0);
  285. }
  286. /* Parsing CUDA devs */
  287. _starpu_drop_comments(f);
  288. ret = fscanf(f, "%u\n", &narchs);
  289. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  290. archmin += STARPU_MAXCPUS;
  291. _STARPU_DEBUG("Parsing %u CUDA devices\n", narchs);
  292. if (narchs > 0)
  293. {
  294. parse_arch(f, model, scan_history,
  295. archmin,
  296. archmin + STARPU_MIN(narchs, STARPU_MAXCUDADEVS),
  297. narchs > STARPU_MAXCUDADEVS ? narchs - STARPU_MAXCUDADEVS : 0);
  298. }
  299. /* Parsing OpenCL devs */
  300. _starpu_drop_comments(f);
  301. ret = fscanf(f, "%u\n", &narchs);
  302. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  303. archmin += STARPU_MAXCUDADEVS;
  304. _STARPU_DEBUG("Parsing %u OpenCL devices\n", narchs);
  305. if (narchs > 0)
  306. {
  307. parse_arch(f, model, scan_history,
  308. archmin,
  309. archmin + STARPU_MIN(narchs, STARPU_MAXOPENCLDEVS),
  310. narchs > STARPU_MAXOPENCLDEVS ? narchs - STARPU_MAXOPENCLDEVS : 0);
  311. }
  312. }
  313. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, unsigned arch, unsigned nimpl)
  314. {
  315. struct starpu_perfmodel_per_arch *per_arch_model;
  316. per_arch_model = &model->per_arch[arch][nimpl];
  317. /* count the number of elements in the lists */
  318. struct starpu_perfmodel_history_list *ptr = NULL;
  319. unsigned nentries = 0;
  320. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  321. {
  322. /* Dump the list of all entries in the history */
  323. ptr = per_arch_model->list;
  324. while(ptr)
  325. {
  326. nentries++;
  327. ptr = ptr->next;
  328. }
  329. }
  330. /* header */
  331. char archname[32];
  332. starpu_perfmodel_get_arch_name((enum starpu_perfmodel_archtype) arch, archname, 32, nimpl);
  333. fprintf(f, "# Model for %s\n", archname);
  334. fprintf(f, "# number of entries\n%u\n", nentries);
  335. dump_reg_model(f, model, arch, nimpl);
  336. /* Dump the history into the model file in case it is necessary */
  337. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  338. {
  339. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean\t\tdev\t\tsum\t\tsum2\t\tn\n");
  340. ptr = per_arch_model->list;
  341. while (ptr)
  342. {
  343. dump_history_entry(f, ptr->entry);
  344. ptr = ptr->next;
  345. }
  346. }
  347. fprintf(f, "\n##################\n");
  348. }
  349. static unsigned get_n_entries(struct starpu_perfmodel *model, unsigned arch, unsigned impl)
  350. {
  351. struct starpu_perfmodel_per_arch *per_arch_model;
  352. per_arch_model = &model->per_arch[arch][impl];
  353. /* count the number of elements in the lists */
  354. struct starpu_perfmodel_history_list *ptr = NULL;
  355. unsigned nentries = 0;
  356. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  357. {
  358. /* Dump the list of all entries in the history */
  359. ptr = per_arch_model->list;
  360. while(ptr)
  361. {
  362. nentries++;
  363. ptr = ptr->next;
  364. }
  365. }
  366. return nentries;
  367. }
  368. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  369. {
  370. unsigned narch[4] = { 0, 0, 0, 0};
  371. unsigned arch, arch_base = 0, my_narch = 0;
  372. unsigned nimpl;
  373. unsigned idx = 0;
  374. /* Finding the number of archs to write for each kind of device */
  375. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  376. {
  377. switch (arch)
  378. {
  379. case STARPU_CUDA_DEFAULT:
  380. case STARPU_OPENCL_DEFAULT:
  381. arch_base = arch;
  382. idx++;
  383. break;
  384. default:
  385. break;
  386. }
  387. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  388. {
  389. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  390. if (get_n_entries(model, arch, nimpl))
  391. {
  392. narch[idx]=arch-arch_base+1;
  393. break;
  394. }
  395. }
  396. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  397. {
  398. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  399. if (model->per_arch[arch][nimpl].regression.nsample)
  400. {
  401. narch[idx]=arch-arch_base+1;
  402. break;
  403. }
  404. }
  405. else
  406. {
  407. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %d", model->type);
  408. }
  409. }
  410. /* Writing stuff */
  411. char *name = "unknown";
  412. unsigned substract_to_arch = 0;
  413. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  414. {
  415. switch (arch)
  416. {
  417. case STARPU_CPU_DEFAULT:
  418. arch_base = arch;
  419. name = "CPU";
  420. fprintf(f, "##################\n");
  421. fprintf(f, "# %ss\n", name);
  422. fprintf(f, "# maximum number of %ss\n", name);
  423. fprintf(f, "%u\n", my_narch = narch[0]);
  424. break;
  425. case STARPU_CUDA_DEFAULT:
  426. arch_base = arch;
  427. name = "CUDA";
  428. substract_to_arch = STARPU_MAXCPUS;
  429. fprintf(f, "##################\n");
  430. fprintf(f, "# %ss\n", name);
  431. fprintf(f, "# number of %s architectures\n", name);
  432. fprintf(f, "%u\n", my_narch = narch[1]);
  433. break;
  434. case STARPU_OPENCL_DEFAULT:
  435. arch_base = arch;
  436. name = "OPENCL";
  437. substract_to_arch += STARPU_MAXCUDADEVS;
  438. fprintf(f, "##################\n");
  439. fprintf(f, "# %ss\n", name);
  440. fprintf(f, "# number of %s architectures\n", name);
  441. fprintf(f, "%u\n", my_narch = narch[2]);
  442. break;
  443. default:
  444. break;
  445. }
  446. unsigned max_impl = 0;
  447. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  448. {
  449. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  450. if (get_n_entries(model, arch, nimpl))
  451. max_impl = nimpl + 1;
  452. }
  453. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  454. {
  455. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  456. if (model->per_arch[arch][nimpl].regression.nsample)
  457. max_impl = nimpl + 1;
  458. }
  459. else
  460. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %u", arch);
  461. if (arch >= my_narch + arch_base)
  462. continue;
  463. fprintf(f, "###########\n");
  464. if (substract_to_arch)
  465. fprintf(f, "# %s_%u\n", name, arch - substract_to_arch);
  466. else
  467. /* CPU */
  468. fprintf(f, "# %u CPU(s) in parallel\n", arch + 1);
  469. fprintf(f, "# number of implementations\n");
  470. fprintf(f, "%u\n", max_impl);
  471. for (nimpl = 0; nimpl < max_impl; nimpl++)
  472. {
  473. dump_per_arch_model_file(f, model, arch, nimpl);
  474. }
  475. }
  476. }
  477. static void initialize_per_arch_model(struct starpu_perfmodel_per_arch *per_arch_model)
  478. {
  479. per_arch_model->history = NULL;
  480. per_arch_model->list = NULL;
  481. per_arch_model->regression.nsample = 0;
  482. per_arch_model->regression.valid = 0;
  483. per_arch_model->regression.nl_valid = 0;
  484. }
  485. static void initialize_model(struct starpu_perfmodel *model)
  486. {
  487. unsigned arch;
  488. unsigned nimpl;
  489. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  490. {
  491. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  492. {
  493. initialize_per_arch_model(&model->per_arch[arch][nimpl]);
  494. }
  495. }
  496. }
  497. static void get_model_debug_path(struct starpu_perfmodel *model, const char *arch, char *path, size_t maxlen)
  498. {
  499. STARPU_ASSERT(path);
  500. _starpu_get_perf_model_dir_debug(path, maxlen);
  501. strncat(path, model->symbol, maxlen);
  502. char hostname[65];
  503. _starpu_gethostname(hostname, sizeof(hostname));
  504. strncat(path, ".", maxlen);
  505. strncat(path, hostname, maxlen);
  506. strncat(path, ".", maxlen);
  507. strncat(path, arch, maxlen);
  508. strncat(path, ".debug", maxlen);
  509. }
  510. /*
  511. * Returns 0 is the model was already loaded, 1 otherwise.
  512. */
  513. int _starpu_register_model(struct starpu_perfmodel *model)
  514. {
  515. /* If the model has already been loaded, there is nothing to do */
  516. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  517. if (model->is_loaded)
  518. {
  519. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  520. return 0;
  521. }
  522. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  523. /* We have to make sure the model has not been loaded since the
  524. * last time we took the lock */
  525. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  526. if (model->is_loaded)
  527. {
  528. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  529. return 0;
  530. }
  531. /* add the model to a linked list */
  532. struct _starpu_perfmodel_list *node = (struct _starpu_perfmodel_list *) malloc(sizeof(struct _starpu_perfmodel_list));
  533. node->model = model;
  534. //model->debug_modelid = debug_modelid++;
  535. /* put this model at the beginning of the list */
  536. node->next = registered_models;
  537. registered_models = node;
  538. #ifdef STARPU_MODEL_DEBUG
  539. _starpu_create_sampling_directory_if_needed();
  540. unsigned arch;
  541. unsigned nimpl;
  542. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  543. {
  544. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  545. {
  546. starpu_perfmodel_debugfilepath(model, arch, model->per_arch[arch][nimpl].debug_path, 256, nimpl);
  547. }
  548. }
  549. #endif
  550. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  551. return 1;
  552. }
  553. static void get_model_path(struct starpu_perfmodel *model, char *path, size_t maxlen)
  554. {
  555. _starpu_get_perf_model_dir_codelets(path, maxlen);
  556. strncat(path, model->symbol, maxlen);
  557. char hostname[65];
  558. _starpu_gethostname(hostname, sizeof(hostname));
  559. strncat(path, ".", maxlen);
  560. strncat(path, hostname, maxlen);
  561. }
  562. static void save_history_based_model(struct starpu_perfmodel *model)
  563. {
  564. STARPU_ASSERT(model);
  565. STARPU_ASSERT(model->symbol);
  566. /* TODO checks */
  567. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  568. char path[256];
  569. get_model_path(model, path, 256);
  570. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  571. /* overwrite existing file, or create it */
  572. FILE *f;
  573. f = fopen(path, "w+");
  574. STARPU_ASSERT_MSG(f, "Could not save performance model %s\n", path);
  575. dump_model_file(f, model);
  576. fclose(f);
  577. }
  578. static void _starpu_dump_registered_models(void)
  579. {
  580. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  581. struct _starpu_perfmodel_list *node;
  582. node = registered_models;
  583. _STARPU_DEBUG("DUMP MODELS !\n");
  584. while (node)
  585. {
  586. save_history_based_model(node->model);
  587. node = node->next;
  588. }
  589. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  590. }
  591. void _starpu_initialize_registered_performance_models(void)
  592. {
  593. registered_models = NULL;
  594. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  595. }
  596. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  597. {
  598. unsigned arch;
  599. unsigned nimpl;
  600. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  601. {
  602. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  603. {
  604. struct starpu_perfmodel_per_arch *archmodel = &model->per_arch[arch][nimpl];
  605. struct starpu_perfmodel_history_list *list, *plist;
  606. struct starpu_perfmodel_history_table *entry, *tmp;
  607. HASH_ITER(hh, archmodel->history, entry, tmp)
  608. {
  609. HASH_DEL(archmodel->history, entry);
  610. free(entry);
  611. }
  612. archmodel->history = NULL;
  613. list = archmodel->list;
  614. while (list)
  615. {
  616. free(list->entry);
  617. plist = list;
  618. list = list->next;
  619. free(plist);
  620. }
  621. archmodel->list = NULL;
  622. }
  623. }
  624. model->is_loaded = 0;
  625. }
  626. void _starpu_deinitialize_registered_performance_models(void)
  627. {
  628. if (_starpu_get_calibrate_flag())
  629. _starpu_dump_registered_models();
  630. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  631. struct _starpu_perfmodel_list *node, *pnode;
  632. node = registered_models;
  633. _STARPU_DEBUG("FREE MODELS !\n");
  634. while (node)
  635. {
  636. struct starpu_perfmodel *model = node->model;
  637. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  638. _starpu_deinitialize_performance_model(model);
  639. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  640. pnode = node;
  641. node = node->next;
  642. free(pnode);
  643. }
  644. registered_models = NULL;
  645. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  646. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  647. }
  648. /*
  649. * XXX: We should probably factorize the beginning of the _starpu_load_*_model
  650. * functions. This is a bit tricky though, because we must be sure to unlock
  651. * registered_models_rwlock at the right place.
  652. */
  653. void _starpu_load_per_arch_based_model(struct starpu_perfmodel *model)
  654. {
  655. STARPU_ASSERT(model && model->symbol);
  656. int already_loaded;
  657. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  658. already_loaded = model->is_loaded;
  659. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  660. if (already_loaded)
  661. return;
  662. /* The model is still not loaded so we grab the lock in write mode, and
  663. * if it's not loaded once we have the lock, we do load it. */
  664. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  665. /* Was the model initialized since the previous test ? */
  666. if (model->is_loaded)
  667. {
  668. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  669. return;
  670. }
  671. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  672. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  673. }
  674. void _starpu_load_common_based_model(struct starpu_perfmodel *model)
  675. {
  676. STARPU_ASSERT(model && model->symbol);
  677. int already_loaded;
  678. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  679. already_loaded = model->is_loaded;
  680. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  681. if (already_loaded)
  682. return;
  683. /* The model is still not loaded so we grab the lock in write mode, and
  684. * if it's not loaded once we have the lock, we do load it. */
  685. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  686. /* Was the model initialized since the previous test ? */
  687. if (model->is_loaded)
  688. {
  689. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  690. return;
  691. }
  692. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  693. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  694. }
  695. /* We first try to grab the global lock in read mode to check whether the model
  696. * was loaded or not (this is very likely to have been already loaded). If the
  697. * model was not loaded yet, we take the lock in write mode, and if the model
  698. * is still not loaded once we have the lock, we do load it. */
  699. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  700. {
  701. STARPU_ASSERT(model);
  702. STARPU_ASSERT(model->symbol);
  703. int already_loaded;
  704. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  705. already_loaded = model->is_loaded;
  706. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  707. if (already_loaded)
  708. return;
  709. /* The model is still not loaded so we grab the lock in write mode, and
  710. * if it's not loaded once we have the lock, we do load it. */
  711. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  712. /* Was the model initialized since the previous test ? */
  713. if (model->is_loaded)
  714. {
  715. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  716. return;
  717. }
  718. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  719. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  720. /* make sure the performance model directory exists (or create it) */
  721. _starpu_create_sampling_directory_if_needed();
  722. char path[256];
  723. get_model_path(model, path, 256);
  724. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  725. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  726. model->benchmarking = calibrate_flag;
  727. /* try to open an existing file and load it */
  728. int res;
  729. res = access(path, F_OK);
  730. if (res == 0)
  731. {
  732. if (calibrate_flag == 2)
  733. {
  734. /* The user specified that the performance model should
  735. * be overwritten, so we don't load the existing file !
  736. * */
  737. _STARPU_DEBUG("Overwrite existing file\n");
  738. initialize_model(model);
  739. }
  740. else
  741. {
  742. /* We load the available file */
  743. _STARPU_DEBUG("File exists\n");
  744. FILE *f;
  745. f = fopen(path, "r");
  746. STARPU_ASSERT(f);
  747. parse_model_file(f, model, scan_history);
  748. fclose(f);
  749. }
  750. }
  751. else
  752. {
  753. _STARPU_DEBUG("File does not exists\n");
  754. if (!calibrate_flag)
  755. {
  756. _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);
  757. _starpu_set_calibrate_flag(1);
  758. model->benchmarking = 1;
  759. }
  760. initialize_model(model);
  761. }
  762. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  763. model->is_loaded = 1;
  764. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  765. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  766. }
  767. /* This function is intended to be used by external tools that should read
  768. * the performance model files */
  769. int starpu_perfmodel_list(FILE *output)
  770. {
  771. char path[256];
  772. DIR *dp;
  773. struct dirent *ep;
  774. char perf_model_dir_codelets[256];
  775. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  776. strncpy(path, perf_model_dir_codelets, 256);
  777. dp = opendir(path);
  778. if (dp != NULL)
  779. {
  780. while ((ep = readdir(dp)))
  781. {
  782. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  783. fprintf(output, "file: <%s>\n", ep->d_name);
  784. }
  785. closedir (dp);
  786. }
  787. else
  788. {
  789. _STARPU_DISP("Could not open the perfmodel directory <%s>\n", path);
  790. }
  791. return 0;
  792. }
  793. /* This function is intended to be used by external tools that should read the
  794. * performance model files */
  795. /* TODO: write an clear function, to free symbol and history */
  796. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  797. {
  798. model->symbol = strdup(symbol);
  799. initialize_model(model);
  800. /* where is the file if it exists ? */
  801. char path[256];
  802. get_model_path(model, path, 256);
  803. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  804. /* does it exist ? */
  805. int res;
  806. res = access(path, F_OK);
  807. if (res)
  808. {
  809. const char *dot = strrchr(symbol, '.');
  810. if (dot)
  811. {
  812. char *symbol2 = strdup(symbol);
  813. symbol2[dot-symbol] = '\0';
  814. int ret;
  815. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  816. ret = starpu_perfmodel_load_symbol(symbol2,model);
  817. free(symbol2);
  818. return ret;
  819. }
  820. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  821. return 1;
  822. }
  823. FILE *f = fopen(path, "r");
  824. STARPU_ASSERT(f);
  825. parse_model_file(f, model, 1);
  826. STARPU_ASSERT(fclose(f) == 0);
  827. return 0;
  828. }
  829. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  830. {
  831. free((char *)model->symbol);
  832. _starpu_deinitialize_performance_model(model);
  833. return 0;
  834. }
  835. void starpu_perfmodel_get_arch_name(enum starpu_perfmodel_archtype arch, char *archname, size_t maxlen,unsigned nimpl)
  836. {
  837. if (arch < STARPU_CUDA_DEFAULT)
  838. {
  839. if (arch == STARPU_CPU_DEFAULT)
  840. {
  841. /* NB: We could just use cpu_1 as well ... */
  842. snprintf(archname, maxlen, "cpu_impl_%u",nimpl);
  843. }
  844. else
  845. {
  846. /* For combined CPU workers */
  847. int cpu_count = arch - STARPU_CPU_DEFAULT + 1;
  848. snprintf(archname, maxlen, "cpu_%d_impl_%u", cpu_count,nimpl);
  849. }
  850. }
  851. else if ((STARPU_CUDA_DEFAULT <= arch)
  852. && (arch < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS))
  853. {
  854. int devid = arch - STARPU_CUDA_DEFAULT;
  855. snprintf(archname, maxlen, "cuda_%d_impl_%u", devid,nimpl);
  856. }
  857. else if ((STARPU_OPENCL_DEFAULT <= arch)
  858. && (arch < STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS))
  859. {
  860. int devid = arch - STARPU_OPENCL_DEFAULT;
  861. snprintf(archname, maxlen, "opencl_%d_impl_%u", devid,nimpl);
  862. }
  863. else
  864. {
  865. STARPU_ABORT();
  866. }
  867. }
  868. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  869. enum starpu_perfmodel_archtype arch, char *path, size_t maxlen, unsigned nimpl)
  870. {
  871. char archname[32];
  872. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  873. STARPU_ASSERT(path);
  874. get_model_debug_path(model, archname, path, maxlen);
  875. }
  876. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j, unsigned nimpl)
  877. {
  878. double exp = NAN;
  879. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  880. struct starpu_perfmodel_regression_model *regmodel;
  881. regmodel = &model->per_arch[arch][nimpl].regression;
  882. if (regmodel->valid)
  883. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  884. return exp;
  885. }
  886. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  887. {
  888. double exp = NAN;
  889. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  890. struct starpu_perfmodel_regression_model *regmodel;
  891. regmodel = &model->per_arch[arch][nimpl].regression;
  892. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  893. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  894. else
  895. {
  896. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  897. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  898. struct starpu_perfmodel_history_table *history;
  899. struct starpu_perfmodel_history_table *entry;
  900. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  901. history = per_arch_model->history;
  902. HASH_FIND_UINT32_T(history, &key, entry);
  903. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  904. /* We do not care about racing access to the mean, we only want a
  905. * good-enough estimation, thus simulate taking the rdlock */
  906. ANNOTATE_RWLOCK_ACQUIRED(&model->model_rwlock, 0);
  907. if (entry && entry->history_entry && entry->history_entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  908. exp = entry->history_entry->mean;
  909. else if (!model->benchmarking)
  910. {
  911. char archname[32];
  912. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  913. _STARPU_DISP("Warning: model %s is not calibrated enough for %s, forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname);
  914. _starpu_set_calibrate_flag(1);
  915. model->benchmarking = 1;
  916. }
  917. ANNOTATE_RWLOCK_RELEASED(&model->model_rwlock, 0);
  918. }
  919. return exp;
  920. }
  921. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  922. {
  923. double exp;
  924. struct starpu_perfmodel_per_arch *per_arch_model;
  925. struct starpu_perfmodel_history_entry *entry;
  926. struct starpu_perfmodel_history_table *history, *elt;
  927. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  928. per_arch_model = &model->per_arch[arch][nimpl];
  929. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  930. history = per_arch_model->history;
  931. HASH_FIND_UINT32_T(history, &key, elt);
  932. entry = (elt == NULL) ? NULL : elt->history_entry;
  933. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  934. /* We do not care about racing access to the mean, we only want a
  935. * good-enough estimation, thus simulate taking the rdlock */
  936. ANNOTATE_RWLOCK_ACQUIRED(&model->model_rwlock, 0);
  937. exp = entry?entry->mean:NAN;
  938. if (entry && entry->nsample < _STARPU_CALIBRATION_MINIMUM)
  939. /* TODO: report differently if we've scheduled really enough
  940. * of that task and the scheduler should perhaps put it aside */
  941. /* Not calibrated enough */
  942. exp = NAN;
  943. if (isnan(exp) && !model->benchmarking)
  944. {
  945. char archname[32];
  946. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  947. _STARPU_DISP("Warning: model %s is not calibrated enough for %s, forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname);
  948. _starpu_set_calibrate_flag(1);
  949. model->benchmarking = 1;
  950. }
  951. ANNOTATE_RWLOCK_RELEASED(&model->model_rwlock, 0);
  952. return exp;
  953. }
  954. double starpu_permodel_history_based_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, uint32_t footprint)
  955. {
  956. struct _starpu_job j =
  957. {
  958. .footprint = footprint,
  959. .footprint_is_computed = 1,
  960. };
  961. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  962. }
  963. void _starpu_update_perfmodel_history(struct _starpu_job *j, struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, unsigned cpuid STARPU_ATTRIBUTE_UNUSED, double measured, unsigned nimpl)
  964. {
  965. if (model)
  966. {
  967. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  968. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  969. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  970. {
  971. struct starpu_perfmodel_history_entry *entry;
  972. struct starpu_perfmodel_history_table *elt;
  973. struct starpu_perfmodel_history_list **list;
  974. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  975. list = &per_arch_model->list;
  976. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  977. entry = (elt == NULL) ? NULL : elt->history_entry;
  978. if (!entry)
  979. {
  980. /* this is the first entry with such a footprint */
  981. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  982. STARPU_ASSERT(entry);
  983. entry->mean = measured;
  984. entry->sum = measured;
  985. entry->deviation = 0.0;
  986. entry->sum2 = measured*measured;
  987. entry->size = _starpu_job_get_data_size(model, arch, nimpl, j);
  988. entry->flops = j->task->flops;
  989. entry->footprint = key;
  990. entry->nsample = 1;
  991. insert_history_entry(entry, list, &per_arch_model->history);
  992. }
  993. else
  994. {
  995. /* there is already some entry with the same footprint */
  996. entry->sum += measured;
  997. entry->sum2 += measured*measured;
  998. entry->nsample++;
  999. unsigned n = entry->nsample;
  1000. entry->mean = entry->sum / n;
  1001. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  1002. if (j->task->flops != 0.)
  1003. {
  1004. if (entry->flops == 0.)
  1005. entry->flops = j->task->flops;
  1006. else if (entry->flops != j->task->flops)
  1007. /* Incoherent flops! forget about trying to record flops */
  1008. entry->flops = NAN;
  1009. }
  1010. }
  1011. STARPU_ASSERT(entry);
  1012. }
  1013. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1014. {
  1015. struct starpu_perfmodel_regression_model *reg_model;
  1016. reg_model = &per_arch_model->regression;
  1017. /* update the regression model */
  1018. size_t job_size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1019. double logy, logx;
  1020. logx = log((double)job_size);
  1021. logy = log(measured);
  1022. reg_model->sumlnx += logx;
  1023. reg_model->sumlnx2 += logx*logx;
  1024. reg_model->sumlny += logy;
  1025. reg_model->sumlnxlny += logx*logy;
  1026. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1027. reg_model->minx = job_size;
  1028. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1029. reg_model->maxx = job_size;
  1030. reg_model->nsample++;
  1031. if (VALID_REGRESSION(reg_model))
  1032. {
  1033. unsigned n = reg_model->nsample;
  1034. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1035. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1036. reg_model->beta = num/denom;
  1037. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1038. reg_model->valid = 1;
  1039. }
  1040. }
  1041. #ifdef STARPU_MODEL_DEBUG
  1042. struct starpu_task *task = j->task;
  1043. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1044. if (f == NULL)
  1045. {
  1046. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1047. STARPU_ABORT();
  1048. }
  1049. if (!j->footprint_is_computed)
  1050. (void) _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1051. STARPU_ASSERT(j->footprint_is_computed);
  1052. fprintf(f, "0x%x\t%lu\t%f\t%f\t%f\t%d\t\t", j->footprint, (unsigned long) _starpu_job_get_data_size(model, arch, nimpl, j), measured, task->predicted, task->predicted_transfer, cpuid);
  1053. unsigned i;
  1054. for (i = 0; i < task->cl->nbuffers; i++)
  1055. {
  1056. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1057. STARPU_ASSERT(handle->ops);
  1058. STARPU_ASSERT(handle->ops->display);
  1059. handle->ops->display(handle, f);
  1060. }
  1061. fprintf(f, "\n");
  1062. fclose(f);
  1063. #endif
  1064. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  1065. }
  1066. }
  1067. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned cpuid, unsigned nimpl, double measured)
  1068. {
  1069. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1070. _starpu_load_perfmodel(model);
  1071. /* Record measurement */
  1072. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1073. /* and save perfmodel on termination */
  1074. _starpu_set_calibrate_flag(1);
  1075. }