sched_ctx.h 11 KB

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