perfmodel_history.c 55 KB

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