component_worker.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2014, 2015 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. int workerid = starpu_worker_get_id();
  131. STARPU_ASSERT(0 <= workerid && workerid < (int) 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. int workerid = starpu_worker_get_id();
  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. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  297. }
  298. static void _starpu_sched_component_worker_unlock_scheduling(unsigned sched_ctx_id)
  299. {
  300. int workerid = starpu_worker_get_id();
  301. starpu_pthread_mutex_t *sched_mutex;
  302. starpu_pthread_cond_t *sched_cond;
  303. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  304. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  305. _starpu_sched_component_unlock_worker(sched_ctx_id, workerid);
  306. }
  307. static void _starpu_sched_component_worker_set_sleep_status(struct starpu_sched_component * worker_component)
  308. {
  309. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  310. struct _starpu_worker_component_data * data = worker_component->data;
  311. data->status = COMPONENT_STATUS_SLEEPING;
  312. }
  313. static void _starpu_sched_component_worker_set_changed_status(struct starpu_sched_component * worker_component)
  314. {
  315. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  316. struct _starpu_worker_component_data * data = worker_component->data;
  317. data->status = COMPONENT_STATUS_CHANGED;
  318. }
  319. static void _starpu_sched_component_worker_reset_status(struct starpu_sched_component * worker_component)
  320. {
  321. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  322. struct _starpu_worker_component_data * data = worker_component->data;
  323. data->status = COMPONENT_STATUS_RESET;
  324. }
  325. static int _starpu_sched_component_worker_is_reset_status(struct starpu_sched_component * worker_component)
  326. {
  327. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  328. struct _starpu_worker_component_data * data = worker_component->data;
  329. return (data->status == COMPONENT_STATUS_RESET);
  330. }
  331. static int _starpu_sched_component_worker_is_changed_status(struct starpu_sched_component * worker_component)
  332. {
  333. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  334. struct _starpu_worker_component_data * data = worker_component->data;
  335. return (data->status == COMPONENT_STATUS_CHANGED);
  336. }
  337. static int _starpu_sched_component_worker_is_sleeping_status(struct starpu_sched_component * worker_component)
  338. {
  339. STARPU_ASSERT(starpu_sched_component_is_worker(worker_component));
  340. struct _starpu_worker_component_data * data = worker_component->data;
  341. return (data->status == COMPONENT_STATUS_SLEEPING);
  342. }
  343. #ifndef STARPU_NO_ASSERT
  344. static int _worker_consistant(struct starpu_sched_component * component)
  345. {
  346. int is_a_worker = 0;
  347. int i;
  348. for(i = 0; i<STARPU_NMAXWORKERS; i++)
  349. if(_worker_components[component->tree->sched_ctx_id][i] == component)
  350. is_a_worker = 1;
  351. if(!is_a_worker)
  352. return 0;
  353. struct _starpu_worker_component_data * data = component->data;
  354. if(data->worker)
  355. {
  356. int id = data->worker->workerid;
  357. return (_worker_components[component->tree->sched_ctx_id][id] == component)
  358. && component->nchildren == 0;
  359. }
  360. return 1;
  361. }
  362. #endif
  363. /******************************************************************************
  364. * Simple Worker Components' Interface Functions *
  365. *****************************************************************************/
  366. static void simple_worker_can_pull(struct starpu_sched_component * worker_component)
  367. {
  368. (void) worker_component;
  369. struct _starpu_worker * w = _starpu_sched_component_worker_get_worker(worker_component);
  370. _starpu_sched_component_lock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  371. if(_starpu_sched_component_worker_is_reset_status(worker_component))
  372. _starpu_sched_component_worker_set_changed_status(worker_component);
  373. if(w->workerid == starpu_worker_get_id())
  374. {
  375. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  376. return;
  377. }
  378. if(_starpu_sched_component_worker_is_sleeping_status(worker_component))
  379. {
  380. starpu_pthread_mutex_t *sched_mutex;
  381. starpu_pthread_cond_t *sched_cond;
  382. starpu_worker_get_sched_condition(w->workerid, &sched_mutex, &sched_cond);
  383. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  384. starpu_wakeup_worker(w->workerid, sched_cond, sched_mutex);
  385. }
  386. else
  387. _starpu_sched_component_unlock_worker(worker_component->tree->sched_ctx_id, w->workerid);
  388. }
  389. static int simple_worker_push_task(struct starpu_sched_component * component, struct starpu_task *task)
  390. {
  391. STARPU_ASSERT(starpu_sched_component_is_worker(component));
  392. /*this function take the worker's mutex */
  393. struct _starpu_worker_component_data * data = component->data;
  394. struct _starpu_task_grid * t = _starpu_task_grid_create();
  395. t->task = task;
  396. t->ntasks = 1;
  397. task->workerid = starpu_bitmap_first(component->workers);
  398. #if 1 /* dead lock problem? */
  399. if (starpu_get_prefetch_flag())
  400. {
  401. unsigned memory_node = starpu_worker_get_memory_node(task->workerid);
  402. starpu_prefetch_task_input_on_node(task, memory_node);
  403. }
  404. #endif
  405. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  406. _starpu_worker_task_list_push(data->list, t);
  407. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  408. simple_worker_can_pull(component);
  409. return 0;
  410. }
  411. static struct starpu_task * simple_worker_pull_task(struct starpu_sched_component *component)
  412. {
  413. int workerid = starpu_worker_get_id();
  414. struct _starpu_worker_component_data * data = component->data;
  415. struct _starpu_worker_task_list * list = data->list;
  416. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  417. struct starpu_task * task = _starpu_worker_task_list_pop(list);
  418. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  419. if(task)
  420. {
  421. starpu_push_task_end(task);
  422. return task;
  423. }
  424. _starpu_sched_component_lock_worker(component->tree->sched_ctx_id, workerid);
  425. int i;
  426. do
  427. {
  428. _starpu_sched_component_worker_reset_status(component);
  429. for(i=0; i < component->nparents; i++)
  430. {
  431. if(component->parents[i] == NULL)
  432. continue;
  433. else
  434. {
  435. _starpu_sched_component_worker_unlock_scheduling(component->tree->sched_ctx_id);
  436. task = starpu_sched_component_pull_task(component->parents[i],component);
  437. _starpu_sched_component_worker_lock_scheduling(component->tree->sched_ctx_id);
  438. if(task)
  439. break;
  440. }
  441. }
  442. }
  443. while((!task) && _starpu_sched_component_worker_is_changed_status(component));
  444. _starpu_sched_component_worker_set_sleep_status(component);
  445. _starpu_sched_component_unlock_worker(component->tree->sched_ctx_id, workerid);
  446. if(!task)
  447. return NULL;
  448. if(task->cl->type == STARPU_SPMD)
  449. {
  450. if(!starpu_worker_is_combined_worker(workerid))
  451. {
  452. starpu_push_task_end(task);
  453. return task;
  454. }
  455. struct starpu_sched_component * combined_worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, workerid);
  456. starpu_sched_component_push_task(component, combined_worker_component, task);
  457. /* we have pushed a task in queue, so can make a recursive call */
  458. return simple_worker_pull_task(component);
  459. }
  460. if(task)
  461. starpu_push_task_end(task);
  462. return task;
  463. }
  464. static double simple_worker_estimated_end(struct starpu_sched_component * component)
  465. {
  466. struct _starpu_worker_component_data * data = component->data;
  467. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  468. data->list->exp_start = STARPU_MAX(starpu_timing_now(), data->list->exp_start);
  469. double tmp = data->list->exp_end = data->list->exp_start + data->list->exp_len;
  470. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  471. return tmp;
  472. }
  473. static double simple_worker_estimated_load(struct starpu_sched_component * component)
  474. {
  475. struct _starpu_worker * worker = _starpu_sched_component_worker_get_worker(component);
  476. int nb_task = 0;
  477. STARPU_PTHREAD_MUTEX_LOCK(&worker->mutex);
  478. struct starpu_task_list list = worker->local_tasks;
  479. struct starpu_task * task;
  480. for(task = starpu_task_list_front(&list);
  481. task != starpu_task_list_end(&list);
  482. task = starpu_task_list_next(task))
  483. nb_task++;
  484. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->mutex);
  485. struct _starpu_worker_component_data * d = component->data;
  486. struct _starpu_worker_task_list * l = d->list;
  487. int ntasks_in_fifo = l ? l->ntasks : 0;
  488. return (double) (nb_task + ntasks_in_fifo)
  489. / starpu_worker_get_relative_speedup(
  490. starpu_worker_get_perf_archtype(starpu_bitmap_first(component->workers), component->tree->sched_ctx_id));
  491. }
  492. static void _worker_component_deinit_data(struct starpu_sched_component * component)
  493. {
  494. struct _starpu_worker_component_data * d = component->data;
  495. _starpu_worker_task_list_destroy(d->list);
  496. if(starpu_sched_component_is_simple_worker(component))
  497. STARPU_PTHREAD_MUTEX_DESTROY(&d->lock);
  498. int i, j;
  499. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  500. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  501. if(_worker_components[j][i] == component)
  502. {
  503. _worker_components[j][i] = NULL;
  504. break;
  505. }
  506. free(d);
  507. }
  508. static struct starpu_sched_component * starpu_sched_component_worker_create(struct starpu_sched_tree *tree, int workerid)
  509. {
  510. STARPU_ASSERT(0 <= workerid && workerid < (int) starpu_worker_get_count());
  511. if(_worker_components[tree->sched_ctx_id][workerid])
  512. return _worker_components[tree->sched_ctx_id][workerid];
  513. struct _starpu_worker * worker = _starpu_get_worker_struct(workerid);
  514. if(worker == NULL)
  515. return NULL;
  516. char name[32];
  517. snprintf(name, sizeof(name), "worker %u", workerid);
  518. struct starpu_sched_component * component = starpu_sched_component_create(tree, name);
  519. struct _starpu_worker_component_data * data = malloc(sizeof(*data));
  520. memset(data, 0, sizeof(*data));
  521. data->worker = worker;
  522. STARPU_PTHREAD_MUTEX_INIT(&data->lock,NULL);
  523. data->status = COMPONENT_STATUS_SLEEPING;
  524. data->list = _starpu_worker_task_list_create();
  525. component->data = data;
  526. component->push_task = simple_worker_push_task;
  527. component->pull_task = simple_worker_pull_task;
  528. component->can_pull = simple_worker_can_pull;
  529. component->estimated_end = simple_worker_estimated_end;
  530. component->estimated_load = simple_worker_estimated_load;
  531. component->deinit_data = _worker_component_deinit_data;
  532. starpu_bitmap_set(component->workers, workerid);
  533. starpu_bitmap_or(component->workers_in_ctx, component->workers);
  534. _worker_components[tree->sched_ctx_id][workerid] = component;
  535. /*
  536. #ifdef STARPU_HAVE_HWLOC
  537. struct _starpu_machine_config *config = _starpu_get_machine_config();
  538. struct _starpu_machine_topology *topology = &config->topology;
  539. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, config->cpu_depth, worker->bindid);
  540. STARPU_ASSERT(obj);
  541. component->obj = obj;
  542. #endif
  543. */
  544. return component;
  545. }
  546. /******************************************************************************
  547. * Combined Worker Components' Interface Functions *
  548. *****************************************************************************/
  549. static void combined_worker_can_pull(struct starpu_sched_component * component)
  550. {
  551. (void) component;
  552. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  553. struct _starpu_worker_component_data * data = component->data;
  554. int workerid = starpu_worker_get_id();
  555. int i;
  556. for(i = 0; i < data->combined_worker->worker_size; i++)
  557. {
  558. if(i == workerid)
  559. continue;
  560. int worker = data->combined_worker->combined_workerid[i];
  561. _starpu_sched_component_lock_worker(component->tree->sched_ctx_id, worker);
  562. if(_starpu_sched_component_worker_is_sleeping_status(component))
  563. {
  564. starpu_pthread_mutex_t *sched_mutex;
  565. starpu_pthread_cond_t *sched_cond;
  566. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  567. starpu_wakeup_worker(worker, sched_cond, sched_mutex);
  568. }
  569. if(_starpu_sched_component_worker_is_reset_status(component))
  570. _starpu_sched_component_worker_set_changed_status(component);
  571. _starpu_sched_component_unlock_worker(component->tree->sched_ctx_id, worker);
  572. }
  573. }
  574. static int combined_worker_push_task(struct starpu_sched_component * component, struct starpu_task *task)
  575. {
  576. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  577. struct _starpu_worker_component_data * data = component->data;
  578. STARPU_ASSERT(data->combined_worker && !data->worker);
  579. struct _starpu_combined_worker * combined_worker = data->combined_worker;
  580. STARPU_ASSERT(combined_worker->worker_size >= 1);
  581. struct _starpu_task_grid * task_alias[combined_worker->worker_size];
  582. starpu_parallel_task_barrier_init(task, starpu_bitmap_first(component->workers));
  583. task_alias[0] = _starpu_task_grid_create();
  584. task_alias[0]->task = starpu_task_dup(task);
  585. task_alias[0]->task->workerid = combined_worker->combined_workerid[0];
  586. task_alias[0]->left = NULL;
  587. task_alias[0]->ntasks = combined_worker->worker_size;
  588. int i;
  589. for(i = 1; i < combined_worker->worker_size; i++)
  590. {
  591. task_alias[i] = _starpu_task_grid_create();
  592. task_alias[i]->task = starpu_task_dup(task);
  593. task_alias[i]->task->workerid = combined_worker->combined_workerid[i];
  594. task_alias[i]->left = task_alias[i-1];
  595. task_alias[i - 1]->right = task_alias[i];
  596. task_alias[i]->pntasks = &task_alias[0]->ntasks;
  597. }
  598. starpu_pthread_mutex_t * mutex_to_unlock = NULL;
  599. i = 0;
  600. do
  601. {
  602. struct starpu_sched_component * worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, combined_worker->combined_workerid[i]);
  603. struct _starpu_worker_component_data * worker_data = worker_component->data;
  604. struct _starpu_worker_task_list * list = worker_data->list;
  605. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  606. if(mutex_to_unlock)
  607. STARPU_PTHREAD_MUTEX_UNLOCK(mutex_to_unlock);
  608. mutex_to_unlock = &list->mutex;
  609. _starpu_worker_task_list_push(list, task_alias[i]);
  610. i++;
  611. }
  612. while(i < combined_worker->worker_size);
  613. STARPU_PTHREAD_MUTEX_UNLOCK(mutex_to_unlock);
  614. int workerid = starpu_worker_get_id();
  615. if(-1 == workerid)
  616. {
  617. combined_worker_can_pull(component);
  618. }
  619. else
  620. {
  621. starpu_pthread_mutex_t *worker_sched_mutex;
  622. starpu_pthread_cond_t *worker_sched_cond;
  623. starpu_worker_get_sched_condition(workerid, &worker_sched_mutex, &worker_sched_cond);
  624. STARPU_PTHREAD_MUTEX_UNLOCK(worker_sched_mutex);
  625. /* wake up all other workers of combined worker */
  626. for(i = 0; i < combined_worker->worker_size; i++)
  627. {
  628. struct starpu_sched_component * worker_component = starpu_sched_component_worker_get(component->tree->sched_ctx_id, combined_worker->combined_workerid[i]);
  629. simple_worker_can_pull(worker_component);
  630. }
  631. combined_worker_can_pull(component);
  632. STARPU_PTHREAD_MUTEX_LOCK(worker_sched_mutex);
  633. }
  634. return 0;
  635. }
  636. static double combined_worker_estimated_end(struct starpu_sched_component * component)
  637. {
  638. STARPU_ASSERT(starpu_sched_component_is_combined_worker(component));
  639. struct _starpu_worker_component_data * data = component->data;
  640. struct _starpu_combined_worker * combined_worker = data->combined_worker;
  641. double max = 0.0;
  642. int i;
  643. for(i = 0; i < combined_worker->worker_size; i++)
  644. {
  645. data = _worker_components[component->tree->sched_ctx_id][combined_worker->combined_workerid[i]]->data;
  646. STARPU_PTHREAD_MUTEX_LOCK(&data->list->mutex);
  647. double tmp = data->list->exp_end;
  648. STARPU_PTHREAD_MUTEX_UNLOCK(&data->list->mutex);
  649. max = tmp > max ? tmp : max;
  650. }
  651. return max;
  652. }
  653. static double combined_worker_estimated_load(struct starpu_sched_component * component)
  654. {
  655. struct _starpu_worker_component_data * d = component->data;
  656. struct _starpu_combined_worker * c = d->combined_worker;
  657. double load = 0;
  658. int i;
  659. for(i = 0; i < c->worker_size; i++)
  660. {
  661. struct starpu_sched_component * n = starpu_sched_component_worker_get(component->tree->sched_ctx_id, c->combined_workerid[i]);
  662. load += n->estimated_load(n);
  663. }
  664. return load;
  665. }
  666. static struct starpu_sched_component * starpu_sched_component_combined_worker_create(struct starpu_sched_tree *tree, int workerid)
  667. {
  668. STARPU_ASSERT(0 <= workerid && workerid < STARPU_NMAXWORKERS);
  669. if(_worker_components[tree->sched_ctx_id][workerid])
  670. return _worker_components[tree->sched_ctx_id][workerid];
  671. struct _starpu_combined_worker * combined_worker = _starpu_get_combined_worker_struct(workerid);
  672. if(combined_worker == NULL)
  673. return NULL;
  674. struct starpu_sched_component * component = starpu_sched_component_create(tree, "combined_worker");
  675. struct _starpu_worker_component_data * data = malloc(sizeof(*data));
  676. memset(data, 0, sizeof(*data));
  677. data->combined_worker = combined_worker;
  678. data->status = COMPONENT_STATUS_SLEEPING;
  679. component->data = data;
  680. component->push_task = combined_worker_push_task;
  681. component->pull_task = NULL;
  682. component->estimated_end = combined_worker_estimated_end;
  683. component->estimated_load = combined_worker_estimated_load;
  684. component->can_pull = combined_worker_can_pull;
  685. component->deinit_data = _worker_component_deinit_data;
  686. starpu_bitmap_set(component->workers, workerid);
  687. starpu_bitmap_or(component->workers_in_ctx, component->workers);
  688. _worker_components[tree->sched_ctx_id][workerid] = component;
  689. #ifdef STARPU_HAVE_HWLOC
  690. struct _starpu_machine_config *config = _starpu_get_machine_config();
  691. struct _starpu_machine_topology *topology = &config->topology;
  692. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, config->cpu_depth, combined_worker->combined_workerid[0]);
  693. STARPU_ASSERT(obj);
  694. component->obj = obj;
  695. #endif
  696. return component;
  697. }
  698. /******************************************************************************
  699. * Worker Components' Public Helper Functions (Part 2) *
  700. *****************************************************************************/
  701. void _starpu_sched_component_lock_all_workers(unsigned sched_ctx_id)
  702. {
  703. unsigned i;
  704. for(i = 0; i < starpu_worker_get_count(); i++)
  705. _starpu_sched_component_lock_worker(sched_ctx_id, i);
  706. }
  707. void _starpu_sched_component_unlock_all_workers(unsigned sched_ctx_id)
  708. {
  709. unsigned i;
  710. for(i = 0; i < starpu_worker_get_count(); i++)
  711. _starpu_sched_component_unlock_worker(sched_ctx_id, i);
  712. }
  713. void _starpu_sched_component_workers_destroy(void)
  714. {
  715. int i, j;
  716. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  717. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  718. if (_worker_components[j][i])
  719. starpu_sched_component_destroy(_worker_components[j][i]);
  720. }
  721. int starpu_sched_component_worker_get_workerid(struct starpu_sched_component * worker_component)
  722. {
  723. #ifndef STARPU_NO_ASSERT
  724. STARPU_ASSERT(_worker_consistant(worker_component));
  725. #endif
  726. STARPU_ASSERT(1 == starpu_bitmap_cardinal(worker_component->workers));
  727. return starpu_bitmap_first(worker_component->workers);
  728. }
  729. void starpu_sched_component_worker_pre_exec_hook(struct starpu_task * task)
  730. {
  731. if(!isnan(task->predicted))
  732. {
  733. unsigned sched_ctx_id = task->sched_ctx;
  734. struct _starpu_worker_task_list * list = _worker_get_list(sched_ctx_id);
  735. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  736. list->exp_start = starpu_timing_now() + task->predicted;
  737. if(list->ntasks == 0)
  738. {
  739. list->exp_end = list->exp_start;
  740. list->exp_len = 0.0;
  741. }
  742. else
  743. list->exp_end = list->exp_start + list->exp_len;
  744. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  745. }
  746. }
  747. void starpu_sched_component_worker_post_exec_hook(struct starpu_task * task)
  748. {
  749. if(task->execute_on_a_specific_worker)
  750. return;
  751. unsigned sched_ctx_id = task->sched_ctx;
  752. struct _starpu_worker_task_list * list = _worker_get_list(sched_ctx_id);
  753. STARPU_PTHREAD_MUTEX_LOCK(&list->mutex);
  754. list->exp_start = starpu_timing_now();
  755. list->exp_end = list->exp_start + list->exp_len;
  756. STARPU_PTHREAD_MUTEX_UNLOCK(&list->mutex);
  757. }
  758. int starpu_sched_component_is_simple_worker(struct starpu_sched_component * component)
  759. {
  760. return component->push_task == simple_worker_push_task;
  761. }
  762. int starpu_sched_component_is_combined_worker(struct starpu_sched_component * component)
  763. {
  764. return component->push_task == combined_worker_push_task;
  765. }
  766. int starpu_sched_component_is_worker(struct starpu_sched_component * component)
  767. {
  768. return starpu_sched_component_is_simple_worker(component)
  769. || starpu_sched_component_is_combined_worker(component);
  770. }
  771. /* As Worker Components' creating functions are protected, this function allows
  772. * the user to get a Worker Component from a worker id */
  773. struct starpu_sched_component * starpu_sched_component_worker_get(unsigned sched_ctx, int workerid)
  774. {
  775. STARPU_ASSERT(workerid >= 0 && workerid < STARPU_NMAXWORKERS);
  776. /* we may need to take a mutex here */
  777. if(_worker_components[sched_ctx][workerid])
  778. return _worker_components[sched_ctx][workerid];
  779. else
  780. {
  781. struct starpu_sched_component * component;
  782. if(workerid < (int) starpu_worker_get_count())
  783. component = starpu_sched_component_worker_create(starpu_sched_tree_get(sched_ctx), workerid);
  784. else
  785. component = starpu_sched_component_combined_worker_create(starpu_sched_tree_get(sched_ctx), workerid);
  786. _worker_components[sched_ctx][workerid] = component;
  787. return component;
  788. }
  789. }