sched_ctx.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <core/sched_policy.h>
  17. #include <core/sched_ctx.h>
  18. #include <common/utils.h>
  19. #include <stdarg.h>
  20. starpu_pthread_rwlock_t changing_ctx_mutex[STARPU_NMAX_SCHED_CTXS];
  21. static starpu_pthread_mutex_t sched_ctx_manag = STARPU_PTHREAD_MUTEX_INITIALIZER;
  22. static starpu_pthread_mutex_t finished_submit_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  23. struct starpu_task stop_submission_task = STARPU_TASK_INITIALIZER;
  24. starpu_pthread_key_t sched_ctx_key;
  25. unsigned with_hypervisor = 0;
  26. double hyp_start_sample[STARPU_NMAX_SCHED_CTXS];
  27. double hyp_start_allow_sample[STARPU_NMAX_SCHED_CTXS];
  28. double flops[STARPU_NMAX_SCHED_CTXS][STARPU_NMAXWORKERS];
  29. size_t data_size[STARPU_NMAX_SCHED_CTXS][STARPU_NMAXWORKERS];
  30. static unsigned _starpu_get_first_free_sched_ctx(struct _starpu_machine_config *config);
  31. static void _starpu_sched_ctx_add_workers_to_master(unsigned sched_ctx_id, int *workerids, int nworkers, int new_master);
  32. static void _starpu_sched_ctx_wake_these_workers_up(unsigned sched_ctx_id, int *workerids, int nworkers);
  33. static void _starpu_worker_gets_into_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker)
  34. {
  35. unsigned ret_sched_ctx = _starpu_sched_ctx_list_get_sched_ctx(worker->sched_ctx_list, sched_ctx_id);
  36. /* the worker was planning to go away in another ctx but finally he changed his mind &
  37. he's staying */
  38. if (ret_sched_ctx == STARPU_NMAX_SCHED_CTXS)
  39. {
  40. /* add context to worker */
  41. _starpu_sched_ctx_list_add(&worker->sched_ctx_list, sched_ctx_id);
  42. worker->nsched_ctxs++;
  43. }
  44. worker->removed_from_ctx[sched_ctx_id] = 0;
  45. if(worker->tmp_sched_ctx == sched_ctx_id)
  46. worker->tmp_sched_ctx = -1;
  47. return;
  48. }
  49. void _starpu_worker_gets_out_of_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker)
  50. {
  51. unsigned ret_sched_ctx = _starpu_sched_ctx_list_get_sched_ctx(worker->sched_ctx_list, sched_ctx_id);
  52. /* remove context from worker */
  53. if(ret_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  54. {
  55. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  56. if(sched_ctx && sched_ctx->sched_policy && sched_ctx->sched_policy->remove_workers)
  57. {
  58. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  59. sched_ctx->sched_policy->remove_workers(sched_ctx_id, &worker->workerid, 1);
  60. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  61. }
  62. _starpu_sched_ctx_list_remove(&worker->sched_ctx_list, sched_ctx_id);
  63. worker->nsched_ctxs--;
  64. }
  65. return;
  66. }
  67. static void _starpu_update_workers_with_ctx(int *workerids, int nworkers, int sched_ctx_id)
  68. {
  69. int i;
  70. struct _starpu_worker *worker = NULL;
  71. for(i = 0; i < nworkers; i++)
  72. {
  73. worker = _starpu_get_worker_struct(workerids[i]);
  74. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  75. _starpu_worker_gets_into_ctx(sched_ctx_id, worker);
  76. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  77. }
  78. return;
  79. }
  80. static void _starpu_update_workers_without_ctx(int *workerids, int nworkers, int sched_ctx_id, unsigned now)
  81. {
  82. int i;
  83. struct _starpu_worker *worker = NULL;
  84. for(i = 0; i < nworkers; i++)
  85. {
  86. worker = _starpu_get_worker_struct(workerids[i]);
  87. if(now)
  88. {
  89. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  90. _starpu_worker_gets_out_of_ctx(sched_ctx_id, worker);
  91. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  92. }
  93. else
  94. {
  95. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  96. worker->removed_from_ctx[sched_ctx_id] = 1;
  97. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  98. }
  99. }
  100. return;
  101. }
  102. void starpu_sched_ctx_stop_task_submission()
  103. {
  104. _starpu_exclude_task_from_dag(&stop_submission_task);
  105. _starpu_task_submit_internally(&stop_submission_task);
  106. }
  107. void starpu_sched_ctx_worker_shares_tasks_lists(int workerid, int sched_ctx_id)
  108. {
  109. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  110. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  111. int curr_workerid = starpu_worker_get_id();
  112. /* if is the initial sched_ctx no point in taking the mutex, the workers are
  113. not launched yet, or if the current worker is calling this */
  114. if(!sched_ctx->is_initial_sched && workerid != curr_workerid)
  115. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  116. worker->shares_tasks_lists[sched_ctx_id] = 1;
  117. if(!sched_ctx->is_initial_sched && workerid != curr_workerid)
  118. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  119. }
  120. static void _starpu_add_workers_to_sched_ctx(struct _starpu_sched_ctx *sched_ctx, int *workerids, int nworkers,
  121. int *added_workers, int *n_added_workers)
  122. {
  123. struct starpu_worker_collection *workers = sched_ctx->workers;
  124. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  125. int nworkers_to_add = nworkers == -1 ? (int)config->topology.nworkers : nworkers;
  126. int workers_to_add[nworkers_to_add];
  127. struct starpu_perfmodel_device devices[nworkers_to_add];
  128. int ndevices = 0;
  129. struct _starpu_worker *str_worker = NULL;
  130. int worker;
  131. int i = 0;
  132. for(i = 0; i < nworkers_to_add; i++)
  133. {
  134. /* added_workers is NULL for the call of this func at the creation of the context*/
  135. /* if the function is called at the creation of the context it's no need to do this verif */
  136. if(added_workers)
  137. {
  138. worker = workers->add(workers, (workerids == NULL ? i : workerids[i]));
  139. if(worker >= 0)
  140. added_workers[(*n_added_workers)++] = worker;
  141. else
  142. {
  143. int curr_workerid = starpu_worker_get_id();
  144. struct _starpu_worker *worker_str = _starpu_get_worker_struct(workerids[i]);
  145. if(curr_workerid != workerids[i])
  146. STARPU_PTHREAD_MUTEX_LOCK(&worker_str->sched_mutex);
  147. worker_str->removed_from_ctx[sched_ctx->id] = 0;
  148. if(curr_workerid != workerids[i])
  149. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_str->sched_mutex);
  150. }
  151. }
  152. else
  153. {
  154. worker = (workerids == NULL ? i : workerids[i]);
  155. workers->add(workers, worker);
  156. workers_to_add[i] = worker;
  157. str_worker = _starpu_get_worker_struct(worker);
  158. str_worker->tmp_sched_ctx = (int)sched_ctx->id;
  159. }
  160. }
  161. int *wa;
  162. int na;
  163. if(added_workers)
  164. {
  165. na = *n_added_workers;
  166. wa = added_workers;
  167. }
  168. else
  169. {
  170. na = nworkers_to_add;
  171. wa = workers_to_add;
  172. }
  173. for(i = 0; i < na; i++)
  174. {
  175. worker = wa[i];
  176. str_worker = _starpu_get_worker_struct(worker);
  177. int dev1, dev2;
  178. unsigned found = 0;
  179. for(dev1 = 0; dev1 < str_worker->perf_arch.ndevices; dev1++)
  180. {
  181. for(dev2 = 0; dev2 < ndevices; dev2++)
  182. {
  183. if(devices[dev2].type == str_worker->perf_arch.devices[dev1].type &&
  184. devices[dev2].devid == str_worker->perf_arch.devices[dev1].devid)
  185. {
  186. devices[dev2].ncores += str_worker->perf_arch.devices[dev1].ncores;
  187. found = 1;
  188. break;
  189. }
  190. }
  191. if(!found)
  192. {
  193. devices[ndevices].type = str_worker->perf_arch.devices[dev1].type;
  194. devices[ndevices].devid = str_worker->perf_arch.devices[dev1].devid;
  195. devices[ndevices].ncores = str_worker->perf_arch.devices[dev1].ncores;
  196. ndevices++;
  197. }
  198. else
  199. found = 0;
  200. }
  201. }
  202. if(ndevices > 0)
  203. {
  204. if(sched_ctx->perf_arch.devices == NULL)
  205. sched_ctx->perf_arch.devices = (struct starpu_perfmodel_device*)malloc(ndevices*sizeof(struct starpu_perfmodel_device));
  206. else
  207. sched_ctx->perf_arch.devices = (struct starpu_perfmodel_device*)realloc(sched_ctx->perf_arch.devices, (sched_ctx->perf_arch.ndevices+ndevices)*sizeof(struct starpu_perfmodel_device));
  208. int dev1, dev2;
  209. unsigned found = 0;
  210. for(dev1 = 0; dev1 < ndevices; dev1++)
  211. {
  212. for(dev2 = 0; dev2 < sched_ctx->perf_arch.ndevices; dev2++)
  213. {
  214. if(sched_ctx->perf_arch.devices[dev2].type == devices[dev1].type && sched_ctx->perf_arch.devices[dev2].devid == devices[dev1].devid)
  215. {
  216. sched_ctx->perf_arch.devices[dev2].ncores += devices[dev1].ncores;
  217. found = 1;
  218. }
  219. }
  220. if(!found)
  221. {
  222. sched_ctx->perf_arch.devices[sched_ctx->perf_arch.ndevices].type = devices[dev1].type;
  223. sched_ctx->perf_arch.devices[sched_ctx->perf_arch.ndevices].devid = devices[dev1].devid;
  224. sched_ctx->perf_arch.devices[sched_ctx->perf_arch.ndevices].ncores = devices[dev1].ncores;
  225. sched_ctx->perf_arch.ndevices++;
  226. }
  227. else
  228. found = 0;
  229. }
  230. }
  231. if(!sched_ctx->sched_policy)
  232. {
  233. if(sched_ctx->main_master == -1)
  234. sched_ctx->main_master = starpu_sched_ctx_book_workers_for_task(sched_ctx->id, workerids, nworkers);
  235. else
  236. {
  237. _starpu_sched_ctx_add_workers_to_master(sched_ctx->id, workerids, nworkers, sched_ctx->main_master);
  238. }
  239. }
  240. else if(sched_ctx->sched_policy->add_workers)
  241. {
  242. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  243. if(added_workers)
  244. {
  245. if(*n_added_workers > 0)
  246. sched_ctx->sched_policy->add_workers(sched_ctx->id, added_workers, *n_added_workers);
  247. }
  248. else
  249. {
  250. sched_ctx->sched_policy->add_workers(sched_ctx->id, workers_to_add, nworkers_to_add);
  251. }
  252. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  253. }
  254. return;
  255. }
  256. static void _starpu_remove_workers_from_sched_ctx(struct _starpu_sched_ctx *sched_ctx, int *workerids,
  257. int nworkers, int *removed_workers, int *n_removed_workers)
  258. {
  259. struct starpu_worker_collection *workers = sched_ctx->workers;
  260. int i = 0;
  261. for(i = 0; i < nworkers; i++)
  262. {
  263. if(workers->nworkers > 0)
  264. {
  265. if(_starpu_worker_belongs_to_a_sched_ctx(workerids[i], sched_ctx->id))
  266. {
  267. int worker = workers->remove(workers, workerids[i]);
  268. if(worker >= 0)
  269. removed_workers[(*n_removed_workers)++] = worker;
  270. }
  271. }
  272. }
  273. struct starpu_perfmodel_device devices[workers->nworkers];
  274. int ndevices = 0;
  275. for(i = 0; i < *n_removed_workers; i++)
  276. {
  277. struct _starpu_worker *str_worker = _starpu_get_worker_struct(removed_workers[i]);
  278. int dev1, dev2;
  279. unsigned found = 0;
  280. for(dev1 = 0; dev1 < sched_ctx->perf_arch.ndevices; dev1++)
  281. {
  282. for(dev2 = 0; dev2 < str_worker->perf_arch.ndevices; dev2++)
  283. {
  284. if(sched_ctx->perf_arch.devices[dev1].type == str_worker->perf_arch.devices[dev2].type &&
  285. sched_ctx->perf_arch.devices[dev1].devid == str_worker->perf_arch.devices[dev2].devid)
  286. {
  287. if(sched_ctx->perf_arch.devices[dev1].ncores > str_worker->perf_arch.devices[dev2].ncores)
  288. {
  289. devices[ndevices].ncores = sched_ctx->perf_arch.devices[dev1].ncores -
  290. str_worker->perf_arch.devices[dev2].ncores;
  291. devices[ndevices].type = sched_ctx->perf_arch.devices[dev1].type;
  292. devices[ndevices].devid = sched_ctx->perf_arch.devices[dev1].devid;
  293. ndevices++;
  294. found = 1;
  295. }
  296. }
  297. }
  298. if(found)
  299. found = 0;
  300. }
  301. }
  302. int worker;
  303. unsigned found = 0;
  304. int dev;
  305. struct starpu_sched_ctx_iterator it;
  306. if(workers->init_iterator)
  307. workers->init_iterator(workers, &it);
  308. while(workers->has_next(workers, &it))
  309. {
  310. worker = workers->get_next(workers, &it);
  311. for(i = 0; i < *n_removed_workers; i++)
  312. {
  313. if(worker == removed_workers[i])
  314. {
  315. found = 1;
  316. break;
  317. }
  318. }
  319. if(!found)
  320. {
  321. struct _starpu_worker *str_worker = _starpu_get_worker_struct(worker);
  322. for(dev = 0; dev < str_worker->perf_arch.ndevices; dev++)
  323. {
  324. devices[ndevices].type = str_worker->perf_arch.devices[dev].type;
  325. devices[ndevices].devid = str_worker->perf_arch.devices[dev].devid;
  326. devices[ndevices].ncores = str_worker->perf_arch.devices[dev].ncores;
  327. ndevices++;
  328. }
  329. }
  330. else
  331. found = 0;
  332. }
  333. sched_ctx->perf_arch.ndevices = ndevices;
  334. for(dev = 0; dev < ndevices; dev++)
  335. {
  336. sched_ctx->perf_arch.devices[dev].type = devices[dev].type;
  337. sched_ctx->perf_arch.devices[dev].devid = devices[dev].devid;
  338. sched_ctx->perf_arch.devices[dev].ncores = devices[dev].ncores;
  339. }
  340. if(!sched_ctx->sched_policy)
  341. _starpu_sched_ctx_wake_these_workers_up(sched_ctx->id, removed_workers, *n_removed_workers);
  342. return;
  343. }
  344. static void _starpu_sched_ctx_free_scheduling_data(struct _starpu_sched_ctx *sched_ctx)
  345. {
  346. int *workerids = NULL;
  347. unsigned nworkers_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  348. if(nworkers_ctx > 0 && sched_ctx->sched_policy->remove_workers)
  349. {
  350. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  351. sched_ctx->sched_policy->remove_workers(sched_ctx->id, workerids, nworkers_ctx);
  352. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  353. }
  354. free(workerids);
  355. return;
  356. }
  357. #ifdef STARPU_HAVE_HWLOC
  358. static void _starpu_sched_ctx_create_hwloc_tree(struct _starpu_sched_ctx *sched_ctx)
  359. {
  360. sched_ctx->hwloc_workers_set = hwloc_bitmap_alloc();
  361. struct starpu_worker_collection *workers = sched_ctx->workers;
  362. struct _starpu_worker *worker;
  363. struct starpu_sched_ctx_iterator it;
  364. if(workers->init_iterator)
  365. workers->init_iterator(workers, &it);
  366. while(workers->has_next(workers, &it))
  367. {
  368. worker = _starpu_get_worker_struct(workers->get_next(workers, &it));
  369. if(!starpu_worker_is_combined_worker(worker->workerid))
  370. {
  371. hwloc_bitmap_or(sched_ctx->hwloc_workers_set,
  372. sched_ctx->hwloc_workers_set,
  373. worker->hwloc_cpu_set);
  374. }
  375. }
  376. return;
  377. }
  378. #endif
  379. struct _starpu_sched_ctx* _starpu_create_sched_ctx(struct starpu_sched_policy *policy, int *workerids,
  380. int nworkers_ctx, unsigned is_initial_sched,
  381. const char *sched_ctx_name,
  382. int min_prio_set, int min_prio,
  383. int max_prio_set, int max_prio)
  384. {
  385. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  386. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  387. STARPU_ASSERT(config->topology.nsched_ctxs < STARPU_NMAX_SCHED_CTXS);
  388. unsigned id = _starpu_get_first_free_sched_ctx(config);
  389. struct _starpu_sched_ctx *sched_ctx = &config->sched_ctxs[id];
  390. sched_ctx->id = id;
  391. config->topology.nsched_ctxs++;
  392. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  393. int nworkers = config->topology.nworkers;
  394. STARPU_ASSERT(nworkers_ctx <= nworkers);
  395. STARPU_PTHREAD_MUTEX_INIT(&sched_ctx->empty_ctx_mutex, NULL);
  396. starpu_task_list_init(&sched_ctx->empty_ctx_tasks);
  397. sched_ctx->sched_policy = policy ? (struct starpu_sched_policy*)malloc(sizeof(struct starpu_sched_policy)) : NULL;
  398. sched_ctx->is_initial_sched = is_initial_sched;
  399. sched_ctx->name = sched_ctx_name;
  400. sched_ctx->inheritor = STARPU_NMAX_SCHED_CTXS;
  401. sched_ctx->finished_submit = 0;
  402. sched_ctx->min_priority_is_set = min_prio_set;
  403. if (sched_ctx->min_priority_is_set) sched_ctx->min_priority = min_prio;
  404. sched_ctx->max_priority_is_set = max_prio_set;
  405. if (sched_ctx->max_priority_is_set) sched_ctx->max_priority = max_prio;
  406. _starpu_barrier_counter_init(&sched_ctx->tasks_barrier, 0);
  407. _starpu_barrier_counter_init(&sched_ctx->ready_tasks_barrier, 0);
  408. sched_ctx->ready_flops = 0.0;
  409. sched_ctx->main_master = -1;
  410. sched_ctx->perf_arch.devices = NULL;
  411. sched_ctx->perf_arch.ndevices = 0;
  412. int w;
  413. for(w = 0; w < nworkers; w++)
  414. {
  415. sem_init(&sched_ctx->fall_asleep_sem[w], 0, 0);
  416. sem_init(&sched_ctx->wake_up_sem[w], 0, 0);
  417. STARPU_PTHREAD_COND_INIT(&sched_ctx->parallel_sect_cond[w], NULL);
  418. STARPU_PTHREAD_MUTEX_INIT(&sched_ctx->parallel_sect_mutex[w], NULL);
  419. sched_ctx->master[w] = -1;
  420. sched_ctx->parallel_sect[w] = 0;
  421. sched_ctx->sleeping[w] = 0;
  422. }
  423. /*init the strategy structs and the worker_collection of the ressources of the context */
  424. if(policy)
  425. _starpu_init_sched_policy(config, sched_ctx, policy);
  426. else
  427. starpu_sched_ctx_create_worker_collection(sched_ctx->id, STARPU_WORKER_LIST);
  428. /* construct the collection of workers(list/tree/etc.) */
  429. sched_ctx->workers->init(sched_ctx->workers);
  430. /* after having an worker_collection on the ressources add them */
  431. _starpu_add_workers_to_sched_ctx(sched_ctx, workerids, nworkers_ctx, NULL, NULL);
  432. #ifdef STARPU_HAVE_HWLOC
  433. /* build hwloc tree of the context */
  434. _starpu_sched_ctx_create_hwloc_tree(sched_ctx);
  435. #endif //STARPU_HAVE_HWLOC
  436. /* if we create the initial big sched ctx we can update workers' status here
  437. because they haven't been launched yet */
  438. if(is_initial_sched)
  439. {
  440. int i;
  441. /*initialize the mutexes for all contexts */
  442. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  443. STARPU_PTHREAD_RWLOCK_INIT(&changing_ctx_mutex[i], NULL);
  444. for(i = 0; i < nworkers; i++)
  445. {
  446. struct _starpu_worker *worker = _starpu_get_worker_struct(i);
  447. worker->sched_ctx_list = (struct _starpu_sched_ctx_list*)malloc(sizeof(struct _starpu_sched_ctx_list));
  448. _starpu_sched_ctx_list_init(worker->sched_ctx_list);
  449. _starpu_sched_ctx_list_add(&worker->sched_ctx_list, sched_ctx->id);
  450. worker->nsched_ctxs++;
  451. }
  452. }
  453. return sched_ctx;
  454. }
  455. static void _get_workers(int min, int max, int *workers, int *nw, enum starpu_worker_archtype arch, unsigned allow_overlap)
  456. {
  457. int pus[max];
  458. int npus = 0;
  459. int i;
  460. int n = 0;
  461. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  462. if(config->topology.nsched_ctxs == 1)
  463. {
  464. /*we have all available resources */
  465. npus = starpu_worker_get_nids_by_type(arch, pus, max);
  466. /*TODO: hierarchical ctxs: get max good workers: close one to another */
  467. for(i = 0; i < npus; i++)
  468. workers[(*nw)++] = pus[i];
  469. }
  470. else
  471. {
  472. unsigned enough_ressources = 0;
  473. npus = starpu_worker_get_nids_ctx_free_by_type(arch, pus, max);
  474. for(i = 0; i < npus; i++)
  475. workers[(*nw)++] = pus[i];
  476. if(npus == max)
  477. /*we have enough available resources */
  478. enough_ressources = 1;
  479. if(!enough_ressources && npus >= min)
  480. /*we have enough available resources */
  481. enough_ressources = 1;
  482. if(!enough_ressources)
  483. {
  484. /* try to get ressources from ctx who have more than the min of workers they need */
  485. int s;
  486. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  487. {
  488. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  489. {
  490. int _npus = 0;
  491. int _pus[STARPU_NMAXWORKERS];
  492. _npus = _starpu_get_workers_of_sched_ctx(config->sched_ctxs[s].id, _pus, arch);
  493. int ctx_min = arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  494. if(_npus > ctx_min)
  495. {
  496. if(npus < min)
  497. {
  498. n = (_npus - ctx_min) > (min - npus) ? min - npus : (_npus - ctx_min);
  499. npus += n;
  500. }
  501. /*TODO: hierarchical ctxs: get n good workers: close to the other ones I already assigned to the ctx */
  502. for(i = 0; i < n; i++)
  503. workers[(*nw)++] = _pus[i];
  504. starpu_sched_ctx_remove_workers(_pus, n, config->sched_ctxs[s].id);
  505. }
  506. }
  507. }
  508. if(npus >= min)
  509. enough_ressources = 1;
  510. }
  511. if(!enough_ressources)
  512. {
  513. /* if there is no available workers to satisfy the minimum required
  514. give them workers proportional to their requirements*/
  515. int global_npus = starpu_worker_get_count_by_type(arch);
  516. int req_npus = 0;
  517. int s;
  518. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  519. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  520. req_npus += arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  521. req_npus += min;
  522. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  523. {
  524. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  525. {
  526. int ctx_min = arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  527. double needed_npus = ((double)ctx_min * (double)global_npus) / (double)req_npus;
  528. int _npus = 0;
  529. int _pus[STARPU_NMAXWORKERS];
  530. _npus = _starpu_get_workers_of_sched_ctx(config->sched_ctxs[s].id, _pus, arch);
  531. if(needed_npus < (double)_npus)
  532. {
  533. double npus_to_rem = (double)_npus - needed_npus;
  534. int x = floor(npus_to_rem);
  535. double x_double = (double)x;
  536. double diff = npus_to_rem - x_double;
  537. int npus_to_remove = diff >= 0.5 ? x+1 : x;
  538. int pus_to_remove[npus_to_remove];
  539. int c = 0;
  540. /*TODO: hierarchical ctxs: get npus_to_remove good workers: close to the other ones I already assigned to the ctx */
  541. for(i = _npus-1; i >= (_npus - npus_to_remove); i--)
  542. {
  543. workers[(*nw)++] = _pus[i];
  544. pus_to_remove[c++] = _pus[i];
  545. }
  546. if(!allow_overlap)
  547. starpu_sched_ctx_remove_workers(pus_to_remove, npus_to_remove, config->sched_ctxs[s].id);
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. unsigned starpu_sched_ctx_create_inside_interval(const char *policy_name, const char *sched_ctx_name,
  555. int min_ncpus, int max_ncpus, int min_ngpus, int max_ngpus,
  556. unsigned allow_overlap)
  557. {
  558. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  559. struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(config, policy_name);
  560. struct _starpu_sched_ctx *sched_ctx = NULL;
  561. int workers[max_ncpus + max_ngpus];
  562. int nw = 0;
  563. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  564. _get_workers(min_ncpus, max_ncpus, workers, &nw, STARPU_CPU_WORKER, allow_overlap);
  565. _get_workers(min_ngpus, max_ngpus, workers, &nw, STARPU_CUDA_WORKER, allow_overlap);
  566. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  567. int i;
  568. printf("%d: ", nw);
  569. for(i = 0; i < nw; i++)
  570. printf("%d ", workers[i]);
  571. printf("\n");
  572. sched_ctx = _starpu_create_sched_ctx(selected_policy, workers, nw, 0, sched_ctx_name, 0, 0, 0, 0);
  573. sched_ctx->min_ncpus = min_ncpus;
  574. sched_ctx->max_ncpus = max_ncpus;
  575. sched_ctx->min_ngpus = min_ngpus;
  576. sched_ctx->max_ngpus = max_ngpus;
  577. _starpu_unlock_mutex_if_prev_locked();
  578. int *added_workerids;
  579. unsigned nw_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &added_workerids);
  580. _starpu_update_workers_without_ctx(added_workerids, nw_ctx, sched_ctx->id, 0);
  581. free(added_workerids);
  582. _starpu_relock_mutex_if_prev_locked();
  583. #ifdef STARPU_USE_SC_HYPERVISOR
  584. sched_ctx->perf_counters = NULL;
  585. #endif
  586. return sched_ctx->id;
  587. }
  588. unsigned starpu_sched_ctx_create(int *workerids, int nworkers, const char *sched_ctx_name, ...)
  589. {
  590. va_list varg_list;
  591. int arg_type;
  592. int min_prio_set = 0;
  593. int max_prio_set = 0;
  594. int min_prio = 0;
  595. int max_prio = 0;
  596. struct starpu_sched_policy *sched_policy = NULL;
  597. unsigned hierarchy_level = 0;
  598. unsigned nesting_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  599. va_start(varg_list, sched_ctx_name);
  600. while ((arg_type = va_arg(varg_list, int)) != 0)
  601. {
  602. if (arg_type == STARPU_SCHED_CTX_POLICY_NAME)
  603. {
  604. char *policy_name = va_arg(varg_list, char *);
  605. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  606. sched_policy = _starpu_select_sched_policy(config, policy_name);
  607. }
  608. else if (arg_type == STARPU_SCHED_CTX_POLICY_STRUCT)
  609. {
  610. sched_policy = va_arg(varg_list, struct starpu_sched_policy *);
  611. }
  612. else if (arg_type == STARPU_SCHED_CTX_POLICY_MIN_PRIO)
  613. {
  614. min_prio = va_arg(varg_list, int);
  615. min_prio_set = 1;
  616. }
  617. else if (arg_type == STARPU_SCHED_CTX_POLICY_MAX_PRIO)
  618. {
  619. max_prio = va_arg(varg_list, int);
  620. max_prio_set = 1;
  621. }
  622. else if (arg_type == STARPU_SCHED_CTX_HIERARCHY_LEVEL)
  623. {
  624. hierarchy_level = va_arg(varg_list, unsigned);
  625. }
  626. else if (arg_type == STARPU_SCHED_CTX_NESTED)
  627. {
  628. nesting_sched_ctx = va_arg(varg_list, unsigned);
  629. }
  630. else
  631. {
  632. STARPU_ABORT_MSG("Unrecognized argument %d\n", arg_type);
  633. }
  634. }
  635. va_end(varg_list);
  636. struct _starpu_sched_ctx *sched_ctx = NULL;
  637. sched_ctx = _starpu_create_sched_ctx(sched_policy, workerids, nworkers, 0, sched_ctx_name, min_prio_set, min_prio, max_prio_set, max_prio);
  638. sched_ctx->hierarchy_level = hierarchy_level;
  639. sched_ctx->nesting_sched_ctx = nesting_sched_ctx;
  640. _starpu_unlock_mutex_if_prev_locked();
  641. int *added_workerids;
  642. unsigned nw_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &added_workerids);
  643. _starpu_update_workers_with_ctx(added_workerids, nw_ctx, sched_ctx->id);
  644. free(added_workerids);
  645. _starpu_relock_mutex_if_prev_locked();
  646. #ifdef STARPU_USE_SC_HYPERVISOR
  647. sched_ctx->perf_counters = NULL;
  648. #endif
  649. return sched_ctx->id;
  650. }
  651. void starpu_sched_ctx_register_close_callback(unsigned sched_ctx_id, void (*close_callback)(unsigned sched_ctx_id, void* args), void *args)
  652. {
  653. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  654. sched_ctx->close_callback = close_callback;
  655. sched_ctx->close_args = args;
  656. return;
  657. }
  658. #ifdef STARPU_USE_SC_HYPERVISOR
  659. void starpu_sched_ctx_set_perf_counters(unsigned sched_ctx_id, void* perf_counters)
  660. {
  661. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  662. sched_ctx->perf_counters = (struct starpu_sched_ctx_performance_counters *)perf_counters;
  663. return;
  664. }
  665. #endif
  666. /* free all structures for the context */
  667. static void _starpu_delete_sched_ctx(struct _starpu_sched_ctx *sched_ctx)
  668. {
  669. STARPU_ASSERT(sched_ctx->id != STARPU_NMAX_SCHED_CTXS);
  670. if(sched_ctx->sched_policy)
  671. {
  672. _starpu_deinit_sched_policy(sched_ctx);
  673. free(sched_ctx->sched_policy);
  674. sched_ctx->sched_policy = NULL;
  675. }
  676. if (sched_ctx->perf_arch.devices)
  677. {
  678. free(sched_ctx->perf_arch.devices);
  679. sched_ctx->perf_arch.devices = NULL;
  680. }
  681. STARPU_PTHREAD_MUTEX_DESTROY(&sched_ctx->empty_ctx_mutex);
  682. sched_ctx->id = STARPU_NMAX_SCHED_CTXS;
  683. #ifdef STARPU_HAVE_HWLOC
  684. hwloc_bitmap_free(sched_ctx->hwloc_workers_set);
  685. #endif //STARPU_HAVE_HWLOC
  686. struct _starpu_machine_config *config = _starpu_get_machine_config();
  687. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  688. config->topology.nsched_ctxs--;
  689. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  690. }
  691. void starpu_sched_ctx_delete(unsigned sched_ctx_id)
  692. {
  693. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  694. #ifdef STARPU_USE_SC_HYPERVISOR
  695. if(sched_ctx != NULL && sched_ctx_id != 0 && sched_ctx_id != STARPU_NMAX_SCHED_CTXS
  696. && sched_ctx->perf_counters != NULL)
  697. {
  698. _STARPU_TRACE_HYPERVISOR_BEGIN();
  699. sched_ctx->perf_counters->notify_delete_context(sched_ctx_id);
  700. _STARPU_TRACE_HYPERVISOR_END();
  701. }
  702. #endif //STARPU_USE_SC_HYPERVISOR
  703. unsigned inheritor_sched_ctx_id = sched_ctx->inheritor;
  704. struct _starpu_sched_ctx *inheritor_sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx->inheritor);
  705. _starpu_unlock_mutex_if_prev_locked();
  706. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  707. STARPU_ASSERT(sched_ctx->id != STARPU_NMAX_SCHED_CTXS);
  708. int *workerids;
  709. unsigned nworkers_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  710. /*if both of them have all the ressources is pointless*/
  711. /*trying to transfer ressources from one ctx to the other*/
  712. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  713. unsigned nworkers = config->topology.nworkers;
  714. if(nworkers_ctx > 0 && inheritor_sched_ctx && inheritor_sched_ctx->id != STARPU_NMAX_SCHED_CTXS &&
  715. !(nworkers_ctx == nworkers && nworkers_ctx == inheritor_sched_ctx->workers->nworkers))
  716. {
  717. starpu_sched_ctx_add_workers(workerids, nworkers_ctx, inheritor_sched_ctx_id);
  718. starpu_sched_ctx_set_priority(workerids, nworkers_ctx, inheritor_sched_ctx_id, 1);
  719. starpu_sched_ctx_set_priority_on_level(workerids, nworkers_ctx, inheritor_sched_ctx_id, 1);
  720. }
  721. if(!_starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx_id))
  722. {
  723. if(!sched_ctx->sched_policy)
  724. starpu_sched_ctx_unbook_workers_for_task(sched_ctx->id, sched_ctx->main_master);
  725. /*if btw the mutex release & the mutex lock the context has changed take care to free all
  726. scheduling data before deleting the context */
  727. _starpu_update_workers_without_ctx(workerids, nworkers_ctx, sched_ctx_id, 1);
  728. // _starpu_sched_ctx_free_scheduling_data(sched_ctx);
  729. _starpu_delete_sched_ctx(sched_ctx);
  730. }
  731. /* workerids is malloc-ed in starpu_sched_ctx_get_workers_list, don't forget to free it when
  732. you don't use it anymore */
  733. free(workerids);
  734. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  735. _starpu_relock_mutex_if_prev_locked();
  736. return;
  737. }
  738. /* called after the workers are terminated so we don't have anything else to do but free the memory*/
  739. void _starpu_delete_all_sched_ctxs()
  740. {
  741. unsigned i;
  742. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  743. {
  744. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(i);
  745. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[i]);
  746. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  747. {
  748. _starpu_sched_ctx_free_scheduling_data(sched_ctx);
  749. _starpu_barrier_counter_destroy(&sched_ctx->tasks_barrier);
  750. _starpu_barrier_counter_destroy(&sched_ctx->ready_tasks_barrier);
  751. _starpu_delete_sched_ctx(sched_ctx);
  752. }
  753. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[i]);
  754. STARPU_PTHREAD_RWLOCK_DESTROY(&changing_ctx_mutex[i]);
  755. }
  756. return;
  757. }
  758. static void _starpu_check_workers(int *workerids, int nworkers)
  759. {
  760. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  761. int nworkers_conf = config->topology.nworkers;
  762. int i;
  763. for(i = 0; i < nworkers; i++)
  764. {
  765. /* take care the user does not ask for a resource that does not exist */
  766. STARPU_ASSERT_MSG(workerids[i] >= 0 && workerids[i] <= nworkers_conf, "requested to add workerid = %d, but that is beyond the range 0 to %d", workerids[i], nworkers_conf);
  767. }
  768. }
  769. void _starpu_fetch_tasks_from_empty_ctx_list(struct _starpu_sched_ctx *sched_ctx)
  770. {
  771. unsigned unlocked = 0;
  772. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  773. if(starpu_task_list_empty(&sched_ctx->empty_ctx_tasks))
  774. {
  775. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  776. return;
  777. }
  778. else
  779. /* you're not suppose to get here if you deleted the context
  780. so no point in having the mutex locked */
  781. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx->id]);
  782. while(!starpu_task_list_empty(&sched_ctx->empty_ctx_tasks))
  783. {
  784. if(unlocked)
  785. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  786. struct starpu_task *old_task = starpu_task_list_pop_back(&sched_ctx->empty_ctx_tasks);
  787. unlocked = 1;
  788. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  789. if(old_task == &stop_submission_task)
  790. break;
  791. int ret = _starpu_push_task_to_workers(old_task);
  792. /* if we should stop poping from empty ctx tasks */
  793. if(ret == -EAGAIN) break;
  794. }
  795. if(!unlocked)
  796. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  797. /* leave the mutex as it was to avoid pbs in the caller function */
  798. STARPU_PTHREAD_RWLOCK_RDLOCK(&changing_ctx_mutex[sched_ctx->id]);
  799. return;
  800. }
  801. void starpu_sched_ctx_set_priority_on_level(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx, unsigned priority)
  802. {
  803. /* int w; */
  804. /* struct _starpu_worker *worker = NULL; */
  805. /* for(w = 0; w < nworkers_to_add; w++) */
  806. /* { */
  807. /* worker = _starpu_get_worker_struct(workers_to_add[w]); */
  808. /* STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex); */
  809. /* struct _starpu_sched_ctx_list *l = NULL; */
  810. /* for (l = worker->sched_ctx_list; l; l = l->next) */
  811. /* { */
  812. /* if(l->sched_ctx != STARPU_NMAX_SCHED_CTXS && l->sched_ctx != sched_ctx && */
  813. /* starpu_sched_ctx_get_hierarchy_level(l->sched_ctx) == starpu_sched_ctx_get_hierarchy_level(sched_ctx)) */
  814. /* { */
  815. /* /\* the lock is taken inside the func *\/ */
  816. /* STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex); */
  817. /* starpu_sched_ctx_set_priority(&workers_to_add[w], 1, l->sched_ctx, priority); */
  818. /* STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex); */
  819. /* } */
  820. /* } */
  821. /* STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex); */
  822. /* } */
  823. /* return; */
  824. }
  825. static void _set_priority_hierarchically(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx, unsigned priority)
  826. {
  827. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  828. {
  829. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctx);
  830. starpu_sched_ctx_set_priority(workers_to_add, nworkers_to_add, father, priority);
  831. starpu_sched_ctx_set_priority_on_level(workers_to_add, nworkers_to_add, father, priority);
  832. _set_priority_hierarchically(workers_to_add, nworkers_to_add, father, priority);
  833. }
  834. return;
  835. }
  836. void starpu_sched_ctx_add_workers(int *workers_to_add, int nworkers_to_add, unsigned sched_ctx_id)
  837. {
  838. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  839. int added_workers[nworkers_to_add];
  840. int n_added_workers = 0;
  841. _starpu_unlock_mutex_if_prev_locked();
  842. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  843. STARPU_ASSERT(workers_to_add != NULL && nworkers_to_add > 0);
  844. _starpu_check_workers(workers_to_add, nworkers_to_add);
  845. /* if the context has not already been deleted */
  846. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  847. {
  848. _starpu_add_workers_to_sched_ctx(sched_ctx, workers_to_add, nworkers_to_add, added_workers, &n_added_workers);
  849. if(n_added_workers > 0)
  850. {
  851. _starpu_update_workers_with_ctx(added_workers, n_added_workers, sched_ctx->id);
  852. }
  853. starpu_sched_ctx_set_priority(workers_to_add, nworkers_to_add, sched_ctx_id, 1);
  854. _set_priority_hierarchically(workers_to_add, nworkers_to_add, sched_ctx_id, 0);
  855. }
  856. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  857. _starpu_relock_mutex_if_prev_locked();
  858. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  859. {
  860. STARPU_PTHREAD_RWLOCK_RDLOCK(&changing_ctx_mutex[sched_ctx_id]);
  861. _starpu_fetch_tasks_from_empty_ctx_list(sched_ctx);
  862. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  863. }
  864. return;
  865. }
  866. void starpu_sched_ctx_remove_workers(int *workers_to_remove, int nworkers_to_remove, unsigned sched_ctx_id)
  867. {
  868. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  869. int removed_workers[sched_ctx->workers->nworkers];
  870. int n_removed_workers = 0;
  871. _starpu_check_workers(workers_to_remove, nworkers_to_remove);
  872. _starpu_unlock_mutex_if_prev_locked();
  873. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  874. /* if the context has not already been deleted */
  875. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  876. {
  877. _starpu_remove_workers_from_sched_ctx(sched_ctx, workers_to_remove, nworkers_to_remove, removed_workers, &n_removed_workers);
  878. if(n_removed_workers > 0)
  879. {
  880. _starpu_update_workers_without_ctx(removed_workers, n_removed_workers, sched_ctx_id, 0);
  881. }
  882. }
  883. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  884. _starpu_relock_mutex_if_prev_locked();
  885. return;
  886. }
  887. int _starpu_nworkers_able_to_execute_task(struct starpu_task *task, struct _starpu_sched_ctx *sched_ctx)
  888. {
  889. unsigned worker = 0, nworkers = 0;
  890. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx->id]);
  891. struct starpu_worker_collection *workers = sched_ctx->workers;
  892. struct starpu_sched_ctx_iterator it;
  893. if(workers->init_iterator)
  894. workers->init_iterator(workers, &it);
  895. while(workers->has_next(workers, &it))
  896. {
  897. worker = workers->get_next(workers, &it);
  898. STARPU_ASSERT_MSG(worker < STARPU_NMAXWORKERS, "worker id %d", worker);
  899. if (starpu_worker_can_execute_task(worker, task, 0))
  900. nworkers++;
  901. }
  902. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx->id]);
  903. return nworkers;
  904. }
  905. /* unused sched_ctx have the id STARPU_NMAX_SCHED_CTXS */
  906. void _starpu_init_all_sched_ctxs(struct _starpu_machine_config *config)
  907. {
  908. starpu_pthread_key_create(&sched_ctx_key, NULL);
  909. unsigned i;
  910. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  911. config->sched_ctxs[i].id = STARPU_NMAX_SCHED_CTXS;
  912. return;
  913. }
  914. /* sched_ctx aren't necessarly one next to another */
  915. /* for eg when we remove one its place is free */
  916. /* when we add new one we reuse its place */
  917. static unsigned _starpu_get_first_free_sched_ctx(struct _starpu_machine_config *config)
  918. {
  919. unsigned i;
  920. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  921. if(config->sched_ctxs[i].id == STARPU_NMAX_SCHED_CTXS)
  922. return i;
  923. STARPU_ASSERT(0);
  924. return STARPU_NMAX_SCHED_CTXS;
  925. }
  926. int _starpu_wait_for_all_tasks_of_sched_ctx(unsigned sched_ctx_id)
  927. {
  928. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  929. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_task_wait_for_all must not be called from a task or callback");
  930. return _starpu_barrier_counter_wait_for_empty_counter(&sched_ctx->tasks_barrier);
  931. }
  932. void _starpu_decrement_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  933. {
  934. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  935. if (!config->watchdog_ok)
  936. config->watchdog_ok = 1;
  937. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  938. int reached = _starpu_barrier_counter_get_reached_start(&sched_ctx->tasks_barrier);
  939. int finished = reached == 1;
  940. /* when finished decrementing the tasks if the user signaled he will not submit tasks anymore
  941. we can move all its workers to the inheritor context */
  942. if(finished && sched_ctx->inheritor != STARPU_NMAX_SCHED_CTXS)
  943. {
  944. STARPU_PTHREAD_MUTEX_LOCK(&finished_submit_mutex);
  945. if(sched_ctx->finished_submit)
  946. {
  947. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  948. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  949. {
  950. if(sched_ctx->close_callback)
  951. sched_ctx->close_callback(sched_ctx->id, sched_ctx->close_args);
  952. int *workerids = NULL;
  953. unsigned nworkers = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  954. if(nworkers > 0)
  955. {
  956. starpu_sched_ctx_add_workers(workerids, nworkers, sched_ctx->inheritor);
  957. free(workerids);
  958. }
  959. }
  960. _starpu_barrier_counter_decrement_until_empty_counter(&sched_ctx->tasks_barrier, 0.0);
  961. return;
  962. }
  963. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  964. }
  965. /* We also need to check for config->submitting = 0 (i.e. the
  966. * user calle starpu_drivers_request_termination()), in which
  967. * case we need to set config->running to 0 and wake workers,
  968. * so they can terminate, just like
  969. * starpu_drivers_request_termination() does.
  970. */
  971. STARPU_PTHREAD_MUTEX_LOCK(&config->submitted_mutex);
  972. if(config->submitting == 0)
  973. {
  974. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  975. {
  976. if(sched_ctx->close_callback)
  977. sched_ctx->close_callback(sched_ctx->id, sched_ctx->close_args);
  978. }
  979. ANNOTATE_HAPPENS_AFTER(&config->running);
  980. config->running = 0;
  981. ANNOTATE_HAPPENS_BEFORE(&config->running);
  982. int s;
  983. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  984. {
  985. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  986. {
  987. _starpu_check_nsubmitted_tasks_of_sched_ctx(config->sched_ctxs[s].id);
  988. }
  989. }
  990. }
  991. STARPU_PTHREAD_MUTEX_UNLOCK(&config->submitted_mutex);
  992. _starpu_barrier_counter_decrement_until_empty_counter(&sched_ctx->tasks_barrier, 0.0);
  993. return;
  994. }
  995. void _starpu_increment_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  996. {
  997. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  998. _starpu_barrier_counter_increment(&sched_ctx->tasks_barrier, 0.0);
  999. }
  1000. int _starpu_get_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  1001. {
  1002. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1003. return _starpu_barrier_counter_get_reached_start(&sched_ctx->tasks_barrier);
  1004. }
  1005. int _starpu_check_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  1006. {
  1007. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1008. return _starpu_barrier_counter_check(&sched_ctx->tasks_barrier);
  1009. }
  1010. void _starpu_increment_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops)
  1011. {
  1012. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1013. _starpu_barrier_counter_increment(&sched_ctx->ready_tasks_barrier, ready_flops);
  1014. }
  1015. void _starpu_decrement_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops)
  1016. {
  1017. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1018. _starpu_barrier_counter_decrement_until_empty_counter(&sched_ctx->ready_tasks_barrier, ready_flops);
  1019. }
  1020. int starpu_sched_ctx_get_nready_tasks(unsigned sched_ctx_id)
  1021. {
  1022. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1023. return _starpu_barrier_counter_get_reached_start(&sched_ctx->ready_tasks_barrier);
  1024. }
  1025. double starpu_sched_ctx_get_nready_flops(unsigned sched_ctx_id)
  1026. {
  1027. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1028. return _starpu_barrier_counter_get_reached_flops(&sched_ctx->ready_tasks_barrier);
  1029. }
  1030. int _starpu_wait_for_no_ready_of_sched_ctx(unsigned sched_ctx_id)
  1031. {
  1032. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1033. return _starpu_barrier_counter_wait_for_empty_counter(&sched_ctx->ready_tasks_barrier);
  1034. }
  1035. void starpu_sched_ctx_set_context(unsigned *sched_ctx)
  1036. {
  1037. starpu_pthread_setspecific(sched_ctx_key, (void*)sched_ctx);
  1038. }
  1039. unsigned starpu_sched_ctx_get_context()
  1040. {
  1041. unsigned *sched_ctx = (unsigned*)starpu_pthread_getspecific(sched_ctx_key);
  1042. if(sched_ctx == NULL)
  1043. return STARPU_NMAX_SCHED_CTXS;
  1044. STARPU_ASSERT(*sched_ctx < STARPU_NMAX_SCHED_CTXS);
  1045. return *sched_ctx;
  1046. }
  1047. unsigned _starpu_sched_ctx_get_current_context()
  1048. {
  1049. unsigned sched_ctx = starpu_sched_ctx_get_context();
  1050. if (sched_ctx == STARPU_NMAX_SCHED_CTXS)
  1051. return _starpu_get_initial_sched_ctx()->id;
  1052. else
  1053. return sched_ctx;
  1054. }
  1055. void starpu_sched_ctx_notify_hypervisor_exists()
  1056. {
  1057. with_hypervisor = 1;
  1058. int i, j;
  1059. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  1060. {
  1061. hyp_start_sample[i] = starpu_timing_now();
  1062. hyp_start_allow_sample[i] = 0.0;
  1063. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  1064. {
  1065. flops[i][j] = 0.0;
  1066. data_size[i][j] = 0;
  1067. }
  1068. }
  1069. }
  1070. unsigned starpu_sched_ctx_check_if_hypervisor_exists()
  1071. {
  1072. return with_hypervisor;
  1073. }
  1074. unsigned _starpu_sched_ctx_allow_hypervisor(unsigned sched_ctx_id)
  1075. {
  1076. return 1;
  1077. double now = starpu_timing_now();
  1078. if(hyp_start_allow_sample[sched_ctx_id] > 0.0)
  1079. {
  1080. double allow_sample = (now - hyp_start_allow_sample[sched_ctx_id]) / 1000000.0;
  1081. if(allow_sample < 0.001)
  1082. return 1;
  1083. else
  1084. {
  1085. hyp_start_allow_sample[sched_ctx_id] = 0.0;
  1086. hyp_start_sample[sched_ctx_id] = starpu_timing_now();
  1087. return 0;
  1088. }
  1089. }
  1090. double forbid_sample = (now - hyp_start_sample[sched_ctx_id]) / 1000000.0;
  1091. if(forbid_sample > 0.01)
  1092. {
  1093. // hyp_start_sample[sched_ctx_id] = starpu_timing_now();
  1094. hyp_start_allow_sample[sched_ctx_id] = starpu_timing_now();
  1095. return 1;
  1096. }
  1097. return 0;
  1098. }
  1099. unsigned _starpu_get_nsched_ctxs()
  1100. {
  1101. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  1102. return config->topology.nsched_ctxs;
  1103. }
  1104. void starpu_sched_ctx_set_policy_data(unsigned sched_ctx_id, void* policy_data)
  1105. {
  1106. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1107. sched_ctx->policy_data = policy_data;
  1108. }
  1109. void* starpu_sched_ctx_get_policy_data(unsigned sched_ctx_id)
  1110. {
  1111. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1112. return sched_ctx->policy_data;
  1113. }
  1114. struct starpu_worker_collection* starpu_sched_ctx_create_worker_collection(unsigned sched_ctx_id, enum starpu_worker_collection_type worker_collection_type)
  1115. {
  1116. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1117. sched_ctx->workers = (struct starpu_worker_collection*)malloc(sizeof(struct starpu_worker_collection));
  1118. switch(worker_collection_type)
  1119. {
  1120. #ifdef STARPU_HAVE_HWLOC
  1121. case STARPU_WORKER_TREE:
  1122. sched_ctx->workers->has_next = worker_tree.has_next;
  1123. sched_ctx->workers->get_next = worker_tree.get_next;
  1124. sched_ctx->workers->has_next_master = worker_tree.has_next_master;
  1125. sched_ctx->workers->get_next_master = worker_tree.get_next_master;
  1126. sched_ctx->workers->add = worker_tree.add;
  1127. sched_ctx->workers->remove = worker_tree.remove;
  1128. sched_ctx->workers->init = worker_tree.init;
  1129. sched_ctx->workers->deinit = worker_tree.deinit;
  1130. sched_ctx->workers->init_iterator = worker_tree.init_iterator;
  1131. sched_ctx->workers->type = STARPU_WORKER_LIST;
  1132. break;
  1133. #endif
  1134. // case STARPU_WORKER_LIST:
  1135. default:
  1136. sched_ctx->workers->has_next = worker_list.has_next;
  1137. sched_ctx->workers->get_next = worker_list.get_next;
  1138. sched_ctx->workers->has_next_master = worker_list.has_next_master;
  1139. sched_ctx->workers->get_next_master = worker_list.get_next_master;
  1140. sched_ctx->workers->add = worker_list.add;
  1141. sched_ctx->workers->remove = worker_list.remove;
  1142. sched_ctx->workers->init = worker_list.init;
  1143. sched_ctx->workers->deinit = worker_list.deinit;
  1144. sched_ctx->workers->init_iterator = worker_list.init_iterator;
  1145. sched_ctx->workers->type = STARPU_WORKER_LIST;
  1146. break;
  1147. }
  1148. return sched_ctx->workers;
  1149. }
  1150. void starpu_sched_ctx_display_workers(unsigned sched_ctx_id, FILE *f)
  1151. {
  1152. int *workerids = NULL;
  1153. unsigned nworkers;
  1154. unsigned i;
  1155. nworkers = starpu_sched_ctx_get_workers_list(sched_ctx_id, &workerids);
  1156. fprintf(f, "[sched_ctx %d]: %d worker%s\n", sched_ctx_id, nworkers, nworkers>1?"s":"");
  1157. for (i = 0; i < nworkers; i++)
  1158. {
  1159. char name[256];
  1160. starpu_worker_get_name(workerids[i], name, 256);
  1161. fprintf(f, "\t\t%s\n", name);
  1162. }
  1163. free(workerids);
  1164. }
  1165. unsigned starpu_sched_ctx_get_workers_list(unsigned sched_ctx_id, int **workerids)
  1166. {
  1167. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1168. struct starpu_worker_collection *workers = sched_ctx->workers;
  1169. *workerids = (int*)malloc(workers->nworkers*sizeof(int));
  1170. int worker;
  1171. unsigned nworkers = 0;
  1172. struct starpu_sched_ctx_iterator it;
  1173. if(workers->init_iterator)
  1174. workers->init_iterator(workers, &it);
  1175. while(workers->has_next(workers, &it))
  1176. {
  1177. worker = workers->get_next(workers, &it);
  1178. (*workerids)[nworkers++] = worker;
  1179. }
  1180. return nworkers;
  1181. }
  1182. void starpu_sched_ctx_delete_worker_collection(unsigned sched_ctx_id)
  1183. {
  1184. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1185. sched_ctx->workers->deinit(sched_ctx->workers);
  1186. free(sched_ctx->workers);
  1187. }
  1188. struct starpu_worker_collection* starpu_sched_ctx_get_worker_collection(unsigned sched_ctx_id)
  1189. {
  1190. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1191. return sched_ctx->workers;
  1192. }
  1193. int _starpu_get_workers_of_sched_ctx(unsigned sched_ctx_id, int *pus, enum starpu_worker_archtype arch)
  1194. {
  1195. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1196. struct starpu_worker_collection *workers = sched_ctx->workers;
  1197. int worker;
  1198. int npus = 0;
  1199. struct starpu_sched_ctx_iterator it;
  1200. if(workers->init_iterator)
  1201. workers->init_iterator(workers, &it);
  1202. while(workers->has_next(workers, &it))
  1203. {
  1204. worker = workers->get_next(workers, &it);
  1205. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  1206. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  1207. pus[npus++] = worker;
  1208. }
  1209. return npus;
  1210. }
  1211. starpu_pthread_rwlock_t* _starpu_sched_ctx_get_changing_ctx_mutex(unsigned sched_ctx_id)
  1212. {
  1213. return &changing_ctx_mutex[sched_ctx_id];
  1214. }
  1215. unsigned starpu_sched_ctx_get_nworkers(unsigned sched_ctx_id)
  1216. {
  1217. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1218. if(sched_ctx != NULL)
  1219. return sched_ctx->workers->nworkers;
  1220. else
  1221. return 0;
  1222. }
  1223. unsigned starpu_sched_ctx_get_nshared_workers(unsigned sched_ctx_id, unsigned sched_ctx_id2)
  1224. {
  1225. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1226. struct _starpu_sched_ctx *sched_ctx2 = _starpu_get_sched_ctx_struct(sched_ctx_id2);
  1227. struct starpu_worker_collection *workers = sched_ctx->workers;
  1228. struct starpu_worker_collection *workers2 = sched_ctx2->workers;
  1229. int worker, worker2;
  1230. int shared_workers = 0;
  1231. struct starpu_sched_ctx_iterator it1, it2;
  1232. if(workers->init_iterator)
  1233. workers->init_iterator(workers, &it1);
  1234. if(workers2->init_iterator)
  1235. workers2->init_iterator(workers2, &it2);
  1236. while(workers->has_next(workers, &it1))
  1237. {
  1238. worker = workers->get_next(workers, &it1);
  1239. while(workers2->has_next(workers2, &it2))
  1240. {
  1241. worker2 = workers2->get_next(workers2, &it2);
  1242. if(worker == worker2)
  1243. shared_workers++;
  1244. }
  1245. }
  1246. return shared_workers;
  1247. }
  1248. unsigned starpu_sched_ctx_contains_worker(int workerid, unsigned sched_ctx_id)
  1249. {
  1250. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1251. struct starpu_worker_collection *workers = sched_ctx->workers;
  1252. int worker;
  1253. struct starpu_sched_ctx_iterator it;
  1254. if(workers->init_iterator)
  1255. workers->init_iterator(workers, &it);
  1256. while(workers->has_next(workers, &it))
  1257. {
  1258. worker = workers->get_next(workers, &it);
  1259. if(worker == workerid)
  1260. return 1;
  1261. }
  1262. return 0;
  1263. }
  1264. unsigned starpu_sched_ctx_contains_type_of_worker(enum starpu_worker_archtype arch, unsigned sched_ctx_id)
  1265. {
  1266. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  1267. int worker;
  1268. struct starpu_sched_ctx_iterator it;
  1269. if(workers->init_iterator)
  1270. workers->init_iterator(workers, &it);
  1271. while(workers->has_next(workers, &it))
  1272. {
  1273. worker = workers->get_next(workers, &it);
  1274. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  1275. if(curr_arch == arch)
  1276. return 1;
  1277. }
  1278. return 0;
  1279. }
  1280. unsigned _starpu_worker_belongs_to_a_sched_ctx(int workerid, unsigned sched_ctx_id)
  1281. {
  1282. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  1283. int i;
  1284. struct _starpu_sched_ctx *sched_ctx = NULL;
  1285. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  1286. {
  1287. sched_ctx = &config->sched_ctxs[i];
  1288. if(sched_ctx && sched_ctx->id != STARPU_NMAX_SCHED_CTXS && sched_ctx->id != sched_ctx_id)
  1289. if(starpu_sched_ctx_contains_worker(workerid, sched_ctx->id))
  1290. return 1;
  1291. }
  1292. return 0;
  1293. }
  1294. unsigned starpu_sched_ctx_worker_get_id(unsigned sched_ctx_id)
  1295. {
  1296. int workerid = starpu_worker_get_id();
  1297. if(workerid != -1)
  1298. if(starpu_sched_ctx_contains_worker(workerid, sched_ctx_id))
  1299. return workerid;
  1300. return -1;
  1301. }
  1302. unsigned starpu_sched_ctx_overlapping_ctxs_on_worker(int workerid)
  1303. {
  1304. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1305. return worker->nsched_ctxs > 1;
  1306. }
  1307. void starpu_sched_ctx_set_inheritor(unsigned sched_ctx_id, unsigned inheritor)
  1308. {
  1309. STARPU_ASSERT(sched_ctx_id < STARPU_NMAX_SCHED_CTXS);
  1310. STARPU_ASSERT(inheritor < STARPU_NMAX_SCHED_CTXS);
  1311. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1312. sched_ctx->inheritor = inheritor;
  1313. return;
  1314. }
  1315. unsigned starpu_sched_ctx_get_inheritor(unsigned sched_ctx_id)
  1316. {
  1317. STARPU_ASSERT(sched_ctx_id < STARPU_NMAX_SCHED_CTXS);
  1318. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1319. return sched_ctx->inheritor;
  1320. }
  1321. unsigned starpu_sched_ctx_get_hierarchy_level(unsigned sched_ctx_id)
  1322. {
  1323. STARPU_ASSERT(sched_ctx_id < STARPU_NMAX_SCHED_CTXS);
  1324. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1325. return sched_ctx->hierarchy_level;
  1326. }
  1327. void starpu_sched_ctx_finished_submit(unsigned sched_ctx_id)
  1328. {
  1329. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1330. STARPU_PTHREAD_MUTEX_LOCK(&finished_submit_mutex);
  1331. sched_ctx->finished_submit = 1;
  1332. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  1333. return;
  1334. }
  1335. #ifdef STARPU_USE_SC_HYPERVISOR
  1336. void _starpu_sched_ctx_post_exec_task_cb(int workerid, struct starpu_task *task, size_t data_size2, uint32_t footprint)
  1337. {
  1338. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  1339. if(sched_ctx != NULL && task->sched_ctx != _starpu_get_initial_sched_ctx()->id &&
  1340. task->sched_ctx != STARPU_NMAX_SCHED_CTXS && sched_ctx->perf_counters != NULL)
  1341. {
  1342. flops[task->sched_ctx][workerid] += task->flops;
  1343. data_size[task->sched_ctx][workerid] += data_size2;
  1344. if(_starpu_sched_ctx_allow_hypervisor(sched_ctx->id) || task->hypervisor_tag > 0)
  1345. {
  1346. _STARPU_TRACE_HYPERVISOR_BEGIN();
  1347. sched_ctx->perf_counters->notify_post_exec_task(task, data_size[task->sched_ctx][workerid], footprint,
  1348. task->hypervisor_tag, flops[task->sched_ctx][workerid]);
  1349. _STARPU_TRACE_HYPERVISOR_END();
  1350. flops[task->sched_ctx][workerid] = 0.0;
  1351. data_size[task->sched_ctx][workerid] = 0;
  1352. }
  1353. }
  1354. }
  1355. void starpu_sched_ctx_call_pushed_task_cb(int workerid, unsigned sched_ctx_id)
  1356. {
  1357. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1358. if(sched_ctx != NULL && sched_ctx_id != _starpu_get_initial_sched_ctx()->id && sched_ctx_id != STARPU_NMAX_SCHED_CTXS
  1359. && sched_ctx->perf_counters != NULL && _starpu_sched_ctx_allow_hypervisor(sched_ctx_id))
  1360. {
  1361. _STARPU_TRACE_HYPERVISOR_BEGIN();
  1362. sched_ctx->perf_counters->notify_pushed_task(sched_ctx_id, workerid);
  1363. _STARPU_TRACE_HYPERVISOR_END();
  1364. }
  1365. }
  1366. #endif //STARPU_USE_SC_HYPERVISOR
  1367. int starpu_sched_get_min_priority(void)
  1368. {
  1369. return starpu_sched_ctx_get_min_priority(_starpu_sched_ctx_get_current_context());
  1370. }
  1371. int starpu_sched_get_max_priority(void)
  1372. {
  1373. return starpu_sched_ctx_get_max_priority(_starpu_sched_ctx_get_current_context());
  1374. }
  1375. int starpu_sched_set_min_priority(int min_prio)
  1376. {
  1377. return starpu_sched_ctx_set_min_priority(_starpu_sched_ctx_get_current_context(), min_prio);
  1378. }
  1379. int starpu_sched_set_max_priority(int max_prio)
  1380. {
  1381. return starpu_sched_ctx_set_max_priority(_starpu_sched_ctx_get_current_context(), max_prio);
  1382. }
  1383. int starpu_sched_ctx_get_min_priority(unsigned sched_ctx_id)
  1384. {
  1385. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1386. return sched_ctx->min_priority;
  1387. }
  1388. int starpu_sched_ctx_get_max_priority(unsigned sched_ctx_id)
  1389. {
  1390. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1391. return sched_ctx->max_priority;
  1392. }
  1393. int starpu_sched_ctx_set_min_priority(unsigned sched_ctx_id, int min_prio)
  1394. {
  1395. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1396. sched_ctx->min_priority = min_prio;
  1397. return 0;
  1398. }
  1399. int starpu_sched_ctx_set_max_priority(unsigned sched_ctx_id, int max_prio)
  1400. {
  1401. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1402. sched_ctx->max_priority = max_prio;
  1403. return 0;
  1404. }
  1405. int starpu_sched_ctx_min_priority_is_set(unsigned sched_ctx_id)
  1406. {
  1407. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1408. return sched_ctx->min_priority_is_set;
  1409. }
  1410. int starpu_sched_ctx_max_priority_is_set(unsigned sched_ctx_id)
  1411. {
  1412. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1413. return sched_ctx->max_priority_is_set;
  1414. }
  1415. void starpu_sched_ctx_set_priority(int *workers, int nworkers, unsigned sched_ctx_id, unsigned priority)
  1416. {
  1417. if(nworkers != -1)
  1418. {
  1419. int w;
  1420. struct _starpu_worker *worker = NULL;
  1421. for(w = 0; w < nworkers; w++)
  1422. {
  1423. worker = _starpu_get_worker_struct(workers[w]);
  1424. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  1425. struct _starpu_sched_ctx_list *l = NULL;
  1426. for (l = worker->sched_ctx_list; l; l = l->next)
  1427. {
  1428. if(l->sched_ctx == sched_ctx_id)
  1429. {
  1430. l->priority = priority;
  1431. break;
  1432. }
  1433. }
  1434. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  1435. }
  1436. }
  1437. return;
  1438. }
  1439. unsigned starpu_sched_ctx_get_priority(int workerid, unsigned sched_ctx_id)
  1440. {
  1441. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1442. struct _starpu_sched_ctx_list *l = NULL;
  1443. for (l = worker->sched_ctx_list; l; l = l->next)
  1444. {
  1445. if(l->sched_ctx == sched_ctx_id)
  1446. {
  1447. return l->priority;
  1448. }
  1449. }
  1450. return 1;
  1451. }
  1452. unsigned _starpu_sched_ctx_last_worker_awake(struct _starpu_worker *worker)
  1453. {
  1454. struct _starpu_sched_ctx_list *l = NULL;
  1455. for (l = worker->sched_ctx_list; l; l = l->next)
  1456. {
  1457. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(l->sched_ctx);
  1458. unsigned last_worker_awake = 1;
  1459. struct starpu_worker_collection *workers = sched_ctx->workers;
  1460. struct starpu_sched_ctx_iterator it;
  1461. int workerid;
  1462. if(workers->init_iterator)
  1463. workers->init_iterator(workers, &it);
  1464. while(workers->has_next(workers, &it))
  1465. {
  1466. workerid = workers->get_next(workers, &it);
  1467. if(workerid != worker->workerid && _starpu_worker_get_status(workerid) != STATUS_SLEEPING)
  1468. {
  1469. last_worker_awake = 0;
  1470. break;
  1471. }
  1472. }
  1473. if(last_worker_awake)
  1474. return 1;
  1475. }
  1476. return 0;
  1477. }
  1478. void starpu_sched_ctx_bind_current_thread_to_cpuid(unsigned cpuid STARPU_ATTRIBUTE_UNUSED)
  1479. {
  1480. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1481. /* FIXME: why not factorize with _starpu_bind_thread_on_cpu? */
  1482. #ifdef STARPU_SIMGRID
  1483. return;
  1484. #else
  1485. if (starpu_get_env_number("STARPU_WORKERS_NOBIND") > 0)
  1486. return;
  1487. #ifdef STARPU_HAVE_HWLOC
  1488. const struct hwloc_topology_support *support = hwloc_topology_get_support (config->topology.hwtopology);
  1489. if (support->cpubind->set_thisthread_cpubind)
  1490. {
  1491. hwloc_obj_t obj = hwloc_get_obj_by_depth (config->topology.hwtopology,
  1492. config->pu_depth, cpuid);
  1493. hwloc_bitmap_t set = obj->cpuset;
  1494. int ret;
  1495. hwloc_bitmap_singlify(set);
  1496. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1497. HWLOC_CPUBIND_THREAD);
  1498. if (ret)
  1499. {
  1500. perror("hwloc_set_cpubind");
  1501. STARPU_ABORT();
  1502. }
  1503. }
  1504. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  1505. int ret;
  1506. /* fix the thread on the correct cpu */
  1507. cpu_set_t aff_mask;
  1508. CPU_ZERO(&aff_mask);
  1509. CPU_SET(cpuid, &aff_mask);
  1510. starpu_pthread_t self = pthread_self();
  1511. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  1512. if (ret)
  1513. {
  1514. perror("binding thread");
  1515. STARPU_ABORT();
  1516. }
  1517. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  1518. DWORD mask = 1 << cpuid;
  1519. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  1520. {
  1521. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  1522. }
  1523. #else
  1524. #warning no CPU binding support
  1525. #endif
  1526. #endif
  1527. }
  1528. unsigned starpu_sched_ctx_worker_is_master_for_child_ctx(int workerid, unsigned sched_ctx_id)
  1529. {
  1530. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1531. struct _starpu_sched_ctx_list *l = NULL;
  1532. struct _starpu_sched_ctx *sched_ctx = NULL;
  1533. for (l = worker->sched_ctx_list; l; l = l->next)
  1534. {
  1535. sched_ctx = _starpu_get_sched_ctx_struct(l->sched_ctx);
  1536. if(sched_ctx-> main_master == workerid && sched_ctx->nesting_sched_ctx == sched_ctx_id)
  1537. return sched_ctx->id;
  1538. }
  1539. return STARPU_NMAX_SCHED_CTXS;
  1540. }
  1541. /* if the worker is the master of a parallel context, and the job is meant to be executed on this parallel context, return a pointer to the context */
  1542. struct _starpu_sched_ctx *_starpu_sched_ctx_get_sched_ctx_for_worker_and_job(struct _starpu_worker *worker, struct _starpu_job *j)
  1543. {
  1544. struct _starpu_sched_ctx_list *l = NULL;
  1545. for (l = worker->sched_ctx_list; l; l = l->next)
  1546. {
  1547. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(l->sched_ctx);
  1548. if (sched_ctx->main_master == worker->workerid && j->task->sched_ctx == sched_ctx->id)
  1549. return sched_ctx;
  1550. }
  1551. return NULL;
  1552. }
  1553. void starpu_sched_ctx_revert_task_counters(unsigned sched_ctx_id, double flops)
  1554. {
  1555. _starpu_decrement_nsubmitted_tasks_of_sched_ctx(sched_ctx_id);
  1556. _starpu_decrement_nready_tasks_of_sched_ctx(sched_ctx_id, flops);
  1557. }
  1558. void starpu_sched_ctx_move_task_to_ctx(struct starpu_task *task, unsigned sched_ctx)
  1559. {
  1560. int workerid = starpu_worker_get_id();
  1561. struct _starpu_worker *worker = NULL;
  1562. if(workerid != -1)
  1563. {
  1564. worker = _starpu_get_worker_struct(workerid);
  1565. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  1566. }
  1567. task->sched_ctx = sched_ctx;
  1568. _starpu_task_submit_nodeps(task);
  1569. if(workerid != -1)
  1570. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  1571. }
  1572. static unsigned _worker_sleeping_in_other_ctx(unsigned sched_ctx_id, int workerid)
  1573. {
  1574. int s;
  1575. for(s = 0; s < STARPU_NMAX_SCHED_CTXS; s++)
  1576. {
  1577. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(s);
  1578. if(sched_ctx && sched_ctx->id > 0 && sched_ctx->id < STARPU_NMAX_SCHED_CTXS && sched_ctx->id != sched_ctx_id)
  1579. {
  1580. if(sched_ctx->parallel_sect[workerid])
  1581. return 1;
  1582. }
  1583. }
  1584. return 0;
  1585. }
  1586. static void _starpu_sched_ctx_get_workers_to_sleep(unsigned sched_ctx_id, int *workerids, int nworkers, int master)
  1587. {
  1588. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1589. int current_worker_id = starpu_worker_get_id();
  1590. unsigned sleeping[nworkers];
  1591. int w;
  1592. for(w = 0; w < nworkers; w++)
  1593. {
  1594. if(current_worker_id == -1 || workerids[w] != current_worker_id)
  1595. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->parallel_sect_mutex[workerids[w]]);
  1596. sleeping[w] = _worker_sleeping_in_other_ctx(sched_ctx_id, workerids[w]);
  1597. sched_ctx->master[workerids[w]] = master;
  1598. sched_ctx->parallel_sect[workerids[w]] = 1;
  1599. if(current_worker_id == -1 || workerids[w] != current_worker_id)
  1600. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->parallel_sect_mutex[workerids[w]]);
  1601. }
  1602. int workerid;
  1603. for(w = 0; w < nworkers; w++)
  1604. {
  1605. workerid = workerids[w];
  1606. if((current_worker_id == -1 || workerid != current_worker_id) && !sleeping[w])
  1607. {
  1608. sem_wait(&sched_ctx->fall_asleep_sem[master]);
  1609. }
  1610. }
  1611. return;
  1612. }
  1613. void _starpu_sched_ctx_signal_worker_blocked(unsigned sched_ctx_id, int workerid)
  1614. {
  1615. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1616. worker->slave = 1;
  1617. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1618. sched_ctx->sleeping[workerid] = 1;
  1619. int master = sched_ctx->master[workerid];
  1620. sem_post(&sched_ctx->fall_asleep_sem[master]);
  1621. return;
  1622. }
  1623. void _starpu_sched_ctx_signal_worker_woke_up(unsigned sched_ctx_id, int workerid)
  1624. {
  1625. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1626. int master = sched_ctx->master[workerid];
  1627. sem_post(&sched_ctx->wake_up_sem[master]);
  1628. sched_ctx->sleeping[workerid] = 0;
  1629. sched_ctx->master[workerid] = -1;
  1630. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1631. worker->slave = 0;
  1632. return;
  1633. }
  1634. static void _starpu_sched_ctx_wake_up_workers(unsigned sched_ctx_id, int master)
  1635. {
  1636. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1637. int current_worker_id = starpu_worker_get_id();
  1638. struct starpu_worker_collection *workers = sched_ctx->workers;
  1639. struct starpu_sched_ctx_iterator it;
  1640. if(workers->init_iterator)
  1641. workers->init_iterator(workers, &it);
  1642. while(workers->has_next(workers, &it))
  1643. {
  1644. int workerid = workers->get_next(workers, &it);
  1645. int curr_master = sched_ctx->master[workerid];
  1646. if(curr_master == master && sched_ctx->parallel_sect[workerid])
  1647. {
  1648. if((current_worker_id == -1 || workerid != current_worker_id) && sched_ctx->sleeping[workerid])
  1649. {
  1650. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->parallel_sect_mutex[workerid]);
  1651. STARPU_PTHREAD_COND_SIGNAL(&sched_ctx->parallel_sect_cond[workerid]);
  1652. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->parallel_sect_mutex[workerid]);
  1653. sem_wait(&sched_ctx->wake_up_sem[master]);
  1654. }
  1655. else
  1656. sched_ctx->parallel_sect[workerid] = 0;
  1657. }
  1658. }
  1659. return;
  1660. }
  1661. void* starpu_sched_ctx_exec_parallel_code(void* (*func)(void*), void* param, unsigned sched_ctx_id)
  1662. {
  1663. int *workerids;
  1664. int nworkers = starpu_sched_ctx_get_workers_list(sched_ctx_id, &workerids);
  1665. _starpu_sched_ctx_get_workers_to_sleep(sched_ctx_id, workerids, nworkers, workerids[nworkers-1]);
  1666. /* execute parallel code */
  1667. void* ret = func(param);
  1668. /* wake up starpu workers */
  1669. _starpu_sched_ctx_wake_up_workers(sched_ctx_id, workerids[nworkers-1]);
  1670. return ret;
  1671. }
  1672. void starpu_sched_ctx_get_available_cpuids(unsigned sched_ctx_id, int **cpuids, int *ncpuids)
  1673. {
  1674. int current_worker_id = starpu_worker_get_id();
  1675. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1676. struct starpu_worker_collection *workers = sched_ctx->workers;
  1677. (*cpuids) = (int*)malloc(workers->nworkers*sizeof(int));
  1678. int w = 0;
  1679. struct _starpu_worker *worker = NULL;
  1680. struct starpu_sched_ctx_iterator it;
  1681. int workerid;
  1682. if(workers->init_iterator)
  1683. workers->init_iterator(workers, &it);
  1684. while(workers->has_next(workers, &it))
  1685. {
  1686. workerid = workers->get_next(workers, &it);
  1687. int master = sched_ctx->master[workerid];
  1688. if(master == current_worker_id || workerid == current_worker_id || current_worker_id == -1)
  1689. {
  1690. (*cpuids)[w++] = starpu_worker_get_bindid(workerid);
  1691. }
  1692. }
  1693. *ncpuids = w;
  1694. return;
  1695. }
  1696. static void _starpu_sched_ctx_wake_these_workers_up(unsigned sched_ctx_id, int *workerids, int nworkers)
  1697. {
  1698. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1699. int current_worker_id = starpu_worker_get_id();
  1700. int masters[nworkers];
  1701. int w;
  1702. struct _starpu_worker *worker = NULL;
  1703. for(w = 0; w < nworkers; w++)
  1704. {
  1705. int workerid = workerids[w];
  1706. masters[w] = sched_ctx->master[workerid];
  1707. if(current_worker_id == -1 || workerid != current_worker_id)
  1708. {
  1709. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->parallel_sect_mutex[workerid]);
  1710. STARPU_PTHREAD_COND_SIGNAL(&sched_ctx->parallel_sect_cond[workerid]);
  1711. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->parallel_sect_mutex[workerid]);
  1712. }
  1713. else
  1714. sched_ctx->parallel_sect[workerid] = 0;
  1715. sched_ctx->master[workerid] = -1;
  1716. }
  1717. int workerid;
  1718. for(w = 0; w < nworkers; w++)
  1719. {
  1720. workerid = workerids[w];
  1721. if(masters[w] != -1)
  1722. {
  1723. int master = sched_ctx->master[workerid];
  1724. if(current_worker_id == -1 || workerid != current_worker_id)
  1725. sem_wait(&sched_ctx->wake_up_sem[master]);
  1726. }
  1727. }
  1728. return;
  1729. }
  1730. static int _starpu_sched_ctx_find_master(unsigned sched_ctx_id, int *workerids, int nworkers)
  1731. {
  1732. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1733. int new_master = workerids[nworkers-1];
  1734. int current_worker_id = starpu_worker_get_id();
  1735. int current_is_in_section = 0;
  1736. int npotential_masters = 0;
  1737. int nawake_workers = 0;
  1738. int ntrue_masters = 0;
  1739. int potential_masters[nworkers];
  1740. int awake_workers[nworkers];
  1741. int true_masters[nworkers];
  1742. int i,w;
  1743. for(w = 0 ; w < nworkers ; w++)
  1744. {
  1745. if (current_worker_id == workerids[w])
  1746. current_is_in_section = 1;
  1747. int master = sched_ctx->master[workerids[w]];
  1748. if (master > -1)
  1749. {
  1750. int already_seen = 0;
  1751. //Could create a function for this. Basically searching an element in an array.
  1752. for (i = 0 ; i < npotential_masters; i++)
  1753. {
  1754. if (potential_masters[i] == master)
  1755. {
  1756. already_seen = 1;
  1757. break;
  1758. }
  1759. }
  1760. if (!already_seen)
  1761. potential_masters[npotential_masters++] = master;
  1762. }
  1763. else if (master == -1)
  1764. awake_workers[nawake_workers++] = workerids[w];
  1765. }
  1766. for (i = 0 ; i < npotential_masters ; i++) {
  1767. int master_is_in_section = 0;
  1768. //Could create a function for this. Basically searching an element in an array.
  1769. for (w = 0 ; w < nworkers ; w++)
  1770. {
  1771. if (workerids[w] == potential_masters[i])
  1772. {
  1773. master_is_in_section = 1;
  1774. break;
  1775. }
  1776. }
  1777. if (master_is_in_section)
  1778. true_masters[ntrue_masters++] = potential_masters[i];
  1779. }
  1780. if (current_is_in_section)
  1781. new_master = current_worker_id;
  1782. else
  1783. {
  1784. if (ntrue_masters > 1)
  1785. {
  1786. if (nawake_workers > 0)
  1787. new_master = awake_workers[nawake_workers - 1];
  1788. else
  1789. new_master = true_masters[ntrue_masters - 1];
  1790. }
  1791. }
  1792. return new_master;
  1793. }
  1794. static void _starpu_sched_ctx_add_workers_to_master(unsigned sched_ctx_id, int *workerids, int nworkers, int new_master)
  1795. {
  1796. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1797. int w;
  1798. int nput_to_sleep = 0;
  1799. int nwake_up = 0;
  1800. int put_to_sleep[nworkers];
  1801. int wake_up[nworkers];
  1802. for(w = 0 ; w < nworkers ; w++)
  1803. {
  1804. int master = sched_ctx->master[workerids[w]];
  1805. if (master == -1 && workerids[w] != new_master)
  1806. put_to_sleep[nput_to_sleep++] = workerids[w];
  1807. else if(master != -1 && workerids[w] == new_master)
  1808. wake_up[nwake_up++] = workerids[w];
  1809. }
  1810. if(nwake_up > 0)
  1811. _starpu_sched_ctx_wake_these_workers_up(sched_ctx_id, wake_up, nwake_up);
  1812. if(nput_to_sleep > 0)
  1813. _starpu_sched_ctx_get_workers_to_sleep(sched_ctx_id, put_to_sleep, nput_to_sleep, new_master);
  1814. }
  1815. int starpu_sched_ctx_book_workers_for_task(unsigned sched_ctx_id, int *workerids, int nworkers)
  1816. {
  1817. int new_master = _starpu_sched_ctx_find_master(sched_ctx_id, workerids, nworkers);
  1818. _starpu_sched_ctx_add_workers_to_master(sched_ctx_id, workerids, nworkers, new_master);
  1819. return new_master;
  1820. }
  1821. void starpu_sched_ctx_unbook_workers_for_task(unsigned sched_ctx_id, int master)
  1822. {
  1823. /* wake up starpu workers */
  1824. _starpu_sched_ctx_wake_up_workers(sched_ctx_id, master);
  1825. }
  1826. struct starpu_perfmodel_arch * _starpu_sched_ctx_get_perf_archtype(unsigned sched_ctx_id)
  1827. {
  1828. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1829. return &sched_ctx->perf_arch;
  1830. }