starpu_sched_ctx.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2016 Uppsala University
  5. * Copyright (C) 2017 Arthur Chevalier
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __STARPU_SCHED_CTX_H__
  19. #define __STARPU_SCHED_CTX_H__
  20. #include <starpu.h>
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. /**
  26. @defgroup API_Scheduling_Contexts Scheduling Contexts
  27. @brief StarPU permits on one hand grouping workers in combined
  28. workers in order to execute a parallel task and on the other hand
  29. grouping tasks in bundles that will be executed by a single
  30. specified worker.
  31. In contrast when we group workers in scheduling contexts we submit
  32. starpu tasks to them and we schedule them with the policy assigned
  33. to the context. Scheduling contexts can be created, deleted and
  34. modified dynamically.
  35. @{
  36. */
  37. /**
  38. @name Scheduling Contexts Basic API
  39. @{
  40. */
  41. /**
  42. Used when calling starpu_sched_ctx_create() to specify a
  43. name for a scheduling policy
  44. */
  45. #define STARPU_SCHED_CTX_POLICY_NAME (1<<16)
  46. /**
  47. Used when calling starpu_sched_ctx_create() to specify a
  48. pointer to a scheduling policy
  49. */
  50. #define STARPU_SCHED_CTX_POLICY_STRUCT (2<<16)
  51. /**
  52. Used when calling starpu_sched_ctx_create() to specify a
  53. minimum scheduler priority value.
  54. */
  55. #define STARPU_SCHED_CTX_POLICY_MIN_PRIO (3<<16)
  56. /**
  57. Used when calling starpu_sched_ctx_create() to specify a
  58. maximum scheduler priority value.
  59. */
  60. #define STARPU_SCHED_CTX_POLICY_MAX_PRIO (4<<16)
  61. #define STARPU_SCHED_CTX_HIERARCHY_LEVEL (5<<16)
  62. #define STARPU_SCHED_CTX_NESTED (6<<16)
  63. /**
  64. Used when calling starpu_sched_ctx_create() to specify ???
  65. */
  66. #define STARPU_SCHED_CTX_AWAKE_WORKERS (7<<16)
  67. /**
  68. Used when calling starpu_sched_ctx_create() to specify a
  69. function pointer allowing to initialize the scheduling policy.
  70. */
  71. #define STARPU_SCHED_CTX_POLICY_INIT (8<<16)
  72. /**
  73. Used when calling starpu_sched_ctx_create() to specify a
  74. pointer to some user data related to the context being created.
  75. */
  76. #define STARPU_SCHED_CTX_USER_DATA (9<<16)
  77. /**
  78. Used when calling starpu_sched_ctx_create() in order to create a
  79. context on the NVIDIA GPU to specify the number of SMs the context
  80. should have
  81. */
  82. #define STARPU_SCHED_CTX_CUDA_NSMS (10<<16)
  83. /**
  84. Used when calling starpu_sched_ctx_create() to specify
  85. a list of sub contexts of the current context.
  86. */
  87. #define STARPU_SCHED_CTX_SUB_CTXS (11<<16)
  88. /**
  89. Create a scheduling context with the given parameters
  90. (see below) and assign the workers in \p workerids_ctx to execute the
  91. tasks submitted to it. The return value represents the identifier of
  92. the context that has just been created. It will be further used to
  93. indicate the context the tasks will be submitted to. The return value
  94. should be at most ::STARPU_NMAX_SCHED_CTXS.
  95. The arguments following the name of the scheduling context can be of
  96. the following types:
  97. <ul>
  98. <li> ::STARPU_SCHED_CTX_POLICY_NAME, followed by the name of a
  99. predefined scheduling policy. Use an empty string to create the
  100. context with the default scheduling policy.
  101. </li>
  102. <li> ::STARPU_SCHED_CTX_POLICY_STRUCT, followed by a pointer to a
  103. custom scheduling policy (struct starpu_sched_policy *)
  104. </li>
  105. <li> ::STARPU_SCHED_CTX_POLICY_MIN_PRIO, followed by a integer
  106. representing the minimum priority value to be defined for the
  107. scheduling policy.
  108. </li>
  109. <li> ::STARPU_SCHED_CTX_POLICY_MAX_PRIO, followed by a integer
  110. representing the maximum priority value to be defined for the
  111. scheduling policy.
  112. </li>
  113. <li> ::STARPU_SCHED_CTX_POLICY_INIT, followed by a function pointer
  114. (ie. void init_sched(void)) allowing to initialize the scheduling policy.
  115. </li>
  116. <li> ::STARPU_SCHED_CTX_USER_DATA, followed by a pointer
  117. to a custom user data structure, to be retrieved by \ref starpu_sched_ctx_get_user_data().
  118. </li>
  119. </ul>
  120. */
  121. unsigned starpu_sched_ctx_create(int *workerids_ctx, int nworkers_ctx, const char *sched_ctx_name, ...);
  122. /**
  123. Create a context indicating an approximate interval of resources
  124. */
  125. unsigned starpu_sched_ctx_create_inside_interval(const char *policy_name, const char *sched_ctx_name, int min_ncpus, int max_ncpus, int min_ngpus, int max_ngpus, unsigned allow_overlap);
  126. /**
  127. Execute the callback whenever the last task of the context finished
  128. executing, it is called with the parameters \p sched_ctx and any
  129. other parameter needed by the application (packed in \p args)
  130. */
  131. void starpu_sched_ctx_register_close_callback(unsigned sched_ctx_id, void (*close_callback)(unsigned sched_ctx_id, void* args), void *args);
  132. /**
  133. Add dynamically the workers in \p workerids_ctx to the context \p
  134. sched_ctx_id. The last argument cannot be greater than
  135. ::STARPU_NMAX_SCHED_CTXS.
  136. */
  137. void starpu_sched_ctx_add_workers(int *workerids_ctx, unsigned nworkers_ctx, unsigned sched_ctx_id);
  138. /**
  139. Remove the workers in \p workerids_ctx from the context
  140. \p sched_ctx_id. The last argument cannot be greater than
  141. ::STARPU_NMAX_SCHED_CTXS.
  142. */
  143. void starpu_sched_ctx_remove_workers(int *workerids_ctx, unsigned nworkers_ctx, unsigned sched_ctx_id);
  144. /**
  145. Print on the file \p f the worker names belonging to the context \p
  146. sched_ctx_id
  147. */
  148. void starpu_sched_ctx_display_workers(unsigned sched_ctx_id, FILE *f);
  149. /**
  150. Delete scheduling context \p sched_ctx_id and transfer remaining
  151. workers to the inheritor scheduling context.
  152. */
  153. void starpu_sched_ctx_delete(unsigned sched_ctx_id);
  154. /**
  155. Indicate that the context \p inheritor will inherit the resources
  156. of the context \p sched_ctx_id when \p sched_ctx_id will be
  157. deleted.
  158. */
  159. void starpu_sched_ctx_set_inheritor(unsigned sched_ctx_id, unsigned inheritor);
  160. unsigned starpu_sched_ctx_get_inheritor(unsigned sched_ctx_id);
  161. unsigned starpu_sched_ctx_get_hierarchy_level(unsigned sched_ctx_id);
  162. /**
  163. Set the scheduling context the subsequent tasks will be submitted
  164. to
  165. */
  166. void starpu_sched_ctx_set_context(unsigned *sched_ctx_id);
  167. /**
  168. Return the scheduling context the tasks are currently submitted to,
  169. or ::STARPU_NMAX_SCHED_CTXS if no default context has been defined
  170. by calling the function starpu_sched_ctx_set_context().
  171. */
  172. unsigned starpu_sched_ctx_get_context(void);
  173. /**
  174. Stop submitting tasks from the empty context list until the next
  175. time the context has time to check the empty context list
  176. */
  177. void starpu_sched_ctx_stop_task_submission(void);
  178. /**
  179. Indicate starpu that the application finished submitting to this
  180. context in order to move the workers to the inheritor as soon as
  181. possible.
  182. */
  183. void starpu_sched_ctx_finished_submit(unsigned sched_ctx_id);
  184. /**
  185. Return the list of workers in the array \p workerids, the return
  186. value is the number of workers. The user should free the \p
  187. workerids table after finishing using it (it is allocated inside
  188. the function with the proper size)
  189. */
  190. unsigned starpu_sched_ctx_get_workers_list(unsigned sched_ctx_id, int **workerids);
  191. /**
  192. Return the list of workers in the array \p workerids, the return
  193. value is the number of workers. This list is provided in raw order,
  194. i.e. not sorted by tree or list order, and the user should not free
  195. the \p workerids table. This function is thus much less costly than
  196. starpu_sched_ctx_get_workers_list().
  197. */
  198. unsigned starpu_sched_ctx_get_workers_list_raw(unsigned sched_ctx_id, int **workerids);
  199. /**
  200. Return the number of workers managed by the specified context
  201. (Usually needed to verify if it manages any workers or if it should
  202. be blocked)
  203. */
  204. unsigned starpu_sched_ctx_get_nworkers(unsigned sched_ctx_id);
  205. /**
  206. Return the number of workers shared by two contexts.
  207. */
  208. unsigned starpu_sched_ctx_get_nshared_workers(unsigned sched_ctx_id, unsigned sched_ctx_id2);
  209. /**
  210. Return 1 if the worker belongs to the context and 0 otherwise
  211. */
  212. unsigned starpu_sched_ctx_contains_worker(int workerid, unsigned sched_ctx_id);
  213. unsigned starpu_sched_ctx_contains_type_of_worker(enum starpu_worker_archtype arch, unsigned sched_ctx_id);
  214. /**
  215. Return the workerid if the worker belongs to the context and -1 otherwise.
  216. If the thread calling this function is not a worker the function returns -1
  217. as it calls the function starpu_worker_get_id().
  218. */
  219. unsigned starpu_sched_ctx_worker_get_id(unsigned sched_ctx_id);
  220. unsigned starpu_sched_ctx_get_ctx_for_task(struct starpu_task *task);
  221. /**
  222. Check if a worker is shared between several contexts
  223. */
  224. unsigned starpu_sched_ctx_overlapping_ctxs_on_worker(int workerid);
  225. /**
  226. Return the user data pointer associated to the scheduling context.
  227. */
  228. void *starpu_sched_ctx_get_user_data(unsigned sched_ctx_id);
  229. void starpu_sched_ctx_set_user_data(unsigned sched_ctx_id, void* user_data);
  230. /**
  231. Allocate the scheduling policy data (private information of the
  232. scheduler like queues, variables, additional condition variables)
  233. the context
  234. */
  235. void starpu_sched_ctx_set_policy_data(unsigned sched_ctx_id, void *policy_data);
  236. /**
  237. Return the scheduling policy data (private information of the
  238. scheduler) of the contexts previously assigned to.
  239. */
  240. void *starpu_sched_ctx_get_policy_data(unsigned sched_ctx_id);
  241. struct starpu_sched_policy *starpu_sched_ctx_get_sched_policy(unsigned sched_ctx_id);
  242. /**
  243. Execute any parallel code on the workers of the sched_ctx (workers
  244. are blocked)
  245. */
  246. void *starpu_sched_ctx_exec_parallel_code(void* (*func)(void*), void *param, unsigned sched_ctx_id);
  247. int starpu_sched_ctx_get_nready_tasks(unsigned sched_ctx_id);
  248. double starpu_sched_ctx_get_nready_flops(unsigned sched_ctx_id);
  249. void starpu_sched_ctx_list_task_counters_increment(unsigned sched_ctx_id, int workerid);
  250. void starpu_sched_ctx_list_task_counters_decrement(unsigned sched_ctx_id, int workerid);
  251. void starpu_sched_ctx_list_task_counters_reset(unsigned sched_ctx_id, int workerid);
  252. void starpu_sched_ctx_list_task_counters_increment_all_ctx_locked(struct starpu_task *task, unsigned sched_ctx_id);
  253. void starpu_sched_ctx_list_task_counters_decrement_all_ctx_locked(struct starpu_task *task, unsigned sched_ctx_id);
  254. void starpu_sched_ctx_list_task_counters_reset_all(struct starpu_task *task, unsigned sched_ctx_id);
  255. void starpu_sched_ctx_set_priority(int *workers, int nworkers, unsigned sched_ctx_id, unsigned priority);
  256. unsigned starpu_sched_ctx_get_priority(int worker, unsigned sched_ctx_id);
  257. void starpu_sched_ctx_get_available_cpuids(unsigned sched_ctx_id, int **cpuids, int *ncpuids);
  258. void starpu_sched_ctx_bind_current_thread_to_cpuid(unsigned cpuid);
  259. int starpu_sched_ctx_book_workers_for_task(unsigned sched_ctx_id, int *workerids, int nworkers);
  260. void starpu_sched_ctx_unbook_workers_for_task(unsigned sched_ctx_id, int master);
  261. /**
  262. Return the first context (child of sched_ctx_id) where the workerid
  263. is master
  264. */
  265. unsigned starpu_sched_ctx_worker_is_master_for_child_ctx(int workerid, unsigned sched_ctx_id);
  266. /**
  267. Return the context id of masterid if it master of a context. If
  268. not, return ::STARPU_NMAX_SCHED_CTXS.
  269. */
  270. unsigned starpu_sched_ctx_master_get_context(int masterid);
  271. void starpu_sched_ctx_revert_task_counters_ctx_locked(unsigned sched_ctx_id, double flops);
  272. void starpu_sched_ctx_move_task_to_ctx_locked(struct starpu_task *task, unsigned sched_ctx, unsigned with_repush);
  273. int starpu_sched_ctx_get_worker_rank(unsigned sched_ctx_id);
  274. /**
  275. Return the function associated with the scheduler context \p
  276. sched_ctx_id which was given through the field
  277. starpu_conf::sched_policy_callback
  278. */
  279. void (*starpu_sched_ctx_get_sched_policy_callback(unsigned sched_ctx_id))(unsigned);
  280. unsigned starpu_sched_ctx_has_starpu_scheduler(unsigned sched_ctx_id, unsigned *awake_workers);
  281. int starpu_sched_ctx_get_stream_worker(unsigned sub_ctx);
  282. int starpu_sched_ctx_get_nsms(unsigned sched_ctx);
  283. void starpu_sched_ctx_get_sms_interval(int stream_workerid, int *start, int *end);
  284. /** @} */
  285. /**
  286. @name Scheduling Context Priorities
  287. @{
  288. */
  289. /**
  290. Return the current minimum priority level supported by the
  291. scheduling policy of the given scheduler context.
  292. */
  293. int starpu_sched_ctx_get_min_priority(unsigned sched_ctx_id);
  294. /**
  295. Return the current maximum priority level supported by the
  296. scheduling policy of the given scheduler context.
  297. */
  298. int starpu_sched_ctx_get_max_priority(unsigned sched_ctx_id);
  299. /**
  300. Define the minimum task priority level supported by the scheduling
  301. policy of the given scheduler context. The default minimum priority
  302. level is the same as the default priority level which is 0 by
  303. convention. The application may access that value by calling the
  304. function starpu_sched_ctx_get_min_priority(). This function should
  305. only be called from the initialization method of the scheduling
  306. policy, and should not be used directly from the application.
  307. */
  308. int starpu_sched_ctx_set_min_priority(unsigned sched_ctx_id, int min_prio);
  309. /**
  310. Define the maximum priority level supported by the scheduling
  311. policy of the given scheduler context. The default maximum priority
  312. level is 1. The application may access that value by calling the
  313. starpu_sched_ctx_get_max_priority() function. This function should
  314. only be called from the initialization method of the scheduling
  315. policy, and should not be used directly from the application.
  316. */
  317. int starpu_sched_ctx_set_max_priority(unsigned sched_ctx_id, int max_prio);
  318. int starpu_sched_ctx_min_priority_is_set(unsigned sched_ctx_id);
  319. int starpu_sched_ctx_max_priority_is_set(unsigned sched_ctx_id);
  320. /**
  321. Provided for legacy reasons.
  322. */
  323. #define STARPU_MIN_PRIO (starpu_sched_get_min_priority())
  324. /**
  325. Provided for legacy reasons.
  326. */
  327. #define STARPU_MAX_PRIO (starpu_sched_get_max_priority())
  328. /**
  329. By convention, the default priority level should be 0 so that we
  330. can statically allocate tasks with a default priority.
  331. */
  332. #define STARPU_DEFAULT_PRIO 0
  333. /** @} */
  334. /**
  335. @name Scheduling Context Worker Collection
  336. @{
  337. */
  338. /**
  339. Create a worker collection of the type indicated by the last
  340. parameter for the context specified through the first parameter.
  341. */
  342. struct starpu_worker_collection *starpu_sched_ctx_create_worker_collection(unsigned sched_ctx_id, enum starpu_worker_collection_type type) STARPU_ATTRIBUTE_MALLOC;
  343. /**
  344. Delete the worker collection of the specified scheduling context
  345. */
  346. void starpu_sched_ctx_delete_worker_collection(unsigned sched_ctx_id);
  347. /**
  348. Return the worker collection managed by the indicated context
  349. */
  350. struct starpu_worker_collection *starpu_sched_ctx_get_worker_collection(unsigned sched_ctx_id);
  351. /** @} */
  352. /** @} */
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #endif /* __STARPU_SCHED_CTX_H__ */