perfmodel_history.c 43 KB

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