sched_ctx.c 40 KB

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