perfmodel_history.c 55 KB

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