perfmodel_history.c 68 KB

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