perfmodel_history.c 54 KB

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