sched_ctx.c 42 KB

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