component_worker.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2014, 2015, 2016 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011-2013 INRIA
  7. * Copyright (C) 2013 Simon Archipoff
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <starpu_sched_component.h>
  21. #include <sched_policies/sched_component.h>
  22. #include <core/workers.h>
  23. #include <float.h>
  24. /* data structure for worker's queue look like this :
  25. * W = worker
  26. * T = simple task
  27. * P = parallel task
  28. *
  29. *
  30. * P--P T
  31. * | | \|
  32. * P--P T T P T
  33. * | | | | | |
  34. * T T P--P--P T
  35. * | | | | | |
  36. * W W W W W W
  37. *
  38. *
  39. *
  40. * its possible that a _starpu_task_grid wont have task, because it have been
  41. * poped by a worker.
  42. *
  43. * N = no task
  44. *
  45. * T T T
  46. * | | |
  47. * P--N--N
  48. * | | |
  49. * W W W
  50. *
  51. *
  52. * this API is a little asymmetric : struct _starpu_task_grid are allocated by the caller and freed by the data structure
  53. *
  54. */
  55. /******************************************************************************
  56. * Worker Components' Data Structures *
  57. *****************************************************************************/
  58. struct _starpu_task_grid
  59. {
  60. /* this member may be NULL if a worker have poped it but its a
  61. * parallel task and we dont want mad pointers
  62. */
  63. struct starpu_task * task;
  64. struct _starpu_task_grid *up, *down, *left, *right;
  65. /* this is used to count the number of task to be poped by a worker
  66. * the leftist _starpu_task_grid maintain the ntasks counter (ie .left == NULL),
  67. * all the others use the pntasks that point to it
  68. *
  69. * when the counter reach 0, all the left and right member are set to NULL,
  70. * that mean that we will free that components.
  71. */
  72. union
  73. {
  74. int ntasks;
  75. int * pntasks;
  76. };
  77. };
  78. /* list->exp_start, list->exp_len, list-exp_end and list->ntasks
  79. * are updated by starpu_sched_component_worker_push_task(component, task) and pre_exec_hook
  80. */
  81. struct _starpu_worker_task_list
  82. {
  83. double exp_start, exp_len, exp_end;
  84. struct _starpu_task_grid *first, *last;
  85. unsigned ntasks;
  86. starpu_pthread_mutex_t mutex;
  87. };
  88. enum _starpu_worker_component_status
  89. {
  90. COMPONENT_STATUS_SLEEPING,
  91. COMPONENT_STATUS_RESET,
  92. COMPONENT_STATUS_CHANGED
  93. };
  94. struct _starpu_worker_component_data
  95. {
  96. union
  97. {
  98. struct
  99. {
  100. struct _starpu_worker * worker;
  101. starpu_pthread_mutex_t lock;
  102. };
  103. struct _starpu_combined_worker * combined_worker;
  104. };
  105. struct _starpu_worker_task_list * list;
  106. enum _starpu_worker_component_status status;
  107. };
  108. /* this array store worker components */
  109. static struct starpu_sched_component * _worker_components[STARPU_NMAX_SCHED_CTXS][STARPU_NMAXWORKERS];
  110. /******************************************************************************
  111. * Worker Components' Task List and Grid Functions *
  112. *****************************************************************************/
  113. static struct _starpu_worker_task_list * _starpu_worker_task_list_create(void)
  114. {
  115. struct _starpu_worker_task_list * l = malloc(sizeof(*l));
  116. memset(l, 0, sizeof(*l));
  117. l->exp_len = 0.0;
  118. l->exp_start = l->exp_end = starpu_timing_now();
  119. STARPU_PTHREAD_MUTEX_INIT(&l->mutex,NULL);
  120. return l;
  121. }
  122. static struct _starpu_task_grid * _starpu_task_grid_create(void)
  123. {
  124. struct _starpu_task_grid * t = malloc(sizeof(*t));
  125. memset(t, 0, sizeof(*t));
  126. return t;
  127. }
  128. static struct _starpu_worker_task_list * _worker_get_list(unsigned sched_ctx_id)
  129. {
  130. unsigned workerid = starpu_worker_get_id_check();
  131. STARPU_ASSERT(workerid < starpu_worker_get_count());
  132. struct _starpu_worker_component_data * d = starpu_sched_component_worker_get(sched_ctx_id, workerid)->data;
  133. return d->list;
  134. }
  135. static void _starpu_task_grid_destroy(struct _starpu_task_grid * t)
  136. {
  137. free(t);
  138. }
  139. static void _starpu_worker_task_list_destroy(struct _starpu_worker_task_list * l)
  140. {
  141. if(l)
  142. {
  143. STARPU_PTHREAD_MUTEX_DESTROY(&l->mutex);
  144. free(l);
  145. }
  146. }
  147. static inline void _starpu_worker_task_list_push(struct _starpu_worker_task_list * l, struct _starpu_task_grid * t)
  148. {
  149. /* the task, ntasks, pntasks, left and right members of t are set by the caller */
  150. STARPU_ASSERT(t->task);
  151. if(l->first == NULL)
  152. l->first = l->last = t;
  153. t->down = l->last;
  154. l->last->up = t;
  155. t->up = NULL;
  156. l->last = t;
  157. l->ntasks++;
  158. double predicted = t->task->predicted;
  159. double predicted_transfer = t->task->predicted_transfer;
  160. /* Sometimes workers didn't take the tasks as early as we expected */
  161. l->exp_start = STARPU_MAX(l->exp_start, starpu_timing_now());
  162. l->exp_end = l->exp_start + l->exp_len;
  163. if (starpu_timing_now() + predicted_transfer < l->exp_end)
  164. {
  165. /* We may hope that the transfer will be finished by
  166. * the start of the task. */
  167. predicted_transfer = 0.0;
  168. }
  169. else
  170. {
  171. /* The transfer will not be finished by then, take the
  172. * remainder into account */
  173. predicted_transfer = (starpu_timing_now() + predicted_transfer) - l->exp_end;
  174. }
  175. if(!isnan(predicted_transfer))
  176. {
  177. l->exp_end += predicted_transfer;
  178. l->exp_len += predicted_transfer;
  179. }
  180. if(!isnan(predicted))
  181. {
  182. l->exp_end += predicted;
  183. l->exp_len += predicted;
  184. }
  185. t->task->predicted = predicted;
  186. t->task->predicted_transfer = predicted_transfer;
  187. }
  188. /* recursively set left and right pointers to NULL */
  189. static inline void _starpu_task_grid_unset_left_right_member(struct _starpu_task_grid * t)
  190. {
  191. STARPU_ASSERT(t->task == NULL);
  192. struct _starpu_task_grid * t_left = t->left;
  193. struct _starpu_task_grid * t_right = t->right;
  194. t->left = t->right = NULL;
  195. while(t_left)
  196. {
  197. STARPU_ASSERT(t_left->task == NULL);
  198. t = t_left;
  199. t_left = t_left->left;
  200. t->left = NULL;
  201. t->right = NULL;
  202. }
  203. while(t_right)
  204. {
  205. STARPU_ASSERT(t_right->task == NULL);
  206. t = t_right;
  207. t_right = t_right->right;
  208. t->left = NULL;
  209. t->right = NULL;
  210. }
  211. }
  212. static inline struct starpu_task * _starpu_worker_task_list_pop(struct _starpu_worker_task_list * l)
  213. {
  214. if(!l->first)
  215. {
  216. l->exp_start = l->exp_end = starpu_timing_now();
  217. l->exp_len = 0;
  218. return NULL;
  219. }
  220. struct _starpu_task_grid * t = l->first;
  221. /* if there is no task there is no tasks linked to this, then we can free it */
  222. if(t->task == NULL && t->right == NULL && t->left == NULL)
  223. {
  224. l->first = t->up;
  225. if(l->first)
  226. l->first->down = NULL;
  227. if(l->last == t)
  228. l->last = NULL;
  229. _starpu_task_grid_destroy(t);
  230. return _starpu_worker_task_list_pop(l);
  231. }
  232. while(t)
  233. {
  234. if(t->task)
  235. {
  236. struct starpu_task * task = t->task;
  237. t->task = NULL;
  238. /* the leftist thing hold the number of tasks, other have a pointer to it */
  239. int * p = t->left ? t->pntasks : &t->ntasks;
  240. /* the worker who pop the last task allow the rope to be freed */
  241. if(STARPU_ATOMIC_ADD(p, -1) == 0)
  242. _starpu_task_grid_unset_left_right_member(t);
  243. l->ntasks--;
  244. if(!isnan(task->predicted))
  245. {
  246. l->exp_len -= task->predicted_transfer;
  247. l->exp_end = l->exp_start + l->exp_len;
  248. }
  249. return task;
  250. }
  251. t = t->up;
  252. }
  253. return NULL;
  254. }
  255. /******************************************************************************
  256. * Worker Components' Public Helper Functions (Part 1) *
  257. *****************************************************************************/
  258. struct _starpu_worker * _starpu_sched_component_worker_get_worker(struct starpu_sched_component * worker_component)
  259. {
  260. STARPU_ASSERT(starpu_sched_component_is_simple_worker(worker_component));
  261. struct _starpu_worker_component_data * data = worker_component->data;
  262. return data->worker;
  263. }
  264. struct _starpu_combined_worker * _starpu_sched_component_combined_worker_get_combined_worker(struct starpu_sched_component * worker_component)
  265. {
  266. STARPU_ASSERT(starpu_sched_component_is_combined_worker(worker_component));
  267. struct _starpu_worker_component_data * data = worker_component->data;
  268. return data->combined_worker;
  269. }
  270. void _starpu_sched_component_lock_worker(unsigned sched_ctx_id, int workerid)
  271. {
  272. STARPU_ASSERT(0 <= workerid && workerid < (int) starpu_worker_get_count());
  273. struct _starpu_worker_component_data * data = starpu_sched_component_worker_get(sched_ctx_id, workerid)->data;
  274. STARPU_PTHREAD_MUTEX_LOCK(&data->lock);
  275. }
  276. void _starpu_sched_component_unlock_worker(unsigned sched_ctx_id, int workerid)
  277. {
  278. STARPU_ASSERT(0 <= workerid && workerid < (int)starpu_worker_get_count());
  279. struct _starpu_worker_component_data * data = starpu_sched_component_worker_get(sched_ctx_id, workerid)->data;
  280. STARPU_PTHREAD_MUTEX_UNLOCK(&data->lock);
  281. }
  282. /******************************************************************************
  283. * Worker Components' Private Helper Functions *
  284. *****************************************************************************/
  285. /* Allows a worker to lock/unlock scheduling mutexes. Currently used in
  286. * self-defined can_push calls to allow can_pull calls to take those mutexes while the
  287. * current worker is pushing tasks on other workers (or itself).
  288. */
  289. static void _starpu_sched_component_worker_lock_scheduling(unsigned sched_ctx_id)
  290. {
  291. unsigned workerid = starpu_worker_get_id_check();
  292. starpu_pthread_mutex_t *sched_mutex;
  293. starpu_pthread_cond_t *sched_cond;
  294. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  295. _starpu_sched_component_lock_worker(sched_ctx_id, workerid);
  296. #ifdef STARPU_DEVEL
  297. #warning Reverses locking order between worker lock and worker component lock!
  298. #warning See helgrind suppression file for the details
  299. #endif
  300. STARPU_PTHREAD_MUTEX_LOCK_SCHED(sched_mutex);
  301. }
  302. static void _starpu_sched_component_worker_unlock_scheduling(unsigned sched_ctx_id)
  303. {
  304. unsigned workerid = starpu_worker_get_id_check();
  305. starpu_pthread_mutex_t *sched_mutex;
  306. starpu_pthread_cond_t *sched_cond;
  307. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  308. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(sched_mutex);
  309. _starpu_sched_component_unlock_worker(sched_ctx_id, workerid);
  310. }
  311. static void _starpu_sched_component_worker_set_sleep_status(struct starpu_sched_component * worker_component)
  312. {
  313. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  314. struct _starpu_worker_component_data * data = worker_component->data;
  315. data->status = COMPONENT_STATUS_SLEEPING;
  316. }
  317. static void _starpu_sched_component_worker_set_changed_status(struct starpu_sched_component * worker_component)
  318. {
  319. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  320. struct _starpu_worker_component_data * data = worker_component->data;
  321. data->status = COMPONENT_STATUS_CHANGED;
  322. }
  323. static void _starpu_sched_component_worker_reset_status(struct starpu_sched_component * worker_component)
  324. {
  325. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  326. struct _starpu_worker_component_data * data = worker_component->data;
  327. data->status = COMPONENT_STATUS_RESET;
  328. }
  329. static int _starpu_sched_component_worker_is_reset_status(struct starpu_sched_component * worker_component)
  330. {
  331. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  332. struct _starpu_worker_component_data * data = worker_component->data;
  333. return (data->status == COMPONENT_STATUS_RESET);
  334. }
  335. static int _starpu_sched_component_worker_is_changed_status(struct starpu_sched_component * worker_component)
  336. {
  337. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  338. struct _starpu_worker_component_data * data = worker_component->data;
  339. return (data->status == COMPONENT_STATUS_CHANGED);
  340. }
  341. static int _starpu_sched_component_worker_is_sleeping_status(struct starpu_sched_component * worker_component)
  342. {
  343. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  344. struct _starpu_worker_component_data * data = worker_component->data;
  345. return (data->status == COMPONENT_STATUS_SLEEPING);
  346. }
  347. #ifndef STARPU_NO_ASSERT
  348. static int _worker_consistant(struct starpu_sched_component * component)
  349. {
  350. int is_a_worker = 0;
  351. int i;
  352. for(i = 0; i<STARPU_NMAXWORKERS; i++)
  353. if(_worker_components[component->tree->sched_ctx_id][i] == component)
  354. is_a_worker = 1;
  355. if(!is_a_worker)
  356. return 0;
  357. struct _starpu_worker_component_data * data = component->data;
  358. if(data->worker)
  359. {
  360. int id = data->worker->workerid;
  361. return (_worker_components[component->tree->sched_ctx_id][id] == component)
  362. && component->nchildren == 0;
  363. }
  364. return 1;
  365. }
  366. #endif
  367. /******************************************************************************
  368. * Simple Worker Components' Interface Functions *
  369. *****************************************************************************/
  370. static void simple_worker_can_pull(struct starpu_sched_component * worker_component)
  371. {
  372. (void) worker_component;
  373. struct _starpu_worker * w = _starpu_sched_component_worker_get_worker(worker_component);
  374. _starpu_sched_component_lock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  375. if(_starpu_sched_component_worker_is_reset_status(worker_component))
  376. _starpu_sched_component_worker_set_changed_status(worker_component);
  377. if(w->workerid == _starpu_worker_get_id())
  378. {
  379. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  380. return;
  381. }
  382. if(_starpu_sched_component_worker_is_sleeping_status(worker_component))
  383. {
  384. starpu_pthread_mutex_t *sched_mutex;
  385. starpu_pthread_cond_t *sched_cond;
  386. starpu_worker_get_sched_condition(w->workerid, &sched_mutex, &sched_cond);
  387. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  388. starpu_wakeup_worker(w->workerid, sched_cond, sched_mutex);
  389. }
  390. else
  391. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  392. }
  393. static int simple_worker_push_task(struct starpu_sched_component * component, struct starpu_task *task)
  394. {
  395. STARPU_ASSERT(starpu_sched_component_is_worker(component));
  396. /*this function take the worker's mutex */
  397. struct _starpu_worker_component_data * data = component->data;
  398. struct _starpu_task_grid * t = _starpu_task_grid_create();
  399. t->task = task;
  400. t->ntasks = 1;
  401. task->workerid = starpu_bitmap_first(component->workers);
  402. #if 1 /* dead lock problem? */
  403. if (starpu_get_prefetch_flag())
  404. {
  405. unsigned memory_node = starpu_worker_get_memory_node(task->workerid);
  406. starpu_prefetch_task_input_on_node(task, memory_node);
  407. }
  408. #endif
  409. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  410. _starpu_worker_task_list_push(data->list, t);
  411. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  412. simple_worker_can_pull(component);
  413. return 0;
  414. }
  415. static struct starpu_task * simple_worker_pull_task(struct starpu_sched_component *component)
  416. {
  417. unsigned workerid = starpu_worker_get_id_check();
  418. struct _starpu_worker_component_data * data = component->data;
  419. struct _starpu_worker_task_list * list = data->list;
  420. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  421. struct starpu_task * task = _starpu_worker_task_list_pop(list);
  422. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  423. if(task)
  424. {
  425. starpu_push_task_end(task);
  426. return task;
  427. }
  428. _starpu_sched_component_lock_worker(component->tree->sched_ctx_id, workerid);
  429. int i;
  430. do
  431. {
  432. _starpu_sched_component_worker_reset_status(component);
  433. for(i=0; i < component->nparents; i++)
  434. {
  435. if(component->parents[i] == NULL)
  436. continue;
  437. else
  438. {
  439. _starpu_sched_component_worker_unlock_scheduling(component->tree->sched_ctx_id);
  440. task = starpu_sched_component_pull_task(component->parents[i],component);
  441. _starpu_sched_component_worker_lock_scheduling(component->tree->sched_ctx_id);
  442. if(task)
  443. break;
  444. }
  445. }
  446. }
  447. while((!task) && _starpu_sched_component_worker_is_changed_status(component));
  448. _starpu_sched_component_worker_set_sleep_status(component);
  449. _starpu_sched_component_unlock_worker(component->tree->sched_ctx_id, workerid);
  450. if(!task)
  451. return NULL;
  452. if(task->cl->type == STARPU_SPMD)
  453. {
  454. if(!starpu_worker_is_combined_worker(workerid))
  455. {
  456. starpu_push_task_end(task);
  457. return task;
  458. }
  459. struct starpu_sched_component * combined_worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, workerid);
  460. starpu_sched_component_push_task(component, combined_worker_component, task);
  461. /* we have pushed a task in queue, so can make a recursive call */
  462. return simple_worker_pull_task(component);
  463. }
  464. if(task)
  465. starpu_push_task_end(task);
  466. return task;
  467. }
  468. static double simple_worker_estimated_end(struct starpu_sched_component * component)
  469. {
  470. struct _starpu_worker_component_data * data = component->data;
  471. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  472. data->list->exp_start = STARPU_MAX(starpu_timing_now(), data->list->exp_start);
  473. double tmp = data->list->exp_end = data->list->exp_start + data->list->exp_len;
  474. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  475. return tmp;
  476. }
  477. static double simple_worker_estimated_load(struct starpu_sched_component * component)
  478. {
  479. struct _starpu_worker * worker = _starpu_sched_component_worker_get_worker(component);
  480. int nb_task = 0;
  481. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  482. struct starpu_task_list list = worker->local_tasks;
  483. struct starpu_task * task;
  484. for(task = starpu_task_list_front(&list);
  485. task != starpu_task_list_end(&list);
  486. task = starpu_task_list_next(task))
  487. nb_task++;
  488. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  489. struct _starpu_worker_component_data * d = component->data;
  490. struct _starpu_worker_task_list * l = d->list;
  491. int ntasks_in_fifo = l ? l->ntasks : 0;
  492. return (double) (nb_task + ntasks_in_fifo)
  493. / starpu_worker_get_relative_speedup(
  494. starpu_worker_get_perf_archtype(starpu_bitmap_first(component->workers), component->tree->sched_ctx_id));
  495. }
  496. static void _worker_component_deinit_data(struct starpu_sched_component * component)
  497. {
  498. struct _starpu_worker_component_data * d = component->data;
  499. _starpu_worker_task_list_destroy(d->list);
  500. if(starpu_sched_component_is_simple_worker(component))
  501. STARPU_PTHREAD_MUTEX_DESTROY(&d->lock);
  502. int i, j;
  503. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  504. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  505. if(_worker_components[j][i] == component)
  506. {
  507. _worker_components[j][i] = NULL;
  508. break;
  509. }
  510. free(d);
  511. }
  512. static struct starpu_sched_component * starpu_sched_component_worker_create(struct starpu_sched_tree *tree, int workerid)
  513. {
  514. STARPU_ASSERT(0 <= workerid && workerid < (int) starpu_worker_get_count());
  515. if(_worker_components[tree->sched_ctx_id][workerid])
  516. return _worker_components[tree->sched_ctx_id][workerid];
  517. struct _starpu_worker * worker = _starpu_get_worker_struct(workerid);
  518. if(worker == NULL)
  519. return NULL;
  520. char name[32];
  521. snprintf(name, sizeof(name), "worker %u", workerid);
  522. struct starpu_sched_component * component = starpu_sched_component_create(tree, name);
  523. struct _starpu_worker_component_data * data = malloc(sizeof(*data));
  524. memset(data, 0, sizeof(*data));
  525. data->worker = worker;
  526. STARPU_PTHREAD_MUTEX_INIT(&data->lock,NULL);
  527. data->status = COMPONENT_STATUS_SLEEPING;
  528. data->list = _starpu_worker_task_list_create();
  529. component->data = data;
  530. component->push_task = simple_worker_push_task;
  531. component->pull_task = simple_worker_pull_task;
  532. component->can_pull = simple_worker_can_pull;
  533. component->estimated_end = simple_worker_estimated_end;
  534. component->estimated_load = simple_worker_estimated_load;
  535. component->deinit_data = _worker_component_deinit_data;
  536. starpu_bitmap_set(component->workers, workerid);
  537. starpu_bitmap_or(component->workers_in_ctx, component->workers);
  538. _worker_components[tree->sched_ctx_id][workerid] = component;
  539. /*
  540. #ifdef STARPU_HAVE_HWLOC
  541. struct _starpu_machine_config *config = _starpu_get_machine_config();
  542. struct _starpu_machine_topology *topology = &config->topology;
  543. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, config->cpu_depth, worker->bindid);
  544. STARPU_ASSERT(obj);
  545. component->obj = obj;
  546. #endif
  547. */
  548. return component;
  549. }
  550. /******************************************************************************
  551. * Combined Worker Components' Interface Functions *
  552. *****************************************************************************/
  553. static void combined_worker_can_pull(struct starpu_sched_component * component)
  554. {
  555. (void) component;
  556. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  557. struct _starpu_worker_component_data * data = component->data;
  558. unsigned workerid = starpu_worker_get_id_check();
  559. int i;
  560. for(i = 0; i < data->combined_worker->worker_size; i++)
  561. {
  562. if(i == workerid)
  563. continue;
  564. int worker = data->combined_worker->combined_workerid[i];
  565. _starpu_sched_component_lock_worker(component->tree->sched_ctx_id, worker);
  566. if(_starpu_sched_component_worker_is_sleeping_status(component))
  567. {
  568. starpu_pthread_mutex_t *sched_mutex;
  569. starpu_pthread_cond_t *sched_cond;
  570. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  571. starpu_wakeup_worker(worker, sched_cond, sched_mutex);
  572. }
  573. if(_starpu_sched_component_worker_is_reset_status(component))
  574. _starpu_sched_component_worker_set_changed_status(component);
  575. _starpu_sched_component_unlock_worker(component->tree->sched_ctx_id, worker);
  576. }
  577. }
  578. static int combined_worker_push_task(struct starpu_sched_component * component, struct starpu_task *task)
  579. {
  580. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  581. struct _starpu_worker_component_data * data = component->data;
  582. STARPU_ASSERT(data->combined_worker && !data->worker);
  583. struct _starpu_combined_worker * combined_worker = data->combined_worker;
  584. STARPU_ASSERT(combined_worker->worker_size >= 1);
  585. struct _starpu_task_grid * task_alias[combined_worker->worker_size];
  586. starpu_parallel_task_barrier_init(task, starpu_bitmap_first(component->workers));
  587. task_alias[0] = _starpu_task_grid_create();
  588. task_alias[0]->task = starpu_task_dup(task);
  589. task_alias[0]->task->workerid = combined_worker->combined_workerid[0];
  590. task_alias[0]->task->destroy = 1;
  591. task_alias[0]->left = NULL;
  592. task_alias[0]->ntasks = combined_worker->worker_size;
  593. int i;
  594. for(i = 1; i < combined_worker->worker_size; i++)
  595. {
  596. task_alias[i] = _starpu_task_grid_create();
  597. task_alias[i]->task = starpu_task_dup(task);
  598. task_alias[i]->task->destroy = 1;
  599. task_alias[i]->task->workerid = combined_worker->combined_workerid[i];
  600. task_alias[i]->left = task_alias[i-1];
  601. task_alias[i - 1]->right = task_alias[i];
  602. task_alias[i]->pntasks = &task_alias[0]->ntasks;
  603. }
  604. starpu_pthread_mutex_t * mutex_to_unlock = NULL;
  605. i = 0;
  606. do
  607. {
  608. struct starpu_sched_component * worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, combined_worker->combined_workerid[i]);
  609. struct _starpu_worker_component_data * worker_data = worker_component->data;
  610. struct _starpu_worker_task_list * list = worker_data->list;
  611. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  612. if(mutex_to_unlock)
  613. STARPU_PTHREAD_MUTEX_UNLOCK(mutex_to_unlock);
  614. mutex_to_unlock = &list->mutex;
  615. _starpu_worker_task_list_push(list, task_alias[i]);
  616. i++;
  617. }
  618. while(i < combined_worker->worker_size);
  619. STARPU_PTHREAD_MUTEX_UNLOCK(mutex_to_unlock);
  620. int workerid = starpu_worker_get_id();
  621. if(-1 == workerid)
  622. {
  623. combined_worker_can_pull(component);
  624. }
  625. else
  626. {
  627. starpu_pthread_mutex_t *worker_sched_mutex;
  628. starpu_pthread_cond_t *worker_sched_cond;
  629. starpu_worker_get_sched_condition(workerid, &worker_sched_mutex, &worker_sched_cond);
  630. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(worker_sched_mutex);
  631. /* wake up all other workers of combined worker */
  632. for(i = 0; i < combined_worker->worker_size; i++)
  633. {
  634. struct starpu_sched_component * worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, combined_worker->combined_workerid[i]);
  635. simple_worker_can_pull(worker_component);
  636. }
  637. combined_worker_can_pull(component);
  638. STARPU_PTHREAD_MUTEX_LOCK_SCHED(worker_sched_mutex);
  639. }
  640. return 0;
  641. }
  642. static double combined_worker_estimated_end(struct starpu_sched_component * component)
  643. {
  644. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  645. struct _starpu_worker_component_data * data = component->data;
  646. struct _starpu_combined_worker * combined_worker = data->combined_worker;
  647. double max = 0.0;
  648. int i;
  649. for(i = 0; i < combined_worker->worker_size; i++)
  650. {
  651. data = _worker_components[component->tree->sched_ctx_id][combined_worker->combined_workerid[i]]->data;
  652. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  653. double tmp = data->list->exp_end;
  654. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  655. max = tmp > max ? tmp : max;
  656. }
  657. return max;
  658. }
  659. static double combined_worker_estimated_load(struct starpu_sched_component * component)
  660. {
  661. struct _starpu_worker_component_data * d = component->data;
  662. struct _starpu_combined_worker * c = d->combined_worker;
  663. double load = 0;
  664. int i;
  665. for(i = 0; i < c->worker_size; i++)
  666. {
  667. struct starpu_sched_component * n = starpu_sched_component_worker_get(component->tree->sched_ctx_id, c->combined_workerid[i]);
  668. load += n->estimated_load(n);
  669. }
  670. return load;
  671. }
  672. static struct starpu_sched_component * starpu_sched_component_combined_worker_create(struct starpu_sched_tree *tree, int workerid)
  673. {
  674. STARPU_ASSERT(0 <= workerid && workerid < STARPU_NMAXWORKERS);
  675. if(_worker_components[tree->sched_ctx_id][workerid])
  676. return _worker_components[tree->sched_ctx_id][workerid];
  677. struct _starpu_combined_worker * combined_worker = _starpu_get_combined_worker_struct(workerid);
  678. if(combined_worker == NULL)
  679. return NULL;
  680. struct starpu_sched_component * component = starpu_sched_component_create(tree, "combined_worker");
  681. struct _starpu_worker_component_data * data = malloc(sizeof(*data));
  682. memset(data, 0, sizeof(*data));
  683. data->combined_worker = combined_worker;
  684. data->status = COMPONENT_STATUS_SLEEPING;
  685. component->data = data;
  686. component->push_task = combined_worker_push_task;
  687. component->pull_task = NULL;
  688. component->estimated_end = combined_worker_estimated_end;
  689. component->estimated_load = combined_worker_estimated_load;
  690. component->can_pull = combined_worker_can_pull;
  691. component->deinit_data = _worker_component_deinit_data;
  692. starpu_bitmap_set(component->workers, workerid);
  693. starpu_bitmap_or(component->workers_in_ctx, component->workers);
  694. _worker_components[tree->sched_ctx_id][workerid] = component;
  695. #ifdef STARPU_HAVE_HWLOC
  696. struct _starpu_machine_config *config = _starpu_get_machine_config();
  697. struct _starpu_machine_topology *topology = &config->topology;
  698. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, config->cpu_depth, combined_worker->combined_workerid[0]);
  699. STARPU_ASSERT(obj);
  700. component->obj = obj;
  701. #endif
  702. return component;
  703. }
  704. /******************************************************************************
  705. * Worker Components' Public Helper Functions (Part 2) *
  706. *****************************************************************************/
  707. void _starpu_sched_component_lock_all_workers(unsigned sched_ctx_id)
  708. {
  709. unsigned i;
  710. for(i = 0; i < starpu_worker_get_count(); i++)
  711. _starpu_sched_component_lock_worker(sched_ctx_id, i);
  712. }
  713. void _starpu_sched_component_unlock_all_workers(unsigned sched_ctx_id)
  714. {
  715. unsigned i;
  716. for(i = 0; i < starpu_worker_get_count(); i++)
  717. _starpu_sched_component_unlock_worker(sched_ctx_id, i);
  718. }
  719. void _starpu_sched_component_workers_destroy(void)
  720. {
  721. int i, j;
  722. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  723. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  724. if (_worker_components[j][i])
  725. starpu_sched_component_destroy(_worker_components[j][i]);
  726. }
  727. int starpu_sched_component_worker_get_workerid(struct starpu_sched_component * worker_component)
  728. {
  729. #ifndef STARPU_NO_ASSERT
  730. STARPU_ASSERT(_worker_consistant(worker_component));
  731. #endif
  732. STARPU_ASSERT(1 == starpu_bitmap_cardinal(worker_component->workers));
  733. return starpu_bitmap_first(worker_component->workers);
  734. }
  735. void starpu_sched_component_worker_pre_exec_hook(struct starpu_task * task)
  736. {
  737. if(!isnan(task->predicted))
  738. {
  739. unsigned sched_ctx_id = task->sched_ctx;
  740. struct _starpu_worker_task_list * list = _worker_get_list(sched_ctx_id);
  741. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  742. list->exp_start = starpu_timing_now() + task->predicted;
  743. if(list->ntasks == 0)
  744. {
  745. list->exp_end = list->exp_start;
  746. list->exp_len = 0.0;
  747. }
  748. else
  749. list->exp_end = list->exp_start + list->exp_len;
  750. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  751. }
  752. }
  753. void starpu_sched_component_worker_post_exec_hook(struct starpu_task * task)
  754. {
  755. if(task->execute_on_a_specific_worker)
  756. return;
  757. unsigned sched_ctx_id = task->sched_ctx;
  758. struct _starpu_worker_task_list * list = _worker_get_list(sched_ctx_id);
  759. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  760. list->exp_start = starpu_timing_now();
  761. list->exp_end = list->exp_start + list->exp_len;
  762. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  763. }
  764. int starpu_sched_component_is_simple_worker(struct starpu_sched_component * component)
  765. {
  766. return component->push_task == simple_worker_push_task;
  767. }
  768. int starpu_sched_component_is_combined_worker(struct starpu_sched_component * component)
  769. {
  770. return component->push_task == combined_worker_push_task;
  771. }
  772. int starpu_sched_component_is_worker(struct starpu_sched_component * component)
  773. {
  774. return starpu_sched_component_is_simple_worker(component)
  775. || starpu_sched_component_is_combined_worker(component);
  776. }
  777. /* As Worker Components' creating functions are protected, this function allows
  778. * the user to get a Worker Component from a worker id */
  779. struct starpu_sched_component * starpu_sched_component_worker_get(unsigned sched_ctx, int workerid)
  780. {
  781. STARPU_ASSERT(workerid >= 0 && workerid < STARPU_NMAXWORKERS);
  782. /* we may need to take a mutex here */
  783. if(_worker_components[sched_ctx][workerid])
  784. return _worker_components[sched_ctx][workerid];
  785. else
  786. {
  787. struct starpu_sched_component * component;
  788. if(workerid < (int) starpu_worker_get_count())
  789. component = starpu_sched_component_worker_create(starpu_sched_tree_get(sched_ctx), workerid);
  790. else
  791. component = starpu_sched_component_combined_worker_create(starpu_sched_tree_get(sched_ctx), workerid);
  792. _worker_components[sched_ctx][workerid] = component;
  793. return component;
  794. }
  795. }