perfmodel_history.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013,2016-2017 Inria
  4. * Copyright (C) 2008-2018 Université de Bordeaux
  5. * Copyright (C) 2010-2017 CNRS
  6. * Copyright (C) 2013 Thibaut Lambert
  7. * Copyright (C) 2011 Télécom-SudParis
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  21. #include <dirent.h>
  22. #include <sys/stat.h>
  23. #endif
  24. #include <errno.h>
  25. #include <common/config.h>
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #include <common/utils.h>
  30. #include <core/perfmodel/perfmodel.h>
  31. #include <core/jobs.h>
  32. #include <core/workers.h>
  33. #include <datawizard/datawizard.h>
  34. #include <core/perfmodel/regression.h>
  35. #include <core/perfmodel/multiple_regression.h>
  36. #include <common/config.h>
  37. #include <starpu_parameters.h>
  38. #include <common/uthash.h>
  39. #include <limits.h>
  40. #include <core/task.h>
  41. #ifdef STARPU_HAVE_WINDOWS
  42. #include <windows.h>
  43. #endif
  44. #define HASH_ADD_UINT32_T(head,field,add) HASH_ADD(hh,head,field,sizeof(uint32_t),add)
  45. #define HASH_FIND_UINT32_T(head,find,out) HASH_FIND(hh,head,find,sizeof(uint32_t),out)
  46. #define STR_SHORT_LENGTH 32
  47. #define STR_LONG_LENGTH 256
  48. #define STR_VERY_LONG_LENGTH 1024
  49. static struct starpu_perfmodel_arch **arch_combs;
  50. static int current_arch_comb;
  51. static int nb_arch_combs;
  52. static starpu_pthread_rwlock_t arch_combs_mutex;
  53. static int historymaxerror;
  54. static char ignore_devid[STARPU_ANY_WORKER];
  55. /* How many executions a codelet will have to be measured before we
  56. * consider that calibration will provide a value good enough for scheduling */
  57. unsigned _starpu_calibration_minimum;
  58. struct starpu_perfmodel_history_table
  59. {
  60. UT_hash_handle hh;
  61. uint32_t footprint;
  62. struct starpu_perfmodel_history_entry *history_entry;
  63. };
  64. /* We want more than 10% variance on X to trust regression */
  65. #define VALID_REGRESSION(reg_model) \
  66. ((reg_model)->minx < (9*(reg_model)->maxx)/10 && (reg_model)->nsample >= _starpu_calibration_minimum)
  67. static starpu_pthread_rwlock_t registered_models_rwlock;
  68. LIST_TYPE(_starpu_perfmodel,
  69. struct starpu_perfmodel *model;
  70. )
  71. static struct _starpu_perfmodel_list registered_models;
  72. void _starpu_perfmodel_malloc_per_arch(struct starpu_perfmodel *model, int comb, int nb_impl)
  73. {
  74. int i;
  75. _STARPU_MALLOC(model->state->per_arch[comb], nb_impl*sizeof(struct starpu_perfmodel_per_arch));
  76. for(i = 0; i < nb_impl; i++)
  77. {
  78. memset(&model->state->per_arch[comb][i], 0, sizeof(struct starpu_perfmodel_per_arch));
  79. }
  80. model->state->nimpls_set[comb] = nb_impl;
  81. }
  82. void _starpu_perfmodel_malloc_per_arch_is_set(struct starpu_perfmodel *model, int comb, int nb_impl)
  83. {
  84. int i;
  85. _STARPU_MALLOC(model->state->per_arch_is_set[comb], nb_impl*sizeof(int));
  86. for(i = 0; i < nb_impl; i++)
  87. {
  88. model->state->per_arch_is_set[comb][i] = 0;
  89. }
  90. }
  91. int _starpu_perfmodel_arch_comb_get(int ndevices, struct starpu_perfmodel_device *devices)
  92. {
  93. int comb, ncomb;
  94. ncomb = current_arch_comb;
  95. for(comb = 0; comb < ncomb; comb++)
  96. {
  97. int found = 0;
  98. if(arch_combs[comb]->ndevices == ndevices)
  99. {
  100. int dev1, dev2;
  101. int nfounded = 0;
  102. for(dev1 = 0; dev1 < arch_combs[comb]->ndevices; dev1++)
  103. {
  104. for(dev2 = 0; dev2 < ndevices; dev2++)
  105. {
  106. if(arch_combs[comb]->devices[dev1].type == devices[dev2].type &&
  107. (ignore_devid[devices[dev2].type] ||
  108. arch_combs[comb]->devices[dev1].devid == devices[dev2].devid) &&
  109. arch_combs[comb]->devices[dev1].ncores == devices[dev2].ncores)
  110. nfounded++;
  111. }
  112. }
  113. if(nfounded == ndevices)
  114. found = 1;
  115. }
  116. if (found)
  117. return comb;
  118. }
  119. return -1;
  120. }
  121. int starpu_perfmodel_arch_comb_get(int ndevices, struct starpu_perfmodel_device *devices)
  122. {
  123. int ret;
  124. STARPU_PTHREAD_RWLOCK_RDLOCK(&arch_combs_mutex);
  125. ret = _starpu_perfmodel_arch_comb_get(ndevices, devices);
  126. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  127. return ret;
  128. }
  129. int starpu_perfmodel_arch_comb_add(int ndevices, struct starpu_perfmodel_device* devices)
  130. {
  131. STARPU_PTHREAD_RWLOCK_WRLOCK(&arch_combs_mutex);
  132. int comb = _starpu_perfmodel_arch_comb_get(ndevices, devices);
  133. if (comb != -1)
  134. {
  135. /* Somebody else added it in between */
  136. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  137. return comb;
  138. }
  139. if (current_arch_comb >= nb_arch_combs)
  140. {
  141. // We need to allocate more arch_combs
  142. nb_arch_combs = current_arch_comb+10;
  143. _STARPU_REALLOC(arch_combs, nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  144. }
  145. _STARPU_MALLOC(arch_combs[current_arch_comb], sizeof(struct starpu_perfmodel_arch));
  146. _STARPU_MALLOC(arch_combs[current_arch_comb]->devices, ndevices*sizeof(struct starpu_perfmodel_device));
  147. arch_combs[current_arch_comb]->ndevices = ndevices;
  148. int dev;
  149. for(dev = 0; dev < ndevices; dev++)
  150. {
  151. arch_combs[current_arch_comb]->devices[dev].type = devices[dev].type;
  152. arch_combs[current_arch_comb]->devices[dev].devid = devices[dev].devid;
  153. arch_combs[current_arch_comb]->devices[dev].ncores = devices[dev].ncores;
  154. }
  155. comb = current_arch_comb++;
  156. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  157. return comb;
  158. }
  159. static void _free_arch_combs(void)
  160. {
  161. int i;
  162. STARPU_PTHREAD_RWLOCK_WRLOCK(&arch_combs_mutex);
  163. for(i = 0; i < current_arch_comb; i++)
  164. {
  165. free(arch_combs[i]->devices);
  166. free(arch_combs[i]);
  167. }
  168. current_arch_comb = 0;
  169. free(arch_combs);
  170. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  171. STARPU_PTHREAD_RWLOCK_DESTROY(&arch_combs_mutex);
  172. }
  173. int starpu_perfmodel_get_narch_combs()
  174. {
  175. return current_arch_comb;
  176. }
  177. struct starpu_perfmodel_arch *starpu_perfmodel_arch_comb_fetch(int comb)
  178. {
  179. return arch_combs[comb];
  180. }
  181. size_t _starpu_job_get_data_size(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, unsigned impl, struct _starpu_job *j)
  182. {
  183. struct starpu_task *task = j->task;
  184. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  185. if (model && model->state->per_arch && comb != -1 && model->state->per_arch[comb] && model->state->per_arch[comb][impl].size_base)
  186. {
  187. return model->state->per_arch[comb][impl].size_base(task, arch, impl);
  188. }
  189. else if (model && model->size_base)
  190. {
  191. return model->size_base(task, impl);
  192. }
  193. else
  194. {
  195. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  196. size_t size = 0;
  197. unsigned buffer;
  198. for (buffer = 0; buffer < nbuffers; buffer++)
  199. {
  200. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  201. size += _starpu_data_get_size(handle);
  202. }
  203. return size;
  204. }
  205. }
  206. /*
  207. * History based model
  208. */
  209. static void insert_history_entry(struct starpu_perfmodel_history_entry *entry, struct starpu_perfmodel_history_list **list, struct starpu_perfmodel_history_table **history_ptr)
  210. {
  211. struct starpu_perfmodel_history_list *link;
  212. struct starpu_perfmodel_history_table *table;
  213. _STARPU_MALLOC(link, sizeof(struct starpu_perfmodel_history_list));
  214. link->next = *list;
  215. link->entry = entry;
  216. *list = link;
  217. /* detect concurrency issue */
  218. //HASH_FIND_UINT32_T(*history_ptr, &entry->footprint, table);
  219. //STARPU_ASSERT(table == NULL);
  220. _STARPU_MALLOC(table, sizeof(*table));
  221. table->footprint = entry->footprint;
  222. table->history_entry = entry;
  223. HASH_ADD_UINT32_T(*history_ptr, footprint, table);
  224. }
  225. #ifndef STARPU_SIMGRID
  226. static void check_reg_model(struct starpu_perfmodel *model, int comb, int impl)
  227. {
  228. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  229. struct starpu_perfmodel_regression_model *reg_model = &per_arch_model->regression;
  230. /*
  231. * Linear Regression model
  232. */
  233. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  234. double alpha = nan(""), beta = nan("");
  235. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  236. {
  237. if (reg_model->nsample > 1)
  238. {
  239. alpha = reg_model->alpha;
  240. beta = reg_model->beta;
  241. }
  242. }
  243. /* TODO: check:
  244. * reg_model->sumlnx
  245. * reg_model->sumlnx2
  246. * reg_model->sumlny
  247. * reg_model->sumlnxlny
  248. * alpha
  249. * beta
  250. * reg_model->minx
  251. * reg_model->maxx
  252. */
  253. (void)alpha;
  254. (void)beta;
  255. /*
  256. * Non-Linear Regression model
  257. */
  258. double a = nan(""), b = nan(""), c = nan("");
  259. if (model->type == STARPU_NL_REGRESSION_BASED)
  260. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  261. /* TODO: check:
  262. * a
  263. * b
  264. * c
  265. */
  266. /*
  267. * Multiple Regression Model
  268. */
  269. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  270. {
  271. /* TODO: check: */
  272. }
  273. }
  274. static void dump_reg_model(FILE *f, struct starpu_perfmodel *model, int comb, int impl)
  275. {
  276. struct starpu_perfmodel_per_arch *per_arch_model;
  277. per_arch_model = &model->state->per_arch[comb][impl];
  278. struct starpu_perfmodel_regression_model *reg_model;
  279. reg_model = &per_arch_model->regression;
  280. /*
  281. * Linear Regression model
  282. */
  283. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  284. double alpha = nan(""), beta = nan("");
  285. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  286. {
  287. if (reg_model->nsample > 1)
  288. {
  289. alpha = reg_model->alpha;
  290. beta = reg_model->beta;
  291. }
  292. }
  293. fprintf(f, "# sumlnx\tsumlnx2\t\tsumlny\t\tsumlnxlny\talpha\t\tbeta\t\tn\tminx\t\tmaxx\n");
  294. fprintf(f, "%-15e\t%-15e\t%-15e\t%-15e\t", reg_model->sumlnx, reg_model->sumlnx2, reg_model->sumlny, reg_model->sumlnxlny);
  295. _starpu_write_double(f, "%-15e", alpha);
  296. fprintf(f, "\t");
  297. _starpu_write_double(f, "%-15e", beta);
  298. fprintf(f, "\t%u\t%-15lu\t%-15lu\n", reg_model->nsample, reg_model->minx, reg_model->maxx);
  299. /*
  300. * Non-Linear Regression model
  301. */
  302. double a = nan(""), b = nan(""), c = nan("");
  303. if (model->type == STARPU_NL_REGRESSION_BASED)
  304. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  305. fprintf(f, "# a\t\tb\t\tc\n");
  306. _starpu_write_double(f, "%-15e", a);
  307. fprintf(f, "\t");
  308. _starpu_write_double(f, "%-15e", b);
  309. fprintf(f, "\t");
  310. _starpu_write_double(f, "%-15e", c);
  311. fprintf(f, "\n");
  312. /*
  313. * Multiple Regression Model
  314. */
  315. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  316. {
  317. if (reg_model->ncoeff==0 && model->ncombinations!=0 && model->combinations!=NULL)
  318. {
  319. reg_model->ncoeff = model->ncombinations + 1;
  320. }
  321. _STARPU_MALLOC(reg_model->coeff, reg_model->ncoeff*sizeof(double));
  322. _starpu_multiple_regression(per_arch_model->list, reg_model->coeff, reg_model->ncoeff, model->nparameters, model->parameters_names, model->combinations, model->symbol);
  323. fprintf(f, "# n\tintercept\t");
  324. if (reg_model->ncoeff==0 || model->ncombinations==0 || model->combinations==NULL)
  325. fprintf(f, "\n1\tnan");
  326. else
  327. {
  328. unsigned i;
  329. for (i=0; i < model->ncombinations; i++)
  330. {
  331. if (model->parameters_names == NULL)
  332. fprintf(f, "c%u", i+1);
  333. else
  334. {
  335. unsigned j;
  336. int first=1;
  337. for(j=0; j < model->nparameters; j++)
  338. {
  339. if (model->combinations[i][j] > 0)
  340. {
  341. if (first)
  342. first=0;
  343. else
  344. fprintf(f, "*");
  345. if(model->parameters_names[j] != NULL)
  346. fprintf(f, "%s", model->parameters_names[j]);
  347. else
  348. fprintf(f, "P%u", j);
  349. if (model->combinations[i][j] > 1)
  350. fprintf(f, "^%d", model->combinations[i][j]);
  351. }
  352. }
  353. }
  354. fprintf(f, "\t\t");
  355. }
  356. fprintf(f, "\n%u", reg_model->ncoeff);
  357. for (i=0; i < reg_model->ncoeff; i++)
  358. fprintf(f, "\t%-15e", reg_model->coeff[i]);
  359. }
  360. }
  361. }
  362. #endif
  363. static void scan_reg_model(FILE *f, const char *path, struct starpu_perfmodel_regression_model *reg_model, enum starpu_perfmodel_type model_type)
  364. {
  365. int res;
  366. /*
  367. * Linear Regression model
  368. */
  369. _starpu_drop_comments(f);
  370. res = fscanf(f, "%le\t%le\t%le\t%le\t", &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny, &reg_model->sumlnxlny);
  371. STARPU_ASSERT_MSG(res == 4, "Incorrect performance model file %s", path);
  372. res = _starpu_read_double(f, "%le", &reg_model->alpha);
  373. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  374. res = _starpu_read_double(f, "\t%le", &reg_model->beta);
  375. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  376. res = fscanf(f, "\t%u\t%lu\t%lu\n", &reg_model->nsample, &reg_model->minx, &reg_model->maxx);
  377. STARPU_ASSERT_MSG(res == 3, "Incorrect performance model file %s", path);
  378. /* If any of the parameters describing the linear regression model is NaN, the model is invalid */
  379. unsigned invalid = (isnan(reg_model->alpha)||isnan(reg_model->beta));
  380. reg_model->valid = !invalid && VALID_REGRESSION(reg_model);
  381. /*
  382. * Non-Linear Regression model
  383. */
  384. _starpu_drop_comments(f);
  385. res = _starpu_read_double(f, "%le", &reg_model->a);
  386. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  387. res = _starpu_read_double(f, "\t%le", &reg_model->b);
  388. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  389. res = _starpu_read_double(f, "%le", &reg_model->c);
  390. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  391. res = fscanf(f, "\n");
  392. STARPU_ASSERT_MSG(res == 0, "Incorrect performance model file %s", path);
  393. /* If any of the parameters describing the non-linear regression model is NaN, the model is invalid */
  394. unsigned nl_invalid = (isnan(reg_model->a)||isnan(reg_model->b)||isnan(reg_model->c));
  395. reg_model->nl_valid = !nl_invalid && VALID_REGRESSION(reg_model);
  396. /*
  397. * Multiple Regression Model
  398. */
  399. if (model_type == STARPU_MULTIPLE_REGRESSION_BASED)
  400. {
  401. _starpu_drop_comments(f);
  402. // Read how many coefficients is there
  403. res = fscanf(f, "%u", &reg_model->ncoeff);
  404. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  405. _STARPU_MALLOC(reg_model->coeff, reg_model->ncoeff*sizeof(double));
  406. unsigned multi_invalid = 0;
  407. unsigned i;
  408. for (i=0; i < reg_model->ncoeff; i++)
  409. {
  410. res = _starpu_read_double(f, "%le", &reg_model->coeff[i]);
  411. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  412. multi_invalid = (multi_invalid||isnan(reg_model->coeff[i]));
  413. }
  414. reg_model->multi_valid = !multi_invalid;
  415. res = fscanf(f, "\n");
  416. STARPU_ASSERT_MSG(res == 0, "Incorrect performance model file %s", path);
  417. }
  418. }
  419. #ifndef STARPU_SIMGRID
  420. static void check_history_entry(struct starpu_perfmodel_history_entry *entry)
  421. {
  422. STARPU_ASSERT_MSG(entry->deviation >= 0, "entry=%p, entry->deviation=%lf\n", entry, entry->deviation);
  423. STARPU_ASSERT_MSG(entry->sum >= 0, "entry=%p, entry->sum=%lf\n", entry, entry->sum);
  424. STARPU_ASSERT_MSG(entry->sum2 >= 0, "entry=%p, entry->sum2=%lf\n", entry, entry->sum2);
  425. STARPU_ASSERT_MSG(entry->mean >= 0, "entry=%p, entry->mean=%lf\n", entry, entry->mean);
  426. STARPU_ASSERT_MSG(isnan(entry->flops)||entry->flops >= 0, "entry=%p, entry->flops=%lf\n", entry, entry->flops);
  427. STARPU_ASSERT_MSG(entry->duration >= 0, "entry=%p, entry->duration=%lf\n", entry, entry->duration);
  428. }
  429. static void dump_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  430. {
  431. fprintf(f, "%08x\t%-15lu\t%-15e\t%-15e\t%-15e\t%-15e\t%-15e\t%u\n", entry->footprint, (unsigned long) entry->size, entry->flops, entry->mean, entry->deviation, entry->sum, entry->sum2, entry->nsample);
  432. }
  433. #endif
  434. static void scan_history_entry(FILE *f, const char *path, struct starpu_perfmodel_history_entry *entry)
  435. {
  436. int res;
  437. _starpu_drop_comments(f);
  438. /* In case entry is NULL, we just drop these values */
  439. unsigned nsample;
  440. uint32_t footprint;
  441. unsigned long size; /* in bytes */
  442. double flops;
  443. double mean;
  444. double deviation;
  445. double sum;
  446. double sum2;
  447. char line[STR_LONG_LENGTH];
  448. char *ret;
  449. ret = fgets(line, sizeof(line), f);
  450. STARPU_ASSERT(ret);
  451. STARPU_ASSERT(strchr(line, '\n'));
  452. /* Read the values from the file */
  453. 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);
  454. if (res != 8)
  455. {
  456. flops = 0.;
  457. /* Read the values from the file */
  458. res = sscanf(line, "%x\t%lu\t%le\t%le\t%le\t%le\t%u", &footprint, &size, &mean, &deviation, &sum, &sum2, &nsample);
  459. STARPU_ASSERT_MSG(res == 7, "Incorrect performance model file %s", path);
  460. }
  461. if (entry)
  462. {
  463. STARPU_ASSERT_MSG(flops >=0, "Negative flops %lf in performance model file %s", flops, path);
  464. STARPU_ASSERT_MSG(mean >=0, "Negative mean %lf in performance model file %s", mean, path);
  465. STARPU_ASSERT_MSG(deviation >=0, "Negative deviation %lf in performance model file %s", deviation, path);
  466. STARPU_ASSERT_MSG(sum >=0, "Negative sum %lf in performance model file %s", sum, path);
  467. STARPU_ASSERT_MSG(sum2 >=0, "Negative sum2 %lf in performance model file %s", sum2, path);
  468. entry->footprint = footprint;
  469. entry->size = size;
  470. entry->flops = flops;
  471. entry->mean = mean;
  472. entry->deviation = deviation;
  473. entry->sum = sum;
  474. entry->sum2 = sum2;
  475. entry->nsample = nsample;
  476. }
  477. }
  478. static void parse_per_arch_model_file(FILE *f, const char *path, struct starpu_perfmodel_per_arch *per_arch_model, unsigned scan_history, enum starpu_perfmodel_type model_type)
  479. {
  480. unsigned nentries;
  481. _starpu_drop_comments(f);
  482. int res = fscanf(f, "%u\n", &nentries);
  483. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  484. scan_reg_model(f, path, &per_arch_model->regression, model_type);
  485. /* parse entries */
  486. unsigned i;
  487. for (i = 0; i < nentries; i++)
  488. {
  489. struct starpu_perfmodel_history_entry *entry = NULL;
  490. if (scan_history)
  491. {
  492. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  493. /* Tell helgrind that we do not care about
  494. * racing access to the sampling, we only want a
  495. * good-enough estimation */
  496. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  497. STARPU_HG_DISABLE_CHECKING(entry->mean);
  498. entry->nerror = 0;
  499. }
  500. scan_history_entry(f, path, entry);
  501. /* insert the entry in the hashtable and the list structures */
  502. /* TODO: Insert it at the end of the list, to avoid reversing
  503. * the order... But efficiently! We may have a lot of entries */
  504. if (scan_history)
  505. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  506. }
  507. }
  508. static void parse_arch(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history, int comb)
  509. {
  510. struct starpu_perfmodel_per_arch dummy;
  511. unsigned nimpls, impl, i, ret;
  512. /* Parsing number of implementation */
  513. _starpu_drop_comments(f);
  514. ret = fscanf(f, "%u\n", &nimpls);
  515. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  516. if( model != NULL)
  517. {
  518. /* Parsing each implementation */
  519. unsigned implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  520. model->state->nimpls[comb] = implmax;
  521. if (!model->state->per_arch[comb])
  522. {
  523. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  524. }
  525. if (!model->state->per_arch_is_set[comb])
  526. {
  527. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  528. }
  529. for (impl = 0; impl < implmax; impl++)
  530. {
  531. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  532. model->state->per_arch_is_set[comb][impl] = 1;
  533. parse_per_arch_model_file(f, path, per_arch_model, scan_history, model->type);
  534. }
  535. }
  536. else
  537. {
  538. impl = 0;
  539. }
  540. /* if the number of implementation is greater than STARPU_MAXIMPLEMENTATIONS
  541. * we skip the last implementation */
  542. for (i = impl; i < nimpls; i++)
  543. {
  544. if( model != NULL)
  545. parse_per_arch_model_file(f, path, &dummy, 0, model->type);
  546. else
  547. parse_per_arch_model_file(f, path, &dummy, 0, 0);
  548. }
  549. }
  550. static enum starpu_worker_archtype _get_enum_type(int type)
  551. {
  552. switch(type)
  553. {
  554. case 0:
  555. return STARPU_CPU_WORKER;
  556. case 1:
  557. return STARPU_CUDA_WORKER;
  558. case 2:
  559. return STARPU_OPENCL_WORKER;
  560. case 3:
  561. return STARPU_MIC_WORKER;
  562. case 4:
  563. return STARPU_SCC_WORKER;
  564. case 5:
  565. return STARPU_MPI_MS_WORKER;
  566. default:
  567. STARPU_ABORT();
  568. }
  569. }
  570. static void parse_comb(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history, int comb)
  571. {
  572. int ndevices = 0;
  573. _starpu_drop_comments(f);
  574. int ret = fscanf(f, "%d\n", &ndevices );
  575. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  576. struct starpu_perfmodel_device devices[ndevices];
  577. int dev;
  578. for(dev = 0; dev < ndevices; dev++)
  579. {
  580. enum starpu_worker_archtype dev_type;
  581. _starpu_drop_comments(f);
  582. int type;
  583. ret = fscanf(f, "%d\n", &type);
  584. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  585. dev_type = _get_enum_type(type);
  586. int dev_id;
  587. _starpu_drop_comments(f);
  588. ret = fscanf(f, "%d\n", &dev_id);
  589. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  590. int ncores;
  591. _starpu_drop_comments(f);
  592. ret = fscanf(f, "%d\n", &ncores);
  593. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  594. devices[dev].type = dev_type;
  595. devices[dev].devid = dev_id;
  596. devices[dev].ncores = ncores;
  597. }
  598. int id_comb = starpu_perfmodel_arch_comb_get(ndevices, devices);
  599. if(id_comb == -1)
  600. id_comb = starpu_perfmodel_arch_comb_add(ndevices, devices);
  601. model->state->combs[comb] = id_comb;
  602. parse_arch(f, path, model, scan_history, id_comb);
  603. }
  604. static int parse_model_file(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history)
  605. {
  606. int ret, version=0;
  607. /* First check that it's not empty (very common corruption result, for
  608. * which there is no solution) */
  609. fseek(f, 0, SEEK_END);
  610. long pos = ftell(f);
  611. if (pos == 0)
  612. {
  613. _STARPU_DISP("Performance model file %s is empty, ignoring it\n", path);
  614. return 1;
  615. }
  616. rewind(f);
  617. /* Parsing performance model version */
  618. _starpu_drop_comments(f);
  619. ret = fscanf(f, "%d\n", &version);
  620. STARPU_ASSERT_MSG(version == _STARPU_PERFMODEL_VERSION, "Incorrect performance model file %s with a model version %d not being the current model version (%d)\n", path,
  621. version, _STARPU_PERFMODEL_VERSION);
  622. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  623. int ncombs = 0;
  624. _starpu_drop_comments(f);
  625. ret = fscanf(f, "%d\n", &ncombs);
  626. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  627. if(ncombs > 0)
  628. {
  629. model->state->ncombs = ncombs;
  630. }
  631. if (ncombs > model->state->ncombs_set)
  632. {
  633. // The model has more combs than the original number of arch_combs, we need to reallocate
  634. _starpu_perfmodel_realloc(model, ncombs);
  635. }
  636. int comb;
  637. for(comb = 0; comb < ncombs; comb++)
  638. parse_comb(f, path, model, scan_history, comb);
  639. return 0;
  640. }
  641. #ifndef STARPU_SIMGRID
  642. static void check_per_arch_model(struct starpu_perfmodel *model, int comb, unsigned impl)
  643. {
  644. struct starpu_perfmodel_per_arch *per_arch_model;
  645. per_arch_model = &model->state->per_arch[comb][impl];
  646. /* count the number of elements in the lists */
  647. struct starpu_perfmodel_history_list *ptr = NULL;
  648. unsigned nentries = 0;
  649. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  650. {
  651. /* Dump the list of all entries in the history */
  652. ptr = per_arch_model->list;
  653. while(ptr)
  654. {
  655. nentries++;
  656. ptr = ptr->next;
  657. }
  658. }
  659. /* header */
  660. char archname[STR_SHORT_LENGTH];
  661. starpu_perfmodel_get_arch_name(arch_combs[comb], archname, sizeof(archname), impl);
  662. STARPU_ASSERT(strlen(archname)>0);
  663. check_reg_model(model, comb, impl);
  664. /* Dump the history into the model file in case it is necessary */
  665. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  666. {
  667. ptr = per_arch_model->list;
  668. while (ptr)
  669. {
  670. check_history_entry(ptr->entry);
  671. ptr = ptr->next;
  672. }
  673. }
  674. }
  675. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, int comb, unsigned impl)
  676. {
  677. struct starpu_perfmodel_per_arch *per_arch_model;
  678. per_arch_model = &model->state->per_arch[comb][impl];
  679. /* count the number of elements in the lists */
  680. struct starpu_perfmodel_history_list *ptr = NULL;
  681. unsigned nentries = 0;
  682. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  683. {
  684. /* Dump the list of all entries in the history */
  685. ptr = per_arch_model->list;
  686. while(ptr)
  687. {
  688. nentries++;
  689. ptr = ptr->next;
  690. }
  691. }
  692. /* header */
  693. char archname[STR_SHORT_LENGTH];
  694. starpu_perfmodel_get_arch_name(arch_combs[comb], archname, sizeof(archname), impl);
  695. fprintf(f, "#####\n");
  696. fprintf(f, "# Model for %s\n", archname);
  697. fprintf(f, "# number of entries\n%u\n", nentries);
  698. dump_reg_model(f, model, comb, impl);
  699. /* Dump the history into the model file in case it is necessary */
  700. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  701. {
  702. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean (us)\tdev (us)\tsum\t\tsum2\t\tn\n");
  703. ptr = per_arch_model->list;
  704. while (ptr)
  705. {
  706. dump_history_entry(f, ptr->entry);
  707. ptr = ptr->next;
  708. }
  709. }
  710. fprintf(f, "\n");
  711. }
  712. static void check_model(struct starpu_perfmodel *model)
  713. {
  714. int ncombs = model->state->ncombs;
  715. STARPU_ASSERT(ncombs >= 0);
  716. int i, impl, dev;
  717. for(i = 0; i < ncombs; i++)
  718. {
  719. int comb = model->state->combs[i];
  720. STARPU_ASSERT(comb >= 0);
  721. int ndevices = arch_combs[comb]->ndevices;
  722. STARPU_ASSERT(ndevices >= 1);
  723. for(dev = 0; dev < ndevices; dev++)
  724. {
  725. STARPU_ASSERT(arch_combs[comb]->devices[dev].type >= 0);
  726. STARPU_ASSERT(arch_combs[comb]->devices[dev].type <= 5);
  727. STARPU_ASSERT(arch_combs[comb]->devices[dev].devid >= 0);
  728. STARPU_ASSERT(arch_combs[comb]->devices[dev].ncores >= 0);
  729. }
  730. int nimpls = model->state->nimpls[comb];
  731. STARPU_ASSERT(nimpls >= 1);
  732. for (impl = 0; impl < nimpls; impl++)
  733. {
  734. check_per_arch_model(model, comb, impl);
  735. }
  736. }
  737. }
  738. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  739. {
  740. fprintf(f, "##################\n");
  741. fprintf(f, "# Performance Model Version\n");
  742. fprintf(f, "%d\n\n", _STARPU_PERFMODEL_VERSION);
  743. int ncombs = model->state->ncombs;
  744. fprintf(f, "####################\n");
  745. fprintf(f, "# COMBs\n");
  746. fprintf(f, "# number of combinations\n");
  747. fprintf(f, "%d\n", ncombs);
  748. int i, impl, dev;
  749. for(i = 0; i < ncombs; i++)
  750. {
  751. int comb = model->state->combs[i];
  752. int ndevices = arch_combs[comb]->ndevices;
  753. fprintf(f, "####################\n");
  754. fprintf(f, "# COMB_%d\n", comb);
  755. fprintf(f, "# number of types devices\n");
  756. fprintf(f, "%d\n", ndevices);
  757. for(dev = 0; dev < ndevices; dev++)
  758. {
  759. fprintf(f, "####################\n");
  760. fprintf(f, "# DEV_%d\n", dev);
  761. fprintf(f, "# device type (CPU - 0, CUDA - 1, OPENCL - 2, MIC - 3, SCC - 4, MPI_MS - 5)\n");
  762. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].type);
  763. fprintf(f, "####################\n");
  764. fprintf(f, "# DEV_%d\n", dev);
  765. fprintf(f, "# device id \n");
  766. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].devid);
  767. fprintf(f, "####################\n");
  768. fprintf(f, "# DEV_%d\n", dev);
  769. fprintf(f, "# number of cores \n");
  770. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].ncores);
  771. }
  772. int nimpls = model->state->nimpls[comb];
  773. fprintf(f, "##########\n");
  774. fprintf(f, "# number of implementations\n");
  775. fprintf(f, "%d\n", nimpls);
  776. for (impl = 0; impl < nimpls; impl++)
  777. {
  778. dump_per_arch_model_file(f, model, comb, impl);
  779. }
  780. }
  781. }
  782. #endif
  783. void _starpu_perfmodel_realloc(struct starpu_perfmodel *model, int nb)
  784. {
  785. int i;
  786. STARPU_ASSERT(nb > model->state->ncombs_set);
  787. #ifdef SSIZE_MAX
  788. STARPU_ASSERT((size_t) nb < SSIZE_MAX / sizeof(struct starpu_perfmodel_per_arch*));
  789. #endif
  790. _STARPU_REALLOC(model->state->per_arch, nb*sizeof(struct starpu_perfmodel_per_arch*));
  791. _STARPU_REALLOC(model->state->per_arch_is_set, nb*sizeof(int*));
  792. _STARPU_REALLOC(model->state->nimpls, nb*sizeof(int));
  793. _STARPU_REALLOC(model->state->nimpls_set, nb*sizeof(int));
  794. _STARPU_REALLOC(model->state->combs, nb*sizeof(int));
  795. for(i = model->state->ncombs_set; i < nb; i++)
  796. {
  797. model->state->per_arch[i] = NULL;
  798. model->state->per_arch_is_set[i] = NULL;
  799. model->state->nimpls[i] = 0;
  800. model->state->nimpls_set[i] = 0;
  801. }
  802. model->state->ncombs_set = nb;
  803. }
  804. void starpu_perfmodel_init(struct starpu_perfmodel *model)
  805. {
  806. int already_init;
  807. int ncombs;
  808. STARPU_ASSERT(model);
  809. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  810. already_init = model->is_init;
  811. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  812. if (already_init)
  813. return;
  814. /* The model is still not loaded so we grab the lock in write mode, and
  815. * if it's not loaded once we have the lock, we do load it. */
  816. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  817. /* Was the model initialized since the previous test ? */
  818. if (model->is_init)
  819. {
  820. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  821. return;
  822. }
  823. _STARPU_MALLOC(model->state, sizeof(struct _starpu_perfmodel_state));
  824. STARPU_PTHREAD_RWLOCK_INIT(&model->state->model_rwlock, NULL);
  825. STARPU_PTHREAD_RWLOCK_RDLOCK(&arch_combs_mutex);
  826. model->state->ncombs_set = ncombs = nb_arch_combs;
  827. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  828. _STARPU_CALLOC(model->state->per_arch, ncombs, sizeof(struct starpu_perfmodel_per_arch*));
  829. _STARPU_CALLOC(model->state->per_arch_is_set, ncombs, sizeof(int*));
  830. _STARPU_CALLOC(model->state->nimpls, ncombs, sizeof(int));
  831. _STARPU_CALLOC(model->state->nimpls_set, ncombs, sizeof(int));
  832. _STARPU_MALLOC(model->state->combs, ncombs*sizeof(int));
  833. model->state->ncombs = 0;
  834. /* add the model to a linked list */
  835. struct _starpu_perfmodel *node = _starpu_perfmodel_new();
  836. node->model = model;
  837. //model->debug_modelid = debug_modelid++;
  838. /* put this model at the beginning of the list */
  839. _starpu_perfmodel_list_push_front(&registered_models, node);
  840. model->is_init = 1;
  841. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  842. }
  843. static void get_model_debug_path(struct starpu_perfmodel *model, const char *arch, char *path, size_t maxlen)
  844. {
  845. STARPU_ASSERT(path);
  846. char hostname[STR_LONG_LENGTH];
  847. _starpu_gethostname(hostname, sizeof(hostname));
  848. snprintf(path, maxlen, "%s/%s.%s.%s.debug", _starpu_get_perf_model_dir_debug(), model->symbol, hostname, arch);
  849. }
  850. void starpu_perfmodel_get_model_path(const char *symbol, char *path, size_t maxlen)
  851. {
  852. char hostname[STR_LONG_LENGTH];
  853. _starpu_gethostname(hostname, sizeof(hostname));
  854. const char *dot = strrchr(symbol, '.');
  855. snprintf(path, maxlen, "%s/%s%s%s", _starpu_get_perf_model_dir_codelet(), symbol, dot?"":".", dot?"":hostname);
  856. }
  857. #ifndef STARPU_SIMGRID
  858. static void save_history_based_model(struct starpu_perfmodel *model)
  859. {
  860. STARPU_ASSERT(model);
  861. STARPU_ASSERT(model->symbol);
  862. int locked;
  863. /* TODO checks */
  864. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  865. char path[STR_LONG_LENGTH];
  866. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  867. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  868. /* overwrite existing file, or create it */
  869. FILE *f;
  870. f = fopen(path, "w+");
  871. STARPU_ASSERT_MSG(f, "Could not save performance model %s\n", path);
  872. locked = _starpu_fwrlock(f) == 0;
  873. check_model(model);
  874. _starpu_fftruncate(f, 0);
  875. dump_model_file(f, model);
  876. if (locked)
  877. _starpu_fwrunlock(f);
  878. fclose(f);
  879. }
  880. #endif
  881. static void _starpu_dump_registered_models(void)
  882. {
  883. #ifndef STARPU_SIMGRID
  884. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  885. struct _starpu_perfmodel *node;
  886. _STARPU_DEBUG("DUMP MODELS !\n");
  887. for (node = _starpu_perfmodel_list_begin(&registered_models);
  888. node != _starpu_perfmodel_list_end(&registered_models);
  889. node = _starpu_perfmodel_list_next(node))
  890. {
  891. if (node->model->is_init)
  892. save_history_based_model(node->model);
  893. }
  894. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  895. #endif
  896. }
  897. void starpu_perfmodel_initialize(void)
  898. {
  899. /* make sure the performance model directory exists (or create it) */
  900. _starpu_create_sampling_directory_if_needed();
  901. _starpu_perfmodel_list_init(&registered_models);
  902. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  903. STARPU_PTHREAD_RWLOCK_INIT(&arch_combs_mutex, NULL);
  904. }
  905. void _starpu_initialize_registered_performance_models(void)
  906. {
  907. starpu_perfmodel_initialize();
  908. struct _starpu_machine_config *conf = _starpu_get_machine_config();
  909. unsigned ncores = conf->topology.nhwcpus;
  910. unsigned ncuda = conf->topology.nhwcudagpus;
  911. unsigned nopencl = conf->topology.nhwopenclgpus;
  912. unsigned nmic = 0;
  913. unsigned i;
  914. for(i = 0; i < conf->topology.nhwmicdevices; i++)
  915. nmic += conf->topology.nhwmiccores[i];
  916. unsigned nmpi = 0;
  917. for(i = 0; i < conf->topology.nhwmpidevices; i++)
  918. nmpi += conf->topology.nhwmpicores[i];
  919. unsigned nscc = conf->topology.nhwscc;
  920. // We used to allocate 2**(ncores + ncuda + nopencl + nmic + nscc + nmpi), this is too big
  921. // We now allocate only 2*(ncores + ncuda + nopencl + nmic + nscc + nmpi), and reallocate when necessary in starpu_perfmodel_arch_comb_add
  922. nb_arch_combs = 2 * (ncores + ncuda + nopencl + nmic + nscc + nmpi);
  923. _STARPU_MALLOC(arch_combs, nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  924. current_arch_comb = 0;
  925. historymaxerror = starpu_get_env_number_default("STARPU_HISTORY_MAX_ERROR", STARPU_HISTORYMAXERROR);
  926. _starpu_calibration_minimum = starpu_get_env_number_default("STARPU_CALIBRATE_MINIMUM", 10);
  927. ignore_devid[STARPU_CPU_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_CPU", 1);
  928. ignore_devid[STARPU_CUDA_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_CUDA", 0);
  929. ignore_devid[STARPU_OPENCL_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_OPENCL", 0);
  930. ignore_devid[STARPU_MIC_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_MIC", 0);
  931. ignore_devid[STARPU_MPI_MS_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_MPI_MS", 0);
  932. ignore_devid[STARPU_SCC_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_SCC", 0);
  933. }
  934. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  935. {
  936. if(model->is_init && model->state && model->state->per_arch != NULL)
  937. {
  938. int i;
  939. for(i=0 ; i<model->state->ncombs_set ; i++)
  940. {
  941. if (model->state->per_arch[i])
  942. {
  943. int impl;
  944. for(impl=0 ; impl<model->state->nimpls_set[i] ; impl++)
  945. {
  946. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[i][impl];
  947. if (archmodel->history)
  948. {
  949. struct starpu_perfmodel_history_list *list;
  950. struct starpu_perfmodel_history_table *entry, *tmp;
  951. HASH_ITER(hh, archmodel->history, entry, tmp)
  952. {
  953. HASH_DEL(archmodel->history, entry);
  954. free(entry);
  955. }
  956. archmodel->history = NULL;
  957. list = archmodel->list;
  958. while (list)
  959. {
  960. struct starpu_perfmodel_history_list *plist;
  961. free(list->entry);
  962. plist = list;
  963. list = list->next;
  964. free(plist);
  965. }
  966. archmodel->list = NULL;
  967. }
  968. }
  969. free(model->state->per_arch[i]);
  970. model->state->per_arch[i] = NULL;
  971. free(model->state->per_arch_is_set[i]);
  972. model->state->per_arch_is_set[i] = NULL;
  973. }
  974. }
  975. free(model->state->per_arch);
  976. model->state->per_arch = NULL;
  977. free(model->state->per_arch_is_set);
  978. model->state->per_arch_is_set = NULL;
  979. free(model->state->nimpls);
  980. model->state->nimpls = NULL;
  981. free(model->state->nimpls_set);
  982. model->state->nimpls_set = NULL;
  983. free(model->state->combs);
  984. model->state->combs = NULL;
  985. model->state->ncombs = 0;
  986. }
  987. model->is_init = 0;
  988. model->is_loaded = 0;
  989. }
  990. void _starpu_deinitialize_registered_performance_models(void)
  991. {
  992. if (_starpu_get_calibrate_flag())
  993. _starpu_dump_registered_models();
  994. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  995. struct _starpu_perfmodel *node, *nnode;
  996. _STARPU_DEBUG("FREE MODELS !\n");
  997. for (node = _starpu_perfmodel_list_begin(&registered_models);
  998. node != _starpu_perfmodel_list_end(&registered_models);
  999. node = nnode)
  1000. {
  1001. struct starpu_perfmodel *model = node->model;
  1002. nnode = _starpu_perfmodel_list_next(node);
  1003. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1004. _starpu_deinitialize_performance_model(model);
  1005. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1006. free(node->model->state);
  1007. node->model->state = NULL;
  1008. _starpu_perfmodel_list_erase(&registered_models, node);
  1009. _starpu_perfmodel_delete(node);
  1010. }
  1011. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  1012. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  1013. _free_arch_combs();
  1014. starpu_perfmodel_free_sampling_directories();
  1015. }
  1016. /* We first try to grab the global lock in read mode to check whether the model
  1017. * was loaded or not (this is very likely to have been already loaded). If the
  1018. * model was not loaded yet, we take the lock in write mode, and if the model
  1019. * is still not loaded once we have the lock, we do load it. */
  1020. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  1021. {
  1022. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1023. if(!model->is_loaded)
  1024. {
  1025. char path[STR_LONG_LENGTH];
  1026. // Check if a symbol is defined before trying to load the model from a file
  1027. STARPU_ASSERT_MSG(model->symbol, "history-based performance models must have a symbol");
  1028. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  1029. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  1030. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  1031. model->benchmarking = calibrate_flag;
  1032. model->is_loaded = 1;
  1033. if (calibrate_flag == 2)
  1034. {
  1035. /* The user specified that the performance model should
  1036. * be overwritten, so we don't load the existing file !
  1037. * */
  1038. _STARPU_DEBUG("Overwrite existing file\n");
  1039. }
  1040. else
  1041. {
  1042. /* We try to load the file */
  1043. FILE *f;
  1044. f = fopen(path, "r");
  1045. if (f)
  1046. {
  1047. int locked;
  1048. locked = _starpu_frdlock(f) == 0;
  1049. parse_model_file(f, path, model, scan_history);
  1050. if (locked)
  1051. _starpu_frdunlock(f);
  1052. fclose(f);
  1053. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  1054. }
  1055. else
  1056. {
  1057. _STARPU_DEBUG("Performance model file %s does not exist or is not readable\n", path);
  1058. }
  1059. }
  1060. }
  1061. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1062. }
  1063. void starpu_perfmodel_directory(FILE *output)
  1064. {
  1065. fprintf(output, "directory: <%s>\n", _starpu_get_perf_model_dir_codelet());
  1066. }
  1067. /* This function is intended to be used by external tools that should read
  1068. * the performance model files */
  1069. int starpu_perfmodel_list(FILE *output)
  1070. {
  1071. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  1072. char *path;
  1073. DIR *dp;
  1074. path = _starpu_get_perf_model_dir_codelet();
  1075. dp = opendir(path);
  1076. if (dp != NULL)
  1077. {
  1078. struct dirent *ep;
  1079. while ((ep = readdir(dp)))
  1080. {
  1081. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  1082. fprintf(output, "file: <%s>\n", ep->d_name);
  1083. }
  1084. closedir (dp);
  1085. }
  1086. else
  1087. {
  1088. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  1089. }
  1090. return 0;
  1091. #else
  1092. _STARPU_MSG("Listing perfmodels is not implemented on pure Windows yet\n");
  1093. return 1;
  1094. #endif
  1095. }
  1096. /* This function is intended to be used by external tools that should read the
  1097. * performance model files */
  1098. /* TODO: write an clear function, to free symbol and history */
  1099. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  1100. {
  1101. model->symbol = strdup(symbol);
  1102. /* where is the file if it exists ? */
  1103. char path[STR_LONG_LENGTH];
  1104. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  1105. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  1106. /* does it exist ? */
  1107. int res;
  1108. res = access(path, F_OK);
  1109. if (res)
  1110. {
  1111. const char *dot = strrchr(symbol, '.');
  1112. if (dot)
  1113. {
  1114. char *symbol2 = strdup(symbol);
  1115. symbol2[dot-symbol] = '\0';
  1116. int ret;
  1117. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  1118. ret = starpu_perfmodel_load_symbol(symbol2,model);
  1119. free(symbol2);
  1120. return ret;
  1121. }
  1122. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  1123. return 1;
  1124. }
  1125. return starpu_perfmodel_load_file(path, model);
  1126. }
  1127. int starpu_perfmodel_load_file(const char *filename, struct starpu_perfmodel *model)
  1128. {
  1129. int res, ret = 0;
  1130. FILE *f = fopen(filename, "r");
  1131. int locked;
  1132. STARPU_ASSERT(f);
  1133. starpu_perfmodel_init(model);
  1134. locked = _starpu_frdlock(f) == 0;
  1135. ret = parse_model_file(f, filename, model, 1);
  1136. if (locked)
  1137. _starpu_frdunlock(f);
  1138. res = fclose(f);
  1139. STARPU_ASSERT(res == 0);
  1140. return ret;
  1141. }
  1142. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  1143. {
  1144. if (model->symbol)
  1145. {
  1146. free((char *)model->symbol);
  1147. model->symbol = NULL;
  1148. }
  1149. _starpu_deinitialize_performance_model(model);
  1150. free(model->state);
  1151. model->state = NULL;
  1152. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  1153. struct _starpu_perfmodel *node;
  1154. for (node = _starpu_perfmodel_list_begin(&registered_models);
  1155. node != _starpu_perfmodel_list_end(&registered_models);
  1156. node = _starpu_perfmodel_list_next(node))
  1157. {
  1158. if (node->model == model)
  1159. {
  1160. _starpu_perfmodel_list_erase(&registered_models, node);
  1161. _starpu_perfmodel_delete(node);
  1162. break;
  1163. }
  1164. }
  1165. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  1166. return 0;
  1167. }
  1168. char* starpu_perfmodel_get_archtype_name(enum starpu_worker_archtype archtype)
  1169. {
  1170. switch(archtype)
  1171. {
  1172. case(STARPU_CPU_WORKER):
  1173. return "cpu";
  1174. break;
  1175. case(STARPU_CUDA_WORKER):
  1176. return "cuda";
  1177. break;
  1178. case(STARPU_OPENCL_WORKER):
  1179. return "opencl";
  1180. break;
  1181. case(STARPU_MIC_WORKER):
  1182. return "mic";
  1183. break;
  1184. case(STARPU_SCC_WORKER):
  1185. return "scc";
  1186. break;
  1187. case(STARPU_MPI_MS_WORKER):
  1188. return "mpi_ms";
  1189. break;
  1190. default:
  1191. STARPU_ABORT();
  1192. break;
  1193. }
  1194. }
  1195. void starpu_perfmodel_get_arch_name(struct starpu_perfmodel_arch* arch, char *archname, size_t maxlen,unsigned impl)
  1196. {
  1197. int i;
  1198. int comb = _starpu_perfmodel_create_comb_if_needed(arch);
  1199. STARPU_ASSERT(comb != -1);
  1200. char devices[STR_VERY_LONG_LENGTH];
  1201. int written = 0;
  1202. devices[0] = '\0';
  1203. for(i=0 ; i<arch->ndevices ; i++)
  1204. {
  1205. written += snprintf(devices + written, sizeof(devices)-written, "%s%d%s", starpu_perfmodel_get_archtype_name(arch->devices[i].type), arch->devices[i].devid, i != arch->ndevices-1 ? "_":"");
  1206. }
  1207. snprintf(archname, maxlen, "%s_impl%u (Comb%d)", devices, impl, comb);
  1208. }
  1209. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  1210. struct starpu_perfmodel_arch* arch, char *path, size_t maxlen, unsigned nimpl)
  1211. {
  1212. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1213. STARPU_ASSERT(comb != -1);
  1214. char archname[STR_SHORT_LENGTH];
  1215. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1216. STARPU_ASSERT(path);
  1217. get_model_debug_path(model, archname, path, maxlen);
  1218. }
  1219. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j, unsigned nimpl)
  1220. {
  1221. int comb;
  1222. double exp = NAN;
  1223. size_t size;
  1224. struct starpu_perfmodel_regression_model *regmodel = NULL;
  1225. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1226. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1227. if(comb == -1)
  1228. goto docal;
  1229. if (model->state->per_arch[comb] == NULL)
  1230. // The model has not been executed on this combination
  1231. goto docal;
  1232. regmodel = &model->state->per_arch[comb][nimpl].regression;
  1233. if (regmodel->valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  1234. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  1235. docal:
  1236. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1237. if (isnan(exp) && !model->benchmarking)
  1238. {
  1239. char archname[STR_SHORT_LENGTH];
  1240. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1241. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %lu (only %u measurements from size %lu to %lu), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, (unsigned long) size, regmodel?regmodel->nsample:0, regmodel?regmodel->minx:0, regmodel?regmodel->maxx:0);
  1242. _starpu_set_calibrate_flag(1);
  1243. model->benchmarking = 1;
  1244. }
  1245. return exp;
  1246. }
  1247. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  1248. {
  1249. int comb;
  1250. double exp = NAN;
  1251. size_t size;
  1252. struct starpu_perfmodel_regression_model *regmodel;
  1253. struct starpu_perfmodel_history_table *entry = NULL;
  1254. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1255. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1256. if(comb == -1)
  1257. goto docal;
  1258. if (model->state->per_arch[comb] == NULL)
  1259. // The model has not been executed on this combination
  1260. goto docal;
  1261. regmodel = &model->state->per_arch[comb][nimpl].regression;
  1262. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  1263. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  1264. else
  1265. {
  1266. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1267. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][nimpl];
  1268. struct starpu_perfmodel_history_table *history;
  1269. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  1270. history = per_arch_model->history;
  1271. HASH_FIND_UINT32_T(history, &key, entry);
  1272. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1273. /* Here helgrind would shout that this is unprotected access.
  1274. * We do not care about racing access to the mean, we only want
  1275. * a good-enough estimation */
  1276. if (entry && entry->history_entry && entry->history_entry->nsample >= _starpu_calibration_minimum)
  1277. exp = entry->history_entry->mean;
  1278. docal:
  1279. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1280. if (isnan(exp) && !model->benchmarking)
  1281. {
  1282. char archname[STR_SHORT_LENGTH];
  1283. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1284. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %lu (only %u measurements), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, (unsigned long) size, entry && entry->history_entry ? entry->history_entry->nsample : 0);
  1285. _starpu_set_calibrate_flag(1);
  1286. model->benchmarking = 1;
  1287. }
  1288. }
  1289. return exp;
  1290. }
  1291. double _starpu_multiple_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j, unsigned nimpl)
  1292. {
  1293. int comb;
  1294. double expected_duration=NAN;
  1295. struct starpu_perfmodel_regression_model *reg_model = NULL;
  1296. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1297. if(comb == -1)
  1298. goto docal;
  1299. if (model->state->per_arch[comb] == NULL)
  1300. // The model has not been executed on this combination
  1301. goto docal;
  1302. reg_model = &model->state->per_arch[comb][nimpl].regression;
  1303. if (reg_model->coeff == NULL)
  1304. goto docal;
  1305. double *parameters;
  1306. _STARPU_MALLOC(parameters, model->nparameters*sizeof(double));
  1307. model->parameters(j->task, parameters);
  1308. expected_duration=reg_model->coeff[0];
  1309. unsigned i;
  1310. for (i=0; i < model->ncombinations; i++)
  1311. {
  1312. double parameter_value=1.;
  1313. unsigned k;
  1314. for (k=0; k < model->nparameters; k++)
  1315. parameter_value *= pow(parameters[k],model->combinations[i][k]);
  1316. expected_duration += reg_model->coeff[i+1]*parameter_value;
  1317. }
  1318. docal:
  1319. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1320. if (isnan(expected_duration) && !model->benchmarking)
  1321. {
  1322. char archname[STR_SHORT_LENGTH];
  1323. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1324. _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);
  1325. _starpu_set_calibrate_flag(1);
  1326. model->benchmarking = 1;
  1327. }
  1328. // In the unlikely event that predicted duration is negative
  1329. // in case multiple linear regression is not so accurate
  1330. if (expected_duration < 0 )
  1331. expected_duration = 0.00001;
  1332. //Make sure that the injected time is in milliseconds
  1333. return expected_duration;
  1334. }
  1335. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  1336. {
  1337. int comb;
  1338. double exp = NAN;
  1339. struct starpu_perfmodel_per_arch *per_arch_model;
  1340. struct starpu_perfmodel_history_entry *entry = NULL;
  1341. struct starpu_perfmodel_history_table *history, *elt;
  1342. uint32_t key;
  1343. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1344. if(comb == -1)
  1345. goto docal;
  1346. if (model->state->per_arch[comb] == NULL)
  1347. // The model has not been executed on this combination
  1348. goto docal;
  1349. per_arch_model = &model->state->per_arch[comb][nimpl];
  1350. key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1351. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  1352. history = per_arch_model->history;
  1353. HASH_FIND_UINT32_T(history, &key, elt);
  1354. entry = (elt == NULL) ? NULL : elt->history_entry;
  1355. STARPU_ASSERT_MSG(!entry || entry->mean >= 0, "entry=%p, entry->mean=%lf\n", entry, entry?entry->mean:NAN);
  1356. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1357. /* Here helgrind would shout that this is unprotected access.
  1358. * We do not care about racing access to the mean, we only want
  1359. * a good-enough estimation */
  1360. if (entry && entry->nsample >= _starpu_calibration_minimum)
  1361. {
  1362. STARPU_ASSERT_MSG(entry->mean >= 0, "entry->mean=%lf\n", entry->mean);
  1363. /* TODO: report differently if we've scheduled really enough
  1364. * of that task and the scheduler should perhaps put it aside */
  1365. /* Calibrated enough */
  1366. exp = entry->mean;
  1367. }
  1368. docal:
  1369. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1370. if (isnan(exp) && !model->benchmarking)
  1371. {
  1372. char archname[STR_SHORT_LENGTH];
  1373. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1374. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %ld (only %u measurements), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, j->task?(long int)_starpu_job_get_data_size(model, arch, nimpl, j):-1, entry ? entry->nsample : 0);
  1375. _starpu_set_calibrate_flag(1);
  1376. model->benchmarking = 1;
  1377. }
  1378. STARPU_ASSERT_MSG(isnan(exp)||exp >= 0, "exp=%lf\n", exp);
  1379. return exp;
  1380. }
  1381. double starpu_perfmodel_history_based_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch * arch, uint32_t footprint)
  1382. {
  1383. struct _starpu_job j =
  1384. {
  1385. .footprint = footprint,
  1386. .footprint_is_computed = 1,
  1387. };
  1388. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  1389. }
  1390. int _starpu_perfmodel_create_comb_if_needed(struct starpu_perfmodel_arch* arch)
  1391. {
  1392. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1393. if(comb == -1)
  1394. comb = starpu_perfmodel_arch_comb_add(arch->ndevices, arch->devices);
  1395. return comb;
  1396. }
  1397. void _starpu_update_perfmodel_history(struct _starpu_job *j, struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, unsigned cpuid STARPU_ATTRIBUTE_UNUSED, double measured, unsigned impl)
  1398. {
  1399. STARPU_ASSERT_MSG(measured >= 0, "measured=%lf\n", measured);
  1400. if (model)
  1401. {
  1402. int c;
  1403. unsigned found = 0;
  1404. int comb = _starpu_perfmodel_create_comb_if_needed(arch);
  1405. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1406. for(c = 0; c < model->state->ncombs; c++)
  1407. {
  1408. if(model->state->combs[c] == comb)
  1409. {
  1410. found = 1;
  1411. break;
  1412. }
  1413. }
  1414. if(!found)
  1415. {
  1416. if (model->state->ncombs + 1 >= model->state->ncombs_set)
  1417. {
  1418. // The number of combinations is bigger than the one which was initially allocated, we need to reallocate,
  1419. // do not only reallocate 1 extra comb, rather reallocate 5 to avoid too frequent calls to _starpu_perfmodel_realloc
  1420. _starpu_perfmodel_realloc(model, model->state->ncombs_set+5);
  1421. }
  1422. model->state->combs[model->state->ncombs++] = comb;
  1423. }
  1424. if(!model->state->per_arch[comb])
  1425. {
  1426. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1427. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1428. }
  1429. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  1430. if (model->state->per_arch_is_set[comb][impl] == 0)
  1431. {
  1432. // We are adding a new implementation for the given comb and the given impl
  1433. model->state->nimpls[comb]++;
  1434. model->state->per_arch_is_set[comb][impl] = 1;
  1435. }
  1436. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1437. {
  1438. struct starpu_perfmodel_history_entry *entry;
  1439. struct starpu_perfmodel_history_table *elt;
  1440. struct starpu_perfmodel_history_list **list;
  1441. uint32_t key = _starpu_compute_buffers_footprint(model, arch, impl, j);
  1442. list = &per_arch_model->list;
  1443. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  1444. entry = (elt == NULL) ? NULL : elt->history_entry;
  1445. if (!entry)
  1446. {
  1447. /* this is the first entry with such a footprint */
  1448. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  1449. /* Tell helgrind that we do not care about
  1450. * racing access to the sampling, we only want a
  1451. * good-enough estimation */
  1452. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  1453. STARPU_HG_DISABLE_CHECKING(entry->mean);
  1454. /* Do not take the first measurement into account, it is very often quite bogus */
  1455. /* TODO: it'd be good to use a better estimation heuristic, like the median, or latest n values, etc. */
  1456. entry->mean = 0;
  1457. entry->sum = 0;
  1458. entry->deviation = 0.0;
  1459. entry->sum2 = 0;
  1460. entry->size = _starpu_job_get_data_size(model, arch, impl, j);
  1461. entry->flops = j->task->flops;
  1462. entry->footprint = key;
  1463. entry->nsample = 0;
  1464. entry->nerror = 0;
  1465. insert_history_entry(entry, list, &per_arch_model->history);
  1466. }
  1467. else
  1468. {
  1469. /* There is already an entry with the same footprint */
  1470. double local_deviation = measured/entry->mean;
  1471. if (entry->nsample &&
  1472. (100 * local_deviation > (100 + historymaxerror)
  1473. || (100 / local_deviation > (100 + historymaxerror))))
  1474. {
  1475. entry->nerror++;
  1476. /* More errors than measurements, we're most probably completely wrong, we flush out all the entries */
  1477. if (entry->nerror >= entry->nsample)
  1478. {
  1479. char archname[STR_SHORT_LENGTH];
  1480. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), impl);
  1481. _STARPU_DISP("Too big deviation for model %s on %s: %f vs average %f, %u such errors against %u samples (%+f%%), flushing the performance model. Use the STARPU_HISTORY_MAX_ERROR environement variable to control the threshold (currently %d%%)\n", model->symbol, archname, measured, entry->mean, entry->nerror, entry->nsample, measured * 100. / entry->mean - 100, historymaxerror);
  1482. entry->sum = 0.0;
  1483. entry->sum2 = 0.0;
  1484. entry->nsample = 0;
  1485. entry->nerror = 0;
  1486. entry->mean = 0.0;
  1487. entry->deviation = 0.0;
  1488. }
  1489. }
  1490. else
  1491. {
  1492. entry->sum += measured;
  1493. entry->sum2 += measured*measured;
  1494. entry->nsample++;
  1495. unsigned n = entry->nsample;
  1496. entry->mean = entry->sum / n;
  1497. entry->deviation = sqrt((fabs(entry->sum2 - (entry->sum*entry->sum)/n))/n);
  1498. }
  1499. if (j->task->flops != 0.)
  1500. {
  1501. if (entry->flops == 0.)
  1502. entry->flops = j->task->flops;
  1503. else if (((entry->flops - j->task->flops) / entry->flops) > 0.00001)
  1504. /* Incoherent flops! forget about trying to record flops */
  1505. entry->flops = NAN;
  1506. }
  1507. }
  1508. STARPU_ASSERT(entry);
  1509. }
  1510. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1511. {
  1512. struct starpu_perfmodel_regression_model *reg_model;
  1513. reg_model = &per_arch_model->regression;
  1514. /* update the regression model */
  1515. size_t job_size = _starpu_job_get_data_size(model, arch, impl, j);
  1516. double logy, logx;
  1517. logx = log((double)job_size);
  1518. logy = log(measured);
  1519. reg_model->sumlnx += logx;
  1520. reg_model->sumlnx2 += logx*logx;
  1521. reg_model->sumlny += logy;
  1522. reg_model->sumlnxlny += logx*logy;
  1523. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1524. reg_model->minx = job_size;
  1525. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1526. reg_model->maxx = job_size;
  1527. reg_model->nsample++;
  1528. if (VALID_REGRESSION(reg_model))
  1529. {
  1530. unsigned n = reg_model->nsample;
  1531. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1532. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1533. reg_model->beta = num/denom;
  1534. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1535. reg_model->valid = 1;
  1536. }
  1537. }
  1538. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  1539. {
  1540. struct starpu_perfmodel_history_entry *entry;
  1541. struct starpu_perfmodel_history_list **list;
  1542. list = &per_arch_model->list;
  1543. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  1544. _STARPU_MALLOC(entry->parameters, model->nparameters*sizeof(double));
  1545. model->parameters(j->task, entry->parameters);
  1546. entry->tag = j->task->tag_id;
  1547. STARPU_ASSERT(measured >= 0);
  1548. entry->duration = measured;
  1549. struct starpu_perfmodel_history_list *link;
  1550. _STARPU_MALLOC(link, sizeof(struct starpu_perfmodel_history_list));
  1551. link->next = *list;
  1552. link->entry = entry;
  1553. *list = link;
  1554. }
  1555. #ifdef STARPU_MODEL_DEBUG
  1556. struct starpu_task *task = j->task;
  1557. starpu_perfmodel_debugfilepath(model, arch_combs[comb], per_arch_model->debug_path, STR_LONG_LENGTH, impl);
  1558. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1559. int locked;
  1560. if (f == NULL)
  1561. {
  1562. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1563. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1564. return;
  1565. }
  1566. locked = _starpu_fwrlock(f) == 0;
  1567. if (!j->footprint_is_computed)
  1568. (void) _starpu_compute_buffers_footprint(model, arch, impl, j);
  1569. STARPU_ASSERT(j->footprint_is_computed);
  1570. fprintf(f, "0x%x\t%lu\t%f\t%f\t%f\t%u\t\t", j->footprint, (unsigned long) _starpu_job_get_data_size(model, arch, impl, j), measured, task->predicted, task->predicted_transfer, cpuid);
  1571. unsigned i;
  1572. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1573. for (i = 0; i < nbuffers; i++)
  1574. {
  1575. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1576. STARPU_ASSERT(handle->ops);
  1577. STARPU_ASSERT(handle->ops->display);
  1578. handle->ops->display(handle, f);
  1579. }
  1580. fprintf(f, "\n");
  1581. if (locked)
  1582. _starpu_fwrunlock(f);
  1583. fclose(f);
  1584. #endif
  1585. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1586. }
  1587. }
  1588. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, struct starpu_perfmodel_arch * arch, unsigned cpuid, unsigned nimpl, double measured)
  1589. {
  1590. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1591. #ifdef STARPU_SIMGRID
  1592. STARPU_ASSERT_MSG(0, "We are not supposed to update history when simulating execution");
  1593. #endif
  1594. _starpu_init_and_load_perfmodel(model);
  1595. /* Record measurement */
  1596. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1597. /* and save perfmodel on termination */
  1598. _starpu_set_calibrate_flag(1);
  1599. }
  1600. int starpu_perfmodel_list_combs(FILE *output, struct starpu_perfmodel *model)
  1601. {
  1602. int comb;
  1603. fprintf(output, "Model <%s>\n", model->symbol);
  1604. for(comb = 0; comb < model->state->ncombs; comb++)
  1605. {
  1606. struct starpu_perfmodel_arch *arch;
  1607. int device;
  1608. arch = starpu_perfmodel_arch_comb_fetch(model->state->combs[comb]);
  1609. fprintf(output, "\tComb %d: %d device%s\n", model->state->combs[comb], arch->ndevices, arch->ndevices>1?"s":"");
  1610. for(device=0 ; device<arch->ndevices ; device++)
  1611. {
  1612. char *name = starpu_perfmodel_get_archtype_name(arch->devices[device].type);
  1613. fprintf(output, "\t\tDevice %d: type: %s - devid: %d - ncores: %d\n", device, name, arch->devices[device].devid, arch->devices[device].ncores);
  1614. }
  1615. }
  1616. return 0;
  1617. }
  1618. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_arch(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, unsigned impl)
  1619. {
  1620. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1621. if (comb == -1)
  1622. return NULL;
  1623. if (!model->state->per_arch[comb])
  1624. return NULL;
  1625. return &model->state->per_arch[comb][impl];
  1626. }
  1627. struct starpu_perfmodel_per_arch *_starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, va_list varg_list)
  1628. {
  1629. struct starpu_perfmodel_arch arch;
  1630. va_list varg_list_copy;
  1631. int i, arg_type;
  1632. int is_cpu_set = 0;
  1633. // We first count the number of devices
  1634. arch.ndevices = 0;
  1635. va_copy(varg_list_copy, varg_list);
  1636. while ((arg_type = va_arg(varg_list_copy, int)) != -1)
  1637. {
  1638. int devid = va_arg(varg_list_copy, int);
  1639. int ncores = va_arg(varg_list_copy, int);
  1640. arch.ndevices ++;
  1641. if (arg_type == STARPU_CPU_WORKER)
  1642. {
  1643. STARPU_ASSERT_MSG(is_cpu_set == 0, "STARPU_CPU_WORKER can only be specified once\n");
  1644. STARPU_ASSERT_MSG(devid==0, "STARPU_CPU_WORKER must be followed by a value 0 for the device id");
  1645. is_cpu_set = 1;
  1646. }
  1647. else
  1648. {
  1649. STARPU_ASSERT_MSG(ncores==1, "%s must be followed by a value 1 for ncores", starpu_worker_get_type_as_string(arg_type));
  1650. }
  1651. }
  1652. va_end(varg_list_copy);
  1653. // We set the devices
  1654. _STARPU_MALLOC(arch.devices, arch.ndevices * sizeof(struct starpu_perfmodel_device));
  1655. va_copy(varg_list_copy, varg_list);
  1656. for(i=0 ; i<arch.ndevices ; i++)
  1657. {
  1658. arch.devices[i].type = va_arg(varg_list_copy, int);
  1659. arch.devices[i].devid = va_arg(varg_list_copy, int);
  1660. arch.devices[i].ncores = va_arg(varg_list_copy, int);
  1661. }
  1662. va_end(varg_list_copy);
  1663. // Get the combination for this set of devices
  1664. int comb = _starpu_perfmodel_create_comb_if_needed(&arch);
  1665. free(arch.devices);
  1666. // Realloc if necessary
  1667. if (comb >= model->state->ncombs_set)
  1668. _starpu_perfmodel_realloc(model, comb+1);
  1669. // Get the per_arch object
  1670. if (model->state->per_arch[comb] == NULL)
  1671. {
  1672. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1673. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1674. model->state->nimpls[comb] = 0;
  1675. }
  1676. model->state->per_arch_is_set[comb][impl] = 1;
  1677. model->state->nimpls[comb] ++;
  1678. return &model->state->per_arch[comb][impl];
  1679. }
  1680. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, ...)
  1681. {
  1682. va_list varg_list;
  1683. struct starpu_perfmodel_per_arch *per_arch;
  1684. va_start(varg_list, impl);
  1685. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1686. va_end(varg_list);
  1687. return per_arch;
  1688. }
  1689. int starpu_perfmodel_set_per_devices_cost_function(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_cost_function func, ...)
  1690. {
  1691. va_list varg_list;
  1692. struct starpu_perfmodel_per_arch *per_arch;
  1693. va_start(varg_list, func);
  1694. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1695. per_arch->cost_function = func;
  1696. va_end(varg_list);
  1697. return 0;
  1698. }
  1699. int starpu_perfmodel_set_per_devices_size_base(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_size_base func, ...)
  1700. {
  1701. va_list varg_list;
  1702. struct starpu_perfmodel_per_arch *per_arch;
  1703. va_start(varg_list, func);
  1704. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1705. per_arch->size_base = func;
  1706. va_end(varg_list);
  1707. return 0;
  1708. }