perfmodel_history.c 48 KB

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