sched_ctx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2018 Inria
  4. * Copyright (C) 2012-2014,2017 CNRS
  5. * Copyright (C) 2011-2013,2015-2017,2019 Université de Bordeaux
  6. * Copyright (C) 2016 Uppsala University
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #ifndef __SCHED_CONTEXT_H__
  20. #define __SCHED_CONTEXT_H__
  21. #include <starpu.h>
  22. #include <starpu_sched_ctx.h>
  23. #include <starpu_sched_ctx_hypervisor.h>
  24. #include <starpu_scheduler.h>
  25. #include <common/config.h>
  26. #include <common/barrier_counter.h>
  27. #include <profiling/profiling.h>
  28. #include <semaphore.h>
  29. #include <core/task.h>
  30. #include "sched_ctx_list.h"
  31. #ifdef STARPU_HAVE_HWLOC
  32. #include <hwloc.h>
  33. #endif
  34. #define NO_RESIZE -1
  35. #define REQ_RESIZE 0
  36. #define DO_RESIZE 1
  37. #define STARPU_GLOBAL_SCHED_CTX 0
  38. #define STARPU_NMAXSMS 13
  39. struct _starpu_sched_ctx
  40. {
  41. /* id of the context used in user mode*/
  42. unsigned id;
  43. /* boolean indicating whether the scheduling_ctx will be considered for scheduling (1) or not (0)*/
  44. unsigned do_schedule;
  45. /* name of context */
  46. const char *name;
  47. /* policy of the context */
  48. struct starpu_sched_policy *sched_policy;
  49. /* data necessary for the policy */
  50. void *policy_data;
  51. /* pointer for application use */
  52. void *user_data;
  53. struct starpu_worker_collection *workers;
  54. /* we keep an initial sched which we never delete */
  55. unsigned is_initial_sched;
  56. /* wait for the tasks submitted to the context to be executed */
  57. struct _starpu_barrier_counter tasks_barrier;
  58. /* wait for the tasks ready of the context to be executed */
  59. struct _starpu_barrier_counter ready_tasks_barrier;
  60. /* amount of ready flops in a context */
  61. double ready_flops;
  62. /* Iteration number, as advertised by application */
  63. long iterations[2];
  64. int iteration_level;
  65. /*ready tasks that couldn't be pushed because the ctx has no workers*/
  66. struct starpu_task_list empty_ctx_tasks;
  67. /*ready tasks that couldn't be pushed because the the window of tasks was already full*/
  68. struct starpu_task_list waiting_tasks;
  69. /* min CPUs to execute*/
  70. int min_ncpus;
  71. /* max CPUs to execute*/
  72. int max_ncpus;
  73. /* min GPUs to execute*/
  74. int min_ngpus;
  75. /* max GPUs to execute*/
  76. int max_ngpus;
  77. /* in case we delete the context leave resources to the inheritor*/
  78. unsigned inheritor;
  79. /* indicates whether the application finished submitting tasks
  80. to this context*/
  81. unsigned finished_submit;
  82. /* By default we have a binary type of priority: either a task is a priority
  83. * task (level 1) or it is not (level 0). */
  84. int min_priority;
  85. int max_priority;
  86. int min_priority_is_set;
  87. int max_priority_is_set;
  88. /* hwloc tree structure of workers */
  89. #ifdef STARPU_HAVE_HWLOC
  90. hwloc_bitmap_t hwloc_workers_set;
  91. #endif
  92. #ifdef STARPU_USE_SC_HYPERVISOR
  93. /* a structure containing a series of performance counters determining the resize procedure */
  94. struct starpu_sched_ctx_performance_counters *perf_counters;
  95. #endif //STARPU_USE_SC_HYPERVISOR
  96. /* callback called when the context finished executed its submitted tasks */
  97. void (*close_callback)(unsigned sched_ctx_id, void* args);
  98. void *close_args;
  99. /* value placing the contexts in their hierarchy */
  100. unsigned hierarchy_level;
  101. /* if we execute non-StarPU code inside the context
  102. we have a single master worker that stays awake,
  103. if not master is -1 */
  104. int main_master;
  105. /* ctx nesting the current ctx */
  106. unsigned nesting_sched_ctx;
  107. /* perf model for the device comb of the ctx */
  108. struct starpu_perfmodel_arch perf_arch;
  109. /* For parallel workers, say whether it is viewed as sequential or not. This
  110. is a helper for the prologue code. */
  111. unsigned parallel_view;
  112. /* for ctxs without policy: flag to indicate that we want to get
  113. the threads to sleep in order to replace them with other threads or leave
  114. them awake & use them in the parallel code*/
  115. unsigned awake_workers;
  116. /* function called when initializing the scheduler */
  117. void (*init_sched)(unsigned);
  118. int sub_ctxs[STARPU_NMAXWORKERS];
  119. int nsub_ctxs;
  120. /* nr of SMs assigned to this ctx if we partition gpus*/
  121. int nsms;
  122. int sms_start_idx;
  123. int sms_end_idx;
  124. int stream_worker;
  125. starpu_pthread_rwlock_t rwlock;
  126. starpu_pthread_t lock_write_owner;
  127. };
  128. /* per-worker list of deferred ctx_change ops */
  129. LIST_TYPE(_starpu_ctx_change,
  130. int sched_ctx_id;
  131. int op;
  132. int nworkers_to_notify;
  133. int *workerids_to_notify;
  134. int nworkers_to_change;
  135. int *workerids_to_change;
  136. );
  137. struct _starpu_machine_config;
  138. /* init sched_ctx_id of all contextes*/
  139. void _starpu_init_all_sched_ctxs(struct _starpu_machine_config *config);
  140. /* allocate all structures belonging to a context */
  141. struct _starpu_sched_ctx* _starpu_create_sched_ctx(struct starpu_sched_policy *policy, int *workerid, int nworkerids, unsigned is_init_sched, const char *sched_name,
  142. int min_prio_set, int min_prio,
  143. int max_prio_set, int max_prio, unsigned awake_workers, void (*sched_policy_init)(unsigned), void *user_data,
  144. int nsub_ctxs, int *sub_ctxs, int nsms);
  145. /* delete all sched_ctx */
  146. void _starpu_delete_all_sched_ctxs();
  147. /* This function waits until all the tasks that were already submitted to a specific
  148. * context have been executed. */
  149. int _starpu_wait_for_all_tasks_of_sched_ctx(unsigned sched_ctx_id);
  150. /* This function waits until at most n tasks are still submitted. */
  151. int _starpu_wait_for_n_submitted_tasks_of_sched_ctx(unsigned sched_ctx_id, unsigned n);
  152. /* In order to implement starpu_wait_for_all_tasks_of_ctx, we keep track of the number of
  153. * task currently submitted to the context */
  154. void _starpu_decrement_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  155. void _starpu_increment_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  156. int _starpu_get_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  157. int _starpu_check_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  158. void _starpu_decrement_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops);
  159. unsigned _starpu_increment_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops, struct starpu_task *task);
  160. int _starpu_wait_for_no_ready_of_sched_ctx(unsigned sched_ctx_id);
  161. /* Return the corresponding index of the workerid in the ctx table */
  162. int _starpu_get_index_in_ctx_of_workerid(unsigned sched_ctx, unsigned workerid);
  163. /* Get the mutex corresponding to the global workerid */
  164. starpu_pthread_mutex_t *_starpu_get_sched_mutex(struct _starpu_sched_ctx *sched_ctx, int worker);
  165. /* Get workers belonging to a certain context, it returns the number of workers
  166. take care: no mutex taken, the list of workers might not be updated */
  167. int _starpu_get_workers_of_sched_ctx(unsigned sched_ctx_id, int *pus, enum starpu_worker_archtype arch);
  168. /* Let the worker know it does not belong to the context and that
  169. it should stop poping from it */
  170. void _starpu_worker_gets_out_of_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker);
  171. /* Check if the worker belongs to another sched_ctx */
  172. unsigned _starpu_worker_belongs_to_a_sched_ctx(int workerid, unsigned sched_ctx_id);
  173. /* indicates wheather this worker should go to sleep or not
  174. (if it is the last one awake in a context he should better keep awake) */
  175. unsigned _starpu_sched_ctx_last_worker_awake(struct _starpu_worker *worker);
  176. /* If starpu_sched_ctx_set_context() has been called, returns the context
  177. * id set by its last call, or the id of the initial context */
  178. unsigned _starpu_sched_ctx_get_current_context();
  179. /* verify how many workers can execute a certain task */
  180. int _starpu_nworkers_able_to_execute_task(struct starpu_task *task, struct _starpu_sched_ctx *sched_ctx);
  181. void _starpu_fetch_tasks_from_empty_ctx_list(struct _starpu_sched_ctx *sched_ctx);
  182. unsigned _starpu_sched_ctx_allow_hypervisor(unsigned sched_ctx_id);
  183. struct starpu_perfmodel_arch * _starpu_sched_ctx_get_perf_archtype(unsigned sched_ctx);
  184. #ifdef STARPU_USE_SC_HYPERVISOR
  185. /* Notifies the hypervisor that a tasks was poped from the workers' list */
  186. void _starpu_sched_ctx_post_exec_task_cb(int workerid, struct starpu_task *task, size_t data_size, uint32_t footprint);
  187. #endif //STARPU_USE_SC_HYPERVISOR
  188. void starpu_sched_ctx_add_combined_workers(int *combined_workers_to_add, unsigned n_combined_workers_to_add, unsigned sched_ctx_id);
  189. /* if the worker is the master of a parallel context, and the job is meant to be executed on this parallel context, return a pointer to the context */
  190. struct _starpu_sched_ctx *__starpu_sched_ctx_get_sched_ctx_for_worker_and_job(struct _starpu_worker *worker, struct _starpu_job *j);
  191. #define _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(w,j) \
  192. (_starpu_get_nsched_ctxs() <= 1 ? _starpu_get_sched_ctx_struct(0) : __starpu_sched_ctx_get_sched_ctx_for_worker_and_job((w),(j)))
  193. static inline struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id);
  194. static inline int _starpu_sched_ctx_check_write_locked(unsigned sched_ctx_id)
  195. {
  196. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  197. return starpu_pthread_equal(sched_ctx->lock_write_owner, starpu_pthread_self());
  198. }
  199. #define STARPU_SCHED_CTX_CHECK_LOCK(sched_ctx_id) STARPU_ASSERT(_starpu_sched_ctx_check_write_locked((sched_ctx_id)))
  200. static inline void _starpu_sched_ctx_lock_write(unsigned sched_ctx_id)
  201. {
  202. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  203. STARPU_HG_DISABLE_CHECKING(sched_ctx->lock_write_owner);
  204. STARPU_ASSERT(!starpu_pthread_equal(sched_ctx->lock_write_owner, starpu_pthread_self()));
  205. STARPU_HG_ENABLE_CHECKING(sched_ctx->lock_write_owner);
  206. STARPU_PTHREAD_RWLOCK_WRLOCK(&sched_ctx->rwlock);
  207. sched_ctx->lock_write_owner = starpu_pthread_self();
  208. }
  209. static inline void _starpu_sched_ctx_unlock_write(unsigned sched_ctx_id)
  210. {
  211. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  212. STARPU_HG_DISABLE_CHECKING(sched_ctx->lock_write_owner);
  213. STARPU_ASSERT(starpu_pthread_equal(sched_ctx->lock_write_owner, starpu_pthread_self()));
  214. memset(&sched_ctx->lock_write_owner, 0, sizeof(sched_ctx->lock_write_owner));
  215. STARPU_HG_ENABLE_CHECKING(sched_ctx->lock_write_owner);
  216. STARPU_PTHREAD_RWLOCK_UNLOCK(&sched_ctx->rwlock);
  217. }
  218. static inline void _starpu_sched_ctx_lock_read(unsigned sched_ctx_id)
  219. {
  220. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  221. STARPU_HG_DISABLE_CHECKING(sched_ctx->lock_write_owner);
  222. STARPU_ASSERT(!starpu_pthread_equal(sched_ctx->lock_write_owner, starpu_pthread_self()));
  223. STARPU_HG_ENABLE_CHECKING(sched_ctx->lock_write_owner);
  224. STARPU_PTHREAD_RWLOCK_RDLOCK(&sched_ctx->rwlock);
  225. }
  226. static inline void _starpu_sched_ctx_unlock_read(unsigned sched_ctx_id)
  227. {
  228. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  229. STARPU_HG_DISABLE_CHECKING(sched_ctx->lock_write_owner);
  230. STARPU_ASSERT(!starpu_pthread_equal(sched_ctx->lock_write_owner, starpu_pthread_self()));
  231. STARPU_HG_ENABLE_CHECKING(sched_ctx->lock_write_owner);
  232. STARPU_PTHREAD_RWLOCK_UNLOCK(&sched_ctx->rwlock);
  233. }
  234. static inline unsigned _starpu_sched_ctx_worker_is_master_for_child_ctx(unsigned sched_ctx_id, unsigned workerid, struct starpu_task *task)
  235. {
  236. unsigned child_sched_ctx = starpu_sched_ctx_worker_is_master_for_child_ctx(workerid, sched_ctx_id);
  237. if(child_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  238. {
  239. starpu_sched_ctx_move_task_to_ctx_locked(task, child_sched_ctx, 1);
  240. starpu_sched_ctx_revert_task_counters_ctx_locked(sched_ctx_id, task->flops);
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. /* Go through the list of deferred ctx changes of the current worker and apply
  246. * any ctx change operation found until the list is empty */
  247. void _starpu_worker_apply_deferred_ctx_changes(void);
  248. #endif // __SCHED_CONTEXT_H__