perfmodel_history.c 40 KB

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