sched_ctx.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. extern struct starpu_worker_collection worker_list;
  22. static starpu_pthread_mutex_t sched_ctx_manag = STARPU_PTHREAD_MUTEX_INITIALIZER;
  23. static starpu_pthread_mutex_t finished_submit_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  24. struct starpu_task stop_submission_task = STARPU_TASK_INITIALIZER;
  25. starpu_pthread_key_t sched_ctx_key;
  26. unsigned with_hypervisor = 0;
  27. static unsigned _starpu_get_first_free_sched_ctx(struct _starpu_machine_config *config);
  28. static void _starpu_worker_gets_into_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker)
  29. {
  30. unsigned ret_sched_ctx = _starpu_sched_ctx_list_get_sched_ctx(worker->sched_ctx_list, sched_ctx_id);
  31. /* the worker was planning to go away in another ctx but finally he changed his mind &
  32. he's staying */
  33. if (ret_sched_ctx == STARPU_NMAX_SCHED_CTXS)
  34. {
  35. /* add context to worker */
  36. _starpu_sched_ctx_list_add(&worker->sched_ctx_list, sched_ctx_id);
  37. worker->nsched_ctxs++;
  38. }
  39. worker->removed_from_ctx[sched_ctx_id] = 0;
  40. return;
  41. }
  42. void _starpu_worker_gets_out_of_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker)
  43. {
  44. unsigned ret_sched_ctx = _starpu_sched_ctx_list_get_sched_ctx(worker->sched_ctx_list, sched_ctx_id);
  45. /* remove context from worker */
  46. if(ret_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  47. {
  48. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  49. if(sched_ctx && sched_ctx->sched_policy && sched_ctx->sched_policy->remove_workers)
  50. sched_ctx->sched_policy->remove_workers(sched_ctx_id, &worker->workerid, 1);
  51. _starpu_sched_ctx_list_remove(&worker->sched_ctx_list, sched_ctx_id);
  52. worker->nsched_ctxs--;
  53. }
  54. return;
  55. }
  56. static void _starpu_update_workers_with_ctx(int *workerids, int nworkers, int sched_ctx_id)
  57. {
  58. int i;
  59. struct _starpu_worker *worker = NULL;
  60. struct _starpu_worker *curr_worker = _starpu_get_local_worker_key();
  61. for(i = 0; i < nworkers; i++)
  62. {
  63. worker = _starpu_get_worker_struct(workerids[i]);
  64. /* if the current thread requires resize it's no need
  65. to lock it in order to change its sched_ctx info */
  66. if(curr_worker && curr_worker == worker)
  67. _starpu_worker_gets_into_ctx(sched_ctx_id, worker);
  68. else
  69. {
  70. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  71. _starpu_worker_gets_into_ctx(sched_ctx_id, worker);
  72. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  73. }
  74. }
  75. return;
  76. }
  77. static void _starpu_update_workers_without_ctx(int *workerids, int nworkers, int sched_ctx_id, unsigned now)
  78. {
  79. int i;
  80. struct _starpu_worker *worker = NULL;
  81. struct _starpu_worker *curr_worker = _starpu_get_local_worker_key();
  82. for(i = 0; i < nworkers; i++)
  83. {
  84. worker = _starpu_get_worker_struct(workerids[i]);
  85. if(now)
  86. {
  87. if(curr_worker && curr_worker == worker)
  88. _starpu_worker_gets_out_of_ctx(sched_ctx_id, worker);
  89. else
  90. {
  91. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  92. _starpu_worker_gets_out_of_ctx(sched_ctx_id, worker);
  93. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  94. }
  95. }
  96. else
  97. {
  98. if(curr_worker && curr_worker == worker)
  99. worker->removed_from_ctx[sched_ctx_id] = 1;
  100. else
  101. {
  102. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  103. worker->removed_from_ctx[sched_ctx_id] = 1;
  104. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  105. }
  106. }
  107. }
  108. return;
  109. }
  110. void starpu_sched_ctx_stop_task_submission()
  111. {
  112. _starpu_exclude_task_from_dag(&stop_submission_task);
  113. _starpu_task_submit_internally(&stop_submission_task);
  114. }
  115. void starpu_sched_ctx_worker_shares_tasks_lists(int workerid, int sched_ctx_id)
  116. {
  117. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  118. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  119. /* if is the initial sched_ctx no point in taking the mutex, the workers are
  120. not launched yet */
  121. if(!sched_ctx->is_initial_sched)
  122. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  123. worker->shares_tasks_lists[sched_ctx_id] = 1;
  124. if(!sched_ctx->is_initial_sched)
  125. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  126. }
  127. static void _starpu_add_workers_to_sched_ctx(struct _starpu_sched_ctx *sched_ctx, int *workerids, int nworkers,
  128. int *added_workers, int *n_added_workers)
  129. {
  130. struct starpu_worker_collection *workers = sched_ctx->workers;
  131. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  132. int nworkers_to_add = nworkers == -1 ? (int)config->topology.nworkers : nworkers;
  133. int workers_to_add[nworkers_to_add];
  134. int i = 0;
  135. for(i = 0; i < nworkers_to_add; i++)
  136. {
  137. /* added_workers is NULL for the call of this func at the creation of the context*/
  138. /* if the function is called at the creation of the context it's no need to do this verif */
  139. if(added_workers)
  140. {
  141. int worker = workers->add(workers, (workerids == NULL ? i : workerids[i]));
  142. if(worker >= 0)
  143. added_workers[(*n_added_workers)++] = worker;
  144. else
  145. {
  146. struct _starpu_worker *worker_str = _starpu_get_worker_struct(workerids[i]);
  147. STARPU_PTHREAD_MUTEX_LOCK(&worker_str->sched_mutex);
  148. worker_str->removed_from_ctx[sched_ctx->id] = 0;
  149. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_str->sched_mutex);
  150. }
  151. }
  152. else
  153. {
  154. int worker = (workerids == NULL ? i : workerids[i]);
  155. workers->add(workers, worker);
  156. workers_to_add[i] = worker;
  157. }
  158. }
  159. if(sched_ctx->sched_policy->add_workers)
  160. {
  161. if(added_workers)
  162. {
  163. if(*n_added_workers > 0)
  164. sched_ctx->sched_policy->add_workers(sched_ctx->id, added_workers, *n_added_workers);
  165. }
  166. else
  167. sched_ctx->sched_policy->add_workers(sched_ctx->id, workers_to_add, nworkers_to_add);
  168. }
  169. return;
  170. }
  171. static void _starpu_remove_workers_from_sched_ctx(struct _starpu_sched_ctx *sched_ctx, int *workerids,
  172. int nworkers, int *removed_workers, int *n_removed_workers)
  173. {
  174. struct starpu_worker_collection *workers = sched_ctx->workers;
  175. int i = 0;
  176. for(i = 0; i < nworkers; i++)
  177. {
  178. if(workers->nworkers > 0)
  179. {
  180. if(_starpu_worker_belongs_to_a_sched_ctx(workerids[i], sched_ctx->id))
  181. {
  182. int worker = workers->remove(workers, workerids[i]);
  183. if(worker >= 0)
  184. removed_workers[(*n_removed_workers)++] = worker;
  185. }
  186. }
  187. }
  188. return;
  189. }
  190. static void _starpu_sched_ctx_free_scheduling_data(struct _starpu_sched_ctx *sched_ctx)
  191. {
  192. int *workerids = NULL;
  193. unsigned nworkers_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  194. if(nworkers_ctx > 0 && sched_ctx->sched_policy->remove_workers)
  195. sched_ctx->sched_policy->remove_workers(sched_ctx->id, workerids, nworkers_ctx);
  196. free(workerids);
  197. return;
  198. }
  199. #ifdef STARPU_HAVE_HWLOC
  200. static void _starpu_sched_ctx_create_hwloc_tree(struct _starpu_sched_ctx *sched_ctx)
  201. {
  202. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  203. sched_ctx->hwloc_workers_set = hwloc_bitmap_alloc();
  204. struct starpu_worker_collection *workers = sched_ctx->workers;
  205. int worker;
  206. struct starpu_sched_ctx_iterator it;
  207. if(workers->init_iterator)
  208. workers->init_iterator(workers, &it);
  209. while(workers->has_next(workers, &it))
  210. {
  211. worker = workers->get_next(workers, &it);
  212. if(!starpu_worker_is_combined_worker(worker))
  213. {
  214. hwloc_bitmap_or(sched_ctx->hwloc_workers_set,
  215. sched_ctx->hwloc_workers_set,
  216. config->workers[worker].hwloc_cpu_set);
  217. }
  218. }
  219. return;
  220. }
  221. #endif
  222. struct _starpu_sched_ctx* _starpu_create_sched_ctx(struct starpu_sched_policy *policy, int *workerids,
  223. int nworkers_ctx, unsigned is_initial_sched,
  224. const char *sched_ctx_name,
  225. int min_prio_set, int min_prio,
  226. int max_prio_set, int max_prio)
  227. {
  228. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  229. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  230. STARPU_ASSERT(config->topology.nsched_ctxs < STARPU_NMAX_SCHED_CTXS);
  231. unsigned id = _starpu_get_first_free_sched_ctx(config);
  232. struct _starpu_sched_ctx *sched_ctx = &config->sched_ctxs[id];
  233. sched_ctx->id = id;
  234. config->topology.nsched_ctxs++;
  235. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  236. int nworkers = config->topology.nworkers;
  237. STARPU_ASSERT(nworkers_ctx <= nworkers);
  238. STARPU_PTHREAD_MUTEX_INIT(&sched_ctx->empty_ctx_mutex, NULL);
  239. starpu_task_list_init(&sched_ctx->empty_ctx_tasks);
  240. sched_ctx->sched_policy = (struct starpu_sched_policy*)malloc(sizeof(struct starpu_sched_policy));
  241. sched_ctx->is_initial_sched = is_initial_sched;
  242. sched_ctx->name = sched_ctx_name;
  243. sched_ctx->inheritor = STARPU_NMAX_SCHED_CTXS;
  244. sched_ctx->finished_submit = 0;
  245. sched_ctx->min_priority_is_set = min_prio_set;
  246. if (sched_ctx->min_priority_is_set) sched_ctx->min_priority = min_prio;
  247. sched_ctx->max_priority_is_set = max_prio_set;
  248. if (sched_ctx->max_priority_is_set) sched_ctx->max_priority = max_prio;
  249. sem_init(&sched_ctx->parallel_code_sem, 0, 0);
  250. _starpu_barrier_counter_init(&sched_ctx->tasks_barrier, 0);
  251. /*init the strategy structs and the worker_collection of the ressources of the context */
  252. _starpu_init_sched_policy(config, sched_ctx, policy);
  253. /* construct the collection of workers(list/tree/etc.) */
  254. sched_ctx->workers->init(sched_ctx->workers);
  255. /* after having an worker_collection on the ressources add them */
  256. _starpu_add_workers_to_sched_ctx(sched_ctx, workerids, nworkers_ctx, NULL, NULL);
  257. #ifdef STARPU_HAVE_HWLOC
  258. /* build hwloc tree of the context */
  259. _starpu_sched_ctx_create_hwloc_tree(sched_ctx);
  260. #endif //STARPU_HAVE_HWLOC
  261. /* if we create the initial big sched ctx we can update workers' status here
  262. because they haven't been launched yet */
  263. if(is_initial_sched)
  264. {
  265. int i;
  266. /*initialize the mutexes for all contexts */
  267. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  268. STARPU_PTHREAD_RWLOCK_INIT(&changing_ctx_mutex[i], NULL);
  269. for(i = 0; i < nworkers; i++)
  270. {
  271. struct _starpu_worker *worker = _starpu_get_worker_struct(i);
  272. worker->sched_ctx_list = (struct _starpu_sched_ctx_list*)malloc(sizeof(struct _starpu_sched_ctx_list));
  273. _starpu_sched_ctx_list_init(worker->sched_ctx_list);
  274. _starpu_sched_ctx_list_add(&worker->sched_ctx_list, sched_ctx->id);
  275. worker->nsched_ctxs++;
  276. }
  277. }
  278. int w;
  279. for(w = 0; w < STARPU_NMAXWORKERS; w++)
  280. {
  281. sched_ctx->pop_counter[w] = 0;
  282. }
  283. return sched_ctx;
  284. }
  285. static void _get_workers(int min, int max, int *workers, int *nw, enum starpu_worker_archtype arch, unsigned allow_overlap)
  286. {
  287. int pus[max];
  288. int npus = 0;
  289. int i;
  290. int n = 0;
  291. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  292. if(config->topology.nsched_ctxs == 1)
  293. {
  294. /*we have all available resources */
  295. npus = starpu_worker_get_nids_by_type(arch, pus, max);
  296. /*TODO: hierarchical ctxs: get max good workers: close one to another */
  297. for(i = 0; i < npus; i++)
  298. workers[(*nw)++] = pus[i];
  299. }
  300. else
  301. {
  302. unsigned enough_ressources = 0;
  303. npus = starpu_worker_get_nids_ctx_free_by_type(arch, pus, max);
  304. for(i = 0; i < npus; i++)
  305. workers[(*nw)++] = pus[i];
  306. if(npus == max)
  307. /*we have enough available resources */
  308. enough_ressources = 1;
  309. if(!enough_ressources && npus >= min)
  310. /*we have enough available resources */
  311. enough_ressources = 1;
  312. if(!enough_ressources)
  313. {
  314. /* try to get ressources from ctx who have more than the min of workers they need */
  315. int s;
  316. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  317. {
  318. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  319. {
  320. int _npus = 0;
  321. int _pus[STARPU_NMAXWORKERS];
  322. _npus = starpu_get_workers_of_sched_ctx(config->sched_ctxs[s].id, _pus, arch);
  323. int ctx_min = arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  324. if(_npus > ctx_min)
  325. {
  326. if(npus < min)
  327. {
  328. n = (_npus - ctx_min) > (min - npus) ? min - npus : (_npus - ctx_min);
  329. npus += n;
  330. }
  331. /*TODO: hierarchical ctxs: get n good workers: close to the other ones I already assigned to the ctx */
  332. for(i = 0; i < n; i++)
  333. workers[(*nw)++] = _pus[i];
  334. starpu_sched_ctx_remove_workers(_pus, n, config->sched_ctxs[s].id);
  335. }
  336. }
  337. }
  338. if(npus >= min)
  339. enough_ressources = 1;
  340. }
  341. if(!enough_ressources)
  342. {
  343. /* if there is no available workers to satisfy the minimum required
  344. give them workers proportional to their requirements*/
  345. int global_npus = starpu_worker_get_count_by_type(arch);
  346. int req_npus = 0;
  347. int s;
  348. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  349. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  350. req_npus += arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  351. req_npus += min;
  352. for(s = 1; s < STARPU_NMAX_SCHED_CTXS; s++)
  353. {
  354. if(config->sched_ctxs[s].id != STARPU_NMAX_SCHED_CTXS)
  355. {
  356. int ctx_min = arch == STARPU_CPU_WORKER ? config->sched_ctxs[s].min_ncpus : config->sched_ctxs[s].min_ngpus;
  357. double needed_npus = ((double)ctx_min * (double)global_npus) / (double)req_npus;
  358. int _npus = 0;
  359. int _pus[STARPU_NMAXWORKERS];
  360. _npus = starpu_get_workers_of_sched_ctx(config->sched_ctxs[s].id, _pus, arch);
  361. if(needed_npus < (double)_npus)
  362. {
  363. double npus_to_rem = (double)_npus - needed_npus;
  364. int x = floor(npus_to_rem);
  365. double x_double = (double)x;
  366. double diff = npus_to_rem - x_double;
  367. int npus_to_remove = diff >= 0.5 ? x+1 : x;
  368. int pus_to_remove[npus_to_remove];
  369. int c = 0;
  370. /*TODO: hierarchical ctxs: get npus_to_remove good workers: close to the other ones I already assigned to the ctx */
  371. for(i = _npus-1; i >= (_npus - npus_to_remove); i--)
  372. {
  373. workers[(*nw)++] = _pus[i];
  374. pus_to_remove[c++] = _pus[i];
  375. }
  376. if(!allow_overlap)
  377. starpu_sched_ctx_remove_workers(pus_to_remove, npus_to_remove, config->sched_ctxs[s].id);
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. unsigned starpu_sched_ctx_create_inside_interval(const char *policy_name, const char *sched_ctx_name,
  385. int min_ncpus, int max_ncpus, int min_ngpus, int max_ngpus,
  386. unsigned allow_overlap)
  387. {
  388. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  389. struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(config, policy_name);
  390. struct _starpu_sched_ctx *sched_ctx = NULL;
  391. int workers[max_ncpus + max_ngpus];
  392. int nw = 0;
  393. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  394. _get_workers(min_ncpus, max_ncpus, workers, &nw, STARPU_CPU_WORKER, allow_overlap);
  395. _get_workers(min_ngpus, max_ngpus, workers, &nw, STARPU_CUDA_WORKER, allow_overlap);
  396. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  397. int i;
  398. printf("%d: ", nw);
  399. for(i = 0; i < nw; i++)
  400. printf("%d ", workers[i]);
  401. printf("\n");
  402. sched_ctx = _starpu_create_sched_ctx(selected_policy, workers, nw, 0, sched_ctx_name, 0, 0, 0, 0);
  403. sched_ctx->min_ncpus = min_ncpus;
  404. sched_ctx->max_ncpus = max_ncpus;
  405. sched_ctx->min_ngpus = min_ngpus;
  406. sched_ctx->max_ngpus = max_ngpus;
  407. _starpu_update_workers_without_ctx(sched_ctx->workers->workerids, sched_ctx->workers->nworkers, sched_ctx->id, 0);
  408. #ifdef STARPU_USE_SC_HYPERVISOR
  409. sched_ctx->perf_counters = NULL;
  410. #endif
  411. return sched_ctx->id;
  412. }
  413. unsigned starpu_sched_ctx_create(int *workerids, int nworkers, const char *sched_ctx_name, ...)
  414. {
  415. va_list varg_list;
  416. int arg_type;
  417. int min_prio_set = 0;
  418. int max_prio_set = 0;
  419. int min_prio = 0;
  420. int max_prio = 0;
  421. struct starpu_sched_policy *sched_policy;
  422. va_start(varg_list, sched_ctx_name);
  423. while ((arg_type = va_arg(varg_list, int)) != 0)
  424. {
  425. if (arg_type == STARPU_SCHED_CTX_POLICY_NAME)
  426. {
  427. char *policy_name = va_arg(varg_list, char *);
  428. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  429. sched_policy = _starpu_select_sched_policy(config, policy_name);
  430. }
  431. else if (arg_type == STARPU_SCHED_CTX_POLICY_STRUCT)
  432. {
  433. sched_policy = va_arg(varg_list, struct starpu_sched_policy *);
  434. }
  435. else if (arg_type == STARPU_SCHED_CTX_POLICY_MIN_PRIO)
  436. {
  437. min_prio = va_arg(varg_list, int);
  438. min_prio_set = 1;
  439. }
  440. else if (arg_type == STARPU_SCHED_CTX_POLICY_MAX_PRIO)
  441. {
  442. max_prio = va_arg(varg_list, int);
  443. max_prio_set = 1;
  444. }
  445. else
  446. {
  447. STARPU_ABORT_MSG("Unrecognized argument %d\n", arg_type);
  448. }
  449. }
  450. va_end(varg_list);
  451. struct _starpu_sched_ctx *sched_ctx = NULL;
  452. sched_ctx = _starpu_create_sched_ctx(sched_policy, workerids, nworkers, 0, sched_ctx_name, min_prio_set, min_prio, max_prio_set, max_prio);
  453. _starpu_update_workers_with_ctx(sched_ctx->workers->workerids, sched_ctx->workers->nworkers, sched_ctx->id);
  454. #ifdef STARPU_USE_SC_HYPERVISOR
  455. sched_ctx->perf_counters = NULL;
  456. #endif
  457. return sched_ctx->id;
  458. }
  459. void starpu_sched_ctx_register_close_callback(unsigned sched_ctx_id, void (*close_callback)(unsigned sched_ctx_id, void* args), void *args)
  460. {
  461. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  462. sched_ctx->close_callback = close_callback;
  463. sched_ctx->close_args = args;
  464. return;
  465. }
  466. #ifdef STARPU_USE_SC_HYPERVISOR
  467. void starpu_sched_ctx_set_perf_counters(unsigned sched_ctx_id, void* perf_counters)
  468. {
  469. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  470. sched_ctx->perf_counters = (struct starpu_sched_ctx_performance_counters *)perf_counters;
  471. return;
  472. }
  473. #endif
  474. /* free all structures for the context */
  475. static void _starpu_delete_sched_ctx(struct _starpu_sched_ctx *sched_ctx)
  476. {
  477. STARPU_ASSERT(sched_ctx->id != STARPU_NMAX_SCHED_CTXS);
  478. _starpu_deinit_sched_policy(sched_ctx);
  479. free(sched_ctx->sched_policy);
  480. sched_ctx->sched_policy = NULL;
  481. STARPU_PTHREAD_MUTEX_DESTROY(&sched_ctx->empty_ctx_mutex);
  482. sem_destroy(&sched_ctx->parallel_code_sem);
  483. sched_ctx->id = STARPU_NMAX_SCHED_CTXS;
  484. #ifdef STARPU_HAVE_HWLOC
  485. hwloc_bitmap_free(sched_ctx->hwloc_workers_set);
  486. #endif //STARPU_HAVE_HWLOC
  487. struct _starpu_machine_config *config = _starpu_get_machine_config();
  488. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx_manag);
  489. config->topology.nsched_ctxs--;
  490. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx_manag);
  491. }
  492. void starpu_sched_ctx_delete(unsigned sched_ctx_id)
  493. {
  494. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  495. #ifdef STARPU_USE_SC_HYPERVISOR
  496. if(sched_ctx != NULL && sched_ctx_id != 0 && sched_ctx_id != STARPU_NMAX_SCHED_CTXS
  497. && sched_ctx->perf_counters != NULL)
  498. sched_ctx->perf_counters->notify_delete_context(sched_ctx_id);
  499. #endif //STARPU_USE_SC_HYPERVISOR
  500. unsigned inheritor_sched_ctx_id = sched_ctx->inheritor;
  501. struct _starpu_sched_ctx *inheritor_sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx->inheritor);
  502. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  503. STARPU_ASSERT(sched_ctx->id != STARPU_NMAX_SCHED_CTXS);
  504. int *workerids;
  505. unsigned nworkers_ctx = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  506. /*if both of them have all the ressources is pointless*/
  507. /*trying to transfer ressources from one ctx to the other*/
  508. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  509. unsigned nworkers = config->topology.nworkers;
  510. if(nworkers_ctx > 0 && inheritor_sched_ctx && inheritor_sched_ctx->id != STARPU_NMAX_SCHED_CTXS &&
  511. !(nworkers_ctx == nworkers && nworkers_ctx == inheritor_sched_ctx->workers->nworkers))
  512. {
  513. starpu_sched_ctx_add_workers(workerids, nworkers_ctx, inheritor_sched_ctx_id);
  514. }
  515. if(!_starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx_id))
  516. {
  517. /*if btw the mutex release & the mutex lock the context has changed take care to free all
  518. scheduling data before deleting the context */
  519. _starpu_update_workers_without_ctx(workerids, nworkers_ctx, sched_ctx_id, 1);
  520. // _starpu_sched_ctx_free_scheduling_data(sched_ctx);
  521. _starpu_delete_sched_ctx(sched_ctx);
  522. }
  523. /* workerids is malloc-ed in starpu_sched_ctx_get_workers_list, don't forget to free it when
  524. you don't use it anymore */
  525. free(workerids);
  526. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  527. return;
  528. }
  529. /* called after the workers are terminated so we don't have anything else to do but free the memory*/
  530. void _starpu_delete_all_sched_ctxs()
  531. {
  532. unsigned i;
  533. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  534. {
  535. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(i);
  536. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[i]);
  537. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  538. {
  539. _starpu_sched_ctx_free_scheduling_data(sched_ctx);
  540. _starpu_barrier_counter_destroy(&sched_ctx->tasks_barrier);
  541. _starpu_delete_sched_ctx(sched_ctx);
  542. }
  543. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[i]);
  544. STARPU_PTHREAD_RWLOCK_DESTROY(&changing_ctx_mutex[i]);
  545. }
  546. return;
  547. }
  548. static void _starpu_check_workers(int *workerids, int nworkers)
  549. {
  550. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  551. int nworkers_conf = config->topology.nworkers;
  552. int i;
  553. for(i = 0; i < nworkers; i++)
  554. {
  555. /* take care the user does not ask for a resource that does not exist */
  556. 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);
  557. }
  558. }
  559. void _starpu_fetch_tasks_from_empty_ctx_list(struct _starpu_sched_ctx *sched_ctx)
  560. {
  561. unsigned unlocked = 0;
  562. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  563. if(starpu_task_list_empty(&sched_ctx->empty_ctx_tasks))
  564. {
  565. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  566. return;
  567. }
  568. else
  569. /* you're not suppose to get here if you deleted the context
  570. so no point in having the mutex locked */
  571. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx->id]);
  572. while(!starpu_task_list_empty(&sched_ctx->empty_ctx_tasks))
  573. {
  574. if(unlocked)
  575. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  576. struct starpu_task *old_task = starpu_task_list_pop_back(&sched_ctx->empty_ctx_tasks);
  577. unlocked = 1;
  578. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  579. if(old_task == &stop_submission_task)
  580. break;
  581. int ret = _starpu_push_task_to_workers(old_task);
  582. /* if we should stop poping from empty ctx tasks */
  583. if(ret == -EAGAIN) break;
  584. }
  585. if(!unlocked)
  586. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  587. /* leave the mutex as it was to avoid pbs in the caller function */
  588. STARPU_PTHREAD_RWLOCK_RDLOCK(&changing_ctx_mutex[sched_ctx->id]);
  589. return;
  590. }
  591. void starpu_sched_ctx_add_workers(int *workers_to_add, int nworkers_to_add, unsigned sched_ctx_id)
  592. {
  593. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  594. int added_workers[nworkers_to_add];
  595. int n_added_workers = 0;
  596. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  597. STARPU_ASSERT(workers_to_add != NULL && nworkers_to_add > 0);
  598. _starpu_check_workers(workers_to_add, nworkers_to_add);
  599. /* if the context has not already been deleted */
  600. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  601. {
  602. _starpu_add_workers_to_sched_ctx(sched_ctx, workers_to_add, nworkers_to_add, added_workers, &n_added_workers);
  603. if(n_added_workers > 0)
  604. {
  605. _starpu_update_workers_with_ctx(added_workers, n_added_workers, sched_ctx->id);
  606. }
  607. }
  608. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  609. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  610. {
  611. STARPU_PTHREAD_RWLOCK_RDLOCK(&changing_ctx_mutex[sched_ctx_id]);
  612. _starpu_fetch_tasks_from_empty_ctx_list(sched_ctx);
  613. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  614. }
  615. return;
  616. }
  617. void starpu_sched_ctx_remove_workers(int *workers_to_remove, int nworkers_to_remove, unsigned sched_ctx_id)
  618. {
  619. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  620. int removed_workers[sched_ctx->workers->nworkers];
  621. int n_removed_workers = 0;
  622. _starpu_check_workers(workers_to_remove, nworkers_to_remove);
  623. STARPU_PTHREAD_RWLOCK_WRLOCK(&changing_ctx_mutex[sched_ctx_id]);
  624. /* if the context has not already been deleted */
  625. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  626. {
  627. _starpu_remove_workers_from_sched_ctx(sched_ctx, workers_to_remove, nworkers_to_remove, removed_workers, &n_removed_workers);
  628. if(n_removed_workers > 0)
  629. _starpu_update_workers_without_ctx(removed_workers, n_removed_workers, sched_ctx->id, 0);
  630. }
  631. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  632. return;
  633. }
  634. /* unused sched_ctx have the id STARPU_NMAX_SCHED_CTXS */
  635. void _starpu_init_all_sched_ctxs(struct _starpu_machine_config *config)
  636. {
  637. starpu_pthread_key_create(&sched_ctx_key, NULL);
  638. unsigned i;
  639. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  640. config->sched_ctxs[i].id = STARPU_NMAX_SCHED_CTXS;
  641. return;
  642. }
  643. /* sched_ctx aren't necessarly one next to another */
  644. /* for eg when we remove one its place is free */
  645. /* when we add new one we reuse its place */
  646. static unsigned _starpu_get_first_free_sched_ctx(struct _starpu_machine_config *config)
  647. {
  648. unsigned i;
  649. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  650. if(config->sched_ctxs[i].id == STARPU_NMAX_SCHED_CTXS)
  651. return i;
  652. STARPU_ASSERT(0);
  653. return STARPU_NMAX_SCHED_CTXS;
  654. }
  655. int _starpu_wait_for_all_tasks_of_sched_ctx(unsigned sched_ctx_id)
  656. {
  657. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  658. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  659. return -EDEADLK;
  660. return _starpu_barrier_counter_wait_for_empty_counter(&sched_ctx->tasks_barrier);
  661. }
  662. void _starpu_decrement_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  663. {
  664. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  665. int finished = _starpu_barrier_counter_decrement_until_empty_counter(&sched_ctx->tasks_barrier);
  666. /* when finished decrementing the tasks if the user signaled he will not submit tasks anymore
  667. we can move all its workers to the inheritor context */
  668. if(finished && sched_ctx->inheritor != STARPU_NMAX_SCHED_CTXS)
  669. {
  670. STARPU_PTHREAD_MUTEX_LOCK(&finished_submit_mutex);
  671. if(sched_ctx->finished_submit)
  672. {
  673. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  674. /* take care the context is not deleted or changed at the same time */
  675. STARPU_PTHREAD_RWLOCK_RDLOCK(&changing_ctx_mutex[sched_ctx_id]);
  676. if(sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  677. {
  678. if(sched_ctx->close_callback)
  679. sched_ctx->close_callback(sched_ctx->id, sched_ctx->close_args);
  680. int *workerids = NULL;
  681. unsigned nworkers = starpu_sched_ctx_get_workers_list(sched_ctx->id, &workerids);
  682. if(nworkers > 0)
  683. {
  684. starpu_sched_ctx_add_workers(workerids, nworkers, sched_ctx->inheritor);
  685. free(workerids);
  686. }
  687. }
  688. STARPU_PTHREAD_RWLOCK_UNLOCK(&changing_ctx_mutex[sched_ctx_id]);
  689. return;
  690. }
  691. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  692. }
  693. return;
  694. }
  695. void _starpu_increment_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id)
  696. {
  697. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  698. _starpu_barrier_counter_increment(&sched_ctx->tasks_barrier);
  699. }
  700. void starpu_sched_ctx_set_context(unsigned *sched_ctx)
  701. {
  702. starpu_pthread_setspecific(sched_ctx_key, (void*)sched_ctx);
  703. }
  704. unsigned starpu_sched_ctx_get_context()
  705. {
  706. unsigned *sched_ctx = (unsigned*)starpu_pthread_getspecific(sched_ctx_key);
  707. if(sched_ctx == NULL)
  708. return STARPU_NMAX_SCHED_CTXS;
  709. STARPU_ASSERT(*sched_ctx < STARPU_NMAX_SCHED_CTXS);
  710. return *sched_ctx;
  711. }
  712. void starpu_sched_ctx_notify_hypervisor_exists()
  713. {
  714. with_hypervisor = 1;
  715. }
  716. unsigned starpu_sched_ctx_check_if_hypervisor_exists()
  717. {
  718. return with_hypervisor;
  719. }
  720. unsigned _starpu_get_nsched_ctxs()
  721. {
  722. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  723. return config->topology.nsched_ctxs;
  724. }
  725. void starpu_sched_ctx_set_policy_data(unsigned sched_ctx_id, void* policy_data)
  726. {
  727. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  728. sched_ctx->policy_data = policy_data;
  729. }
  730. void* starpu_sched_ctx_get_policy_data(unsigned sched_ctx_id)
  731. {
  732. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  733. return sched_ctx->policy_data;
  734. }
  735. struct starpu_worker_collection* starpu_sched_ctx_create_worker_collection(unsigned sched_ctx_id, enum starpu_worker_collection_type worker_collection_type)
  736. {
  737. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  738. sched_ctx->workers = (struct starpu_worker_collection*)malloc(sizeof(struct starpu_worker_collection));
  739. switch(worker_collection_type)
  740. {
  741. case STARPU_WORKER_LIST:
  742. sched_ctx->workers->has_next = worker_list.has_next;
  743. sched_ctx->workers->get_next = worker_list.get_next;
  744. sched_ctx->workers->add = worker_list.add;
  745. sched_ctx->workers->remove = worker_list.remove;
  746. sched_ctx->workers->init = worker_list.init;
  747. sched_ctx->workers->deinit = worker_list.deinit;
  748. sched_ctx->workers->init_iterator = worker_list.init_iterator;
  749. sched_ctx->workers->type = STARPU_WORKER_LIST;
  750. break;
  751. }
  752. return sched_ctx->workers;
  753. }
  754. unsigned starpu_sched_ctx_get_workers_list(unsigned sched_ctx_id, int **workerids)
  755. {
  756. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  757. struct starpu_worker_collection *workers = sched_ctx->workers;
  758. *workerids = (int*)malloc(workers->nworkers*sizeof(int));
  759. int worker;
  760. unsigned nworkers = 0;
  761. struct starpu_sched_ctx_iterator it;
  762. if(workers->init_iterator)
  763. workers->init_iterator(workers, &it);
  764. while(workers->has_next(workers, &it))
  765. {
  766. worker = workers->get_next(workers, &it);
  767. (*workerids)[nworkers++] = worker;
  768. }
  769. return nworkers;
  770. }
  771. void starpu_sched_ctx_delete_worker_collection(unsigned sched_ctx_id)
  772. {
  773. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  774. sched_ctx->workers->deinit(sched_ctx->workers);
  775. free(sched_ctx->workers);
  776. }
  777. struct starpu_worker_collection* starpu_sched_ctx_get_worker_collection(unsigned sched_ctx_id)
  778. {
  779. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  780. return sched_ctx->workers;
  781. }
  782. int starpu_get_workers_of_sched_ctx(unsigned sched_ctx_id, int *pus, enum starpu_worker_archtype arch)
  783. {
  784. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  785. struct starpu_worker_collection *workers = sched_ctx->workers;
  786. int worker;
  787. int npus = 0;
  788. struct starpu_sched_ctx_iterator it;
  789. if(workers->init_iterator)
  790. workers->init_iterator(workers, &it);
  791. while(workers->has_next(workers, &it))
  792. {
  793. worker = workers->get_next(workers, &it);
  794. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  795. if(curr_arch == arch)
  796. pus[npus++] = worker;
  797. }
  798. return npus;
  799. }
  800. starpu_pthread_rwlock_t* _starpu_sched_ctx_get_changing_ctx_mutex(unsigned sched_ctx_id)
  801. {
  802. return &changing_ctx_mutex[sched_ctx_id];
  803. }
  804. unsigned starpu_sched_ctx_get_nworkers(unsigned sched_ctx_id)
  805. {
  806. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  807. if(sched_ctx != NULL)
  808. return sched_ctx->workers->nworkers;
  809. else
  810. return 0;
  811. }
  812. unsigned starpu_sched_ctx_get_nshared_workers(unsigned sched_ctx_id, unsigned sched_ctx_id2)
  813. {
  814. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  815. struct _starpu_sched_ctx *sched_ctx2 = _starpu_get_sched_ctx_struct(sched_ctx_id2);
  816. struct starpu_worker_collection *workers = sched_ctx->workers;
  817. struct starpu_worker_collection *workers2 = sched_ctx2->workers;
  818. int worker, worker2;
  819. int shared_workers = 0;
  820. struct starpu_sched_ctx_iterator it1, it2;
  821. if(workers->init_iterator)
  822. workers->init_iterator(workers, &it1);
  823. if(workers2->init_iterator)
  824. workers2->init_iterator(workers2, &it2);
  825. while(workers->has_next(workers, &it1))
  826. {
  827. worker = workers->get_next(workers, &it1);
  828. while(workers2->has_next(workers2, &it2))
  829. {
  830. worker2 = workers2->get_next(workers2, &it2);
  831. if(worker == worker2)
  832. shared_workers++;
  833. }
  834. }
  835. return shared_workers;
  836. }
  837. unsigned starpu_sched_ctx_contains_worker(int workerid, unsigned sched_ctx_id)
  838. {
  839. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  840. struct starpu_worker_collection *workers = sched_ctx->workers;
  841. int worker;
  842. struct starpu_sched_ctx_iterator it;
  843. if(workers->init_iterator)
  844. workers->init_iterator(workers, &it);
  845. while(workers->has_next(workers, &it))
  846. {
  847. worker = workers->get_next(workers, &it);
  848. if(worker == workerid)
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. unsigned starpu_sched_ctx_contains_type_of_worker(enum starpu_worker_archtype arch, unsigned sched_ctx_id)
  854. {
  855. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  856. int worker;
  857. struct starpu_sched_ctx_iterator it;
  858. if(workers->init_iterator)
  859. workers->init_iterator(workers, &it);
  860. while(workers->has_next(workers, &it))
  861. {
  862. worker = workers->get_next(workers, &it);
  863. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  864. if(curr_arch == arch)
  865. return 1;
  866. }
  867. return 0;
  868. }
  869. unsigned _starpu_worker_belongs_to_a_sched_ctx(int workerid, unsigned sched_ctx_id)
  870. {
  871. struct _starpu_machine_config *config = (struct _starpu_machine_config *)_starpu_get_machine_config();
  872. int i;
  873. struct _starpu_sched_ctx *sched_ctx = NULL;
  874. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  875. {
  876. sched_ctx = &config->sched_ctxs[i];
  877. if(sched_ctx && sched_ctx->id != STARPU_NMAX_SCHED_CTXS && sched_ctx->id != sched_ctx_id)
  878. if(starpu_sched_ctx_contains_worker(workerid, sched_ctx->id))
  879. return 1;
  880. }
  881. return 0;
  882. }
  883. unsigned starpu_sched_ctx_worker_get_id(unsigned sched_ctx_id)
  884. {
  885. int workerid = starpu_worker_get_id();
  886. if(workerid != -1)
  887. if(starpu_sched_ctx_contains_worker(workerid, sched_ctx_id))
  888. return workerid;
  889. return -1;
  890. }
  891. unsigned starpu_sched_ctx_overlapping_ctxs_on_worker(int workerid)
  892. {
  893. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  894. return worker->nsched_ctxs > 1;
  895. }
  896. void starpu_sched_ctx_set_inheritor(unsigned sched_ctx_id, unsigned inheritor)
  897. {
  898. STARPU_ASSERT(inheritor < STARPU_NMAX_SCHED_CTXS);
  899. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  900. sched_ctx->inheritor = inheritor;
  901. return;
  902. }
  903. void starpu_sched_ctx_finished_submit(unsigned sched_ctx_id)
  904. {
  905. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  906. STARPU_PTHREAD_MUTEX_LOCK(&finished_submit_mutex);
  907. sched_ctx->finished_submit = 1;
  908. STARPU_PTHREAD_MUTEX_UNLOCK(&finished_submit_mutex);
  909. return;
  910. }
  911. #ifdef STARPU_USE_SC_HYPERVISOR
  912. void _starpu_sched_ctx_post_exec_task_cb(int workerid, struct starpu_task *task, size_t data_size, uint32_t footprint)
  913. {
  914. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  915. if(sched_ctx != NULL && task->sched_ctx != _starpu_get_initial_sched_ctx()->id &&
  916. task->sched_ctx != STARPU_NMAX_SCHED_CTXS && sched_ctx->perf_counters != NULL)
  917. sched_ctx->perf_counters->notify_post_exec_task(task, data_size, footprint, task->hypervisor_tag);
  918. }
  919. void starpu_sched_ctx_call_pushed_task_cb(int workerid, unsigned sched_ctx_id)
  920. {
  921. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  922. if(sched_ctx != NULL && sched_ctx_id != _starpu_get_initial_sched_ctx()->id && sched_ctx_id != STARPU_NMAX_SCHED_CTXS
  923. && sched_ctx->perf_counters != NULL)
  924. sched_ctx->perf_counters->notify_pushed_task(sched_ctx_id, workerid);
  925. }
  926. #endif //STARPU_USE_SC_HYPERVISOR
  927. int starpu_sched_get_min_priority(void)
  928. {
  929. return starpu_sched_ctx_get_min_priority(_starpu_get_initial_sched_ctx()->id);
  930. }
  931. int starpu_sched_get_max_priority(void)
  932. {
  933. return starpu_sched_ctx_get_max_priority(_starpu_get_initial_sched_ctx()->id);
  934. }
  935. int starpu_sched_set_min_priority(int min_prio)
  936. {
  937. return starpu_sched_ctx_set_min_priority(_starpu_get_initial_sched_ctx()->id, min_prio);
  938. }
  939. int starpu_sched_set_max_priority(int max_prio)
  940. {
  941. return starpu_sched_ctx_set_max_priority(_starpu_get_initial_sched_ctx()->id, max_prio);
  942. }
  943. int starpu_sched_ctx_get_min_priority(unsigned sched_ctx_id)
  944. {
  945. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  946. return sched_ctx->min_priority;
  947. }
  948. int starpu_sched_ctx_get_max_priority(unsigned sched_ctx_id)
  949. {
  950. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  951. return sched_ctx->max_priority;
  952. }
  953. int starpu_sched_ctx_set_min_priority(unsigned sched_ctx_id, int min_prio)
  954. {
  955. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  956. sched_ctx->min_priority = min_prio;
  957. return 0;
  958. }
  959. int starpu_sched_ctx_set_max_priority(unsigned sched_ctx_id, int max_prio)
  960. {
  961. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  962. sched_ctx->max_priority = max_prio;
  963. return 0;
  964. }
  965. int starpu_sched_ctx_min_priority_is_set(unsigned sched_ctx_id)
  966. {
  967. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  968. return sched_ctx->min_priority_is_set;
  969. }
  970. int starpu_sched_ctx_max_priority_is_set(unsigned sched_ctx_id)
  971. {
  972. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  973. return sched_ctx->max_priority_is_set;
  974. }
  975. unsigned _starpu_sched_ctx_last_worker_awake(struct _starpu_worker *worker)
  976. {
  977. struct _starpu_sched_ctx_list *l = NULL;
  978. for (l = worker->sched_ctx_list; l; l = l->next)
  979. {
  980. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(l->sched_ctx);
  981. unsigned last_worker_awake = 1;
  982. struct starpu_worker_collection *workers = sched_ctx->workers;
  983. struct starpu_sched_ctx_iterator it;
  984. int workerid;
  985. if(workers->init_iterator)
  986. workers->init_iterator(workers, &it);
  987. while(workers->has_next(workers, &it))
  988. {
  989. workerid = workers->get_next(workers, &it);
  990. if(workerid != worker->workerid && _starpu_worker_get_status(workerid) != STATUS_SLEEPING)
  991. {
  992. last_worker_awake = 0;
  993. break;
  994. }
  995. }
  996. if(last_worker_awake)
  997. return 1;
  998. }
  999. return 0;
  1000. }
  1001. static void _starpu_sched_ctx_bind_thread_to_ctx_cpus(unsigned sched_ctx_id)
  1002. {
  1003. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1004. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1005. #ifdef STARPU_HAVE_HWLOC
  1006. const struct hwloc_topology_support *support = hwloc_topology_get_support(config->topology.hwtopology);
  1007. if (support->cpubind->set_thisthread_cpubind)
  1008. {
  1009. hwloc_bitmap_t set = sched_ctx->hwloc_workers_set;
  1010. int ret;
  1011. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1012. HWLOC_CPUBIND_THREAD);
  1013. if (ret)
  1014. {
  1015. perror("binding thread");
  1016. STARPU_ABORT();
  1017. }
  1018. }
  1019. #else
  1020. #warning no sched ctx CPU binding support
  1021. #endif
  1022. return;
  1023. }
  1024. void _starpu_sched_ctx_rebind_thread_to_its_cpu(unsigned cpuid)
  1025. {
  1026. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1027. #ifdef STARPU_SIMGRID
  1028. return;
  1029. #endif
  1030. if (starpu_get_env_number("STARPU_WORKERS_NOBIND") > 0)
  1031. return;
  1032. #ifdef STARPU_HAVE_HWLOC
  1033. const struct hwloc_topology_support *support = hwloc_topology_get_support (config->topology.hwtopology);
  1034. if (support->cpubind->set_thisthread_cpubind)
  1035. {
  1036. hwloc_obj_t obj = hwloc_get_obj_by_depth (config->topology.hwtopology,
  1037. config->cpu_depth, cpuid);
  1038. hwloc_bitmap_t set = obj->cpuset;
  1039. int ret;
  1040. hwloc_bitmap_singlify(set);
  1041. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1042. HWLOC_CPUBIND_THREAD);
  1043. if (ret)
  1044. {
  1045. perror("hwloc_set_cpubind");
  1046. STARPU_ABORT();
  1047. }
  1048. }
  1049. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  1050. int ret;
  1051. /* fix the thread on the correct cpu */
  1052. cpu_set_t aff_mask;
  1053. CPU_ZERO(&aff_mask);
  1054. CPU_SET(cpuid, &aff_mask);
  1055. starpu_pthread_t self = pthread_self();
  1056. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  1057. if (ret)
  1058. {
  1059. perror("binding thread");
  1060. STARPU_ABORT();
  1061. }
  1062. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  1063. DWORD mask = 1 << cpuid;
  1064. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  1065. {
  1066. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  1067. }
  1068. #else
  1069. #warning no CPU binding support
  1070. #endif
  1071. }
  1072. static void _starpu_sched_ctx_get_workers_to_sleep(unsigned sched_ctx_id)
  1073. {
  1074. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1075. struct starpu_worker_collection *workers = sched_ctx->workers;
  1076. struct starpu_sched_ctx_iterator it;
  1077. struct _starpu_worker *worker = NULL;
  1078. if(workers->init_iterator)
  1079. workers->init_iterator(workers, &it);
  1080. while(workers->has_next(workers, &it))
  1081. {
  1082. worker = _starpu_get_worker_struct(workers->get_next(workers, &it));
  1083. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  1084. worker->parallel_sect = 1;
  1085. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  1086. }
  1087. while(workers->has_next(workers, &it))
  1088. {
  1089. workers->get_next(workers, &it);
  1090. sem_wait(&sched_ctx->parallel_code_sem);
  1091. }
  1092. return;
  1093. }
  1094. void _starpu_sched_ctx_signal_worker_blocked(int workerid)
  1095. {
  1096. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1097. struct _starpu_sched_ctx *sched_ctx = NULL;
  1098. struct _starpu_sched_ctx_list *l = NULL;
  1099. for (l = worker->sched_ctx_list; l; l = l->next)
  1100. {
  1101. sched_ctx = _starpu_get_sched_ctx_struct(l->sched_ctx);
  1102. if(sched_ctx->id != 0)
  1103. sem_post(&sched_ctx->parallel_code_sem);
  1104. }
  1105. return;
  1106. }
  1107. static void _starpu_sched_ctx_wake_up_workers(unsigned sched_ctx_id)
  1108. {
  1109. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  1110. struct starpu_worker_collection *workers = sched_ctx->workers;
  1111. struct starpu_sched_ctx_iterator it;
  1112. struct _starpu_worker *worker = NULL;
  1113. if(workers->init_iterator)
  1114. workers->init_iterator(workers, &it);
  1115. while(workers->has_next(workers, &it))
  1116. {
  1117. worker = _starpu_get_worker_struct(workers->get_next(workers, &it));
  1118. STARPU_PTHREAD_MUTEX_LOCK(&worker->parallel_sect_mutex);
  1119. STARPU_PTHREAD_COND_SIGNAL(&worker->parallel_sect_cond);
  1120. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->parallel_sect_mutex);
  1121. }
  1122. return;
  1123. }
  1124. void* starpu_sched_ctx_exec_parallel_code(void* (*func)(void*), void* param, unsigned sched_ctx_id)
  1125. {
  1126. /* get starpu workers to sleep */
  1127. _starpu_sched_ctx_get_workers_to_sleep(sched_ctx_id);
  1128. /* bind current thread on all workers of the context */
  1129. _starpu_sched_ctx_bind_thread_to_ctx_cpus(sched_ctx_id);
  1130. /* execute parallel code */
  1131. void* ret = func(param);
  1132. /* wake up starpu workers */
  1133. _starpu_sched_ctx_wake_up_workers(sched_ctx_id);
  1134. return ret;
  1135. }