perfmodel_history.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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. for(i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  316. {
  317. memset(&model->state->per_arch[comb][i], 0, sizeof(struct starpu_perfmodel_per_arch));
  318. model->state->per_arch_is_set[comb][i] = 0;
  319. }
  320. for (impl = 0; impl < implmax; impl++)
  321. {
  322. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  323. model->state->per_arch_is_set[comb][impl] = 1;
  324. parse_per_arch_model_file(f, per_arch_model, scan_history);
  325. }
  326. }
  327. else
  328. {
  329. impl = 0;
  330. }
  331. /* if the number of implementation is greater than STARPU_MAXIMPLEMENTATIONS
  332. * we skip the last implementation */
  333. for (i = impl; i < nimpls; i++)
  334. parse_per_arch_model_file(f, &dummy, 0);
  335. }
  336. static enum starpu_worker_archtype _get_enum_type(int type)
  337. {
  338. switch(type)
  339. {
  340. case 0:
  341. return STARPU_CPU_WORKER;
  342. case 1:
  343. return STARPU_CUDA_WORKER;
  344. case 2:
  345. return STARPU_OPENCL_WORKER;
  346. case 3:
  347. return STARPU_MIC_WORKER;
  348. case 4:
  349. return STARPU_SCC_WORKER;
  350. default:
  351. STARPU_ABORT();
  352. }
  353. }
  354. static void parse_comb(FILE *f, struct starpu_perfmodel *model, unsigned scan_history, int comb)
  355. {
  356. int ndevices = 0;
  357. _starpu_drop_comments(f);
  358. int ret = fscanf(f, "%d\n", &ndevices );
  359. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  360. struct starpu_perfmodel_device devices[ndevices];
  361. int dev;
  362. for(dev = 0; dev < ndevices; dev++)
  363. {
  364. enum starpu_worker_archtype dev_type;
  365. _starpu_drop_comments(f);
  366. int type;
  367. ret = fscanf(f, "%d\n", &type);
  368. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  369. dev_type = _get_enum_type(type);
  370. int dev_id;
  371. _starpu_drop_comments(f);
  372. ret = fscanf(f, "%d\n", &dev_id);
  373. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  374. int ncores;
  375. _starpu_drop_comments(f);
  376. ret = fscanf(f, "%d\n", &ncores);
  377. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  378. devices[dev].type = dev_type;
  379. devices[dev].devid = dev_id;
  380. devices[dev].ncores = ncores;
  381. }
  382. int id_comb = starpu_perfmodel_arch_comb_get(ndevices, devices);
  383. if(id_comb == -1)
  384. id_comb = starpu_perfmodel_arch_comb_add(ndevices, devices);
  385. model->state->combs[comb] = id_comb;
  386. parse_arch(f, model, scan_history, id_comb);
  387. }
  388. static void parse_model_file(FILE *f, struct starpu_perfmodel *model, unsigned scan_history)
  389. {
  390. int ret, version;
  391. /* Parsing performance model version */
  392. _starpu_drop_comments(f);
  393. ret = fscanf(f, "%d\n", &version);
  394. STARPU_ASSERT_MSG(version == _STARPU_PERFMODEL_VERSION, "Incorrect performance model file with a model version %d not being the current model version (%d)\n",
  395. version, _STARPU_PERFMODEL_VERSION);
  396. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  397. int ncombs = 0;
  398. _starpu_drop_comments(f);
  399. ret = fscanf(f, "%d\n", &ncombs);
  400. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  401. if(ncombs > 0)
  402. {
  403. model->state->ncombs = ncombs;
  404. }
  405. if (ncombs > nb_arch_combs)
  406. {
  407. // The model has more combs than the original number of arch_combs, we need to reallocate
  408. nb_arch_combs = ncombs;
  409. arch_combs = (struct starpu_perfmodel_arch**) realloc(arch_combs, nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  410. _starpu_perfmodel_realloc(model, nb_arch_combs);
  411. }
  412. int comb;
  413. for(comb = 0; comb < ncombs; comb++)
  414. parse_comb(f, model, scan_history, comb);
  415. }
  416. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, int comb, unsigned impl)
  417. {
  418. struct starpu_perfmodel_per_arch *per_arch_model;
  419. per_arch_model = &model->state->per_arch[comb][impl];
  420. /* count the number of elements in the lists */
  421. struct starpu_perfmodel_history_list *ptr = NULL;
  422. unsigned nentries = 0;
  423. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  424. {
  425. /* Dump the list of all entries in the history */
  426. ptr = per_arch_model->list;
  427. while(ptr)
  428. {
  429. nentries++;
  430. ptr = ptr->next;
  431. }
  432. }
  433. /* header */
  434. char archname[32];
  435. starpu_perfmodel_get_arch_name(arch_combs[comb], archname, 32, impl);
  436. fprintf(f, "#####\n");
  437. fprintf(f, "# Model for %s\n", archname);
  438. fprintf(f, "# number of entries\n%u\n", nentries);
  439. dump_reg_model(f, model, comb, impl);
  440. /* Dump the history into the model file in case it is necessary */
  441. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  442. {
  443. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean (us)\tdev (us)\tsum\t\tsum2\t\tn\n");
  444. ptr = per_arch_model->list;
  445. while (ptr)
  446. {
  447. dump_history_entry(f, ptr->entry);
  448. ptr = ptr->next;
  449. }
  450. }
  451. fprintf(f, "\n");
  452. }
  453. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  454. {
  455. fprintf(f, "##################\n");
  456. fprintf(f, "# Performance Model Version\n");
  457. fprintf(f, "%d\n\n", _STARPU_PERFMODEL_VERSION);
  458. int ncombs = model->state->ncombs;
  459. fprintf(f, "####################\n");
  460. fprintf(f, "# COMBs\n");
  461. fprintf(f, "# number of combinations\n");
  462. fprintf(f, "%u\n", ncombs);
  463. int i, impl, dev;
  464. for(i = 0; i < ncombs; i++)
  465. {
  466. int comb = model->state->combs[i];
  467. int ndevices = arch_combs[comb]->ndevices;
  468. fprintf(f, "####################\n");
  469. fprintf(f, "# COMB_%d\n", 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[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[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[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->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->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->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. if (node->model->is_init)
  606. save_history_based_model(node->model);
  607. node = node->next;
  608. }
  609. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  610. #endif
  611. }
  612. void _starpu_initialize_registered_performance_models(void)
  613. {
  614. /* make sure the performance model directory exists (or create it) */
  615. _starpu_create_sampling_directory_if_needed();
  616. registered_models = NULL;
  617. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  618. struct _starpu_machine_config *conf = _starpu_get_machine_config();
  619. unsigned ncores = conf->topology.nhwcpus;
  620. unsigned ncuda = conf->topology.nhwcudagpus;
  621. unsigned nopencl = conf->topology.nhwopenclgpus;
  622. unsigned nmic = 0;
  623. unsigned i;
  624. for(i = 0; i < conf->topology.nhwmicdevices; i++)
  625. nmic += conf->topology.nhwmiccores[i];
  626. unsigned nscc = conf->topology.nhwscc;
  627. // We used to allocate 2**(ncores + ncuda + nopencl + nmic + nscc), this is too big
  628. // We now allocate only 2*(ncores + ncuda + nopencl + nmic + nscc), and reallocate when necessary in starpu_perfmodel_arch_comb_add
  629. nb_arch_combs = 2 * (ncores + ncuda + nopencl + nmic + nscc);
  630. arch_combs = (struct starpu_perfmodel_arch**) malloc(nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  631. current_arch_comb = 0;
  632. }
  633. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  634. {
  635. if(model->is_init && model->state && model->state->per_arch != NULL)
  636. {
  637. int ncombs = model->state->ncombs;
  638. int i, impl;
  639. for(i=0; i < ncombs; i++)
  640. {
  641. int comb = model->state->combs[i];
  642. int nimpls = model->state->nimpls[comb];
  643. for(impl = 0; impl < nimpls; impl++)
  644. {
  645. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[comb][impl];
  646. struct starpu_perfmodel_history_list *list, *plist;
  647. struct starpu_perfmodel_history_table *entry, *tmp;
  648. HASH_ITER(hh, archmodel->history, entry, tmp)
  649. {
  650. HASH_DEL(archmodel->history, entry);
  651. free(entry);
  652. }
  653. archmodel->history = NULL;
  654. list = archmodel->list;
  655. while (list)
  656. {
  657. free(list->entry);
  658. plist = list;
  659. list = list->next;
  660. free(plist);
  661. }
  662. archmodel->list = NULL;
  663. }
  664. free(model->state->per_arch[comb]);
  665. model->state->per_arch[comb] = NULL;
  666. free(model->state->per_arch_is_set[comb]);
  667. model->state->per_arch_is_set[comb] = NULL;
  668. }
  669. free(model->state->per_arch);
  670. model->state->per_arch = NULL;
  671. free(model->state->per_arch_is_set);
  672. model->state->per_arch_is_set = NULL;
  673. free(model->state->nimpls);
  674. model->state->nimpls = NULL;
  675. free(model->state->combs);
  676. model->state->combs = NULL;
  677. model->state->ncombs = 0;
  678. }
  679. model->is_init = 0;
  680. model->is_loaded = 0;
  681. }
  682. void _starpu_deinitialize_registered_performance_models(void)
  683. {
  684. if (_starpu_get_calibrate_flag())
  685. _starpu_dump_registered_models();
  686. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  687. struct _starpu_perfmodel_list *node, *pnode;
  688. node = registered_models;
  689. _STARPU_DEBUG("FREE MODELS !\n");
  690. while (node)
  691. {
  692. struct starpu_perfmodel *model = node->model;
  693. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  694. _starpu_deinitialize_performance_model(model);
  695. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  696. free(node->model->state);
  697. node->model->state = NULL;
  698. pnode = node;
  699. node = node->next;
  700. free(pnode);
  701. }
  702. registered_models = NULL;
  703. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  704. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  705. _free_arch_combs();
  706. }
  707. /*
  708. * XXX: We should probably factorize the beginning of the _starpu_load_*_model
  709. * functions. This is a bit tricky though, because we must be sure to unlock
  710. * registered_models_rwlock at the right place.
  711. */
  712. void _starpu_load_per_arch_based_model(struct starpu_perfmodel *model)
  713. {
  714. starpu_perfmodel_init(NULL, model);
  715. }
  716. void _starpu_load_common_based_model(struct starpu_perfmodel *model)
  717. {
  718. starpu_perfmodel_init(NULL, model);
  719. }
  720. /* We first try to grab the global lock in read mode to check whether the model
  721. * was loaded or not (this is very likely to have been already loaded). If the
  722. * model was not loaded yet, we take the lock in write mode, and if the model
  723. * is still not loaded once we have the lock, we do load it. */
  724. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  725. {
  726. starpu_perfmodel_init(NULL, model);
  727. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  728. if(!model->is_loaded)
  729. {
  730. char path[256];
  731. get_model_path(model, path, 256);
  732. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  733. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  734. model->benchmarking = calibrate_flag;
  735. /* try to open an existing file and load it */
  736. int res;
  737. res = access(path, F_OK);
  738. if (res == 0)
  739. {
  740. if (calibrate_flag == 2)
  741. {
  742. /* The user specified that the performance model should
  743. * be overwritten, so we don't load the existing file !
  744. * */
  745. _STARPU_DEBUG("Overwrite existing file\n");
  746. }
  747. else
  748. {
  749. /* We load the available file */
  750. _STARPU_DEBUG("File exists\n");
  751. FILE *f;
  752. f = fopen(path, "r");
  753. STARPU_ASSERT(f);
  754. parse_model_file(f, model, scan_history);
  755. fclose(f);
  756. }
  757. }
  758. else
  759. {
  760. _STARPU_DEBUG("File does not exists\n");
  761. }
  762. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  763. model->is_loaded = 1;
  764. }
  765. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  766. }
  767. void starpu_perfmodel_directory(FILE *output)
  768. {
  769. char perf_model_dir[256];
  770. _starpu_get_perf_model_dir_codelets(perf_model_dir, 256);
  771. fprintf(output, "directory: <%s>\n", perf_model_dir);
  772. }
  773. /* This function is intended to be used by external tools that should read
  774. * the performance model files */
  775. int starpu_perfmodel_list(FILE *output)
  776. {
  777. char path[256];
  778. DIR *dp;
  779. struct dirent *ep;
  780. char perf_model_dir_codelets[256];
  781. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  782. strncpy(path, perf_model_dir_codelets, 256);
  783. dp = opendir(path);
  784. if (dp != NULL)
  785. {
  786. while ((ep = readdir(dp)))
  787. {
  788. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  789. fprintf(output, "file: <%s>\n", ep->d_name);
  790. }
  791. closedir (dp);
  792. }
  793. else
  794. {
  795. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  796. }
  797. return 0;
  798. }
  799. /* This function is intended to be used by external tools that should read the
  800. * performance model files */
  801. /* TODO: write an clear function, to free symbol and history */
  802. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  803. {
  804. model->symbol = strdup(symbol);
  805. /* where is the file if it exists ? */
  806. char path[256];
  807. get_model_path(model, path, 256);
  808. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  809. /* does it exist ? */
  810. int res;
  811. res = access(path, F_OK);
  812. if (res)
  813. {
  814. const char *dot = strrchr(symbol, '.');
  815. if (dot)
  816. {
  817. char *symbol2 = strdup(symbol);
  818. symbol2[dot-symbol] = '\0';
  819. int ret;
  820. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  821. ret = starpu_perfmodel_load_symbol(symbol2,model);
  822. free(symbol2);
  823. return ret;
  824. }
  825. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  826. return 1;
  827. }
  828. FILE *f = fopen(path, "r");
  829. STARPU_ASSERT(f);
  830. starpu_perfmodel_init(NULL, model);
  831. parse_model_file(f, model, 1);
  832. STARPU_ASSERT(fclose(f) == 0);
  833. return 0;
  834. }
  835. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  836. {
  837. free((char *)model->symbol);
  838. _starpu_deinitialize_performance_model(model);
  839. return 0;
  840. }
  841. char* starpu_perfmodel_get_archtype_name(enum starpu_worker_archtype archtype)
  842. {
  843. switch(archtype)
  844. {
  845. case(STARPU_CPU_WORKER):
  846. return "cpu";
  847. break;
  848. case(STARPU_CUDA_WORKER):
  849. return "cuda";
  850. break;
  851. case(STARPU_OPENCL_WORKER):
  852. return "opencl";
  853. break;
  854. case(STARPU_MIC_WORKER):
  855. return "mic";
  856. break;
  857. case(STARPU_SCC_WORKER):
  858. return "scc";
  859. break;
  860. default:
  861. STARPU_ABORT();
  862. break;
  863. }
  864. }
  865. void starpu_perfmodel_get_arch_name(struct starpu_perfmodel_arch* arch, char *archname, size_t maxlen,unsigned impl)
  866. {
  867. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  868. STARPU_ASSERT(comb != -1);
  869. snprintf(archname, maxlen, "Comb%d_impl%u", comb, impl);
  870. }
  871. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  872. struct starpu_perfmodel_arch* arch, char *path, size_t maxlen, unsigned nimpl)
  873. {
  874. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  875. STARPU_ASSERT(comb != -1);
  876. char archname[32];
  877. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  878. STARPU_ASSERT(path);
  879. get_model_debug_path(model, archname, path, maxlen);
  880. }
  881. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j, unsigned nimpl)
  882. {
  883. int comb;
  884. double exp = NAN;
  885. size_t size;
  886. struct starpu_perfmodel_regression_model *regmodel;
  887. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  888. if(comb == -1)
  889. return NAN;
  890. if (model->state->per_arch[comb] == NULL)
  891. // The model has not been executed on this combination
  892. return NAN;
  893. regmodel = &model->state->per_arch[comb][nimpl].regression;
  894. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  895. if (regmodel->valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  896. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  897. return exp;
  898. }
  899. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  900. {
  901. int comb;
  902. double exp = NAN;
  903. size_t size;
  904. struct starpu_perfmodel_regression_model *regmodel;
  905. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  906. if(comb == -1)
  907. return NAN;
  908. if (model->state->per_arch[comb] == NULL)
  909. // The model has not been executed on this combination
  910. return NAN;
  911. regmodel = &model->state->per_arch[comb][nimpl].regression;
  912. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  913. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  914. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  915. else
  916. {
  917. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  918. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][nimpl];
  919. struct starpu_perfmodel_history_table *history;
  920. struct starpu_perfmodel_history_table *entry;
  921. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  922. history = per_arch_model->history;
  923. HASH_FIND_UINT32_T(history, &key, entry);
  924. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  925. /* Here helgrind would shout that this is unprotected access.
  926. * We do not care about racing access to the mean, we only want
  927. * a good-enough estimation */
  928. if (entry && entry->history_entry && entry->history_entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  929. exp = entry->history_entry->mean;
  930. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  931. if (isnan(exp) && !model->benchmarking)
  932. {
  933. char archname[32];
  934. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  935. _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);
  936. _starpu_set_calibrate_flag(1);
  937. model->benchmarking = 1;
  938. }
  939. }
  940. return exp;
  941. }
  942. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  943. {
  944. int comb;
  945. double exp = NAN;
  946. struct starpu_perfmodel_per_arch *per_arch_model;
  947. struct starpu_perfmodel_history_entry *entry;
  948. struct starpu_perfmodel_history_table *history, *elt;
  949. uint32_t key;
  950. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  951. if(comb == -1)
  952. return NAN;
  953. if (model->state->per_arch[comb] == NULL)
  954. // The model has not been executed on this combination
  955. return NAN;
  956. per_arch_model = &model->state->per_arch[comb][nimpl];
  957. key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  958. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  959. history = per_arch_model->history;
  960. HASH_FIND_UINT32_T(history, &key, elt);
  961. entry = (elt == NULL) ? NULL : elt->history_entry;
  962. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  963. /* Here helgrind would shout that this is unprotected access.
  964. * We do not care about racing access to the mean, we only want
  965. * a good-enough estimation */
  966. if (entry && entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  967. /* TODO: report differently if we've scheduled really enough
  968. * of that task and the scheduler should perhaps put it aside */
  969. /* Calibrated enough */
  970. exp = entry->mean;
  971. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  972. if (isnan(exp) && !model->benchmarking)
  973. {
  974. char archname[32];
  975. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  976. _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);
  977. _starpu_set_calibrate_flag(1);
  978. model->benchmarking = 1;
  979. }
  980. return exp;
  981. }
  982. double starpu_permodel_history_based_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch * arch, uint32_t footprint)
  983. {
  984. struct _starpu_job j =
  985. {
  986. .footprint = footprint,
  987. .footprint_is_computed = 1,
  988. };
  989. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  990. }
  991. 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)
  992. {
  993. if (model)
  994. {
  995. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  996. if(comb == -1)
  997. comb = starpu_perfmodel_arch_comb_add(arch->ndevices, arch->devices);
  998. int c;
  999. unsigned found = 0;
  1000. for(c = 0; c < model->state->ncombs; c++)
  1001. {
  1002. if(model->state->combs[c] == comb)
  1003. {
  1004. found = 1;
  1005. break;
  1006. }
  1007. }
  1008. if(!found)
  1009. {
  1010. if (model->state->ncombs + 1 >= model->state->ncombs_set)
  1011. {
  1012. // The number of combinations is bigger than the one which was initially allocated, we need to reallocate
  1013. _starpu_perfmodel_realloc(model, nb_arch_combs);
  1014. }
  1015. model->state->combs[model->state->ncombs++] = comb;
  1016. }
  1017. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1018. if(!model->state->per_arch[comb])
  1019. {
  1020. model->state->per_arch[comb] = (struct starpu_perfmodel_per_arch*)malloc(STARPU_MAXIMPLEMENTATIONS*sizeof(struct starpu_perfmodel_per_arch));
  1021. model->state->per_arch_is_set[comb] = (int*)malloc(STARPU_MAXIMPLEMENTATIONS*sizeof(int));
  1022. int i;
  1023. for(i = 0; i < STARPU_MAXIMPLEMENTATIONS; i++)
  1024. {
  1025. memset(&model->state->per_arch[comb][i], 0, sizeof(struct starpu_perfmodel_per_arch));
  1026. model->state->per_arch_is_set[comb][i] = 0;
  1027. }
  1028. }
  1029. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  1030. if (model->state->per_arch_is_set[comb][impl] == 0)
  1031. {
  1032. // We are adding a new implementation for the given comb and the given impl
  1033. model->state->nimpls[comb]++;
  1034. model->state->per_arch_is_set[comb][impl] = 1;
  1035. }
  1036. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1037. {
  1038. struct starpu_perfmodel_history_entry *entry;
  1039. struct starpu_perfmodel_history_table *elt;
  1040. struct starpu_perfmodel_history_list **list;
  1041. uint32_t key = _starpu_compute_buffers_footprint(model, arch, impl, j);
  1042. list = &per_arch_model->list;
  1043. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  1044. entry = (elt == NULL) ? NULL : elt->history_entry;
  1045. if (!entry)
  1046. {
  1047. /* this is the first entry with such a footprint */
  1048. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  1049. STARPU_ASSERT(entry);
  1050. /* Tell helgrind that we do not care about
  1051. * racing access to the sampling, we only want a
  1052. * good-enough estimation */
  1053. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  1054. STARPU_HG_DISABLE_CHECKING(entry->mean);
  1055. /* Do not take the first measurement into account, it is very often quite bogus */
  1056. /* TODO: it'd be good to use a better estimation heuristic, like the median, or latest n values, etc. */
  1057. entry->mean = 0;
  1058. entry->sum = 0;
  1059. entry->deviation = 0.0;
  1060. entry->sum2 = 0;
  1061. entry->size = _starpu_job_get_data_size(model, arch, impl, j);
  1062. entry->flops = j->task->flops;
  1063. entry->footprint = key;
  1064. entry->nsample = 0;
  1065. entry->nerror = 0;
  1066. insert_history_entry(entry, list, &per_arch_model->history);
  1067. }
  1068. else
  1069. {
  1070. /* There is already an entry with the same footprint */
  1071. double local_deviation = measured/entry->mean;
  1072. int historymaxerror = starpu_get_env_number_default("STARPU_HISTORY_MAX_ERROR", STARPU_HISTORYMAXERROR);
  1073. if (entry->nsample &&
  1074. (100 * local_deviation > (100 + historymaxerror)
  1075. || (100 / local_deviation > (100 + historymaxerror))))
  1076. {
  1077. entry->nerror++;
  1078. /* More errors than measurements, we're most probably completely wrong, we flush out all the entries */
  1079. if (entry->nerror >= entry->nsample)
  1080. {
  1081. char archname[32];
  1082. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), impl);
  1083. _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);
  1084. entry->sum = 0.0;
  1085. entry->sum2 = 0.0;
  1086. entry->nsample = 0;
  1087. entry->nerror = 0;
  1088. entry->mean = 0.0;
  1089. entry->deviation = 0.0;
  1090. }
  1091. }
  1092. else
  1093. {
  1094. entry->sum += measured;
  1095. entry->sum2 += measured*measured;
  1096. entry->nsample++;
  1097. unsigned n = entry->nsample;
  1098. entry->mean = entry->sum / n;
  1099. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  1100. }
  1101. if (j->task->flops != 0.)
  1102. {
  1103. if (entry->flops == 0.)
  1104. entry->flops = j->task->flops;
  1105. else if (entry->flops != j->task->flops)
  1106. /* Incoherent flops! forget about trying to record flops */
  1107. entry->flops = NAN;
  1108. }
  1109. }
  1110. STARPU_ASSERT(entry);
  1111. }
  1112. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1113. {
  1114. struct starpu_perfmodel_regression_model *reg_model;
  1115. reg_model = &per_arch_model->regression;
  1116. /* update the regression model */
  1117. size_t job_size = _starpu_job_get_data_size(model, arch, impl, j);
  1118. double logy, logx;
  1119. logx = log((double)job_size);
  1120. logy = log(measured);
  1121. reg_model->sumlnx += logx;
  1122. reg_model->sumlnx2 += logx*logx;
  1123. reg_model->sumlny += logy;
  1124. reg_model->sumlnxlny += logx*logy;
  1125. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1126. reg_model->minx = job_size;
  1127. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1128. reg_model->maxx = job_size;
  1129. reg_model->nsample++;
  1130. if (VALID_REGRESSION(reg_model))
  1131. {
  1132. unsigned n = reg_model->nsample;
  1133. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1134. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1135. reg_model->beta = num/denom;
  1136. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1137. reg_model->valid = 1;
  1138. }
  1139. }
  1140. #ifdef STARPU_MODEL_DEBUG
  1141. struct starpu_task *task = j->task;
  1142. starpu_perfmodel_debugfilepath(model, arch_combs[comb], per_arch_model->debug_path, 256, impl);
  1143. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1144. if (f == NULL)
  1145. {
  1146. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1147. STARPU_ABORT();
  1148. }
  1149. if (!j->footprint_is_computed)
  1150. (void) _starpu_compute_buffers_footprint(model, arch, impl, j);
  1151. STARPU_ASSERT(j->footprint_is_computed);
  1152. 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);
  1153. unsigned i;
  1154. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1155. for (i = 0; i < nbuffers; i++)
  1156. {
  1157. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1158. STARPU_ASSERT(handle->ops);
  1159. STARPU_ASSERT(handle->ops->display);
  1160. handle->ops->display(handle, f);
  1161. }
  1162. fprintf(f, "\n");
  1163. fclose(f);
  1164. #endif
  1165. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1166. }
  1167. }
  1168. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, struct starpu_perfmodel_arch * arch, unsigned cpuid, unsigned nimpl, double measured)
  1169. {
  1170. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1171. #ifdef STARPU_SIMGRID
  1172. STARPU_ASSERT_MSG(0, "We are not supposed to update history when simulating execution");
  1173. #endif
  1174. _starpu_init_and_load_perfmodel(model);
  1175. /* Record measurement */
  1176. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1177. /* and save perfmodel on termination */
  1178. _starpu_set_calibrate_flag(1);
  1179. }
  1180. int starpu_perfmodel_list_combs(FILE *output, struct starpu_perfmodel *model)
  1181. {
  1182. int comb;
  1183. fprintf(output, "Model <%s>\n", model->symbol);
  1184. for(comb = 0; comb < model->state->ncombs; comb++)
  1185. {
  1186. struct starpu_perfmodel_arch *arch;
  1187. int device;
  1188. arch = _starpu_arch_comb_get(model->state->combs[comb]);
  1189. fprintf(output, "\tComb %d: %d device%s\n", model->state->combs[comb], arch->ndevices, arch->ndevices>1?"s":"");
  1190. for(device=0 ; device<arch->ndevices ; device++)
  1191. {
  1192. char *name = starpu_perfmodel_get_archtype_name(arch->devices[device].type);
  1193. fprintf(output, "\t\tDevice %d: type: %s - devid: %d - ncores: %d\n", device, name, arch->devices[device].devid, arch->devices[device].ncores);
  1194. }
  1195. }
  1196. return 0;
  1197. }
  1198. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_arch(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, unsigned impl)
  1199. {
  1200. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1201. if(comb == -1) return NULL;
  1202. return &model->state->per_arch[comb][impl];
  1203. }
  1204. struct starpu_perfmodel_per_arch *_starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, va_list varg_list)
  1205. {
  1206. struct starpu_perfmodel_arch arch;
  1207. va_list varg_list_copy;
  1208. int i, arg_type;
  1209. int is_cpu_set = 0;
  1210. // We first count the number of devices
  1211. arch.ndevices = 0;
  1212. va_copy(varg_list_copy, varg_list);
  1213. while ((arg_type = va_arg(varg_list_copy, int)) != -1)
  1214. {
  1215. int devid = va_arg(varg_list_copy, int);
  1216. int ncores = va_arg(varg_list_copy, int);
  1217. arch.ndevices ++;
  1218. if (arg_type == STARPU_CPU_WORKER)
  1219. {
  1220. STARPU_ASSERT_MSG(is_cpu_set == 0, "STARPU_CPU_WORKER can only be specified once\n");
  1221. STARPU_ASSERT_MSG(devid==0, "STARPU_CPU_WORKER must be followed by a value 0 for the device id");
  1222. is_cpu_set = 1;
  1223. }
  1224. else
  1225. {
  1226. STARPU_ASSERT_MSG(ncores==1, "%s must be followed by a value 1 for ncores", starpu_worker_get_type_as_string(arg_type));
  1227. }
  1228. }
  1229. va_end(varg_list_copy);
  1230. // We set the devices
  1231. arch.devices = (struct starpu_perfmodel_device*)malloc(arch.ndevices * sizeof(struct starpu_perfmodel_device));
  1232. va_copy(varg_list_copy, varg_list);
  1233. for(i=0 ; i<arch.ndevices ; i++)
  1234. {
  1235. arch.devices[i].type = va_arg(varg_list_copy, int);
  1236. arch.devices[i].devid = va_arg(varg_list_copy, int);
  1237. arch.devices[i].ncores = va_arg(varg_list_copy, int);
  1238. }
  1239. va_end(varg_list_copy);
  1240. // Get the combination for this set of devices
  1241. int comb = starpu_perfmodel_arch_comb_get(arch.ndevices, arch.devices);
  1242. if (comb == -1)
  1243. comb = starpu_perfmodel_arch_comb_add(arch.ndevices, arch.devices);
  1244. free(arch.devices);
  1245. // Realloc if necessary
  1246. if (comb >= model->state->ncombs_set)
  1247. _starpu_perfmodel_realloc(model, comb+1);
  1248. // Get the per_arch object
  1249. if (model->state->per_arch[comb] == NULL)
  1250. {
  1251. model->state->per_arch[comb] = (struct starpu_perfmodel_per_arch*)malloc((impl+1) * sizeof(struct starpu_perfmodel_per_arch));
  1252. model->state->per_arch_is_set[comb] = (int*)malloc((impl+1) * sizeof(int));
  1253. model->state->nimpls[comb] = 0;
  1254. }
  1255. memset(&model->state->per_arch[comb][impl], 0, sizeof(struct starpu_perfmodel_per_arch));
  1256. model->state->per_arch_is_set[comb][impl] = 1;
  1257. model->state->nimpls[comb] ++;
  1258. return &model->state->per_arch[comb][impl];
  1259. }
  1260. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, ...)
  1261. {
  1262. va_list varg_list;
  1263. struct starpu_perfmodel_per_arch *per_arch;
  1264. va_start(varg_list, impl);
  1265. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1266. va_end(varg_list);
  1267. return per_arch;
  1268. }
  1269. int starpu_perfmodel_set_per_devices_cost_function(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_cost_function func, ...)
  1270. {
  1271. va_list varg_list;
  1272. struct starpu_perfmodel_per_arch *per_arch;
  1273. va_start(varg_list, func);
  1274. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1275. per_arch->cost_function = func;
  1276. va_end(varg_list);
  1277. return 0;
  1278. }
  1279. int starpu_perfmodel_set_per_devices_size_base(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_size_base func, ...)
  1280. {
  1281. va_list varg_list;
  1282. struct starpu_perfmodel_per_arch *per_arch;
  1283. va_start(varg_list, func);
  1284. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1285. per_arch->size_base = func;
  1286. va_end(varg_list);
  1287. return 0;
  1288. }