perfmodel_history.c 62 KB

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