perfmodel_history.c 49 KB

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