sched_ctx.c 40 KB

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