perfmodel_history.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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_history_table
  38. {
  39. UT_hash_handle hh;
  40. uint32_t footprint;
  41. struct starpu_perfmodel_history_entry *history_entry;
  42. };
  43. /* We want more than 10% variance on X to trust regression */
  44. #define VALID_REGRESSION(reg_model) \
  45. ((reg_model)->minx < (9*(reg_model)->maxx)/10 && (reg_model)->nsample >= _STARPU_CALIBRATION_MINIMUM)
  46. static starpu_pthread_rwlock_t registered_models_rwlock;
  47. static struct _starpu_perfmodel_list *registered_models = NULL;
  48. size_t _starpu_job_get_data_size(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, unsigned nimpl, struct _starpu_job *j)
  49. {
  50. struct starpu_task *task = j->task;
  51. if (model && model->per_arch[arch][nimpl].size_base)
  52. {
  53. return model->per_arch[arch][nimpl].size_base(task, arch, nimpl);
  54. }
  55. else if (model && model->size_base)
  56. {
  57. return model->size_base(task, nimpl);
  58. }
  59. else
  60. {
  61. unsigned nbuffers = task->cl->nbuffers;
  62. size_t size = 0;
  63. unsigned buffer;
  64. for (buffer = 0; buffer < nbuffers; buffer++)
  65. {
  66. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  67. size += _starpu_data_get_size(handle);
  68. }
  69. return size;
  70. }
  71. }
  72. /*
  73. * History based model
  74. */
  75. static void insert_history_entry(struct starpu_perfmodel_history_entry *entry, struct starpu_perfmodel_history_list **list, struct starpu_perfmodel_history_table **history_ptr)
  76. {
  77. struct starpu_perfmodel_history_list *link;
  78. struct starpu_perfmodel_history_table *table;
  79. link = (struct starpu_perfmodel_history_list *) malloc(sizeof(struct starpu_perfmodel_history_list));
  80. link->next = *list;
  81. link->entry = entry;
  82. *list = link;
  83. /* detect concurrency issue */
  84. //HASH_FIND_UINT32_T(*history_ptr, &entry->footprint, table);
  85. //STARPU_ASSERT(table == NULL);
  86. table = (struct starpu_perfmodel_history_table*) malloc(sizeof(*table));
  87. STARPU_ASSERT(table != NULL);
  88. table->footprint = entry->footprint;
  89. table->history_entry = entry;
  90. HASH_ADD_UINT32_T(*history_ptr, footprint, table);
  91. }
  92. static void dump_reg_model(FILE *f, struct starpu_perfmodel *model, unsigned arch, unsigned nimpl)
  93. {
  94. struct starpu_perfmodel_per_arch *per_arch_model;
  95. per_arch_model = &model->per_arch[arch][nimpl];
  96. struct starpu_perfmodel_regression_model *reg_model;
  97. reg_model = &per_arch_model->regression;
  98. /*
  99. * Linear Regression model
  100. */
  101. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  102. double alpha = nan(""), beta = nan("");
  103. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  104. {
  105. if (reg_model->nsample > 1)
  106. {
  107. alpha = reg_model->alpha;
  108. beta = reg_model->beta;
  109. }
  110. }
  111. fprintf(f, "# sumlnx\tsumlnx2\t\tsumlny\t\tsumlnxlny\talpha\t\tbeta\t\tn\tminx\t\tmaxx\n");
  112. 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);
  113. /*
  114. * Non-Linear Regression model
  115. */
  116. double a = nan(""), b = nan(""), c = nan("");
  117. if (model->type == STARPU_NL_REGRESSION_BASED)
  118. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  119. fprintf(f, "# a\t\tb\t\tc\n");
  120. fprintf(f, "%-15le\t%-15le\t%-15le\n", a, b, c);
  121. }
  122. static void scan_reg_model(FILE *f, struct starpu_perfmodel_regression_model *reg_model)
  123. {
  124. int res;
  125. /*
  126. * Linear Regression model
  127. */
  128. _starpu_drop_comments(f);
  129. res = fscanf(f, "%le\t%le\t%le\t%le", &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny, &reg_model->sumlnxlny);
  130. STARPU_ASSERT_MSG(res == 4, "Incorrect performance model file");
  131. res = _starpu_read_double(f, "\t%le", &reg_model->alpha);
  132. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  133. res = _starpu_read_double(f, "\t%le", &reg_model->beta);
  134. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  135. res = fscanf(f, "\t%u\t%lu\t%lu\n", &reg_model->nsample, &reg_model->minx, &reg_model->maxx);
  136. STARPU_ASSERT_MSG(res == 3, "Incorrect performance model file");
  137. /* If any of the parameters describing the linear regression model is NaN, the model is invalid */
  138. unsigned invalid = (isnan(reg_model->alpha)||isnan(reg_model->beta));
  139. reg_model->valid = !invalid && VALID_REGRESSION(reg_model);
  140. /*
  141. * Non-Linear Regression model
  142. */
  143. _starpu_drop_comments(f);
  144. res = _starpu_read_double(f, "%le\t", &reg_model->a);
  145. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  146. res = _starpu_read_double(f, "%le\t", &reg_model->b);
  147. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  148. res = _starpu_read_double(f, "%le\n", &reg_model->c);
  149. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  150. /* If any of the parameters describing the non-linear regression model is NaN, the model is invalid */
  151. unsigned nl_invalid = (isnan(reg_model->a)||isnan(reg_model->b)||isnan(reg_model->c));
  152. reg_model->nl_valid = !nl_invalid && VALID_REGRESSION(reg_model);
  153. }
  154. static void dump_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  155. {
  156. 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);
  157. }
  158. static void scan_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  159. {
  160. int res;
  161. _starpu_drop_comments(f);
  162. /* In case entry is NULL, we just drop these values */
  163. unsigned nsample;
  164. uint32_t footprint;
  165. unsigned long size; /* in bytes */
  166. double flops;
  167. double mean;
  168. double deviation;
  169. double sum;
  170. double sum2;
  171. char line[256];
  172. char *ret;
  173. ret = fgets(line, sizeof(line), f);
  174. STARPU_ASSERT(ret);
  175. STARPU_ASSERT(strchr(line, '\n'));
  176. /* Read the values from the file */
  177. 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);
  178. if (res != 8)
  179. {
  180. flops = 0.;
  181. /* Read the values from the file */
  182. res = sscanf(line, "%x\t%lu\t%le\t%le\t%le\t%le\t%u", &footprint, &size, &mean, &deviation, &sum, &sum2, &nsample);
  183. STARPU_ASSERT_MSG(res == 7, "Incorrect performance model file");
  184. }
  185. if (entry)
  186. {
  187. entry->footprint = footprint;
  188. entry->size = size;
  189. entry->flops = flops;
  190. entry->mean = mean;
  191. entry->deviation = deviation;
  192. entry->sum = sum;
  193. entry->sum2 = sum2;
  194. entry->nsample = nsample;
  195. }
  196. }
  197. static void parse_per_arch_model_file(FILE *f, struct starpu_perfmodel_per_arch *per_arch_model, unsigned scan_history)
  198. {
  199. unsigned nentries;
  200. _starpu_drop_comments(f);
  201. int res = fscanf(f, "%u\n", &nentries);
  202. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file");
  203. _STARPU_DEBUG("nentries:%u\n", nentries);
  204. scan_reg_model(f, &per_arch_model->regression);
  205. /* parse cpu entries */
  206. unsigned i;
  207. for (i = 0; i < nentries; i++)
  208. {
  209. struct starpu_perfmodel_history_entry *entry = NULL;
  210. if (scan_history)
  211. {
  212. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  213. STARPU_ASSERT(entry);
  214. }
  215. scan_history_entry(f, entry);
  216. /* insert the entry in the hashtable and the list structures */
  217. /* TODO: Insert it at the end of the list, to avoid reversing
  218. * the order... But efficiently! We may have a lot of entries */
  219. if (scan_history)
  220. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  221. }
  222. }
  223. static void parse_arch(FILE *f, struct starpu_perfmodel *model, unsigned scan_history, unsigned* arch, unsigned archmax)
  224. {
  225. struct starpu_perfmodel_per_arch dummy;
  226. unsigned nimpls, implmax, impl, i, ret;
  227. _STARPU_DEBUG("Parsing arch %u \n",*arch);
  228. /* Parsing number of implementation */
  229. _starpu_drop_comments(f);
  230. ret = fscanf(f, "%u\n", &nimpls);
  231. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  232. /* Parsing each implementation */
  233. implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  234. for (impl = 0; impl < implmax; impl++)
  235. {
  236. if(*arch < archmax)
  237. parse_per_arch_model_file(f, &model->per_arch[*arch][impl], scan_history);
  238. else
  239. parse_per_arch_model_file(f, &dummy, 0);
  240. }
  241. /* if the number of implementation is greater than STARPU_MAXIMPLEMENTATIONS
  242. * we skip the last implementation */
  243. if (impl < nimpls)
  244. for (i = impl; impl < nimpls; i++)
  245. parse_per_arch_model_file(f, &dummy, 0);
  246. }
  247. static void parse_device(FILE *f, struct starpu_perfmodel *model, unsigned scan_history, unsigned * arch, unsigned archmax)
  248. {
  249. unsigned maxncore, ncore, ret;
  250. /* Parsing maximun number of worker for this device */
  251. _starpu_drop_comments(f);
  252. ret = fscanf(f, "%u\n", &maxncore);
  253. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  254. /* Parsing each arch */
  255. for(ncore=0; ncore < maxncore; ncore++)
  256. {
  257. parse_arch(f,model,scan_history,arch,archmax);
  258. (*arch)++;
  259. }
  260. }
  261. static void parse_archtype(FILE *f, struct starpu_perfmodel *model, unsigned scan_history, unsigned * arch, unsigned archmax)
  262. {
  263. unsigned ndevice, devid, ret;
  264. /* Parsing number of device for this archtype */
  265. _starpu_drop_comments(f);
  266. ret = fscanf(f, "%u\n", &ndevice);
  267. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  268. /* Parsing each device for this archtype*/
  269. for(devid=0; devid < ndevice; devid++)
  270. parse_device(f,model,scan_history,arch,archmax);
  271. }
  272. static void parse_model_file(FILE *f, struct starpu_perfmodel *model, unsigned scan_history)
  273. {
  274. int ret, version;
  275. unsigned arch, archmax;
  276. _STARPU_DEBUG("Start parsing\n");
  277. /* Parsing performance model version */
  278. _starpu_drop_comments(f);
  279. ret = fscanf(f, "%d\n", &version);
  280. STARPU_ASSERT_MSG(version == _STARPU_PERFMODEL_VERSION, "Incorrect performance model file with a model version %d not being the current model version (%d)\n",
  281. version, _STARPU_PERFMODEL_VERSION);
  282. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file");
  283. /* Parsing each kind of archtype */
  284. /* Parsing CPU */
  285. arch = STARPU_CPU_DEFAULT;
  286. archmax = STARPU_CPU_DEFAULT + STARPU_MAXCPUS;
  287. parse_archtype(f, model, scan_history, &arch, archmax);
  288. /* Parsing CUDA */
  289. arch = STARPU_CUDA_DEFAULT;
  290. archmax = STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS;
  291. parse_archtype(f, model, scan_history, &arch, archmax);
  292. /* Parsing OpenCL */
  293. arch = STARPU_OPENCL_DEFAULT;
  294. archmax = STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS;
  295. parse_archtype(f, model, scan_history, &arch, archmax);
  296. }
  297. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, unsigned arch, unsigned nimpl)
  298. {
  299. struct starpu_perfmodel_per_arch *per_arch_model;
  300. per_arch_model = &model->per_arch[arch][nimpl];
  301. /* count the number of elements in the lists */
  302. struct starpu_perfmodel_history_list *ptr = NULL;
  303. unsigned nentries = 0;
  304. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  305. {
  306. /* Dump the list of all entries in the history */
  307. ptr = per_arch_model->list;
  308. while(ptr)
  309. {
  310. nentries++;
  311. ptr = ptr->next;
  312. }
  313. }
  314. /* header */
  315. char archname[32];
  316. starpu_perfmodel_get_arch_name((enum starpu_perfmodel_archtype) arch, archname, 32, nimpl);
  317. fprintf(f, "#####\n");
  318. fprintf(f, "# Model for %s\n", archname);
  319. fprintf(f, "# number of entries\n%u\n", nentries);
  320. dump_reg_model(f, model, arch, nimpl);
  321. /* Dump the history into the model file in case it is necessary */
  322. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  323. {
  324. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean (us)\tdev (us)\t\tsum\t\tsum2\t\tn\n");
  325. ptr = per_arch_model->list;
  326. while (ptr)
  327. {
  328. dump_history_entry(f, ptr->entry);
  329. ptr = ptr->next;
  330. }
  331. }
  332. fprintf(f, "\n");
  333. }
  334. static unsigned get_n_entries(struct starpu_perfmodel *model, unsigned arch, unsigned impl)
  335. {
  336. struct starpu_perfmodel_per_arch *per_arch_model;
  337. per_arch_model = &model->per_arch[arch][impl];
  338. /* count the number of elements in the lists */
  339. struct starpu_perfmodel_history_list *ptr = NULL;
  340. unsigned nentries = 0;
  341. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  342. {
  343. /* Dump the list of all entries in the history */
  344. ptr = per_arch_model->list;
  345. while(ptr)
  346. {
  347. nentries++;
  348. ptr = ptr->next;
  349. }
  350. }
  351. return nentries;
  352. }
  353. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  354. {
  355. unsigned narch[4] = { 0, 0, 0, 0};
  356. unsigned arch, arch_base = 0, my_narch = 0;
  357. unsigned nimpl;
  358. unsigned idx = 0;
  359. fprintf(f, "##################\n");
  360. fprintf(f, "# Performance Model Version\n");
  361. fprintf(f, "%d\n\n", _STARPU_PERFMODEL_VERSION);
  362. /* Finding the number of archs to write for each kind of device */
  363. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  364. {
  365. switch (arch)
  366. {
  367. case STARPU_CUDA_DEFAULT:
  368. case STARPU_OPENCL_DEFAULT:
  369. case STARPU_MIC_DEFAULT:
  370. arch_base = arch;
  371. idx++;
  372. break;
  373. default:
  374. break;
  375. }
  376. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  377. {
  378. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  379. if (get_n_entries(model, arch, nimpl))
  380. {
  381. narch[idx]=arch-arch_base+1;
  382. break;
  383. }
  384. }
  385. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  386. {
  387. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  388. if (model->per_arch[arch][nimpl].regression.nsample)
  389. {
  390. narch[idx]=arch-arch_base+1;
  391. break;
  392. }
  393. }
  394. else
  395. {
  396. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %d", model->type);
  397. }
  398. }
  399. /* Writing stuff */
  400. char *name = "unknown";
  401. unsigned substract_to_arch = 0;
  402. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  403. {
  404. switch (arch)
  405. {
  406. case STARPU_CPU_DEFAULT:
  407. arch_base = arch;
  408. name = "CPU";
  409. fprintf(f, "####################\n");
  410. fprintf(f, "# %ss\n", name);
  411. fprintf(f, "# number of %s devices\n", name);
  412. fprintf(f, "1\n");
  413. fprintf(f, "###############\n");
  414. fprintf(f, "# CPU_0\n");
  415. fprintf(f, "# number of workers on device CPU_0\n");
  416. fprintf(f, "%u\n", my_narch = narch[0]);
  417. break;
  418. case STARPU_CUDA_DEFAULT:
  419. arch_base = arch;
  420. name = "CUDA";
  421. substract_to_arch = STARPU_MAXCPUS;
  422. fprintf(f, "####################\n");
  423. fprintf(f, "# %ss\n", name);
  424. fprintf(f, "# number of %s devices\n", name);
  425. fprintf(f, "%u\n", my_narch = narch[1]);
  426. break;
  427. case STARPU_OPENCL_DEFAULT:
  428. arch_base = arch;
  429. name = "OPENCL";
  430. substract_to_arch += STARPU_MAXCUDADEVS;
  431. fprintf(f, "####################\n");
  432. fprintf(f, "# %ss\n", name);
  433. fprintf(f, "# number of %ss devices\n", name);
  434. fprintf(f, "%u\n", my_narch = narch[2]);
  435. break;
  436. default:
  437. break;
  438. }
  439. unsigned max_impl = 0;
  440. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  441. {
  442. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  443. if (get_n_entries(model, arch, nimpl))
  444. max_impl = nimpl + 1;
  445. }
  446. else if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_PER_ARCH || model->type == STARPU_COMMON)
  447. {
  448. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  449. if (model->per_arch[arch][nimpl].regression.nsample)
  450. max_impl = nimpl + 1;
  451. }
  452. else
  453. STARPU_ASSERT_MSG(0, "Unknown history-based performance model %u", arch);
  454. if (arch >= my_narch + arch_base)
  455. continue;
  456. if(substract_to_arch)
  457. {
  458. fprintf(f, "###############\n");
  459. fprintf(f, "# %s_%u\n", name, arch - substract_to_arch);
  460. fprintf(f, "# number of workers on device %s_%u\n", name, arch - substract_to_arch);
  461. fprintf(f, "1\n");
  462. fprintf(f, "##########\n");
  463. fprintf(f, "# 1 worker(s) in parallel\n");
  464. }
  465. else
  466. {
  467. /* CPU */
  468. fprintf(f, "##########\n");
  469. fprintf(f, "# %u worker(s) in parallel\n", arch +1);
  470. }
  471. fprintf(f, "# number of implementations\n");
  472. fprintf(f, "%u\n", max_impl);
  473. for (nimpl = 0; nimpl < max_impl; nimpl++)
  474. {
  475. dump_per_arch_model_file(f, model, arch, nimpl);
  476. }
  477. }
  478. fprintf(f,"####################\n");
  479. fprintf(f,"# MICs\n");
  480. fprintf(f,"# number of MIC devices\n");
  481. fprintf(f,"0\n");
  482. fprintf(f,"####################\n");
  483. fprintf(f,"# SCCs\n");
  484. fprintf(f,"# number of SCC devices\n");
  485. fprintf(f,"0\n");
  486. }
  487. static void initialize_per_arch_model(struct starpu_perfmodel_per_arch *per_arch_model)
  488. {
  489. per_arch_model->history = NULL;
  490. per_arch_model->list = NULL;
  491. per_arch_model->regression.nsample = 0;
  492. per_arch_model->regression.valid = 0;
  493. per_arch_model->regression.nl_valid = 0;
  494. }
  495. static void initialize_model(struct starpu_perfmodel *model)
  496. {
  497. unsigned arch;
  498. unsigned nimpl;
  499. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  500. {
  501. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  502. {
  503. initialize_per_arch_model(&model->per_arch[arch][nimpl]);
  504. }
  505. }
  506. }
  507. static void get_model_debug_path(struct starpu_perfmodel *model, const char *arch, char *path, size_t maxlen)
  508. {
  509. STARPU_ASSERT(path);
  510. _starpu_get_perf_model_dir_debug(path, maxlen);
  511. strncat(path, model->symbol, maxlen);
  512. char hostname[65];
  513. _starpu_gethostname(hostname, sizeof(hostname));
  514. strncat(path, ".", maxlen);
  515. strncat(path, hostname, maxlen);
  516. strncat(path, ".", maxlen);
  517. strncat(path, arch, maxlen);
  518. strncat(path, ".debug", maxlen);
  519. }
  520. /*
  521. * Returns 0 is the model was already loaded, 1 otherwise.
  522. */
  523. int _starpu_register_model(struct starpu_perfmodel *model)
  524. {
  525. /* If the model has already been loaded, there is nothing to do */
  526. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  527. if (model->is_loaded)
  528. {
  529. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  530. return 0;
  531. }
  532. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  533. /* We have to make sure the model has not been loaded since the
  534. * last time we took the lock */
  535. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  536. if (model->is_loaded)
  537. {
  538. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  539. return 0;
  540. }
  541. /* add the model to a linked list */
  542. struct _starpu_perfmodel_list *node = (struct _starpu_perfmodel_list *) malloc(sizeof(struct _starpu_perfmodel_list));
  543. node->model = model;
  544. //model->debug_modelid = debug_modelid++;
  545. /* put this model at the beginning of the list */
  546. node->next = registered_models;
  547. registered_models = node;
  548. #ifdef STARPU_MODEL_DEBUG
  549. _starpu_create_sampling_directory_if_needed();
  550. unsigned arch;
  551. unsigned nimpl;
  552. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  553. {
  554. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  555. {
  556. starpu_perfmodel_debugfilepath(model, arch, model->per_arch[arch][nimpl].debug_path, 256, nimpl);
  557. }
  558. }
  559. #endif
  560. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  561. return 1;
  562. }
  563. static void get_model_path(struct starpu_perfmodel *model, char *path, size_t maxlen)
  564. {
  565. _starpu_get_perf_model_dir_codelets(path, maxlen);
  566. strncat(path, model->symbol, maxlen);
  567. char hostname[65];
  568. _starpu_gethostname(hostname, sizeof(hostname));
  569. strncat(path, ".", maxlen);
  570. strncat(path, hostname, maxlen);
  571. }
  572. static void save_history_based_model(struct starpu_perfmodel *model)
  573. {
  574. STARPU_ASSERT(model);
  575. STARPU_ASSERT(model->symbol);
  576. /* TODO checks */
  577. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  578. char path[256];
  579. get_model_path(model, path, 256);
  580. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  581. /* overwrite existing file, or create it */
  582. FILE *f;
  583. f = fopen(path, "w+");
  584. STARPU_ASSERT_MSG(f, "Could not save performance model %s\n", path);
  585. dump_model_file(f, model);
  586. fclose(f);
  587. }
  588. static void _starpu_dump_registered_models(void)
  589. {
  590. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  591. struct _starpu_perfmodel_list *node;
  592. node = registered_models;
  593. _STARPU_DEBUG("DUMP MODELS !\n");
  594. while (node)
  595. {
  596. save_history_based_model(node->model);
  597. node = node->next;
  598. }
  599. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  600. }
  601. void _starpu_initialize_registered_performance_models(void)
  602. {
  603. registered_models = NULL;
  604. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  605. }
  606. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  607. {
  608. unsigned arch;
  609. unsigned nimpl;
  610. for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  611. {
  612. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  613. {
  614. struct starpu_perfmodel_per_arch *archmodel = &model->per_arch[arch][nimpl];
  615. struct starpu_perfmodel_history_list *list, *plist;
  616. struct starpu_perfmodel_history_table *entry, *tmp;
  617. HASH_ITER(hh, archmodel->history, entry, tmp)
  618. {
  619. HASH_DEL(archmodel->history, entry);
  620. free(entry);
  621. }
  622. archmodel->history = NULL;
  623. list = archmodel->list;
  624. while (list)
  625. {
  626. free(list->entry);
  627. plist = list;
  628. list = list->next;
  629. free(plist);
  630. }
  631. archmodel->list = NULL;
  632. }
  633. }
  634. model->is_loaded = 0;
  635. }
  636. void _starpu_deinitialize_registered_performance_models(void)
  637. {
  638. if (_starpu_get_calibrate_flag())
  639. _starpu_dump_registered_models();
  640. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  641. struct _starpu_perfmodel_list *node, *pnode;
  642. node = registered_models;
  643. _STARPU_DEBUG("FREE MODELS !\n");
  644. while (node)
  645. {
  646. struct starpu_perfmodel *model = node->model;
  647. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  648. _starpu_deinitialize_performance_model(model);
  649. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  650. pnode = node;
  651. node = node->next;
  652. free(pnode);
  653. }
  654. registered_models = NULL;
  655. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  656. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  657. }
  658. /*
  659. * XXX: We should probably factorize the beginning of the _starpu_load_*_model
  660. * functions. This is a bit tricky though, because we must be sure to unlock
  661. * registered_models_rwlock at the right place.
  662. */
  663. void _starpu_load_per_arch_based_model(struct starpu_perfmodel *model)
  664. {
  665. STARPU_ASSERT(model && model->symbol);
  666. int already_loaded;
  667. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  668. already_loaded = model->is_loaded;
  669. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  670. if (already_loaded)
  671. return;
  672. /* The model is still not loaded so we grab the lock in write mode, and
  673. * if it's not loaded once we have the lock, we do load it. */
  674. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  675. /* Was the model initialized since the previous test ? */
  676. if (model->is_loaded)
  677. {
  678. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  679. return;
  680. }
  681. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  682. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  683. }
  684. void _starpu_load_common_based_model(struct starpu_perfmodel *model)
  685. {
  686. STARPU_ASSERT(model && model->symbol);
  687. int already_loaded;
  688. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  689. already_loaded = model->is_loaded;
  690. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  691. if (already_loaded)
  692. return;
  693. /* The model is still not loaded so we grab the lock in write mode, and
  694. * if it's not loaded once we have the lock, we do load it. */
  695. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  696. /* Was the model initialized since the previous test ? */
  697. if (model->is_loaded)
  698. {
  699. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  700. return;
  701. }
  702. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  703. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  704. }
  705. /* We first try to grab the global lock in read mode to check whether the model
  706. * was loaded or not (this is very likely to have been already loaded). If the
  707. * model was not loaded yet, we take the lock in write mode, and if the model
  708. * is still not loaded once we have the lock, we do load it. */
  709. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  710. {
  711. STARPU_ASSERT(model);
  712. STARPU_ASSERT(model->symbol);
  713. int already_loaded;
  714. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  715. already_loaded = model->is_loaded;
  716. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  717. if (already_loaded)
  718. return;
  719. /* The model is still not loaded so we grab the lock in write mode, and
  720. * if it's not loaded once we have the lock, we do load it. */
  721. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  722. /* Was the model initialized since the previous test ? */
  723. if (model->is_loaded)
  724. {
  725. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  726. return;
  727. }
  728. STARPU_PTHREAD_RWLOCK_INIT(&model->model_rwlock, NULL);
  729. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  730. /* make sure the performance model directory exists (or create it) */
  731. _starpu_create_sampling_directory_if_needed();
  732. char path[256];
  733. get_model_path(model, path, 256);
  734. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  735. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  736. model->benchmarking = calibrate_flag;
  737. /* try to open an existing file and load it */
  738. int res;
  739. res = access(path, F_OK);
  740. if (res == 0)
  741. {
  742. if (calibrate_flag == 2)
  743. {
  744. /* The user specified that the performance model should
  745. * be overwritten, so we don't load the existing file !
  746. * */
  747. _STARPU_DEBUG("Overwrite existing file\n");
  748. initialize_model(model);
  749. }
  750. else
  751. {
  752. /* We load the available file */
  753. _STARPU_DEBUG("File exists\n");
  754. FILE *f;
  755. f = fopen(path, "r");
  756. STARPU_ASSERT(f);
  757. parse_model_file(f, model, scan_history);
  758. fclose(f);
  759. }
  760. }
  761. else
  762. {
  763. _STARPU_DEBUG("File does not exists\n");
  764. if (!calibrate_flag)
  765. {
  766. _STARPU_DISP("Warning: model %s is not calibrated, forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol);
  767. _starpu_set_calibrate_flag(1);
  768. model->benchmarking = 1;
  769. }
  770. initialize_model(model);
  771. }
  772. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  773. model->is_loaded = 1;
  774. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  775. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  776. }
  777. void starpu_perfmodel_directory(FILE *output)
  778. {
  779. char perf_model_dir[256];
  780. _starpu_get_perf_model_dir_codelets(perf_model_dir, 256);
  781. fprintf(output, "directory: <%s>\n", perf_model_dir);
  782. }
  783. /* This function is intended to be used by external tools that should read
  784. * the performance model files */
  785. int starpu_perfmodel_list(FILE *output)
  786. {
  787. char path[256];
  788. DIR *dp;
  789. struct dirent *ep;
  790. char perf_model_dir_codelets[256];
  791. _starpu_get_perf_model_dir_codelets(perf_model_dir_codelets, 256);
  792. strncpy(path, perf_model_dir_codelets, 256);
  793. dp = opendir(path);
  794. if (dp != NULL)
  795. {
  796. while ((ep = readdir(dp)))
  797. {
  798. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  799. fprintf(output, "file: <%s>\n", ep->d_name);
  800. }
  801. closedir (dp);
  802. }
  803. else
  804. {
  805. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  806. }
  807. return 0;
  808. }
  809. /* This function is intended to be used by external tools that should read the
  810. * performance model files */
  811. /* TODO: write an clear function, to free symbol and history */
  812. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  813. {
  814. model->symbol = strdup(symbol);
  815. initialize_model(model);
  816. /* where is the file if it exists ? */
  817. char path[256];
  818. get_model_path(model, path, 256);
  819. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  820. /* does it exist ? */
  821. int res;
  822. res = access(path, F_OK);
  823. if (res)
  824. {
  825. const char *dot = strrchr(symbol, '.');
  826. if (dot)
  827. {
  828. char *symbol2 = strdup(symbol);
  829. symbol2[dot-symbol] = '\0';
  830. int ret;
  831. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  832. ret = starpu_perfmodel_load_symbol(symbol2,model);
  833. free(symbol2);
  834. return ret;
  835. }
  836. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  837. return 1;
  838. }
  839. FILE *f = fopen(path, "r");
  840. STARPU_ASSERT(f);
  841. parse_model_file(f, model, 1);
  842. STARPU_ASSERT(fclose(f) == 0);
  843. return 0;
  844. }
  845. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  846. {
  847. free((char *)model->symbol);
  848. _starpu_deinitialize_performance_model(model);
  849. return 0;
  850. }
  851. void starpu_perfmodel_get_arch_name(enum starpu_perfmodel_archtype arch, char *archname, size_t maxlen,unsigned nimpl)
  852. {
  853. if (arch < STARPU_CUDA_DEFAULT)
  854. {
  855. if (arch == STARPU_CPU_DEFAULT)
  856. {
  857. /* NB: We could just use cpu_1 as well ... */
  858. snprintf(archname, maxlen, "cpu_impl_%u",nimpl);
  859. }
  860. else
  861. {
  862. /* For combined CPU workers */
  863. int cpu_count = arch - STARPU_CPU_DEFAULT + 1;
  864. snprintf(archname, maxlen, "cpu_%d_impl_%u", cpu_count,nimpl);
  865. }
  866. }
  867. else if ((STARPU_CUDA_DEFAULT <= arch)
  868. && (arch < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS))
  869. {
  870. int devid = arch - STARPU_CUDA_DEFAULT;
  871. snprintf(archname, maxlen, "cuda_%d_impl_%u", devid,nimpl);
  872. }
  873. else if ((STARPU_OPENCL_DEFAULT <= arch)
  874. && (arch < STARPU_OPENCL_DEFAULT + STARPU_MAXOPENCLDEVS))
  875. {
  876. int devid = arch - STARPU_OPENCL_DEFAULT;
  877. snprintf(archname, maxlen, "opencl_%d_impl_%u", devid,nimpl);
  878. }
  879. else if ((STARPU_MIC_DEFAULT <= arch)
  880. && (arch < STARPU_MIC_DEFAULT + STARPU_MAXMICDEVS))
  881. {
  882. int devid = arch - STARPU_MIC_DEFAULT;
  883. snprintf(archname, maxlen, "mic_%d_impl_%u", devid, nimpl);
  884. }
  885. else
  886. {
  887. STARPU_ABORT();
  888. }
  889. }
  890. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  891. enum starpu_perfmodel_archtype arch, char *path, size_t maxlen, unsigned nimpl)
  892. {
  893. char archname[32];
  894. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  895. STARPU_ASSERT(path);
  896. get_model_debug_path(model, archname, path, maxlen);
  897. }
  898. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j, unsigned nimpl)
  899. {
  900. double exp = NAN;
  901. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  902. struct starpu_perfmodel_regression_model *regmodel;
  903. regmodel = &model->per_arch[arch][nimpl].regression;
  904. if (regmodel->valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  905. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  906. return exp;
  907. }
  908. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  909. {
  910. double exp = NAN;
  911. size_t size = _starpu_job_get_data_size(model, arch, nimpl, j);
  912. struct starpu_perfmodel_regression_model *regmodel;
  913. regmodel = &model->per_arch[arch][nimpl].regression;
  914. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  915. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  916. else
  917. {
  918. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  919. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  920. struct starpu_perfmodel_history_table *history;
  921. struct starpu_perfmodel_history_table *entry;
  922. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  923. history = per_arch_model->history;
  924. HASH_FIND_UINT32_T(history, &key, entry);
  925. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  926. /* Here helgrind would shout that this is unprotected access.
  927. * We do not care about racing access to the mean, we only want
  928. * a good-enough estimation */
  929. if (entry && entry->history_entry && entry->history_entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  930. exp = entry->history_entry->mean;
  931. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  932. if (isnan(exp) && !model->benchmarking)
  933. {
  934. char archname[32];
  935. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  936. _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);
  937. _starpu_set_calibrate_flag(1);
  938. model->benchmarking = 1;
  939. }
  940. }
  941. return exp;
  942. }
  943. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, struct _starpu_job *j,unsigned nimpl)
  944. {
  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 = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  950. per_arch_model = &model->per_arch[arch][nimpl];
  951. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->model_rwlock);
  952. history = per_arch_model->history;
  953. HASH_FIND_UINT32_T(history, &key, elt);
  954. entry = (elt == NULL) ? NULL : elt->history_entry;
  955. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  956. /* Here helgrind would shout that this is unprotected access.
  957. * We do not care about racing access to the mean, we only want
  958. * a good-enough estimation */
  959. if (entry && entry->nsample >= _STARPU_CALIBRATION_MINIMUM)
  960. /* TODO: report differently if we've scheduled really enough
  961. * of that task and the scheduler should perhaps put it aside */
  962. /* Calibrated enough */
  963. exp = entry->mean;
  964. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  965. if (isnan(exp) && !model->benchmarking)
  966. {
  967. char archname[32];
  968. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  969. _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);
  970. _starpu_set_calibrate_flag(1);
  971. model->benchmarking = 1;
  972. }
  973. return exp;
  974. }
  975. double starpu_permodel_history_based_expected_perf(struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, uint32_t footprint)
  976. {
  977. struct _starpu_job j =
  978. {
  979. .footprint = footprint,
  980. .footprint_is_computed = 1,
  981. };
  982. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  983. }
  984. void _starpu_update_perfmodel_history(struct _starpu_job *j, struct starpu_perfmodel *model, enum starpu_perfmodel_archtype arch, unsigned cpuid STARPU_ATTRIBUTE_UNUSED, double measured, unsigned nimpl)
  985. {
  986. if (model)
  987. {
  988. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->model_rwlock);
  989. struct starpu_perfmodel_per_arch *per_arch_model = &model->per_arch[arch][nimpl];
  990. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  991. {
  992. struct starpu_perfmodel_history_entry *entry;
  993. struct starpu_perfmodel_history_table *elt;
  994. struct starpu_perfmodel_history_list **list;
  995. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  996. list = &per_arch_model->list;
  997. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  998. entry = (elt == NULL) ? NULL : elt->history_entry;
  999. if (!entry)
  1000. {
  1001. /* this is the first entry with such a footprint */
  1002. entry = (struct starpu_perfmodel_history_entry *) malloc(sizeof(struct starpu_perfmodel_history_entry));
  1003. STARPU_ASSERT(entry);
  1004. /* Tell helgrind that we do not care about
  1005. * racing access to the sampling, we only want a
  1006. * good-enough estimation */
  1007. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  1008. STARPU_HG_DISABLE_CHECKING(entry->mean);
  1009. entry->mean = measured;
  1010. entry->sum = measured;
  1011. entry->deviation = 0.0;
  1012. entry->sum2 = measured*measured;
  1013. entry->size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1014. entry->flops = j->task->flops;
  1015. entry->footprint = key;
  1016. entry->nsample = 1;
  1017. insert_history_entry(entry, list, &per_arch_model->history);
  1018. }
  1019. else
  1020. {
  1021. /* there is already some entry with the same footprint */
  1022. entry->sum += measured;
  1023. entry->sum2 += measured*measured;
  1024. entry->nsample++;
  1025. unsigned n = entry->nsample;
  1026. entry->mean = entry->sum / n;
  1027. entry->deviation = sqrt((entry->sum2 - (entry->sum*entry->sum)/n)/n);
  1028. if (j->task->flops != 0.)
  1029. {
  1030. if (entry->flops == 0.)
  1031. entry->flops = j->task->flops;
  1032. else if (entry->flops != j->task->flops)
  1033. /* Incoherent flops! forget about trying to record flops */
  1034. entry->flops = NAN;
  1035. }
  1036. }
  1037. STARPU_ASSERT(entry);
  1038. }
  1039. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1040. {
  1041. struct starpu_perfmodel_regression_model *reg_model;
  1042. reg_model = &per_arch_model->regression;
  1043. /* update the regression model */
  1044. size_t job_size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1045. double logy, logx;
  1046. logx = log((double)job_size);
  1047. logy = log(measured);
  1048. reg_model->sumlnx += logx;
  1049. reg_model->sumlnx2 += logx*logx;
  1050. reg_model->sumlny += logy;
  1051. reg_model->sumlnxlny += logx*logy;
  1052. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1053. reg_model->minx = job_size;
  1054. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1055. reg_model->maxx = job_size;
  1056. reg_model->nsample++;
  1057. if (VALID_REGRESSION(reg_model))
  1058. {
  1059. unsigned n = reg_model->nsample;
  1060. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1061. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1062. reg_model->beta = num/denom;
  1063. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1064. reg_model->valid = 1;
  1065. }
  1066. }
  1067. #ifdef STARPU_MODEL_DEBUG
  1068. struct starpu_task *task = j->task;
  1069. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1070. if (f == NULL)
  1071. {
  1072. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1073. STARPU_ABORT();
  1074. }
  1075. if (!j->footprint_is_computed)
  1076. (void) _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1077. STARPU_ASSERT(j->footprint_is_computed);
  1078. 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, nimpl, j), measured, task->predicted, task->predicted_transfer, cpuid);
  1079. unsigned i;
  1080. for (i = 0; i < task->cl->nbuffers; i++)
  1081. {
  1082. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1083. STARPU_ASSERT(handle->ops);
  1084. STARPU_ASSERT(handle->ops->display);
  1085. handle->ops->display(handle, f);
  1086. }
  1087. fprintf(f, "\n");
  1088. fclose(f);
  1089. #endif
  1090. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->model_rwlock);
  1091. }
  1092. }
  1093. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, enum starpu_perfmodel_archtype arch, unsigned cpuid, unsigned nimpl, double measured)
  1094. {
  1095. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1096. _starpu_load_perfmodel(model);
  1097. /* Record measurement */
  1098. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1099. /* and save perfmodel on termination */
  1100. _starpu_set_calibrate_flag(1);
  1101. }