sched_ctx.c 48 KB

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