perfmodel_history.c 47 KB

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