starpu_sched_component.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Simon Archipoff
  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. #ifndef __STARPU_SCHED_COMPONENT_H__
  17. #define __STARPU_SCHED_COMPONENT_H__
  18. #include <starpu.h>
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. #ifdef STARPU_HAVE_HWLOC
  24. #include <hwloc.h>
  25. #endif
  26. enum starpu_sched_component_properties
  27. {
  28. STARPU_SCHED_COMPONENT_HOMOGENEOUS = (1<<0),
  29. STARPU_SCHED_COMPONENT_SINGLE_MEMORY_NODE = (1<<1)
  30. };
  31. #define STARPU_SCHED_COMPONENT_IS_HOMOGENEOUS(component) ((component)->properties & STARPU_SCHED_COMPONENT_HOMOGENEOUS)
  32. #define STARPU_SCHED_COMPONENT_IS_SINGLE_MEMORY_NODE(component) ((component)->properties & STARPU_SCHED_COMPONENT_SINGLE_MEMORY_NODE)
  33. /* struct starpu_sched_component are scheduler modules, a scheduler is a tree-like
  34. * structure of them, some parts of scheduler can be shared by several contexes
  35. * to perform some local optimisations, so, for all components, a list of parent is
  36. * defined indexed by sched_ctx_id
  37. *
  38. * they embed there specialised method in a pseudo object-style, so calls are like component->push_task(component,task)
  39. *
  40. */
  41. struct starpu_sched_component
  42. {
  43. /* The tree containing the component */
  44. struct starpu_sched_tree *tree;
  45. /* the set of workers in the component's subtree
  46. */
  47. struct starpu_bitmap *workers;
  48. /* the workers available in context
  49. * this member is set with :
  50. * component->workers UNION tree->workers UNION
  51. * component->child[i]->workers_in_ctx iff exist x such as component->children[i]->parents[x] == component
  52. */
  53. struct starpu_bitmap *workers_in_ctx;
  54. /* component's private data, no restriction on use
  55. */
  56. void *data;
  57. /* the numbers of component's children
  58. */
  59. int nchildren;
  60. /* the vector of component's children
  61. */
  62. struct starpu_sched_component **children;
  63. /* the numbers of component's parents
  64. */
  65. int nparents;
  66. /* may be shared by several contexts
  67. * so we need several parents
  68. */
  69. struct starpu_sched_component **parents;
  70. void (*add_child)(struct starpu_sched_component *component, struct starpu_sched_component *child);
  71. void (*remove_child)(struct starpu_sched_component *component, struct starpu_sched_component *child);
  72. void (*add_parent)(struct starpu_sched_component *component, struct starpu_sched_component *parent);
  73. void (*remove_parent)(struct starpu_sched_component *component, struct starpu_sched_component *parent);
  74. /* component->push_task(component, task)
  75. * this function is called to push a task on component subtree, this can either
  76. * perform a recursive call on a child or store the task in the component, then
  77. * it will be returned by a further pull_task call
  78. *
  79. * the caller must ensure that component is able to execute task
  80. */
  81. int (*push_task)(struct starpu_sched_component *,
  82. struct starpu_task *);
  83. /* this function is called by workers to get a task on them parents
  84. * this function should first return a localy stored task or perform
  85. * a recursive call on parent
  86. *
  87. * a default implementation simply do a recursive call on parent
  88. */
  89. struct starpu_task * (*pull_task)(struct starpu_sched_component *);
  90. /* This function is called by a component which implements a queue, allowing it to
  91. * signify to its parents that an empty slot is available in its queue.
  92. * The basic implementation of this function is a recursive call to its
  93. * parents, the user have to specify a personally-made function to catch those
  94. * calls.
  95. */
  96. int (*can_push)(struct starpu_sched_component *component);
  97. /* This function allow a component to wake up a worker.
  98. * It is currently called by component which implements a queue, to signify to
  99. * its children that a task have been pushed in its local queue, and is
  100. * available to been popped by a worker, for example.
  101. * The basic implementation of this function is a recursive call to
  102. * its children, until at least one worker have been woken up.
  103. */
  104. void (*can_pull)(struct starpu_sched_component *component);
  105. /* this function is an heuristic that compute load of subtree, basicaly
  106. * it compute
  107. * estimated_load(component) = sum(estimated_load(component_children)) +
  108. * nb_local_tasks / average(relative_speedup(underlying_worker))
  109. */
  110. double (*estimated_load)(struct starpu_sched_component *component);
  111. double (*estimated_end)(struct starpu_sched_component *component);
  112. /* this function is called by starpu_sched_component_destroy just before freeing component
  113. */
  114. void (*deinit_data)(struct starpu_sched_component *component);
  115. /* this function is called for each component when workers are added or removed from a context
  116. */
  117. void (*notify_change_workers)(struct starpu_sched_component *component);
  118. /* is_homogeneous is 0 if workers in the component's subtree are heterogeneous,
  119. * this field is set and updated automaticaly, you shouldn't write on it
  120. */
  121. int properties;
  122. #ifdef STARPU_HAVE_HWLOC
  123. /* in case of a modularized scheduler, this is set to the part of
  124. * topology that is binded to this component, eg: a numa node for a ws
  125. * component that would balance load between underlying sockets
  126. */
  127. hwloc_obj_t obj;
  128. #else
  129. void *obj;
  130. #endif
  131. };
  132. struct starpu_sched_tree
  133. {
  134. struct starpu_sched_component *root;
  135. struct starpu_bitmap *workers;
  136. unsigned sched_ctx_id;
  137. /* this array store worker components */
  138. struct starpu_sched_component *worker_components[STARPU_NMAXWORKERS];
  139. /* this lock is used to protect the scheduler,
  140. * it is taken in read mode pushing a task
  141. * and in write mode for adding or removing workers
  142. */
  143. starpu_pthread_mutex_t lock;
  144. };
  145. /*******************************************************************************
  146. * Scheduling Tree's Interface *
  147. ******************************************************************************/
  148. /* create an empty tree
  149. */
  150. struct starpu_sched_tree *starpu_sched_tree_create(unsigned sched_ctx_id);
  151. void starpu_sched_tree_destroy(struct starpu_sched_tree *tree);
  152. struct starpu_sched_tree *starpu_get_tree(unsigned sched_ctx_id);
  153. /* destroy component and all his child
  154. * except if they are shared between several contexts
  155. */
  156. void starpu_sched_component_destroy_rec(struct starpu_sched_component *component);
  157. /* update all the component->workers member recursively
  158. */
  159. void starpu_sched_tree_update_workers(struct starpu_sched_tree *t);
  160. /* idem for workers_in_ctx
  161. */
  162. void starpu_sched_tree_update_workers_in_ctx(struct starpu_sched_tree *t);
  163. int starpu_sched_tree_push_task(struct starpu_task *task);
  164. struct starpu_task *starpu_sched_tree_pop_task();
  165. void starpu_sched_tree_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers);
  166. void starpu_sched_tree_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers);
  167. /*******************************************************************************
  168. * Generic Scheduling Component's Interface *
  169. ******************************************************************************/
  170. struct starpu_sched_component *starpu_sched_component_create(struct starpu_sched_tree *tree);
  171. void starpu_sched_component_destroy(struct starpu_sched_component *component);
  172. int starpu_sched_component_can_execute_task(struct starpu_sched_component *component, struct starpu_task *task);
  173. int STARPU_WARN_UNUSED_RESULT starpu_sched_component_execute_preds(struct starpu_sched_component *component, struct starpu_task *task, double *length);
  174. double starpu_sched_component_transfer_length(struct starpu_sched_component *component, struct starpu_task *task);
  175. void starpu_sched_component_prefetch_on_node(struct starpu_sched_component *component, struct starpu_task *task);
  176. /*******************************************************************************
  177. * Worker Component's Interface *
  178. ******************************************************************************/
  179. /* no public create function for workers because we dont want to have several component_worker for a single workerid */
  180. struct starpu_sched_component *starpu_sched_component_worker_get(unsigned sched_ctx, int workerid);
  181. int starpu_sched_component_worker_get_workerid(struct starpu_sched_component *worker_component);
  182. /* this function compare the available function of the component with the standard available for worker components*/
  183. int starpu_sched_component_is_worker(struct starpu_sched_component *component);
  184. int starpu_sched_component_is_simple_worker(struct starpu_sched_component *component);
  185. int starpu_sched_component_is_combined_worker(struct starpu_sched_component *component);
  186. void starpu_sched_component_worker_pre_exec_hook(struct starpu_task *task);
  187. void starpu_sched_component_worker_post_exec_hook(struct starpu_task *task);
  188. /*******************************************************************************
  189. * Flow-control Fifo Component's Interface *
  190. ******************************************************************************/
  191. struct starpu_fifo_data
  192. {
  193. unsigned ntasks_threshold;
  194. double exp_len_threshold;
  195. };
  196. struct starpu_sched_component *starpu_sched_component_fifo_create(struct starpu_sched_tree *tree, struct starpu_fifo_data *fifo_data);
  197. int starpu_sched_component_is_fifo(struct starpu_sched_component *component);
  198. /*******************************************************************************
  199. * Flow-control Prio Component's Interface *
  200. ******************************************************************************/
  201. struct starpu_prio_data
  202. {
  203. unsigned ntasks_threshold;
  204. double exp_len_threshold;
  205. };
  206. struct starpu_sched_component *starpu_sched_component_prio_create(struct starpu_sched_tree *tree, struct starpu_prio_data *prio_data);
  207. int starpu_sched_component_is_prio(struct starpu_sched_component *component);
  208. /*******************************************************************************
  209. * Resource-mapping Work-Stealing Component's Interface *
  210. ******************************************************************************/
  211. struct starpu_sched_component *starpu_sched_component_work_stealing_create(struct starpu_sched_tree *tree, void *arg STARPU_ATTRIBUTE_UNUSED);
  212. int starpu_sched_component_is_work_stealing(struct starpu_sched_component *component);
  213. int starpu_sched_tree_work_stealing_push_task(struct starpu_task *task);
  214. /*******************************************************************************
  215. * Resource-mapping Random Component's Interface *
  216. ******************************************************************************/
  217. struct starpu_sched_component *starpu_sched_component_random_create(struct starpu_sched_tree *tree, void *arg STARPU_ATTRIBUTE_UNUSED);
  218. int starpu_sched_component_is_random(struct starpu_sched_component *);
  219. /*******************************************************************************
  220. * Resource-mapping Eager Component's Interface *
  221. ******************************************************************************/
  222. struct starpu_sched_component *starpu_sched_component_eager_create(struct starpu_sched_tree *tree, void *arg STARPU_ATTRIBUTE_UNUSED);
  223. int starpu_sched_component_is_eager(struct starpu_sched_component *);
  224. /*******************************************************************************
  225. * Resource-mapping Eager-Calibration Component's Interface *
  226. ******************************************************************************/
  227. struct starpu_sched_component *starpu_sched_component_eager_calibration_create(struct starpu_sched_tree *tree, void *arg STARPU_ATTRIBUTE_UNUSED);
  228. int starpu_sched_component_is_eager_calibration(struct starpu_sched_component *);
  229. /*******************************************************************************
  230. * Resource-mapping MCT Component's Interface *
  231. ******************************************************************************/
  232. struct starpu_mct_data
  233. {
  234. double alpha;
  235. double beta;
  236. double gamma;
  237. double idle_power;
  238. };
  239. /* create a component with mct_data paremeters
  240. a copy the struct starpu_mct_data * given is performed during the init_data call
  241. the mct component doesnt do anything but pushing tasks on no_perf_model_component and calibrating_component
  242. */
  243. struct starpu_sched_component *starpu_sched_component_mct_create(struct starpu_sched_tree *tree, struct starpu_mct_data *mct_data);
  244. int starpu_sched_component_is_mct(struct starpu_sched_component *component);
  245. /*******************************************************************************
  246. * Resource-mapping HEFT Component's Interface *
  247. ******************************************************************************/
  248. struct starpu_sched_component *starpu_sched_component_heft_create(struct starpu_sched_tree *tree, struct starpu_mct_data *mct_data);
  249. int starpu_sched_component_is_heft(struct starpu_sched_component *component);
  250. /*******************************************************************************
  251. * Special-purpose Best_Implementation Component's Interface *
  252. ******************************************************************************/
  253. /* this component select the best implementation for the first worker in context that can execute task.
  254. * and fill task->predicted and task->predicted_transfer
  255. * cannot have several child if push_task is called
  256. */
  257. struct starpu_sched_component *starpu_sched_component_best_implementation_create(struct starpu_sched_tree *tree, void *arg STARPU_ATTRIBUTE_UNUSED);
  258. struct starpu_perfmodel_select_data
  259. {
  260. struct starpu_sched_component *calibrator_component;
  261. struct starpu_sched_component *no_perfmodel_component;
  262. struct starpu_sched_component *perfmodel_component;
  263. };
  264. /*******************************************************************************
  265. * Special-purpose Perfmodel_Select Component's Interface *
  266. ******************************************************************************/
  267. struct starpu_sched_component *starpu_sched_component_perfmodel_select_create(struct starpu_sched_tree *tree, struct starpu_perfmodel_select_data *perfmodel_select_data);
  268. int starpu_sched_component_is_perfmodel_select(struct starpu_sched_component *component);
  269. /*******************************************************************************
  270. * Recipe Component's Interface *
  271. ******************************************************************************/
  272. struct starpu_sched_component_composed_recipe;
  273. /* create empty recipe */
  274. struct starpu_sched_component_composed_recipe *starpu_sched_component_create_recipe(void);
  275. struct starpu_sched_component_composed_recipe *starpu_sched_component_create_recipe_singleton(struct starpu_sched_component *(*create_component)(struct starpu_sched_tree *tree, void *arg), void *arg);
  276. /* add a function creation component to recipe */
  277. void starpu_sched_recipe_add_component(struct starpu_sched_component_composed_recipe *recipe, struct starpu_sched_component *(*create_component)(struct starpu_sched_tree *tree, void *arg), void *arg);
  278. void starpu_destroy_composed_sched_component_recipe(struct starpu_sched_component_composed_recipe *);
  279. struct starpu_sched_component *starpu_sched_component_composed_component_create(struct starpu_sched_tree *tree, struct starpu_sched_component_composed_recipe *recipe);
  280. #ifdef STARPU_HAVE_HWLOC
  281. /* null pointer mean to ignore a level L of hierarchy, then components of levels > L become children of level L - 1 */
  282. struct starpu_sched_specs
  283. {
  284. /* hw_loc_machine_composed_sched_component must be set as its the root of the topology */
  285. struct starpu_sched_component_composed_recipe *hwloc_machine_composed_sched_component;
  286. struct starpu_sched_component_composed_recipe *hwloc_component_composed_sched_component;
  287. struct starpu_sched_component_composed_recipe *hwloc_socket_composed_sched_component;
  288. struct starpu_sched_component_composed_recipe *hwloc_cache_composed_sched_component;
  289. /* this member should return a new allocated starpu_sched_component_composed_recipe or NULL
  290. * the starpu_sched_component_composed_recipe_t must not include the worker component
  291. */
  292. struct starpu_sched_component_composed_recipe *(*worker_composed_sched_component)(enum starpu_worker_archtype archtype);
  293. /* this flag indicate if heterogenous workers should be brothers or cousins,
  294. * as example, if a gpu and a cpu should share or not there numa node
  295. */
  296. int mix_heterogeneous_workers;
  297. };
  298. struct starpu_sched_tree *starpu_sched_component_make_scheduler(unsigned sched_ctx_id, struct starpu_sched_specs);
  299. #endif /* STARPU_HAVE_HWLOC */
  300. #ifdef __cplusplus
  301. }
  302. #endif
  303. #endif /* __STARPU_SCHED_COMPONENT_H__ */