sched_ctx.c 42 KB

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