perfmodel_history.c 70 KB

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