perfmodel_history.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2016, 2017 Inria
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  20. #include <dirent.h>
  21. #include <sys/stat.h>
  22. #endif
  23. #include <errno.h>
  24. #include <common/config.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <common/utils.h>
  29. #include <core/perfmodel/perfmodel.h>
  30. #include <core/jobs.h>
  31. #include <core/workers.h>
  32. #include <datawizard/datawizard.h>
  33. #include <core/perfmodel/regression.h>
  34. #include <core/perfmodel/multiple_regression.h>
  35. #include <common/config.h>
  36. #include <starpu_parameters.h>
  37. #include <common/uthash.h>
  38. #include <limits.h>
  39. #include <core/task.h>
  40. #ifdef STARPU_HAVE_WINDOWS
  41. #include <windows.h>
  42. #endif
  43. #define HASH_ADD_UINT32_T(head,field,add) HASH_ADD(hh,head,field,sizeof(uint32_t),add)
  44. #define HASH_FIND_UINT32_T(head,find,out) HASH_FIND(hh,head,find,sizeof(uint32_t),out)
  45. #define STR_SHORT_LENGTH 32
  46. #define STR_LONG_LENGTH 256
  47. #define STR_VERY_LONG_LENGTH 1024
  48. static struct starpu_perfmodel_arch **arch_combs;
  49. static int current_arch_comb;
  50. static int nb_arch_combs;
  51. static starpu_pthread_rwlock_t arch_combs_mutex;
  52. static int historymaxerror;
  53. static char ignore_devid[STARPU_ANY_WORKER];
  54. /* How many executions a codelet will have to be measured before we
  55. * consider that calibration will provide a value good enough for scheduling */
  56. unsigned _starpu_calibration_minimum;
  57. struct starpu_perfmodel_history_table
  58. {
  59. UT_hash_handle hh;
  60. uint32_t footprint;
  61. struct starpu_perfmodel_history_entry *history_entry;
  62. };
  63. /* We want more than 10% variance on X to trust regression */
  64. #define VALID_REGRESSION(reg_model) \
  65. ((reg_model)->minx < (9*(reg_model)->maxx)/10 && (reg_model)->nsample >= _starpu_calibration_minimum)
  66. static starpu_pthread_rwlock_t registered_models_rwlock;
  67. LIST_TYPE(_starpu_perfmodel,
  68. struct starpu_perfmodel *model;
  69. )
  70. static struct _starpu_perfmodel_list registered_models;
  71. void _starpu_perfmodel_malloc_per_arch(struct starpu_perfmodel *model, int comb, int nb_impl)
  72. {
  73. int i;
  74. _STARPU_MALLOC(model->state->per_arch[comb], nb_impl*sizeof(struct starpu_perfmodel_per_arch));
  75. for(i = 0; i < nb_impl; i++)
  76. {
  77. memset(&model->state->per_arch[comb][i], 0, sizeof(struct starpu_perfmodel_per_arch));
  78. }
  79. model->state->nimpls_set[comb] = nb_impl;
  80. }
  81. void _starpu_perfmodel_malloc_per_arch_is_set(struct starpu_perfmodel *model, int comb, int nb_impl)
  82. {
  83. int i;
  84. _STARPU_MALLOC(model->state->per_arch_is_set[comb], nb_impl*sizeof(int));
  85. for(i = 0; i < nb_impl; i++)
  86. {
  87. model->state->per_arch_is_set[comb][i] = 0;
  88. }
  89. }
  90. int _starpu_perfmodel_arch_comb_get(int ndevices, struct starpu_perfmodel_device *devices)
  91. {
  92. int comb, ncomb;
  93. ncomb = current_arch_comb;
  94. for(comb = 0; comb < ncomb; comb++)
  95. {
  96. int found = 0;
  97. if(arch_combs[comb]->ndevices == ndevices)
  98. {
  99. int dev1, dev2;
  100. int nfounded = 0;
  101. for(dev1 = 0; dev1 < arch_combs[comb]->ndevices; dev1++)
  102. {
  103. for(dev2 = 0; dev2 < ndevices; dev2++)
  104. {
  105. if(arch_combs[comb]->devices[dev1].type == devices[dev2].type &&
  106. (ignore_devid[devices[dev2].type] ||
  107. arch_combs[comb]->devices[dev1].devid == devices[dev2].devid) &&
  108. arch_combs[comb]->devices[dev1].ncores == devices[dev2].ncores)
  109. nfounded++;
  110. }
  111. }
  112. if(nfounded == ndevices)
  113. found = 1;
  114. }
  115. if (found)
  116. return comb;
  117. }
  118. return -1;
  119. }
  120. int starpu_perfmodel_arch_comb_get(int ndevices, struct starpu_perfmodel_device *devices)
  121. {
  122. int ret;
  123. STARPU_PTHREAD_RWLOCK_RDLOCK(&arch_combs_mutex);
  124. ret = _starpu_perfmodel_arch_comb_get(ndevices, devices);
  125. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  126. return ret;
  127. }
  128. int starpu_perfmodel_arch_comb_add(int ndevices, struct starpu_perfmodel_device* devices)
  129. {
  130. STARPU_PTHREAD_RWLOCK_WRLOCK(&arch_combs_mutex);
  131. int comb = _starpu_perfmodel_arch_comb_get(ndevices, devices);
  132. if (comb != -1)
  133. {
  134. /* Somebody else added it in between */
  135. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  136. return comb;
  137. }
  138. if (current_arch_comb >= nb_arch_combs)
  139. {
  140. // We need to allocate more arch_combs
  141. nb_arch_combs = current_arch_comb+10;
  142. _STARPU_REALLOC(arch_combs, nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  143. }
  144. _STARPU_MALLOC(arch_combs[current_arch_comb], sizeof(struct starpu_perfmodel_arch));
  145. _STARPU_MALLOC(arch_combs[current_arch_comb]->devices, ndevices*sizeof(struct starpu_perfmodel_device));
  146. arch_combs[current_arch_comb]->ndevices = ndevices;
  147. int dev;
  148. for(dev = 0; dev < ndevices; dev++)
  149. {
  150. arch_combs[current_arch_comb]->devices[dev].type = devices[dev].type;
  151. arch_combs[current_arch_comb]->devices[dev].devid = devices[dev].devid;
  152. arch_combs[current_arch_comb]->devices[dev].ncores = devices[dev].ncores;
  153. }
  154. comb = current_arch_comb++;
  155. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  156. return comb;
  157. }
  158. static void _free_arch_combs(void)
  159. {
  160. int i;
  161. STARPU_PTHREAD_RWLOCK_WRLOCK(&arch_combs_mutex);
  162. for(i = 0; i < current_arch_comb; i++)
  163. {
  164. free(arch_combs[i]->devices);
  165. free(arch_combs[i]);
  166. }
  167. current_arch_comb = 0;
  168. free(arch_combs);
  169. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  170. STARPU_PTHREAD_RWLOCK_DESTROY(&arch_combs_mutex);
  171. }
  172. int starpu_perfmodel_get_narch_combs()
  173. {
  174. return current_arch_comb;
  175. }
  176. struct starpu_perfmodel_arch *starpu_perfmodel_arch_comb_fetch(int comb)
  177. {
  178. return arch_combs[comb];
  179. }
  180. size_t _starpu_job_get_data_size(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, unsigned impl, struct _starpu_job *j)
  181. {
  182. struct starpu_task *task = j->task;
  183. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  184. if (model && model->state->per_arch && comb != -1 && model->state->per_arch[comb] && model->state->per_arch[comb][impl].size_base)
  185. {
  186. return model->state->per_arch[comb][impl].size_base(task, arch, impl);
  187. }
  188. else if (model && model->size_base)
  189. {
  190. return model->size_base(task, impl);
  191. }
  192. else
  193. {
  194. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  195. size_t size = 0;
  196. unsigned buffer;
  197. for (buffer = 0; buffer < nbuffers; buffer++)
  198. {
  199. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  200. size += _starpu_data_get_size(handle);
  201. }
  202. return size;
  203. }
  204. }
  205. /*
  206. * History based model
  207. */
  208. static void insert_history_entry(struct starpu_perfmodel_history_entry *entry, struct starpu_perfmodel_history_list **list, struct starpu_perfmodel_history_table **history_ptr)
  209. {
  210. struct starpu_perfmodel_history_list *link;
  211. struct starpu_perfmodel_history_table *table;
  212. _STARPU_MALLOC(link, sizeof(struct starpu_perfmodel_history_list));
  213. link->next = *list;
  214. link->entry = entry;
  215. *list = link;
  216. /* detect concurrency issue */
  217. //HASH_FIND_UINT32_T(*history_ptr, &entry->footprint, table);
  218. //STARPU_ASSERT(table == NULL);
  219. _STARPU_MALLOC(table, sizeof(*table));
  220. table->footprint = entry->footprint;
  221. table->history_entry = entry;
  222. HASH_ADD_UINT32_T(*history_ptr, footprint, table);
  223. }
  224. #ifndef STARPU_SIMGRID
  225. static void check_reg_model(struct starpu_perfmodel *model, int comb, int impl)
  226. {
  227. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  228. struct starpu_perfmodel_regression_model *reg_model = &per_arch_model->regression;
  229. /*
  230. * Linear Regression model
  231. */
  232. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  233. double alpha = nan(""), beta = nan("");
  234. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  235. {
  236. if (reg_model->nsample > 1)
  237. {
  238. alpha = reg_model->alpha;
  239. beta = reg_model->beta;
  240. }
  241. }
  242. /* TODO: check:
  243. * reg_model->sumlnx
  244. * reg_model->sumlnx2
  245. * reg_model->sumlny
  246. * reg_model->sumlnxlny
  247. * alpha
  248. * beta
  249. * reg_model->minx
  250. * reg_model->maxx
  251. */
  252. (void)alpha;
  253. (void)beta;
  254. /*
  255. * Non-Linear Regression model
  256. */
  257. double a = nan(""), b = nan(""), c = nan("");
  258. if (model->type == STARPU_NL_REGRESSION_BASED)
  259. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  260. /* TODO: check:
  261. * a
  262. * b
  263. * c
  264. */
  265. /*
  266. * Multiple Regression Model
  267. */
  268. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  269. {
  270. /* TODO: check: */
  271. }
  272. }
  273. static void dump_reg_model(FILE *f, struct starpu_perfmodel *model, int comb, int impl)
  274. {
  275. struct starpu_perfmodel_per_arch *per_arch_model;
  276. per_arch_model = &model->state->per_arch[comb][impl];
  277. struct starpu_perfmodel_regression_model *reg_model;
  278. reg_model = &per_arch_model->regression;
  279. /*
  280. * Linear Regression model
  281. */
  282. /* Unless we have enough measurements, we put NaN in the file to indicate the model is invalid */
  283. double alpha = nan(""), beta = nan("");
  284. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  285. {
  286. if (reg_model->nsample > 1)
  287. {
  288. alpha = reg_model->alpha;
  289. beta = reg_model->beta;
  290. }
  291. }
  292. fprintf(f, "# sumlnx\tsumlnx2\t\tsumlny\t\tsumlnxlny\talpha\t\tbeta\t\tn\tminx\t\tmaxx\n");
  293. fprintf(f, "%-15e\t%-15e\t%-15e\t%-15e\t", reg_model->sumlnx, reg_model->sumlnx2, reg_model->sumlny, reg_model->sumlnxlny);
  294. _starpu_write_double(f, "%-15e", alpha);
  295. fprintf(f, "\t");
  296. _starpu_write_double(f, "%-15e", beta);
  297. fprintf(f, "\t%u\t%-15lu\t%-15lu\n", reg_model->nsample, reg_model->minx, reg_model->maxx);
  298. /*
  299. * Non-Linear Regression model
  300. */
  301. double a = nan(""), b = nan(""), c = nan("");
  302. if (model->type == STARPU_NL_REGRESSION_BASED)
  303. _starpu_regression_non_linear_power(per_arch_model->list, &a, &b, &c);
  304. fprintf(f, "# a\t\tb\t\tc\n");
  305. _starpu_write_double(f, "%-15e", a);
  306. fprintf(f, "\t");
  307. _starpu_write_double(f, "%-15e", b);
  308. fprintf(f, "\t");
  309. _starpu_write_double(f, "%-15e", c);
  310. fprintf(f, "\n");
  311. /*
  312. * Multiple Regression Model
  313. */
  314. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  315. {
  316. if (reg_model->ncoeff==0 && model->ncombinations!=0 && model->combinations!=NULL)
  317. {
  318. reg_model->ncoeff = model->ncombinations + 1;
  319. }
  320. _STARPU_MALLOC(reg_model->coeff, reg_model->ncoeff*sizeof(double));
  321. _starpu_multiple_regression(per_arch_model->list, reg_model->coeff, reg_model->ncoeff, model->nparameters, model->parameters_names, model->combinations, model->symbol);
  322. fprintf(f, "# n\tintercept\t");
  323. if (reg_model->ncoeff==0 || model->ncombinations==0 || model->combinations==NULL)
  324. fprintf(f, "\n1\tnan");
  325. else
  326. {
  327. unsigned i;
  328. for (i=0; i < model->ncombinations; i++)
  329. {
  330. if (model->parameters_names == NULL)
  331. fprintf(f, "c%u", i+1);
  332. else
  333. {
  334. unsigned j;
  335. int first=1;
  336. for(j=0; j < model->nparameters; j++)
  337. {
  338. if (model->combinations[i][j] > 0)
  339. {
  340. if (first)
  341. first=0;
  342. else
  343. fprintf(f, "*");
  344. if(model->parameters_names[j] != NULL)
  345. fprintf(f, "%s", model->parameters_names[j]);
  346. else
  347. fprintf(f, "P%u", j);
  348. if (model->combinations[i][j] > 1)
  349. fprintf(f, "^%d", model->combinations[i][j]);
  350. }
  351. }
  352. }
  353. fprintf(f, "\t\t");
  354. }
  355. fprintf(f, "\n%u", reg_model->ncoeff);
  356. for (i=0; i < reg_model->ncoeff; i++)
  357. fprintf(f, "\t%-15e", reg_model->coeff[i]);
  358. }
  359. }
  360. }
  361. #endif
  362. static void scan_reg_model(FILE *f, const char *path, struct starpu_perfmodel_regression_model *reg_model, enum starpu_perfmodel_type model_type)
  363. {
  364. int res;
  365. /*
  366. * Linear Regression model
  367. */
  368. _starpu_drop_comments(f);
  369. res = fscanf(f, "%le\t%le\t%le\t%le\t", &reg_model->sumlnx, &reg_model->sumlnx2, &reg_model->sumlny, &reg_model->sumlnxlny);
  370. STARPU_ASSERT_MSG(res == 4, "Incorrect performance model file %s", path);
  371. res = _starpu_read_double(f, "%le", &reg_model->alpha);
  372. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  373. res = _starpu_read_double(f, "\t%le", &reg_model->beta);
  374. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  375. res = fscanf(f, "\t%u\t%lu\t%lu\n", &reg_model->nsample, &reg_model->minx, &reg_model->maxx);
  376. STARPU_ASSERT_MSG(res == 3, "Incorrect performance model file %s", path);
  377. /* If any of the parameters describing the linear regression model is NaN, the model is invalid */
  378. unsigned invalid = (isnan(reg_model->alpha)||isnan(reg_model->beta));
  379. reg_model->valid = !invalid && VALID_REGRESSION(reg_model);
  380. /*
  381. * Non-Linear Regression model
  382. */
  383. _starpu_drop_comments(f);
  384. res = _starpu_read_double(f, "%le", &reg_model->a);
  385. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  386. res = _starpu_read_double(f, "\t%le", &reg_model->b);
  387. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  388. res = _starpu_read_double(f, "%le", &reg_model->c);
  389. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  390. res = fscanf(f, "\n");
  391. STARPU_ASSERT_MSG(res == 0, "Incorrect performance model file %s", path);
  392. /* If any of the parameters describing the non-linear regression model is NaN, the model is invalid */
  393. unsigned nl_invalid = (isnan(reg_model->a)||isnan(reg_model->b)||isnan(reg_model->c));
  394. reg_model->nl_valid = !nl_invalid && VALID_REGRESSION(reg_model);
  395. /*
  396. * Multiple Regression Model
  397. */
  398. if (model_type == STARPU_MULTIPLE_REGRESSION_BASED)
  399. {
  400. _starpu_drop_comments(f);
  401. // Read how many coefficients is there
  402. res = fscanf(f, "%u", &reg_model->ncoeff);
  403. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  404. _STARPU_MALLOC(reg_model->coeff, reg_model->ncoeff*sizeof(double));
  405. unsigned multi_invalid = 0;
  406. unsigned i;
  407. for (i=0; i < reg_model->ncoeff; i++)
  408. {
  409. res = _starpu_read_double(f, "%le", &reg_model->coeff[i]);
  410. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  411. multi_invalid = (multi_invalid||isnan(reg_model->coeff[i]));
  412. }
  413. reg_model->multi_valid = !multi_invalid;
  414. }
  415. }
  416. #ifndef STARPU_SIMGRID
  417. static void check_history_entry(struct starpu_perfmodel_history_entry *entry)
  418. {
  419. STARPU_ASSERT_MSG(entry->deviation >= 0, "entry=%p, entry->deviation=%lf\n", entry, entry->deviation);
  420. STARPU_ASSERT_MSG(entry->sum >= 0, "entry=%p, entry->sum=%lf\n", entry, entry->sum);
  421. STARPU_ASSERT_MSG(entry->sum2 >= 0, "entry=%p, entry->sum2=%lf\n", entry, entry->sum2);
  422. STARPU_ASSERT_MSG(entry->mean >= 0, "entry=%p, entry->mean=%lf\n", entry, entry->mean);
  423. STARPU_ASSERT_MSG(isnan(entry->flops)||entry->flops >= 0, "entry=%p, entry->flops=%lf\n", entry, entry->flops);
  424. STARPU_ASSERT_MSG(entry->duration >= 0, "entry=%p, entry->duration=%lf\n", entry, entry->duration);
  425. }
  426. static void dump_history_entry(FILE *f, struct starpu_perfmodel_history_entry *entry)
  427. {
  428. fprintf(f, "%08x\t%-15lu\t%-15e\t%-15e\t%-15e\t%-15e\t%-15e\t%u\n", entry->footprint, (unsigned long) entry->size, entry->flops, entry->mean, entry->deviation, entry->sum, entry->sum2, entry->nsample);
  429. }
  430. #endif
  431. static void scan_history_entry(FILE *f, const char *path, struct starpu_perfmodel_history_entry *entry)
  432. {
  433. int res;
  434. _starpu_drop_comments(f);
  435. /* In case entry is NULL, we just drop these values */
  436. unsigned nsample;
  437. uint32_t footprint;
  438. unsigned long size; /* in bytes */
  439. double flops;
  440. double mean;
  441. double deviation;
  442. double sum;
  443. double sum2;
  444. char line[STR_LONG_LENGTH];
  445. char *ret;
  446. ret = fgets(line, sizeof(line), f);
  447. STARPU_ASSERT(ret);
  448. STARPU_ASSERT(strchr(line, '\n'));
  449. /* Read the values from the file */
  450. 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);
  451. if (res != 8)
  452. {
  453. flops = 0.;
  454. /* Read the values from the file */
  455. res = sscanf(line, "%x\t%lu\t%le\t%le\t%le\t%le\t%u", &footprint, &size, &mean, &deviation, &sum, &sum2, &nsample);
  456. STARPU_ASSERT_MSG(res == 7, "Incorrect performance model file %s", path);
  457. }
  458. if (entry)
  459. {
  460. STARPU_ASSERT_MSG(flops >=0, "Negative flops %lf in performance model file %s", flops, path);
  461. STARPU_ASSERT_MSG(mean >=0, "Negative mean %lf in performance model file %s", mean, path);
  462. STARPU_ASSERT_MSG(deviation >=0, "Negative deviation %lf in performance model file %s", deviation, path);
  463. STARPU_ASSERT_MSG(sum >=0, "Negative sum %lf in performance model file %s", sum, path);
  464. STARPU_ASSERT_MSG(sum2 >=0, "Negative sum2 %lf in performance model file %s", sum2, path);
  465. entry->footprint = footprint;
  466. entry->size = size;
  467. entry->flops = flops;
  468. entry->mean = mean;
  469. entry->deviation = deviation;
  470. entry->sum = sum;
  471. entry->sum2 = sum2;
  472. entry->nsample = nsample;
  473. }
  474. }
  475. static void parse_per_arch_model_file(FILE *f, const char *path, struct starpu_perfmodel_per_arch *per_arch_model, unsigned scan_history, enum starpu_perfmodel_type model_type)
  476. {
  477. unsigned nentries;
  478. _starpu_drop_comments(f);
  479. int res = fscanf(f, "%u\n", &nentries);
  480. STARPU_ASSERT_MSG(res == 1, "Incorrect performance model file %s", path);
  481. scan_reg_model(f, path, &per_arch_model->regression, model_type);
  482. /* parse entries */
  483. unsigned i;
  484. for (i = 0; i < nentries; i++)
  485. {
  486. struct starpu_perfmodel_history_entry *entry = NULL;
  487. if (scan_history)
  488. {
  489. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  490. /* Tell helgrind that we do not care about
  491. * racing access to the sampling, we only want a
  492. * good-enough estimation */
  493. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  494. STARPU_HG_DISABLE_CHECKING(entry->mean);
  495. entry->nerror = 0;
  496. }
  497. scan_history_entry(f, path, entry);
  498. /* insert the entry in the hashtable and the list structures */
  499. /* TODO: Insert it at the end of the list, to avoid reversing
  500. * the order... But efficiently! We may have a lot of entries */
  501. if (scan_history)
  502. insert_history_entry(entry, &per_arch_model->list, &per_arch_model->history);
  503. }
  504. }
  505. static void parse_arch(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history, int comb)
  506. {
  507. struct starpu_perfmodel_per_arch dummy;
  508. unsigned nimpls, impl, i, ret;
  509. /* Parsing number of implementation */
  510. _starpu_drop_comments(f);
  511. ret = fscanf(f, "%u\n", &nimpls);
  512. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  513. if( model != NULL)
  514. {
  515. /* Parsing each implementation */
  516. unsigned implmax = STARPU_MIN(nimpls, STARPU_MAXIMPLEMENTATIONS);
  517. model->state->nimpls[comb] = implmax;
  518. if (!model->state->per_arch[comb])
  519. {
  520. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  521. }
  522. if (!model->state->per_arch_is_set[comb])
  523. {
  524. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  525. }
  526. for (impl = 0; impl < implmax; impl++)
  527. {
  528. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  529. model->state->per_arch_is_set[comb][impl] = 1;
  530. parse_per_arch_model_file(f, path, per_arch_model, scan_history, model->type);
  531. }
  532. }
  533. else
  534. {
  535. impl = 0;
  536. }
  537. /* if the number of implementation is greater than STARPU_MAXIMPLEMENTATIONS
  538. * we skip the last implementation */
  539. for (i = impl; i < nimpls; i++)
  540. {
  541. if( model != NULL)
  542. parse_per_arch_model_file(f, path, &dummy, 0, model->type);
  543. else
  544. parse_per_arch_model_file(f, path, &dummy, 0, 0);
  545. }
  546. }
  547. static enum starpu_worker_archtype _get_enum_type(int type)
  548. {
  549. switch(type)
  550. {
  551. case 0:
  552. return STARPU_CPU_WORKER;
  553. case 1:
  554. return STARPU_CUDA_WORKER;
  555. case 2:
  556. return STARPU_OPENCL_WORKER;
  557. case 3:
  558. return STARPU_MIC_WORKER;
  559. case 4:
  560. return STARPU_SCC_WORKER;
  561. case 5:
  562. return STARPU_MPI_MS_WORKER;
  563. default:
  564. STARPU_ABORT();
  565. }
  566. }
  567. static void parse_comb(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history, int comb)
  568. {
  569. int ndevices = 0;
  570. _starpu_drop_comments(f);
  571. int ret = fscanf(f, "%d\n", &ndevices );
  572. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  573. struct starpu_perfmodel_device devices[ndevices];
  574. int dev;
  575. for(dev = 0; dev < ndevices; dev++)
  576. {
  577. enum starpu_worker_archtype dev_type;
  578. _starpu_drop_comments(f);
  579. int type;
  580. ret = fscanf(f, "%d\n", &type);
  581. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  582. dev_type = _get_enum_type(type);
  583. int dev_id;
  584. _starpu_drop_comments(f);
  585. ret = fscanf(f, "%d\n", &dev_id);
  586. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  587. int ncores;
  588. _starpu_drop_comments(f);
  589. ret = fscanf(f, "%d\n", &ncores);
  590. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  591. devices[dev].type = dev_type;
  592. devices[dev].devid = dev_id;
  593. devices[dev].ncores = ncores;
  594. }
  595. int id_comb = starpu_perfmodel_arch_comb_get(ndevices, devices);
  596. if(id_comb == -1)
  597. id_comb = starpu_perfmodel_arch_comb_add(ndevices, devices);
  598. model->state->combs[comb] = id_comb;
  599. parse_arch(f, path, model, scan_history, id_comb);
  600. }
  601. static int parse_model_file(FILE *f, const char *path, struct starpu_perfmodel *model, unsigned scan_history)
  602. {
  603. int ret, version=0;
  604. /* First check that it's not empty (very common corruption result, for
  605. * which there is no solution) */
  606. fseek(f, 0, SEEK_END);
  607. long pos = ftell(f);
  608. if (pos == 0)
  609. {
  610. _STARPU_DISP("Performance model file %s is empty, ignoring it\n", path);
  611. return 1;
  612. }
  613. rewind(f);
  614. /* Parsing performance model version */
  615. _starpu_drop_comments(f);
  616. ret = fscanf(f, "%d\n", &version);
  617. STARPU_ASSERT_MSG(version == _STARPU_PERFMODEL_VERSION, "Incorrect performance model file %s with a model version %d not being the current model version (%d)\n", path,
  618. version, _STARPU_PERFMODEL_VERSION);
  619. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  620. int ncombs = 0;
  621. _starpu_drop_comments(f);
  622. ret = fscanf(f, "%d\n", &ncombs);
  623. STARPU_ASSERT_MSG(ret == 1, "Incorrect performance model file %s", path);
  624. if(ncombs > 0)
  625. {
  626. model->state->ncombs = ncombs;
  627. }
  628. if (ncombs > model->state->ncombs_set)
  629. {
  630. // The model has more combs than the original number of arch_combs, we need to reallocate
  631. _starpu_perfmodel_realloc(model, ncombs);
  632. }
  633. int comb;
  634. for(comb = 0; comb < ncombs; comb++)
  635. parse_comb(f, path, model, scan_history, comb);
  636. return 0;
  637. }
  638. #ifndef STARPU_SIMGRID
  639. static void check_per_arch_model(struct starpu_perfmodel *model, int comb, unsigned impl)
  640. {
  641. struct starpu_perfmodel_per_arch *per_arch_model;
  642. per_arch_model = &model->state->per_arch[comb][impl];
  643. /* count the number of elements in the lists */
  644. struct starpu_perfmodel_history_list *ptr = NULL;
  645. unsigned nentries = 0;
  646. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  647. {
  648. /* Dump the list of all entries in the history */
  649. ptr = per_arch_model->list;
  650. while(ptr)
  651. {
  652. nentries++;
  653. ptr = ptr->next;
  654. }
  655. }
  656. /* header */
  657. char archname[STR_SHORT_LENGTH];
  658. starpu_perfmodel_get_arch_name(arch_combs[comb], archname, sizeof(archname), impl);
  659. STARPU_ASSERT(strlen(archname)>0);
  660. check_reg_model(model, comb, impl);
  661. /* Dump the history into the model file in case it is necessary */
  662. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  663. {
  664. ptr = per_arch_model->list;
  665. while (ptr)
  666. {
  667. check_history_entry(ptr->entry);
  668. ptr = ptr->next;
  669. }
  670. }
  671. }
  672. static void dump_per_arch_model_file(FILE *f, struct starpu_perfmodel *model, int comb, unsigned impl)
  673. {
  674. struct starpu_perfmodel_per_arch *per_arch_model;
  675. per_arch_model = &model->state->per_arch[comb][impl];
  676. /* count the number of elements in the lists */
  677. struct starpu_perfmodel_history_list *ptr = NULL;
  678. unsigned nentries = 0;
  679. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  680. {
  681. /* Dump the list of all entries in the history */
  682. ptr = per_arch_model->list;
  683. while(ptr)
  684. {
  685. nentries++;
  686. ptr = ptr->next;
  687. }
  688. }
  689. /* header */
  690. char archname[STR_SHORT_LENGTH];
  691. starpu_perfmodel_get_arch_name(arch_combs[comb], archname, sizeof(archname), impl);
  692. fprintf(f, "#####\n");
  693. fprintf(f, "# Model for %s\n", archname);
  694. fprintf(f, "# number of entries\n%u\n", nentries);
  695. dump_reg_model(f, model, comb, impl);
  696. /* Dump the history into the model file in case it is necessary */
  697. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  698. {
  699. fprintf(f, "# hash\t\tsize\t\tflops\t\tmean (us)\tdev (us)\tsum\t\tsum2\t\tn\n");
  700. ptr = per_arch_model->list;
  701. while (ptr)
  702. {
  703. dump_history_entry(f, ptr->entry);
  704. ptr = ptr->next;
  705. }
  706. }
  707. fprintf(f, "\n");
  708. }
  709. static void check_model(struct starpu_perfmodel *model)
  710. {
  711. int ncombs = model->state->ncombs;
  712. STARPU_ASSERT(ncombs >= 0);
  713. int i, impl, dev;
  714. for(i = 0; i < ncombs; i++)
  715. {
  716. int comb = model->state->combs[i];
  717. STARPU_ASSERT(comb >= 0);
  718. int ndevices = arch_combs[comb]->ndevices;
  719. STARPU_ASSERT(ndevices >= 1);
  720. for(dev = 0; dev < ndevices; dev++)
  721. {
  722. STARPU_ASSERT(arch_combs[comb]->devices[dev].type >= 0);
  723. STARPU_ASSERT(arch_combs[comb]->devices[dev].type <= 5);
  724. STARPU_ASSERT(arch_combs[comb]->devices[dev].devid >= 0);
  725. STARPU_ASSERT(arch_combs[comb]->devices[dev].ncores >= 0);
  726. }
  727. int nimpls = model->state->nimpls[comb];
  728. STARPU_ASSERT(nimpls >= 1);
  729. for (impl = 0; impl < nimpls; impl++)
  730. {
  731. check_per_arch_model(model, comb, impl);
  732. }
  733. }
  734. }
  735. static void dump_model_file(FILE *f, struct starpu_perfmodel *model)
  736. {
  737. fprintf(f, "##################\n");
  738. fprintf(f, "# Performance Model Version\n");
  739. fprintf(f, "%d\n\n", _STARPU_PERFMODEL_VERSION);
  740. int ncombs = model->state->ncombs;
  741. fprintf(f, "####################\n");
  742. fprintf(f, "# COMBs\n");
  743. fprintf(f, "# number of combinations\n");
  744. fprintf(f, "%d\n", ncombs);
  745. int i, impl, dev;
  746. for(i = 0; i < ncombs; i++)
  747. {
  748. int comb = model->state->combs[i];
  749. int ndevices = arch_combs[comb]->ndevices;
  750. fprintf(f, "####################\n");
  751. fprintf(f, "# COMB_%d\n", comb);
  752. fprintf(f, "# number of types devices\n");
  753. fprintf(f, "%d\n", ndevices);
  754. for(dev = 0; dev < ndevices; dev++)
  755. {
  756. fprintf(f, "####################\n");
  757. fprintf(f, "# DEV_%d\n", dev);
  758. fprintf(f, "# device type (CPU - 0, CUDA - 1, OPENCL - 2, MIC - 3, SCC - 4, MPI_MS - 5)\n");
  759. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].type);
  760. fprintf(f, "####################\n");
  761. fprintf(f, "# DEV_%d\n", dev);
  762. fprintf(f, "# device id \n");
  763. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].devid);
  764. fprintf(f, "####################\n");
  765. fprintf(f, "# DEV_%d\n", dev);
  766. fprintf(f, "# number of cores \n");
  767. fprintf(f, "%u\n", arch_combs[comb]->devices[dev].ncores);
  768. }
  769. int nimpls = model->state->nimpls[comb];
  770. fprintf(f, "##########\n");
  771. fprintf(f, "# number of implementations\n");
  772. fprintf(f, "%d\n", nimpls);
  773. for (impl = 0; impl < nimpls; impl++)
  774. {
  775. dump_per_arch_model_file(f, model, comb, impl);
  776. }
  777. }
  778. }
  779. #endif
  780. void _starpu_perfmodel_realloc(struct starpu_perfmodel *model, int nb)
  781. {
  782. int i;
  783. STARPU_ASSERT(nb > model->state->ncombs_set);
  784. #ifdef SSIZE_MAX
  785. STARPU_ASSERT((size_t) nb < SSIZE_MAX / sizeof(struct starpu_perfmodel_per_arch*));
  786. #endif
  787. _STARPU_REALLOC(model->state->per_arch, nb*sizeof(struct starpu_perfmodel_per_arch*));
  788. _STARPU_REALLOC(model->state->per_arch_is_set, nb*sizeof(int*));
  789. _STARPU_REALLOC(model->state->nimpls, nb*sizeof(int));
  790. _STARPU_REALLOC(model->state->nimpls_set, nb*sizeof(int));
  791. _STARPU_REALLOC(model->state->combs, nb*sizeof(int));
  792. for(i = model->state->ncombs_set; i < nb; i++)
  793. {
  794. model->state->per_arch[i] = NULL;
  795. model->state->per_arch_is_set[i] = NULL;
  796. model->state->nimpls[i] = 0;
  797. model->state->nimpls_set[i] = 0;
  798. }
  799. model->state->ncombs_set = nb;
  800. }
  801. void starpu_perfmodel_init(struct starpu_perfmodel *model)
  802. {
  803. int already_init;
  804. int ncombs;
  805. STARPU_ASSERT(model);
  806. STARPU_PTHREAD_RWLOCK_RDLOCK(&registered_models_rwlock);
  807. already_init = model->is_init;
  808. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  809. if (already_init)
  810. return;
  811. /* The model is still not loaded so we grab the lock in write mode, and
  812. * if it's not loaded once we have the lock, we do load it. */
  813. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  814. /* Was the model initialized since the previous test ? */
  815. if (model->is_init)
  816. {
  817. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  818. return;
  819. }
  820. _STARPU_MALLOC(model->state, sizeof(struct _starpu_perfmodel_state));
  821. STARPU_PTHREAD_RWLOCK_INIT(&model->state->model_rwlock, NULL);
  822. STARPU_PTHREAD_RWLOCK_RDLOCK(&arch_combs_mutex);
  823. model->state->ncombs_set = ncombs = nb_arch_combs;
  824. STARPU_PTHREAD_RWLOCK_UNLOCK(&arch_combs_mutex);
  825. _STARPU_CALLOC(model->state->per_arch, ncombs, sizeof(struct starpu_perfmodel_per_arch*));
  826. _STARPU_CALLOC(model->state->per_arch_is_set, ncombs, sizeof(int*));
  827. _STARPU_CALLOC(model->state->nimpls, ncombs, sizeof(int));
  828. _STARPU_CALLOC(model->state->nimpls_set, ncombs, sizeof(int));
  829. _STARPU_MALLOC(model->state->combs, ncombs*sizeof(int));
  830. model->state->ncombs = 0;
  831. /* add the model to a linked list */
  832. struct _starpu_perfmodel *node = _starpu_perfmodel_new();
  833. node->model = model;
  834. //model->debug_modelid = debug_modelid++;
  835. /* put this model at the beginning of the list */
  836. _starpu_perfmodel_list_push_front(&registered_models, node);
  837. model->is_init = 1;
  838. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  839. }
  840. static void get_model_debug_path(struct starpu_perfmodel *model, const char *arch, char *path, size_t maxlen)
  841. {
  842. STARPU_ASSERT(path);
  843. char hostname[STR_LONG_LENGTH];
  844. _starpu_gethostname(hostname, sizeof(hostname));
  845. snprintf(path, maxlen, "%s/%s.%s.%s.debug", _starpu_get_perf_model_dir_debug(), model->symbol, hostname, arch);
  846. }
  847. void starpu_perfmodel_get_model_path(const char *symbol, char *path, size_t maxlen)
  848. {
  849. char hostname[STR_LONG_LENGTH];
  850. _starpu_gethostname(hostname, sizeof(hostname));
  851. const char *dot = strrchr(symbol, '.');
  852. snprintf(path, maxlen, "%s/%s%s%s", _starpu_get_perf_model_dir_codelet(), symbol, dot?"":".", dot?"":hostname);
  853. }
  854. #ifndef STARPU_SIMGRID
  855. static void save_history_based_model(struct starpu_perfmodel *model)
  856. {
  857. STARPU_ASSERT(model);
  858. STARPU_ASSERT(model->symbol);
  859. int locked;
  860. /* TODO checks */
  861. /* filename = $STARPU_PERF_MODEL_DIR/codelets/symbol.hostname */
  862. char path[STR_LONG_LENGTH];
  863. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  864. _STARPU_DEBUG("Opening performance model file %s for model %s\n", path, model->symbol);
  865. /* overwrite existing file, or create it */
  866. FILE *f;
  867. f = fopen(path, "w+");
  868. STARPU_ASSERT_MSG(f, "Could not save performance model %s\n", path);
  869. locked = _starpu_fwrlock(f) == 0;
  870. check_model(model);
  871. _starpu_fftruncate(f, 0);
  872. dump_model_file(f, model);
  873. if (locked)
  874. _starpu_fwrunlock(f);
  875. fclose(f);
  876. }
  877. #endif
  878. static void _starpu_dump_registered_models(void)
  879. {
  880. #ifndef STARPU_SIMGRID
  881. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  882. struct _starpu_perfmodel *node;
  883. _STARPU_DEBUG("DUMP MODELS !\n");
  884. for (node = _starpu_perfmodel_list_begin(&registered_models);
  885. node != _starpu_perfmodel_list_end(&registered_models);
  886. node = _starpu_perfmodel_list_next(node))
  887. {
  888. if (node->model->is_init)
  889. save_history_based_model(node->model);
  890. }
  891. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  892. #endif
  893. }
  894. void _starpu_initialize_registered_performance_models(void)
  895. {
  896. /* make sure the performance model directory exists (or create it) */
  897. _starpu_create_sampling_directory_if_needed();
  898. _starpu_perfmodel_list_init(&registered_models);
  899. STARPU_PTHREAD_RWLOCK_INIT(&registered_models_rwlock, NULL);
  900. struct _starpu_machine_config *conf = _starpu_get_machine_config();
  901. unsigned ncores = conf->topology.nhwcpus;
  902. unsigned ncuda = conf->topology.nhwcudagpus;
  903. unsigned nopencl = conf->topology.nhwopenclgpus;
  904. unsigned nmic = 0;
  905. unsigned i;
  906. for(i = 0; i < conf->topology.nhwmicdevices; i++)
  907. nmic += conf->topology.nhwmiccores[i];
  908. unsigned nmpi = 0;
  909. for(i = 0; i < conf->topology.nhwmpidevices; i++)
  910. nmpi += conf->topology.nhwmpicores[i];
  911. unsigned nscc = conf->topology.nhwscc;
  912. // We used to allocate 2**(ncores + ncuda + nopencl + nmic + nscc + nmpi), this is too big
  913. // We now allocate only 2*(ncores + ncuda + nopencl + nmic + nscc + nmpi), and reallocate when necessary in starpu_perfmodel_arch_comb_add
  914. nb_arch_combs = 2 * (ncores + ncuda + nopencl + nmic + nscc + nmpi);
  915. _STARPU_MALLOC(arch_combs, nb_arch_combs*sizeof(struct starpu_perfmodel_arch*));
  916. current_arch_comb = 0;
  917. STARPU_PTHREAD_RWLOCK_INIT(&arch_combs_mutex, NULL);
  918. historymaxerror = starpu_get_env_number_default("STARPU_HISTORY_MAX_ERROR", STARPU_HISTORYMAXERROR);
  919. _starpu_calibration_minimum = starpu_get_env_number_default("STARPU_CALIBRATE_MINIMUM", 10);
  920. ignore_devid[STARPU_CPU_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_CPU", 1);
  921. ignore_devid[STARPU_CUDA_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_CUDA", 0);
  922. ignore_devid[STARPU_OPENCL_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_OPENCL", 0);
  923. ignore_devid[STARPU_MIC_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_MIC", 0);
  924. ignore_devid[STARPU_MPI_MS_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_MPI_MS", 0);
  925. ignore_devid[STARPU_SCC_WORKER] = starpu_get_env_number_default("STARPU_PERF_MODEL_HOMOGENEOUS_SCC", 0);
  926. }
  927. void _starpu_deinitialize_performance_model(struct starpu_perfmodel *model)
  928. {
  929. if(model->is_init && model->state && model->state->per_arch != NULL)
  930. {
  931. int i;
  932. for(i=0 ; i<model->state->ncombs_set ; i++)
  933. {
  934. if (model->state->per_arch[i])
  935. {
  936. int impl;
  937. for(impl=0 ; impl<model->state->nimpls_set[i] ; impl++)
  938. {
  939. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[i][impl];
  940. if (archmodel->history)
  941. {
  942. struct starpu_perfmodel_history_list *list;
  943. struct starpu_perfmodel_history_table *entry, *tmp;
  944. HASH_ITER(hh, archmodel->history, entry, tmp)
  945. {
  946. HASH_DEL(archmodel->history, entry);
  947. free(entry);
  948. }
  949. archmodel->history = NULL;
  950. list = archmodel->list;
  951. while (list)
  952. {
  953. struct starpu_perfmodel_history_list *plist;
  954. free(list->entry);
  955. plist = list;
  956. list = list->next;
  957. free(plist);
  958. }
  959. archmodel->list = NULL;
  960. }
  961. }
  962. free(model->state->per_arch[i]);
  963. model->state->per_arch[i] = NULL;
  964. free(model->state->per_arch_is_set[i]);
  965. model->state->per_arch_is_set[i] = NULL;
  966. }
  967. }
  968. free(model->state->per_arch);
  969. model->state->per_arch = NULL;
  970. free(model->state->per_arch_is_set);
  971. model->state->per_arch_is_set = NULL;
  972. free(model->state->nimpls);
  973. model->state->nimpls = NULL;
  974. free(model->state->nimpls_set);
  975. model->state->nimpls_set = NULL;
  976. free(model->state->combs);
  977. model->state->combs = NULL;
  978. model->state->ncombs = 0;
  979. }
  980. model->is_init = 0;
  981. model->is_loaded = 0;
  982. }
  983. void _starpu_deinitialize_registered_performance_models(void)
  984. {
  985. if (_starpu_get_calibrate_flag())
  986. _starpu_dump_registered_models();
  987. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  988. struct _starpu_perfmodel *node, *nnode;
  989. _STARPU_DEBUG("FREE MODELS !\n");
  990. for (node = _starpu_perfmodel_list_begin(&registered_models);
  991. node != _starpu_perfmodel_list_end(&registered_models);
  992. node = nnode)
  993. {
  994. struct starpu_perfmodel *model = node->model;
  995. nnode = _starpu_perfmodel_list_next(node);
  996. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  997. _starpu_deinitialize_performance_model(model);
  998. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  999. free(node->model->state);
  1000. node->model->state = NULL;
  1001. _starpu_perfmodel_list_erase(&registered_models, node);
  1002. _starpu_perfmodel_delete(node);
  1003. }
  1004. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  1005. STARPU_PTHREAD_RWLOCK_DESTROY(&registered_models_rwlock);
  1006. _free_arch_combs();
  1007. starpu_perfmodel_free_sampling_directories();
  1008. }
  1009. /* We first try to grab the global lock in read mode to check whether the model
  1010. * was loaded or not (this is very likely to have been already loaded). If the
  1011. * model was not loaded yet, we take the lock in write mode, and if the model
  1012. * is still not loaded once we have the lock, we do load it. */
  1013. void _starpu_load_history_based_model(struct starpu_perfmodel *model, unsigned scan_history)
  1014. {
  1015. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1016. if(!model->is_loaded)
  1017. {
  1018. char path[STR_LONG_LENGTH];
  1019. // Check if a symbol is defined before trying to load the model from a file
  1020. STARPU_ASSERT_MSG(model->symbol, "history-based performance models must have a symbol");
  1021. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  1022. _STARPU_DEBUG("Opening performance model file %s for model %s ...\n", path, model->symbol);
  1023. unsigned calibrate_flag = _starpu_get_calibrate_flag();
  1024. model->benchmarking = calibrate_flag;
  1025. model->is_loaded = 1;
  1026. if (calibrate_flag == 2)
  1027. {
  1028. /* The user specified that the performance model should
  1029. * be overwritten, so we don't load the existing file !
  1030. * */
  1031. _STARPU_DEBUG("Overwrite existing file\n");
  1032. }
  1033. else
  1034. {
  1035. /* We try to load the file */
  1036. FILE *f;
  1037. f = fopen(path, "r");
  1038. if (f)
  1039. {
  1040. int locked;
  1041. locked = _starpu_frdlock(f) == 0;
  1042. parse_model_file(f, path, model, scan_history);
  1043. if (locked)
  1044. _starpu_frdunlock(f);
  1045. fclose(f);
  1046. _STARPU_DEBUG("Performance model file %s for model %s is loaded\n", path, model->symbol);
  1047. }
  1048. else
  1049. {
  1050. _STARPU_DEBUG("Performance model file %s does not exist or is not readable\n", path);
  1051. }
  1052. }
  1053. }
  1054. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1055. }
  1056. void starpu_perfmodel_directory(FILE *output)
  1057. {
  1058. fprintf(output, "directory: <%s>\n", _starpu_get_perf_model_dir_codelet());
  1059. }
  1060. /* This function is intended to be used by external tools that should read
  1061. * the performance model files */
  1062. int starpu_perfmodel_list(FILE *output)
  1063. {
  1064. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  1065. char *path;
  1066. DIR *dp;
  1067. path = _starpu_get_perf_model_dir_codelet();
  1068. dp = opendir(path);
  1069. if (dp != NULL)
  1070. {
  1071. struct dirent *ep;
  1072. while ((ep = readdir(dp)))
  1073. {
  1074. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, ".."))
  1075. fprintf(output, "file: <%s>\n", ep->d_name);
  1076. }
  1077. closedir (dp);
  1078. }
  1079. else
  1080. {
  1081. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  1082. }
  1083. return 0;
  1084. #else
  1085. _STARPU_MSG("Listing perfmodels is not implemented on pure Windows yet\n");
  1086. return 1;
  1087. #endif
  1088. }
  1089. /* This function is intended to be used by external tools that should read the
  1090. * performance model files */
  1091. /* TODO: write an clear function, to free symbol and history */
  1092. int starpu_perfmodel_load_symbol(const char *symbol, struct starpu_perfmodel *model)
  1093. {
  1094. model->symbol = strdup(symbol);
  1095. /* where is the file if it exists ? */
  1096. char path[STR_LONG_LENGTH];
  1097. starpu_perfmodel_get_model_path(model->symbol, path, sizeof(path));
  1098. // _STARPU_DEBUG("get_model_path -> %s\n", path);
  1099. /* does it exist ? */
  1100. int res;
  1101. res = access(path, F_OK);
  1102. if (res)
  1103. {
  1104. const char *dot = strrchr(symbol, '.');
  1105. if (dot)
  1106. {
  1107. char *symbol2 = strdup(symbol);
  1108. symbol2[dot-symbol] = '\0';
  1109. int ret;
  1110. _STARPU_DISP("note: loading history from %s instead of %s\n", symbol2, symbol);
  1111. ret = starpu_perfmodel_load_symbol(symbol2,model);
  1112. free(symbol2);
  1113. return ret;
  1114. }
  1115. _STARPU_DISP("There is no performance model for symbol %s\n", symbol);
  1116. return 1;
  1117. }
  1118. return starpu_perfmodel_load_file(path, model);
  1119. }
  1120. int starpu_perfmodel_load_file(const char *filename, struct starpu_perfmodel *model)
  1121. {
  1122. int res, ret = 0;
  1123. FILE *f = fopen(filename, "r");
  1124. int locked;
  1125. STARPU_ASSERT(f);
  1126. starpu_perfmodel_init(model);
  1127. locked = _starpu_frdlock(f) == 0;
  1128. ret = parse_model_file(f, filename, model, 1);
  1129. if (locked)
  1130. _starpu_frdunlock(f);
  1131. res = fclose(f);
  1132. STARPU_ASSERT(res == 0);
  1133. return ret;
  1134. }
  1135. int starpu_perfmodel_unload_model(struct starpu_perfmodel *model)
  1136. {
  1137. if (model->symbol)
  1138. {
  1139. free((char *)model->symbol);
  1140. model->symbol = NULL;
  1141. }
  1142. _starpu_deinitialize_performance_model(model);
  1143. free(model->state);
  1144. model->state = NULL;
  1145. STARPU_PTHREAD_RWLOCK_WRLOCK(&registered_models_rwlock);
  1146. struct _starpu_perfmodel *node;
  1147. for (node = _starpu_perfmodel_list_begin(&registered_models);
  1148. node != _starpu_perfmodel_list_end(&registered_models);
  1149. node = _starpu_perfmodel_list_next(node)) {
  1150. if (node->model == model) {
  1151. _starpu_perfmodel_list_erase(&registered_models, node);
  1152. _starpu_perfmodel_delete(node);
  1153. break;
  1154. }
  1155. }
  1156. STARPU_PTHREAD_RWLOCK_UNLOCK(&registered_models_rwlock);
  1157. return 0;
  1158. }
  1159. char* starpu_perfmodel_get_archtype_name(enum starpu_worker_archtype archtype)
  1160. {
  1161. switch(archtype)
  1162. {
  1163. case(STARPU_CPU_WORKER):
  1164. return "cpu";
  1165. break;
  1166. case(STARPU_CUDA_WORKER):
  1167. return "cuda";
  1168. break;
  1169. case(STARPU_OPENCL_WORKER):
  1170. return "opencl";
  1171. break;
  1172. case(STARPU_MIC_WORKER):
  1173. return "mic";
  1174. break;
  1175. case(STARPU_SCC_WORKER):
  1176. return "scc";
  1177. break;
  1178. case(STARPU_MPI_MS_WORKER):
  1179. return "mpi_ms";
  1180. break;
  1181. default:
  1182. STARPU_ABORT();
  1183. break;
  1184. }
  1185. }
  1186. void starpu_perfmodel_get_arch_name(struct starpu_perfmodel_arch* arch, char *archname, size_t maxlen,unsigned impl)
  1187. {
  1188. int i;
  1189. int comb = _starpu_perfmodel_create_comb_if_needed(arch);
  1190. STARPU_ASSERT(comb != -1);
  1191. char devices[STR_VERY_LONG_LENGTH];
  1192. int written = 0;
  1193. strcpy(devices, "");
  1194. for(i=0 ; i<arch->ndevices ; i++)
  1195. {
  1196. written += snprintf(devices + written, sizeof(devices)-written, "%s%d%s", starpu_perfmodel_get_archtype_name(arch->devices[i].type), arch->devices[i].devid, i != arch->ndevices-1 ? "_":"");
  1197. }
  1198. snprintf(archname, maxlen, "%s_impl%u (Comb%d)", devices, impl, comb);
  1199. }
  1200. void starpu_perfmodel_debugfilepath(struct starpu_perfmodel *model,
  1201. struct starpu_perfmodel_arch* arch, char *path, size_t maxlen, unsigned nimpl)
  1202. {
  1203. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1204. STARPU_ASSERT(comb != -1);
  1205. char archname[STR_SHORT_LENGTH];
  1206. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1207. STARPU_ASSERT(path);
  1208. get_model_debug_path(model, archname, path, maxlen);
  1209. }
  1210. double _starpu_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j, unsigned nimpl)
  1211. {
  1212. int comb;
  1213. double exp = NAN;
  1214. size_t size;
  1215. struct starpu_perfmodel_regression_model *regmodel = NULL;
  1216. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1217. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1218. if(comb == -1)
  1219. goto docal;
  1220. if (model->state->per_arch[comb] == NULL)
  1221. // The model has not been executed on this combination
  1222. goto docal;
  1223. regmodel = &model->state->per_arch[comb][nimpl].regression;
  1224. if (regmodel->valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  1225. exp = regmodel->alpha*pow((double)size, regmodel->beta);
  1226. docal:
  1227. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1228. if (isnan(exp) && !model->benchmarking)
  1229. {
  1230. char archname[STR_SHORT_LENGTH];
  1231. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1232. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %lu (only %u measurements from size %lu to %lu), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, (unsigned long) size, regmodel?regmodel->nsample:0, regmodel?regmodel->minx:0, regmodel?regmodel->maxx:0);
  1233. _starpu_set_calibrate_flag(1);
  1234. model->benchmarking = 1;
  1235. }
  1236. return exp;
  1237. }
  1238. double _starpu_non_linear_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  1239. {
  1240. int comb;
  1241. double exp = NAN;
  1242. size_t size;
  1243. struct starpu_perfmodel_regression_model *regmodel;
  1244. struct starpu_perfmodel_history_table *entry = NULL;
  1245. size = _starpu_job_get_data_size(model, arch, nimpl, j);
  1246. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1247. if(comb == -1)
  1248. goto docal;
  1249. if (model->state->per_arch[comb] == NULL)
  1250. // The model has not been executed on this combination
  1251. goto docal;
  1252. regmodel = &model->state->per_arch[comb][nimpl].regression;
  1253. if (regmodel->nl_valid && size >= regmodel->minx * 0.9 && size <= regmodel->maxx * 1.1)
  1254. exp = regmodel->a*pow((double)size, regmodel->b) + regmodel->c;
  1255. else
  1256. {
  1257. uint32_t key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1258. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][nimpl];
  1259. struct starpu_perfmodel_history_table *history;
  1260. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  1261. history = per_arch_model->history;
  1262. HASH_FIND_UINT32_T(history, &key, entry);
  1263. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1264. /* Here helgrind would shout that this is unprotected access.
  1265. * We do not care about racing access to the mean, we only want
  1266. * a good-enough estimation */
  1267. if (entry && entry->history_entry && entry->history_entry->nsample >= _starpu_calibration_minimum)
  1268. exp = entry->history_entry->mean;
  1269. docal:
  1270. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1271. if (isnan(exp) && !model->benchmarking)
  1272. {
  1273. char archname[STR_SHORT_LENGTH];
  1274. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1275. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %lu (only %u measurements), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, (unsigned long) size, entry && entry->history_entry ? entry->history_entry->nsample : 0);
  1276. _starpu_set_calibrate_flag(1);
  1277. model->benchmarking = 1;
  1278. }
  1279. }
  1280. return exp;
  1281. }
  1282. double _starpu_multiple_regression_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j, unsigned nimpl)
  1283. {
  1284. int comb;
  1285. double expected_duration=NAN;
  1286. struct starpu_perfmodel_regression_model *reg_model = NULL;
  1287. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1288. if(comb == -1)
  1289. goto docal;
  1290. if (model->state->per_arch[comb] == NULL)
  1291. // The model has not been executed on this combination
  1292. goto docal;
  1293. reg_model = &model->state->per_arch[comb][nimpl].regression;
  1294. if (reg_model->coeff == NULL)
  1295. goto docal;
  1296. double *parameters;
  1297. _STARPU_MALLOC(parameters, model->nparameters*sizeof(double));
  1298. model->parameters(j->task, parameters);
  1299. expected_duration=reg_model->coeff[0];
  1300. unsigned i;
  1301. for (i=0; i < model->ncombinations; i++)
  1302. {
  1303. double parameter_value=1.;
  1304. unsigned k;
  1305. for (k=0; k < model->nparameters; k++)
  1306. parameter_value *= pow(parameters[k],model->combinations[i][k]);
  1307. expected_duration += reg_model->coeff[i+1]*parameter_value;
  1308. }
  1309. docal:
  1310. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1311. if (isnan(expected_duration) && !model->benchmarking)
  1312. {
  1313. char archname[STR_SHORT_LENGTH];
  1314. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1315. _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);
  1316. _starpu_set_calibrate_flag(1);
  1317. model->benchmarking = 1;
  1318. }
  1319. // In the unlikely event that predicted duration is negative
  1320. // in case multiple linear regression is not so accurate
  1321. if (expected_duration < 0 )
  1322. expected_duration = 0.00001;
  1323. //Make sure that the injected time is in milliseconds
  1324. return expected_duration;
  1325. }
  1326. double _starpu_history_based_job_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct _starpu_job *j,unsigned nimpl)
  1327. {
  1328. int comb;
  1329. double exp = NAN;
  1330. struct starpu_perfmodel_per_arch *per_arch_model;
  1331. struct starpu_perfmodel_history_entry *entry = NULL;
  1332. struct starpu_perfmodel_history_table *history, *elt;
  1333. uint32_t key;
  1334. comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1335. if(comb == -1)
  1336. goto docal;
  1337. if (model->state->per_arch[comb] == NULL)
  1338. // The model has not been executed on this combination
  1339. goto docal;
  1340. per_arch_model = &model->state->per_arch[comb][nimpl];
  1341. key = _starpu_compute_buffers_footprint(model, arch, nimpl, j);
  1342. STARPU_PTHREAD_RWLOCK_RDLOCK(&model->state->model_rwlock);
  1343. history = per_arch_model->history;
  1344. HASH_FIND_UINT32_T(history, &key, elt);
  1345. entry = (elt == NULL) ? NULL : elt->history_entry;
  1346. STARPU_ASSERT_MSG(!entry || entry->mean >= 0, "entry=%p, entry->mean=%lf\n", entry, entry?entry->mean:NAN);
  1347. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1348. /* Here helgrind would shout that this is unprotected access.
  1349. * We do not care about racing access to the mean, we only want
  1350. * a good-enough estimation */
  1351. if (entry && entry->nsample >= _starpu_calibration_minimum)
  1352. {
  1353. STARPU_ASSERT_MSG(entry->mean >= 0, "entry->mean=%lf\n", entry->mean);
  1354. /* TODO: report differently if we've scheduled really enough
  1355. * of that task and the scheduler should perhaps put it aside */
  1356. /* Calibrated enough */
  1357. exp = entry->mean;
  1358. }
  1359. docal:
  1360. STARPU_HG_DISABLE_CHECKING(model->benchmarking);
  1361. if (isnan(exp) && !model->benchmarking)
  1362. {
  1363. char archname[STR_SHORT_LENGTH];
  1364. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), nimpl);
  1365. _STARPU_DISP("Warning: model %s is not calibrated enough for %s size %ld (only %u measurements), forcing calibration for this run. Use the STARPU_CALIBRATE environment variable to control this.\n", model->symbol, archname, j->task?(long int)_starpu_job_get_data_size(model, arch, nimpl, j):-1, entry ? entry->nsample : 0);
  1366. _starpu_set_calibrate_flag(1);
  1367. model->benchmarking = 1;
  1368. }
  1369. STARPU_ASSERT_MSG(isnan(exp)||exp >= 0, "exp=%lf\n", exp);
  1370. return exp;
  1371. }
  1372. double starpu_perfmodel_history_based_expected_perf(struct starpu_perfmodel *model, struct starpu_perfmodel_arch * arch, uint32_t footprint)
  1373. {
  1374. struct _starpu_job j =
  1375. {
  1376. .footprint = footprint,
  1377. .footprint_is_computed = 1,
  1378. };
  1379. return _starpu_history_based_job_expected_perf(model, arch, &j, j.nimpl);
  1380. }
  1381. int _starpu_perfmodel_create_comb_if_needed(struct starpu_perfmodel_arch* arch)
  1382. {
  1383. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1384. if(comb == -1)
  1385. comb = starpu_perfmodel_arch_comb_add(arch->ndevices, arch->devices);
  1386. return comb;
  1387. }
  1388. 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)
  1389. {
  1390. STARPU_ASSERT_MSG(measured >= 0, "measured=%lf\n", measured);
  1391. if (model)
  1392. {
  1393. int c;
  1394. unsigned found = 0;
  1395. int comb = _starpu_perfmodel_create_comb_if_needed(arch);
  1396. for(c = 0; c < model->state->ncombs; c++)
  1397. {
  1398. if(model->state->combs[c] == comb)
  1399. {
  1400. found = 1;
  1401. break;
  1402. }
  1403. }
  1404. if(!found)
  1405. {
  1406. if (model->state->ncombs + 1 >= model->state->ncombs_set)
  1407. {
  1408. // The number of combinations is bigger than the one which was initially allocated, we need to reallocate,
  1409. // do not only reallocate 1 extra comb, rather reallocate 5 to avoid too frequent calls to _starpu_perfmodel_realloc
  1410. _starpu_perfmodel_realloc(model, model->state->ncombs_set+5);
  1411. }
  1412. model->state->combs[model->state->ncombs++] = comb;
  1413. }
  1414. STARPU_PTHREAD_RWLOCK_WRLOCK(&model->state->model_rwlock);
  1415. if(!model->state->per_arch[comb])
  1416. {
  1417. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1418. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1419. }
  1420. struct starpu_perfmodel_per_arch *per_arch_model = &model->state->per_arch[comb][impl];
  1421. if (model->state->per_arch_is_set[comb][impl] == 0)
  1422. {
  1423. // We are adding a new implementation for the given comb and the given impl
  1424. model->state->nimpls[comb]++;
  1425. model->state->per_arch_is_set[comb][impl] = 1;
  1426. }
  1427. if (model->type == STARPU_HISTORY_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1428. {
  1429. struct starpu_perfmodel_history_entry *entry;
  1430. struct starpu_perfmodel_history_table *elt;
  1431. struct starpu_perfmodel_history_list **list;
  1432. uint32_t key = _starpu_compute_buffers_footprint(model, arch, impl, j);
  1433. list = &per_arch_model->list;
  1434. HASH_FIND_UINT32_T(per_arch_model->history, &key, elt);
  1435. entry = (elt == NULL) ? NULL : elt->history_entry;
  1436. if (!entry)
  1437. {
  1438. /* this is the first entry with such a footprint */
  1439. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  1440. /* Tell helgrind that we do not care about
  1441. * racing access to the sampling, we only want a
  1442. * good-enough estimation */
  1443. STARPU_HG_DISABLE_CHECKING(entry->nsample);
  1444. STARPU_HG_DISABLE_CHECKING(entry->mean);
  1445. /* Do not take the first measurement into account, it is very often quite bogus */
  1446. /* TODO: it'd be good to use a better estimation heuristic, like the median, or latest n values, etc. */
  1447. entry->mean = 0;
  1448. entry->sum = 0;
  1449. entry->deviation = 0.0;
  1450. entry->sum2 = 0;
  1451. entry->size = _starpu_job_get_data_size(model, arch, impl, j);
  1452. entry->flops = j->task->flops;
  1453. entry->footprint = key;
  1454. entry->nsample = 0;
  1455. entry->nerror = 0;
  1456. insert_history_entry(entry, list, &per_arch_model->history);
  1457. }
  1458. else
  1459. {
  1460. /* There is already an entry with the same footprint */
  1461. double local_deviation = measured/entry->mean;
  1462. if (entry->nsample &&
  1463. (100 * local_deviation > (100 + historymaxerror)
  1464. || (100 / local_deviation > (100 + historymaxerror))))
  1465. {
  1466. entry->nerror++;
  1467. /* More errors than measurements, we're most probably completely wrong, we flush out all the entries */
  1468. if (entry->nerror >= entry->nsample)
  1469. {
  1470. char archname[STR_SHORT_LENGTH];
  1471. starpu_perfmodel_get_arch_name(arch, archname, sizeof(archname), impl);
  1472. _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);
  1473. entry->sum = 0.0;
  1474. entry->sum2 = 0.0;
  1475. entry->nsample = 0;
  1476. entry->nerror = 0;
  1477. entry->mean = 0.0;
  1478. entry->deviation = 0.0;
  1479. }
  1480. }
  1481. else
  1482. {
  1483. entry->sum += measured;
  1484. entry->sum2 += measured*measured;
  1485. entry->nsample++;
  1486. unsigned n = entry->nsample;
  1487. entry->mean = entry->sum / n;
  1488. entry->deviation = sqrt((fabs(entry->sum2 - (entry->sum*entry->sum))/n)/n);
  1489. }
  1490. if (j->task->flops != 0.)
  1491. {
  1492. if (entry->flops == 0.)
  1493. entry->flops = j->task->flops;
  1494. else if (((entry->flops - j->task->flops) / entry->flops) > 0.00001)
  1495. /* Incoherent flops! forget about trying to record flops */
  1496. entry->flops = NAN;
  1497. }
  1498. }
  1499. STARPU_ASSERT(entry);
  1500. }
  1501. if (model->type == STARPU_REGRESSION_BASED || model->type == STARPU_NL_REGRESSION_BASED)
  1502. {
  1503. struct starpu_perfmodel_regression_model *reg_model;
  1504. reg_model = &per_arch_model->regression;
  1505. /* update the regression model */
  1506. size_t job_size = _starpu_job_get_data_size(model, arch, impl, j);
  1507. double logy, logx;
  1508. logx = log((double)job_size);
  1509. logy = log(measured);
  1510. reg_model->sumlnx += logx;
  1511. reg_model->sumlnx2 += logx*logx;
  1512. reg_model->sumlny += logy;
  1513. reg_model->sumlnxlny += logx*logy;
  1514. if (reg_model->minx == 0 || job_size < reg_model->minx)
  1515. reg_model->minx = job_size;
  1516. if (reg_model->maxx == 0 || job_size > reg_model->maxx)
  1517. reg_model->maxx = job_size;
  1518. reg_model->nsample++;
  1519. if (VALID_REGRESSION(reg_model))
  1520. {
  1521. unsigned n = reg_model->nsample;
  1522. double num = (n*reg_model->sumlnxlny - reg_model->sumlnx*reg_model->sumlny);
  1523. double denom = (n*reg_model->sumlnx2 - reg_model->sumlnx*reg_model->sumlnx);
  1524. reg_model->beta = num/denom;
  1525. reg_model->alpha = exp((reg_model->sumlny - reg_model->beta*reg_model->sumlnx)/n);
  1526. reg_model->valid = 1;
  1527. }
  1528. }
  1529. if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
  1530. {
  1531. struct starpu_perfmodel_history_entry *entry;
  1532. struct starpu_perfmodel_history_list **list;
  1533. list = &per_arch_model->list;
  1534. _STARPU_CALLOC(entry, 1, sizeof(struct starpu_perfmodel_history_entry));
  1535. _STARPU_MALLOC(entry->parameters, model->nparameters*sizeof(double));
  1536. model->parameters(j->task, entry->parameters);
  1537. entry->tag = j->task->tag_id;
  1538. STARPU_ASSERT(measured >= 0);
  1539. entry->duration = measured;
  1540. struct starpu_perfmodel_history_list *link;
  1541. _STARPU_MALLOC(link, sizeof(struct starpu_perfmodel_history_list));
  1542. link->next = *list;
  1543. link->entry = entry;
  1544. *list = link;
  1545. }
  1546. #ifdef STARPU_MODEL_DEBUG
  1547. struct starpu_task *task = j->task;
  1548. starpu_perfmodel_debugfilepath(model, arch_combs[comb], per_arch_model->debug_path, STR_LONG_LENGTH, impl);
  1549. FILE *f = fopen(per_arch_model->debug_path, "a+");
  1550. int locked;
  1551. if (f == NULL)
  1552. {
  1553. _STARPU_DISP("Error <%s> when opening file <%s>\n", strerror(errno), per_arch_model->debug_path);
  1554. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1555. return;
  1556. }
  1557. locked = _starpu_fwrlock(f) == 0;
  1558. if (!j->footprint_is_computed)
  1559. (void) _starpu_compute_buffers_footprint(model, arch, impl, j);
  1560. STARPU_ASSERT(j->footprint_is_computed);
  1561. fprintf(f, "0x%x\t%lu\t%f\t%f\t%f\t%u\t\t", j->footprint, (unsigned long) _starpu_job_get_data_size(model, arch, impl, j), measured, task->predicted, task->predicted_transfer, cpuid);
  1562. unsigned i;
  1563. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1564. for (i = 0; i < nbuffers; i++)
  1565. {
  1566. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  1567. STARPU_ASSERT(handle->ops);
  1568. STARPU_ASSERT(handle->ops->display);
  1569. handle->ops->display(handle, f);
  1570. }
  1571. fprintf(f, "\n");
  1572. if (locked)
  1573. _starpu_fwrunlock(f);
  1574. fclose(f);
  1575. #endif
  1576. STARPU_PTHREAD_RWLOCK_UNLOCK(&model->state->model_rwlock);
  1577. }
  1578. }
  1579. void starpu_perfmodel_update_history(struct starpu_perfmodel *model, struct starpu_task *task, struct starpu_perfmodel_arch * arch, unsigned cpuid, unsigned nimpl, double measured)
  1580. {
  1581. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  1582. #ifdef STARPU_SIMGRID
  1583. STARPU_ASSERT_MSG(0, "We are not supposed to update history when simulating execution");
  1584. #endif
  1585. _starpu_init_and_load_perfmodel(model);
  1586. /* Record measurement */
  1587. _starpu_update_perfmodel_history(job, model, arch, cpuid, measured, nimpl);
  1588. /* and save perfmodel on termination */
  1589. _starpu_set_calibrate_flag(1);
  1590. }
  1591. int starpu_perfmodel_list_combs(FILE *output, struct starpu_perfmodel *model)
  1592. {
  1593. int comb;
  1594. fprintf(output, "Model <%s>\n", model->symbol);
  1595. for(comb = 0; comb < model->state->ncombs; comb++)
  1596. {
  1597. struct starpu_perfmodel_arch *arch;
  1598. int device;
  1599. arch = starpu_perfmodel_arch_comb_fetch(model->state->combs[comb]);
  1600. fprintf(output, "\tComb %d: %d device%s\n", model->state->combs[comb], arch->ndevices, arch->ndevices>1?"s":"");
  1601. for(device=0 ; device<arch->ndevices ; device++)
  1602. {
  1603. char *name = starpu_perfmodel_get_archtype_name(arch->devices[device].type);
  1604. fprintf(output, "\t\tDevice %d: type: %s - devid: %d - ncores: %d\n", device, name, arch->devices[device].devid, arch->devices[device].ncores);
  1605. }
  1606. }
  1607. return 0;
  1608. }
  1609. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_arch(struct starpu_perfmodel *model, struct starpu_perfmodel_arch *arch, unsigned impl)
  1610. {
  1611. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  1612. if(comb == -1) return NULL;
  1613. if(!model->state->per_arch[comb]) return NULL;
  1614. return &model->state->per_arch[comb][impl];
  1615. }
  1616. struct starpu_perfmodel_per_arch *_starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, va_list varg_list)
  1617. {
  1618. struct starpu_perfmodel_arch arch;
  1619. va_list varg_list_copy;
  1620. int i, arg_type;
  1621. int is_cpu_set = 0;
  1622. // We first count the number of devices
  1623. arch.ndevices = 0;
  1624. va_copy(varg_list_copy, varg_list);
  1625. while ((arg_type = va_arg(varg_list_copy, int)) != -1)
  1626. {
  1627. int devid = va_arg(varg_list_copy, int);
  1628. int ncores = va_arg(varg_list_copy, int);
  1629. arch.ndevices ++;
  1630. if (arg_type == STARPU_CPU_WORKER)
  1631. {
  1632. STARPU_ASSERT_MSG(is_cpu_set == 0, "STARPU_CPU_WORKER can only be specified once\n");
  1633. STARPU_ASSERT_MSG(devid==0, "STARPU_CPU_WORKER must be followed by a value 0 for the device id");
  1634. is_cpu_set = 1;
  1635. }
  1636. else
  1637. {
  1638. STARPU_ASSERT_MSG(ncores==1, "%s must be followed by a value 1 for ncores", starpu_worker_get_type_as_string(arg_type));
  1639. }
  1640. }
  1641. va_end(varg_list_copy);
  1642. // We set the devices
  1643. _STARPU_MALLOC(arch.devices, arch.ndevices * sizeof(struct starpu_perfmodel_device));
  1644. va_copy(varg_list_copy, varg_list);
  1645. for(i=0 ; i<arch.ndevices ; i++)
  1646. {
  1647. arch.devices[i].type = va_arg(varg_list_copy, int);
  1648. arch.devices[i].devid = va_arg(varg_list_copy, int);
  1649. arch.devices[i].ncores = va_arg(varg_list_copy, int);
  1650. }
  1651. va_end(varg_list_copy);
  1652. // Get the combination for this set of devices
  1653. int comb = _starpu_perfmodel_create_comb_if_needed(&arch);
  1654. free(arch.devices);
  1655. // Realloc if necessary
  1656. if (comb >= model->state->ncombs_set)
  1657. _starpu_perfmodel_realloc(model, comb+1);
  1658. // Get the per_arch object
  1659. if (model->state->per_arch[comb] == NULL)
  1660. {
  1661. _starpu_perfmodel_malloc_per_arch(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1662. _starpu_perfmodel_malloc_per_arch_is_set(model, comb, STARPU_MAXIMPLEMENTATIONS);
  1663. model->state->nimpls[comb] = 0;
  1664. }
  1665. model->state->per_arch_is_set[comb][impl] = 1;
  1666. model->state->nimpls[comb] ++;
  1667. return &model->state->per_arch[comb][impl];
  1668. }
  1669. struct starpu_perfmodel_per_arch *starpu_perfmodel_get_model_per_devices(struct starpu_perfmodel *model, int impl, ...)
  1670. {
  1671. va_list varg_list;
  1672. struct starpu_perfmodel_per_arch *per_arch;
  1673. va_start(varg_list, impl);
  1674. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1675. va_end(varg_list);
  1676. return per_arch;
  1677. }
  1678. int starpu_perfmodel_set_per_devices_cost_function(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_cost_function func, ...)
  1679. {
  1680. va_list varg_list;
  1681. struct starpu_perfmodel_per_arch *per_arch;
  1682. va_start(varg_list, func);
  1683. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1684. per_arch->cost_function = func;
  1685. va_end(varg_list);
  1686. return 0;
  1687. }
  1688. int starpu_perfmodel_set_per_devices_size_base(struct starpu_perfmodel *model, int impl, starpu_perfmodel_per_arch_size_base func, ...)
  1689. {
  1690. va_list varg_list;
  1691. struct starpu_perfmodel_per_arch *per_arch;
  1692. va_start(varg_list, func);
  1693. per_arch = _starpu_perfmodel_get_model_per_devices(model, impl, varg_list);
  1694. per_arch->size_base = func;
  1695. va_end(varg_list);
  1696. return 0;
  1697. }