perfmodel_history.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. /* Parsing MIC devs */
  313. _starpu_drop_comments(f);
  314. ret = fscanf(f, "%u\n", &narchs);
  315. if (ret == 0)
  316. narchs = 0;
  317. archmin += STARPU_MAXOPENCLDEVS;
  318. _STARPU_DEBUG("Parsing %u MIC devices\n", narchs);
  319. if (narchs > 0)
  320. {
  321. parse_arch(f, model, scan_history,
  322. archmin,
  323. archmin + STARPU_MIN(narchs, STARPU_MAXMICDEVS),
  324. narchs > STARPU_MAXMICDEVS ? narchs - STARPU_MAXMICDEVS : 0);
  325. }
  326. }
  327. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, unsigned arch, unsigned nimpl)
  328. {
  329. struct starpu_perfmodel_per_arch *per_arch_model;
  330. per_arch_model = &model->per_arch[arch][nimpl];
  331. /* count the number of elements in the lists */
  332. struct starpu_perfmodel_history_list *ptr = NULL;
  333. unsigned nentries = 0;
  334. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  335. {
  336. /* Dump the list of all entries in the history */
  337. ptr = per_arch_model->list;
  338. while(ptr)
  339. {
  340. nentries++;
  341. ptr = ptr->next;
  342. }
  343. }
  344. /* header */
  345. char archname[32];
  346. starpu_perfmodel_get_arch_name((enum starpu_perfmodel_archtype) arch, archname, 32, nimpl);
  347. fprintf(f, "# Model for %s\n", archname);
  348. fprintf(f, "# number of entries\n%u\n", nentries);
  349. dump_reg_model(f, model, arch, nimpl);
  350. /* Dump the history into the model file in case it is necessary */
  351. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  352. {
  353. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean\t\tdev\t\tsum\t\tsum2\t\tn\n");
  354. ptr = per_arch_model->list;
  355. while (ptr)
  356. {
  357. dump_history_entry(f, ptr->entry);
  358. ptr = ptr->next;
  359. }
  360. }
  361. fprintf(f, "\n##################\n");
  362. }
  363. static unsigned get_n_entries(struct starpu_perfmodel *model, unsigned arch, unsigned impl)
  364. {
  365. struct starpu_perfmodel_per_arch *per_arch_model;
  366. per_arch_model = &model->per_arch[arch][impl];
  367. /* count the number of elements in the lists */
  368. struct starpu_perfmodel_history_list *ptr = NULL;
  369. unsigned nentries = 0;
  370. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  371. {
  372. /* Dump the list of all entries in the history */
  373. ptr = per_arch_model->list;
  374. while(ptr)
  375. {
  376. nentries++;
  377. ptr = ptr->next;
  378. }
  379. }
  380. return nentries;
  381. }
  382. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  383. {
  384. unsigned narch[4] = { 0, 0, 0, 0};
  385. unsigned arch, arch_base = 0, my_narch = 0;
  386. unsigned nimpl;
  387. unsigned idx = 0;
  388. /* Finding the number of archs to write for each kind of device */
  389. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  390. {
  391. switch (arch)
  392. {
  393. case STARPU_CUDA_DEFAULT:
  394. case STARPU_OPENCL_DEFAULT:
  395. case STARPU_MIC_DEFAULT:
  396. arch_base = arch;
  397. idx++;
  398. break;
  399. default:
  400. break;
  401. }
  402. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  403. {
  404. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  405. if (get_n_entries(model, arch, nimpl))
  406. {
  407. narch[idx]=arch-arch_base+1;
  408. break;
  409. }
  410. }
  411. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  412. {
  413. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  414. if (model->per_arch[arch][nimpl].regression.nsample)
  415. {
  416. narch[idx]=arch-arch_base+1;
  417. break;
  418. }
  419. }
  420. else
  421. {
  422. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %d", model->type);
  423. }
  424. }
  425. /* Writing stuff */
  426. char *name = "unknown";
  427. unsigned substract_to_arch = 0;
  428. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  429. {
  430. unsigned char arch_already_visited = 0;
  431. switch (arch)
  432. {
  433. case STARPU_CPU_DEFAULT:
  434. name = "CPU";
  435. my_narch = narch[0];
  436. break;
  437. case STARPU_CUDA_DEFAULT:
  438. name = "CUDA";
  439. substract_to_arch = STARPU_MAXCPUS;
  440. my_narch = narch[1];
  441. break;
  442. case STARPU_OPENCL_DEFAULT:
  443. name = "OPENCL";
  444. my_narch = narch[2];
  445. break;
  446. case STARPU_MIC_DEFAULT:
  447. name = "MIC";
  448. my_narch = narch[3];
  449. break;
  450. default:
  451. /* The current worker arch was already written,
  452. * we don't need to write it again */
  453. arch_already_visited = 1;
  454. break;
  455. }
  456. if (!arch_already_visited)
  457. {
  458. arch_base = arch;
  459. fprintf(f, "##################\n");
  460. fprintf(f, "# %ss\n", name);
  461. fprintf(f, "# number of %s architectures\n", name);
  462. fprintf(f, "%u\n", my_narch);
  463. }
  464. unsigned max_impl = 0;
  465. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  466. {
  467. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  468. if (get_n_entries(model, arch, nimpl))
  469. max_impl = nimpl + 1;
  470. }
  471. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  472. {
  473. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  474. if (model->per_arch[arch][nimpl].regression.nsample)
  475. max_impl = nimpl + 1;
  476. }
  477. else
  478. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %u", arch);
  479. if (arch >= my_narch + arch_base)
  480. continue;
  481. fprintf(f, "###########\n");
  482. if (substract_to_arch)
  483. fprintf(f, "# %s_%u\n", name, arch - substract_to_arch);
  484. else
  485. /* CPU */
  486. fprintf(f, "# %u CPU(s) in parallel\n", arch + 1);
  487. fprintf(f, "# number of implementations\n");
  488. fprintf(f, "%u\n", max_impl);
  489. for (nimpl = 0; nimpl < max_impl; nimpl++)
  490. {
  491. dump_per_arch_model_file(f, model, arch, nimpl);
  492. }
  493. }
  494. }
  495. static void initialize_per_arch_model(struct starpu_perfmodel_per_arch *per_arch_model)
  496. {
  497. per_arch_model->history = NULL;
  498. per_arch_model->list = NULL;
  499. per_arch_model->regression.nsample = 0;
  500. per_arch_model->regression.valid = 0;
  501. per_arch_model->regression.nl_valid = 0;
  502. }
  503. static void initialize_model(struct starpu_perfmodel *model)
  504. {
  505. unsigned arch;
  506. unsigned nimpl;
  507. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  508. {
  509. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  510. {
  511. initialize_per_arch_model(&model->per_arch[arch][nimpl]);
  512. }
  513. }
  514. }
  515. static void get_model_debug_path(struct starpu_perfmodel *model, const char *arch, char *path, size_t maxlen)
  516. {
  517. STARPU_ASSERT(path);
  518. _starpu_get_perf_model_dir_debug(path, maxlen);
  519. strncat(path, model->symbol, maxlen);
  520. char hostname[65];
  521. _starpu_gethostname(hostname, sizeof(hostname));
  522. strncat(path, ".", maxlen);
  523. strncat(path, hostname, maxlen);
  524. strncat(path, ".", maxlen);
  525. strncat(path, arch, maxlen);
  526. strncat(path, ".debug", maxlen);
  527. }
  528. /*
  529. * Returns 0 is the model was already loaded, 1 otherwise.
  530. */
  531. int _starpu_register_model(struct starpu_perfmodel *model)
  532. {
  533. /* If the model has already been loaded, there is nothing to do */
  534. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  535. if (model->is_loaded)
  536. {
  537. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  538. return 0;
  539. }
  540. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  541. /* We have to make sure the model has not been loaded since the
  542. * last time we took the lock */
  543. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  544. if (model->is_loaded)
  545. {
  546. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  547. return 0;
  548. }
  549. /* add the model to a linked list */
  550. struct _starpu_perfmodel_list *node = (struct _starpu_perfmodel_list *) malloc(sizeof(struct _starpu_perfmodel_list));
  551. node->model = model;
  552. //model->debug_modelid = debug_modelid++;
  553. /* put this model at the beginning of the list */
  554. node->next = registered_models;
  555. registered_models = node;
  556. #ifdef STARPU_MODEL_DEBUG
  557. _starpu_create_sampling_directory_if_needed();
  558. unsigned arch;
  559. unsigned nimpl;
  560. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  561. {
  562. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  563. {
  564. starpu_perfmodel_debugfilepath(model, arch, model->per_arch[arch][nimpl].debug_path, 256, nimpl);
  565. }
  566. }
  567. #endif
  568. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  569. return 1;
  570. }
  571. static void get_model_path(struct starpu_perfmodel *model, char *path, size_t maxlen)
  572. {
  573. _starpu_get_perf_model_dir_codelets(path, maxlen);
  574. strncat(path, model->symbol, maxlen);
  575. char hostname[65];
  576. _starpu_gethostname(hostname, sizeof(hostname));
  577. strncat(path, ".", maxlen);
  578. strncat(path, hostname, maxlen);
  579. }
  580. static void save_history_based_model(struct starpu_perfmodel *model)
  581. {
  582. STARPU_ASSERT(model);
  583. STARPU_ASSERT(model->symbol);
  584. /* TODO checks */
  585. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  586. char path[256];
  587. get_model_path(model, path, 256);
  588. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  589. /* overwrite existing file, or create it */
  590. FILE *f;
  591. f = fopen(path, "w+");
  592. STARPU_ASSERT_MSG(f, "Could not save performance model %s\n", path);
  593. dump_model_file(f, model);
  594. fclose(f);
  595. }
  596. static void _starpu_dump_registered_models(void)
  597. {
  598. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  599. struct _starpu_perfmodel_list *node;
  600. node = registered_models;
  601. _STARPU_DEBUG("DUMP MODELS !\n");
  602. while (node)
  603. {
  604. save_history_based_model(node->model);
  605. node = node->next;
  606. }
  607. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  608. }
  609. void _starpu_initialize_registered_performance_models(void)
  610. {
  611. registered_models = NULL;
  612. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  613. }
  614. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  615. {
  616. unsigned arch;
  617. unsigned nimpl;
  618. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  619. {
  620. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  621. {
  622. struct starpu_perfmodel_per_arch *archmodel = &model->per_arch[arch][nimpl];
  623. struct starpu_perfmodel_history_list *list, *plist;
  624. struct starpu_perfmodel_history_table *entry, *tmp;
  625. HASH_ITER(hh, archmodel->history, entry, tmp)
  626. {
  627. HASH_DEL(archmodel->history, entry);
  628. free(entry);
  629. }
  630. archmodel->history = NULL;
  631. list = archmodel->list;
  632. while (list)
  633. {
  634. free(list->entry);
  635. plist = list;
  636. list = list->next;
  637. free(plist);
  638. }
  639. archmodel->list = NULL;
  640. }
  641. }
  642. model->is_loaded = 0;
  643. }
  644. void _starpu_deinitialize_registered_performance_models(void)
  645. {
  646. if (_starpu_get_calibrate_flag())
  647. _starpu_dump_registered_models();
  648. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  649. struct _starpu_perfmodel_list *node, *pnode;
  650. node = registered_models;
  651. _STARPU_DEBUG("FREE MODELS !\n");
  652. while (node)
  653. {
  654. struct starpu_perfmodel *model = node->model;
  655. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  656. _starpu_deinitialize_performance_model(model);
  657. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  658. pnode = node;
  659. node = node->next;
  660. free(pnode);
  661. }
  662. registered_models = NULL;
  663. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  664. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  665. }
  666. /*
  667. * XXX: We should probably factorize the beginning of the _starpu_load_*_model
  668. * functions. This is a bit tricky though, because we must be sure to unlock
  669. * registered_models_rwlock at the right place.
  670. */
  671. void _starpu_load_per_arch_based_model(struct starpu_perfmodel *model)
  672. {
  673. STARPU_ASSERT(model && model->symbol);
  674. int already_loaded;
  675. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  676. already_loaded = model->is_loaded;
  677. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  678. if (already_loaded)
  679. return;
  680. /* The model is still not loaded so we grab the lock in write mode, and
  681. * if it's not loaded once we have the lock, we do load it. */
  682. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  683. /* Was the model initialized since the previous test ? */
  684. if (model->is_loaded)
  685. {
  686. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  687. return;
  688. }
  689. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  690. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  691. }
  692. void _starpu_load_common_based_model(struct starpu_perfmodel *model)
  693. {
  694. STARPU_ASSERT(model && model->symbol);
  695. int already_loaded;
  696. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  697. already_loaded = model->is_loaded;
  698. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  699. if (already_loaded)
  700. return;
  701. /* The model is still not loaded so we grab the lock in write mode, and
  702. * if it's not loaded once we have the lock, we do load it. */
  703. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  704. /* Was the model initialized since the previous test ? */
  705. if (model->is_loaded)
  706. {
  707. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  708. return;
  709. }
  710. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  711. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  712. }
  713. /* We first try to grab the global lock in read mode to check whether the model
  714. * was loaded or not (this is very likely to have been already loaded). If the
  715. * model was not loaded yet, we take the lock in write mode, and if the model
  716. * is still not loaded once we have the lock, we do load it. */
  717. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  718. {
  719. STARPU_ASSERT(model);
  720. STARPU_ASSERT(model->symbol);
  721. int already_loaded;
  722. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  723. already_loaded = model->is_loaded;
  724. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  725. if (already_loaded)
  726. return;
  727. /* The model is still not loaded so we grab the lock in write mode, and
  728. * if it's not loaded once we have the lock, we do load it. */
  729. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  730. /* Was the model initialized since the previous test ? */
  731. if (model->is_loaded)
  732. {
  733. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  734. return;
  735. }
  736. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  737. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  738. /* make sure the performance model directory exists (or create it) */
  739. _starpu_create_sampling_directory_if_needed();
  740. char path[256];
  741. get_model_path(model, path, 256);
  742. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  743. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  744. model->benchmarking = calibrate_flag;
  745. /* try to open an existing file and load it */
  746. int res;
  747. res = access(path, F_OK);
  748. if (res == 0)
  749. {
  750. if (calibrate_flag == 2)
  751. {
  752. /* The user specified that the performance model should
  753. * be overwritten, so we don't load the existing file !
  754. * */
  755. _STARPU_DEBUG("Overwrite existing file\n");
  756. initialize_model(model);
  757. }
  758. else
  759. {
  760. /* We load the available file */
  761. _STARPU_DEBUG("File exists\n");
  762. FILE *f;
  763. f = fopen(path, "r");
  764. STARPU_ASSERT(f);
  765. parse_model_file(f, model, scan_history);
  766. fclose(f);
  767. }
  768. }
  769. else
  770. {
  771. _STARPU_DEBUG("File does not exists\n");
  772. if (!calibrate_flag)
  773. {
  774. _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);
  775. _starpu_set_calibrate_flag(1);
  776. model->benchmarking = 1;
  777. }
  778. initialize_model(model);
  779. }
  780. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  781. model->is_loaded = 1;
  782. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  783. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  784. }
  785. /* This function is intended to be used by external tools that should read
  786. * the performance model files */
  787. int starpu_perfmodel_list(FILE *output)
  788. {
  789. char path[256];
  790. DIR *dp;
  791. struct dirent *ep;
  792. char perf_model_dir_codelets[256];
  793. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  794. strncpy(path, perf_model_dir_codelets, 256);
  795. dp = opendir(path);
  796. if (dp != NULL)
  797. {
  798. while ((ep = readdir(dp)))
  799. {
  800. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  801. fprintf(output, "file: <%s>\n", ep->d_name);
  802. }
  803. closedir (dp);
  804. }
  805. else
  806. {
  807. _STARPU_DISP("Could not open the perfmodel directory <%s>\n", path);
  808. }
  809. return 0;
  810. }
  811. /* This function is intended to be used by external tools that should read the
  812. * performance model files */
  813. /* TODO: write an clear function, to free symbol and history */
  814. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  815. {
  816. model->symbol = strdup(symbol);
  817. initialize_model(model);
  818. /* where is the file if it exists ? */
  819. char path[256];
  820. get_model_path(model, path, 256);
  821. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  822. /* does it exist ? */
  823. int res;
  824. res = access(path, F_OK);
  825. if (res)
  826. {
  827. const char *dot = strrchr(symbol, '.');
  828. if (dot)
  829. {
  830. char *symbol2 = strdup(symbol);
  831. symbol2[dot-symbol] = '\0';
  832. int ret;
  833. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  834. ret = starpu_perfmodel_load_symbol(symbol2,model);
  835. free(symbol2);
  836. return ret;
  837. }
  838. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  839. return 1;
  840. }
  841. FILE *f = fopen(path, "r");
  842. STARPU_ASSERT(f);
  843. parse_model_file(f, model, 1);
  844. STARPU_ASSERT(fclose(f) == 0);
  845. return 0;
  846. }
  847. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  848. {
  849. free((char *)model->symbol);
  850. _starpu_deinitialize_performance_model(model);
  851. return 0;
  852. }
  853. void starpu_perfmodel_get_arch_name(enum starpu_perfmodel_archtype arch, char *archname, size_t maxlen,unsigned nimpl)
  854. {
  855. if (arch < STARPU_CUDA_DEFAULT)
  856. {
  857. if (arch == STARPU_CPU_DEFAULT)
  858. {
  859. /* NB: We could just use cpu_1 as well ... */
  860. snprintf(archname, maxlen, "cpu_impl_%u",nimpl);
  861. }
  862. else
  863. {
  864. /* For combined CPU workers */
  865. int cpu_count = arch - STARPU_CPU_DEFAULT + 1;
  866. snprintf(archname, maxlen, "cpu_%d_impl_%u", cpu_count,nimpl);
  867. }
  868. }
  869. else if ((STARPU_CUDA_DEFAULT <= arch)
  870. && (arch < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS))
  871. {
  872. int devid = arch - STARPU_CUDA_DEFAULT;
  873. snprintf(archname, maxlen, "cuda_%d_impl_%u", devid,nimpl);
  874. }
  875. else if ((STARPU_OPENCL_DEFAULT <= arch)
  876. && (arch < STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS))
  877. {
  878. int devid = arch - STARPU_OPENCL_DEFAULT;
  879. snprintf(archname, maxlen, "opencl_%d_impl_%u", devid,nimpl);
  880. }
  881. else if ((STARPU_MIC_DEFAULT <= arch)
  882. && (arch < STARPU_MIC_DEFAULT + STARPU_MAXMICDEVS))
  883. {
  884. int devid = arch - STARPU_MIC_DEFAULT;
  885. snprintf(archname, maxlen, "mic_%d_impl_%u", devid, nimpl);
  886. }
  887. else
  888. {
  889. STARPU_ABORT();
  890. }
  891. }
  892. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  893. enum starpu_perfmodel_archtype arch, char *path, size_t maxlen, unsigned nimpl)
  894. {
  895. char archname[32];
  896. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  897. STARPU_ASSERT(path);
  898. get_model_debug_path(model, archname, path, maxlen);
  899. }
  900. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j, unsigned nimpl)
  901. {
  902. double exp = NAN;
  903. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  904. struct starpu_perfmodel_regression_model *regmodel;
  905. regmodel = &model->per_arch[arch][nimpl].regression;
  906. if (regmodel->valid)
  907. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  908. return exp;
  909. }
  910. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  911. {
  912. double exp = NAN;
  913. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  914. struct starpu_perfmodel_regression_model *regmodel;
  915. regmodel = &model->per_arch[arch][nimpl].regression;
  916. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  917. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  918. else
  919. {
  920. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  921. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  922. struct starpu_perfmodel_history_table *history;
  923. struct starpu_perfmodel_history_table *entry;
  924. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  925. history = per_arch_model->history;
  926. HASH_FIND_UINT32_T(history, &key, entry);
  927. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  928. /* We do not care about racing access to the mean, we only want a
  929. * good-enough estimation, thus simulate taking the rdlock */
  930. ANNOTATE_RWLOCK_ACQUIRED(&model->model_rwlock, 0);
  931. if (entry && entry->history_entry && entry->history_entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  932. exp = entry->history_entry->mean;
  933. else if (!model->benchmarking)
  934. {
  935. char archname[32];
  936. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  937. _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);
  938. _starpu_set_calibrate_flag(1);
  939. model->benchmarking = 1;
  940. }
  941. ANNOTATE_RWLOCK_RELEASED(&model->model_rwlock, 0);
  942. }
  943. return exp;
  944. }
  945. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  946. {
  947. double exp;
  948. struct starpu_perfmodel_per_arch *per_arch_model;
  949. struct starpu_perfmodel_history_entry *entry;
  950. struct starpu_perfmodel_history_table *history, *elt;
  951. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  952. per_arch_model = &model->per_arch[arch][nimpl];
  953. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  954. history = per_arch_model->history;
  955. HASH_FIND_UINT32_T(history, &key, elt);
  956. entry = (elt == NULL) ? NULL : elt->history_entry;
  957. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  958. /* We do not care about racing access to the mean, we only want a
  959. * good-enough estimation, thus simulate taking the rdlock */
  960. ANNOTATE_RWLOCK_ACQUIRED(&model->model_rwlock, 0);
  961. exp = entry?entry->mean:NAN;
  962. if (entry && entry->nsample < _STARPU_CALIBRATION_MINIMUM)
  963. /* TODO: report differently if we've scheduled really enough
  964. * of that task and the scheduler should perhaps put it aside */
  965. /* Not calibrated enough */
  966. exp = NAN;
  967. if (isnan(exp) && !model->benchmarking)
  968. {
  969. char archname[32];
  970. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  971. _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);
  972. _starpu_set_calibrate_flag(1);
  973. model->benchmarking = 1;
  974. }
  975. ANNOTATE_RWLOCK_RELEASED(&model->model_rwlock, 0);
  976. return exp;
  977. }
  978. double starpu_permodel_history_based_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, uint32_t footprint)
  979. {
  980. struct _starpu_job j =
  981. {
  982. .footprint = footprint,
  983. .footprint_is_computed = 1,
  984. };
  985. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  986. }
  987. 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)
  988. {
  989. if (model)
  990. {
  991. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  992. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  993. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  994. {
  995. struct starpu_perfmodel_history_entry *entry;
  996. struct starpu_perfmodel_history_table *elt;
  997. struct starpu_perfmodel_history_list **list;
  998. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  999. list = &per_arch_model->list;
  1000. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  1001. entry = (elt == NULL) ? NULL : elt->history_entry;
  1002. if (!entry)
  1003. {
  1004. /* this is the first entry with such a footprint */
  1005. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  1006. STARPU_ASSERT(entry);
  1007. entry->mean = measured;
  1008. entry->sum = measured;
  1009. entry->deviation = 0.0;
  1010. entry->sum2 = measured*measured;
  1011. entry->size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1012. entry->flops = j->task->flops;
  1013. entry->footprint = key;
  1014. entry->nsample = 1;
  1015. insert_history_entry(entry, list, &per_arch_model->history);
  1016. }
  1017. else
  1018. {
  1019. /* there is already some entry with the same footprint */
  1020. entry->sum += measured;
  1021. entry->sum2 += measured*measured;
  1022. entry->nsample++;
  1023. unsigned n = entry->nsample;
  1024. entry->mean = entry->sum / n;
  1025. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  1026. if (j->task->flops != 0.)
  1027. {
  1028. if (entry->flops == 0.)
  1029. entry->flops = j->task->flops;
  1030. else if (entry->flops != j->task->flops)
  1031. /* Incoherent flops! forget about trying to record flops */
  1032. entry->flops = NAN;
  1033. }
  1034. }
  1035. STARPU_ASSERT(entry);
  1036. }
  1037. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1038. {
  1039. struct starpu_perfmodel_regression_model *reg_model;
  1040. reg_model = &per_arch_model->regression;
  1041. /* update the regression model */
  1042. size_t job_size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1043. double logy, logx;
  1044. logx = log((double)job_size);
  1045. logy = log(measured);
  1046. reg_model->sumlnx += logx;
  1047. reg_model->sumlnx2 += logx*logx;
  1048. reg_model->sumlny += logy;
  1049. reg_model->sumlnxlny += logx*logy;
  1050. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1051. reg_model->minx = job_size;
  1052. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1053. reg_model->maxx = job_size;
  1054. reg_model->nsample++;
  1055. if (VALID_REGRESSION(reg_model))
  1056. {
  1057. unsigned n = reg_model->nsample;
  1058. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1059. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1060. reg_model->beta = num/denom;
  1061. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1062. reg_model->valid = 1;
  1063. }
  1064. }
  1065. #ifdef STARPU_MODEL_DEBUG
  1066. struct starpu_task *task = j->task;
  1067. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1068. if (f == NULL)
  1069. {
  1070. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1071. STARPU_ABORT();
  1072. }
  1073. if (!j->footprint_is_computed)
  1074. (void) _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1075. STARPU_ASSERT(j->footprint_is_computed);
  1076. 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);
  1077. unsigned i;
  1078. for (i = 0; i < task->cl->nbuffers; i++)
  1079. {
  1080. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1081. STARPU_ASSERT(handle->ops);
  1082. STARPU_ASSERT(handle->ops->display);
  1083. handle->ops->display(handle, f);
  1084. }
  1085. fprintf(f, "\n");
  1086. fclose(f);
  1087. #endif
  1088. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  1089. }
  1090. }
  1091. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned cpuid, unsigned nimpl, double measured)
  1092. {
  1093. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1094. _starpu_load_perfmodel(model);
  1095. /* Record measurement */
  1096. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1097. /* and save perfmodel on termination */
  1098. _starpu_set_calibrate_flag(1);
  1099. }