starpu_sched_component.h 17 KB

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