sched_ctx.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 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. /* cond to block push when there are no workers in the ctx */
  62. starpu_pthread_cond_t no_workers_cond;
  63. /* mutex to block push when there are no workers in the ctx */
  64. starpu_pthread_mutex_t no_workers_mutex;
  65. /*ready tasks that couldn't be pushed because the ctx has no workers*/
  66. struct starpu_task_list empty_ctx_tasks;
  67. /* mutext protecting empty_ctx_tasks list */
  68. starpu_pthread_mutex_t empty_ctx_mutex;
  69. /*ready tasks that couldn't be pushed because the the window of tasks was already full*/
  70. struct starpu_task_list waiting_tasks;
  71. /* mutext protecting waiting_tasks list */
  72. starpu_pthread_mutex_t waiting_tasks_mutex;
  73. /* mutext protecting write to all worker's sched_ctx_list structure for this sched_ctx */
  74. starpu_pthread_mutex_t sched_ctx_list_mutex;
  75. /* min CPUs to execute*/
  76. int min_ncpus;
  77. /* max CPUs to execute*/
  78. int max_ncpus;
  79. /* min GPUs to execute*/
  80. int min_ngpus;
  81. /* max GPUs to execute*/
  82. int max_ngpus;
  83. /* in case we delete the context leave resources to the inheritor*/
  84. unsigned inheritor;
  85. /* indicates whether the application finished submitting tasks
  86. to this context*/
  87. unsigned finished_submit;
  88. /* By default we have a binary type of priority: either a task is a priority
  89. * task (level 1) or it is not (level 0). */
  90. int min_priority;
  91. int max_priority;
  92. int min_priority_is_set;
  93. int max_priority_is_set;
  94. /* hwloc tree structure of workers */
  95. #ifdef STARPU_HAVE_HWLOC
  96. hwloc_bitmap_t hwloc_workers_set;
  97. #endif
  98. #ifdef STARPU_USE_SC_HYPERVISOR
  99. /* a structure containing a series of performance counters determining the resize procedure */
  100. struct starpu_sched_ctx_performance_counters *perf_counters;
  101. #endif //STARPU_USE_SC_HYPERVISOR
  102. /* callback called when the context finished executed its submitted tasks */
  103. void (*close_callback)(unsigned sched_ctx_id, void* args);
  104. void *close_args;
  105. /* value placing the contexts in their hierarchy */
  106. unsigned hierarchy_level;
  107. /* if we execute non-StarPU code inside the context
  108. we have a single master worker that stays awake,
  109. if not master is -1 */
  110. int main_master;
  111. /* conditions variables used when parallel sections are executed in contexts */
  112. starpu_pthread_cond_t parallel_sect_cond[STARPU_NMAXWORKERS];
  113. starpu_pthread_mutex_t parallel_sect_mutex[STARPU_NMAXWORKERS];
  114. starpu_pthread_cond_t parallel_sect_cond_busy[STARPU_NMAXWORKERS];
  115. int busy[STARPU_NMAXWORKERS];
  116. /* boolean indicating that workers should block in order to allow
  117. parallel sections to be executed on their allocated resources */
  118. unsigned parallel_sect[STARPU_NMAXWORKERS];
  119. /* semaphore that block appl thread until starpu threads are
  120. all blocked and ready to exec the parallel code */
  121. starpu_sem_t fall_asleep_sem[STARPU_NMAXWORKERS];
  122. /* semaphore that block appl thread until starpu threads are
  123. all woke up and ready continue appl */
  124. starpu_sem_t wake_up_sem[STARPU_NMAXWORKERS];
  125. /* bool indicating if the workers is sleeping in this ctx */
  126. unsigned sleeping[STARPU_NMAXWORKERS];
  127. /* ctx nesting the current ctx */
  128. unsigned nesting_sched_ctx;
  129. /* perf model for the device comb of the ctx */
  130. struct starpu_perfmodel_arch perf_arch;
  131. /* For parallel workers, say whether it is viewed as sequential or not. This
  132. is a helper for the prologue code. */
  133. unsigned parallel_view;
  134. /* for ctxs without policy: flag to indicate that we want to get
  135. the threads to sleep in order to replace them with other threads or leave
  136. them awake & use them in the parallel code*/
  137. unsigned awake_workers;
  138. /* function called when initializing the scheduler */
  139. void (*init_sched)(unsigned);
  140. int sub_ctxs[STARPU_NMAXWORKERS];
  141. int nsub_ctxs;
  142. /* nr of SMs assigned to this ctx if we partition gpus*/
  143. int nsms;
  144. int sms_start_idx;
  145. int sms_end_idx;
  146. int stream_worker;
  147. };
  148. struct _starpu_machine_config;
  149. /* init sched_ctx_id of all contextes*/
  150. void _starpu_init_all_sched_ctxs(struct _starpu_machine_config *config);
  151. /* allocate all structures belonging to a context */
  152. 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,
  153. int min_prio_set, int min_prio,
  154. int max_prio_set, int max_prio, unsigned awake_workers, void (*sched_policy_init)(unsigned), void *user_data,
  155. int nsub_ctxs, int *sub_ctxs, int nsms);
  156. /* delete all sched_ctx */
  157. void _starpu_delete_all_sched_ctxs();
  158. /* This function waits until all the tasks that were already submitted to a specific
  159. * context have been executed. */
  160. int _starpu_wait_for_all_tasks_of_sched_ctx(unsigned sched_ctx_id);
  161. /* This function waits until at most n tasks are still submitted. */
  162. int _starpu_wait_for_n_submitted_tasks_of_sched_ctx(unsigned sched_ctx_id, unsigned n);
  163. /* In order to implement starpu_wait_for_all_tasks_of_ctx, we keep track of the number of
  164. * task currently submitted to the context */
  165. void _starpu_decrement_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  166. void _starpu_increment_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  167. int _starpu_get_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  168. int _starpu_check_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  169. void _starpu_decrement_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops);
  170. unsigned _starpu_increment_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops, struct starpu_task *task);
  171. int _starpu_wait_for_no_ready_of_sched_ctx(unsigned sched_ctx_id);
  172. /* Return the corresponding index of the workerid in the ctx table */
  173. int _starpu_get_index_in_ctx_of_workerid(unsigned sched_ctx, unsigned workerid);
  174. /* Get the mutex corresponding to the global workerid */
  175. starpu_pthread_mutex_t *_starpu_get_sched_mutex(struct _starpu_sched_ctx *sched_ctx, int worker);
  176. /* Get workers belonging to a certain context, it returns the number of workers
  177. take care: no mutex taken, the list of workers might not be updated */
  178. int _starpu_get_workers_of_sched_ctx(unsigned sched_ctx_id, int *pus, enum starpu_worker_archtype arch);
  179. /* Let the worker know it does not belong to the context and that
  180. it should stop poping from it */
  181. void _starpu_worker_gets_out_of_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker);
  182. /* Check if the worker belongs to another sched_ctx */
  183. unsigned _starpu_worker_belongs_to_a_sched_ctx(int workerid, unsigned sched_ctx_id);
  184. /* mutex synchronising several simultaneous modifications of a context */
  185. starpu_pthread_rwlock_t* _starpu_sched_ctx_get_changing_ctx_mutex(unsigned sched_ctx_id);
  186. /* indicates wheather this worker should go to sleep or not
  187. (if it is the last one awake in a context he should better keep awake) */
  188. unsigned _starpu_sched_ctx_last_worker_awake(struct _starpu_worker *worker);
  189. /* let the appl know that the worker blocked to execute parallel code */
  190. void _starpu_sched_ctx_signal_worker_blocked(unsigned sched_ctx_id, int workerid);
  191. /* let the appl know that the worker woke up */
  192. void _starpu_sched_ctx_signal_worker_woke_up(unsigned sched_ctx_id, int workerid);
  193. /* If starpu_sched_ctx_set_context() has been called, returns the context
  194. * id set by its last call, or the id of the initial context */
  195. unsigned _starpu_sched_ctx_get_current_context();
  196. /* verify how many workers can execute a certain task */
  197. int _starpu_nworkers_able_to_execute_task(struct starpu_task *task, struct _starpu_sched_ctx *sched_ctx);
  198. void _starpu_fetch_tasks_from_empty_ctx_list(struct _starpu_sched_ctx *sched_ctx);
  199. unsigned _starpu_sched_ctx_allow_hypervisor(unsigned sched_ctx_id);
  200. struct starpu_perfmodel_arch * _starpu_sched_ctx_get_perf_archtype(unsigned sched_ctx);
  201. #ifdef STARPU_USE_SC_HYPERVISOR
  202. /* Notifies the hypervisor that a tasks was poped from the workers' list */
  203. void _starpu_sched_ctx_post_exec_task_cb(int workerid, struct starpu_task *task, size_t data_size, uint32_t footprint);
  204. #endif //STARPU_USE_SC_HYPERVISOR
  205. /* 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 */
  206. struct _starpu_sched_ctx *__starpu_sched_ctx_get_sched_ctx_for_worker_and_job(struct _starpu_worker *worker, struct _starpu_job *j);
  207. #define _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(w,j) \
  208. (_starpu_get_nsched_ctxs() <= 1 ? _starpu_get_sched_ctx_struct(0) : __starpu_sched_ctx_get_sched_ctx_for_worker_and_job((w),(j)))
  209. #endif // __SCHED_CONTEXT_H__