perfmodel_history.c 47 KB

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