perfmodel_history.c 49 KB

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