perfmodel_history.c 54 KB

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