sched_ctx.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __SCHED_CONTEXT_H__
  17. #define __SCHED_CONTEXT_H__
  18. #include <starpu.h>
  19. #include <starpu_sched_ctx.h>
  20. #include <starpu_sched_ctx_hypervisor.h>
  21. #include <starpu_scheduler.h>
  22. #include <common/config.h>
  23. #include <common/barrier_counter.h>
  24. #include <profiling/profiling.h>
  25. #include <semaphore.h>
  26. #include "sched_ctx_list.h"
  27. #ifdef STARPU_HAVE_HWLOC
  28. #include <hwloc.h>
  29. #endif
  30. #define NO_RESIZE -1
  31. #define REQ_RESIZE 0
  32. #define DO_RESIZE 1
  33. #define STARPU_GLOBAL_SCHED_CTX 0
  34. struct _starpu_sched_ctx
  35. {
  36. /* id of the context used in user mode*/
  37. unsigned id;
  38. /* name of context */
  39. const char *name;
  40. /* policy of the context */
  41. struct starpu_sched_policy *sched_policy;
  42. /* data necessary for the policy */
  43. void *policy_data;
  44. struct starpu_worker_collection *workers;
  45. /* we keep an initial sched which we never delete */
  46. unsigned is_initial_sched;
  47. /* wait for the tasks submitted to the context to be executed */
  48. struct _starpu_barrier_counter tasks_barrier;
  49. /* wait for the tasks ready of the context to be executed */
  50. struct _starpu_barrier_counter ready_tasks_barrier;
  51. /* amount of ready flops in a context */
  52. double ready_flops;
  53. /* cond to block push when there are no workers in the ctx */
  54. starpu_pthread_cond_t no_workers_cond;
  55. /* mutex to block push when there are no workers in the ctx */
  56. starpu_pthread_mutex_t no_workers_mutex;
  57. /*ready tasks that couldn't be pushed because the ctx has no workers*/
  58. struct starpu_task_list empty_ctx_tasks;
  59. /* mutext protecting empty_ctx_tasks list */
  60. starpu_pthread_mutex_t empty_ctx_mutex;
  61. /* min CPUs to execute*/
  62. int min_ncpus;
  63. /* max CPUs to execute*/
  64. int max_ncpus;
  65. /* min GPUs to execute*/
  66. int min_ngpus;
  67. /* max GPUs to execute*/
  68. int max_ngpus;
  69. /* in case we delete the context leave resources to the inheritor*/
  70. unsigned inheritor;
  71. /* indicates whether the application finished submitting tasks
  72. to this context*/
  73. unsigned finished_submit;
  74. /* By default we have a binary type of priority: either a task is a priority
  75. * task (level 1) or it is not (level 0). */
  76. int min_priority;
  77. int max_priority;
  78. int min_priority_is_set;
  79. int max_priority_is_set;
  80. /* hwloc tree structure of workers */
  81. #ifdef STARPU_HAVE_HWLOC
  82. hwloc_bitmap_t hwloc_workers_set;
  83. #endif
  84. #ifdef STARPU_USE_SC_HYPERVISOR
  85. /* a structure containing a series of performance counters determining the resize procedure */
  86. struct starpu_sched_ctx_performance_counters *perf_counters;
  87. #endif //STARPU_USE_SC_HYPERVISOR
  88. /* callback called when the context finished executed its submitted tasks */
  89. void (*close_callback)(unsigned sched_ctx_id, void* args);
  90. void *close_args;
  91. /* value placing the contexts in their hierarchy */
  92. unsigned hierarchy_level;
  93. /* if we execute non-StarPU code inside the context
  94. we have a single master worker that stays awake,
  95. if not master is -1 */
  96. int main_master;
  97. /* conditions variables used when parallel sections are executed in contexts */
  98. starpu_pthread_cond_t parallel_sect_cond[STARPU_NMAXWORKERS];
  99. starpu_pthread_mutex_t parallel_sect_mutex[STARPU_NMAXWORKERS];
  100. /* boolean indicating that workers should block in order to allow
  101. parallel sections to be executed on their allocated resources */
  102. unsigned parallel_sect[STARPU_NMAXWORKERS];
  103. /* id of the master worker */
  104. int master[STARPU_NMAXWORKERS];
  105. /* semaphore that block appl thread until starpu threads are
  106. all blocked and ready to exec the parallel code */
  107. sem_t fall_asleep_sem[STARPU_NMAXWORKERS];
  108. /* semaphore that block appl thread until starpu threads are
  109. all woke up and ready continue appl */
  110. sem_t wake_up_sem[STARPU_NMAXWORKERS];
  111. /* bool indicating if the workers is sleeping in this ctx */
  112. unsigned sleeping[STARPU_NMAXWORKERS];
  113. };
  114. struct _starpu_machine_config;
  115. /* init sched_ctx_id of all contextes*/
  116. void _starpu_init_all_sched_ctxs(struct _starpu_machine_config *config);
  117. /* allocate all structures belonging to a context */
  118. 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,
  119. int min_prio_set, int min_prio,
  120. int max_prio_set, int max_prio);
  121. /* delete all sched_ctx */
  122. void _starpu_delete_all_sched_ctxs();
  123. /* This function waits until all the tasks that were already submitted to a specific
  124. * context have been executed. */
  125. int _starpu_wait_for_all_tasks_of_sched_ctx(unsigned sched_ctx_id);
  126. /* In order to implement starpu_wait_for_all_tasks_of_ctx, we keep track of the number of
  127. * task currently submitted to the context */
  128. void _starpu_decrement_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  129. void _starpu_increment_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  130. int _starpu_get_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  131. int _starpu_check_nsubmitted_tasks_of_sched_ctx(unsigned sched_ctx_id);
  132. void _starpu_decrement_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops);
  133. void _starpu_increment_nready_tasks_of_sched_ctx(unsigned sched_ctx_id, double ready_flops);
  134. int _starpu_wait_for_no_ready_of_sched_ctx(unsigned sched_ctx_id);
  135. /* Return the corresponding index of the workerid in the ctx table */
  136. int _starpu_get_index_in_ctx_of_workerid(unsigned sched_ctx, unsigned workerid);
  137. /* Get the total number of sched_ctxs created till now */
  138. unsigned _starpu_get_nsched_ctxs();
  139. /* Get the mutex corresponding to the global workerid */
  140. starpu_pthread_mutex_t *_starpu_get_sched_mutex(struct _starpu_sched_ctx *sched_ctx, int worker);
  141. /* Get workers belonging to a certain context, it returns the number of workers
  142. take care: no mutex taken, the list of workers might not be updated */
  143. int _starpu_get_workers_of_sched_ctx(unsigned sched_ctx_id, int *pus, enum starpu_worker_archtype arch);
  144. /* Let the worker know it does not belong to the context and that
  145. it should stop poping from it */
  146. void _starpu_worker_gets_out_of_ctx(unsigned sched_ctx_id, struct _starpu_worker *worker);
  147. /* Check if the worker belongs to another sched_ctx */
  148. unsigned _starpu_worker_belongs_to_a_sched_ctx(int workerid, unsigned sched_ctx_id);
  149. /* mutex synchronising several simultaneous modifications of a context */
  150. starpu_pthread_rwlock_t* _starpu_sched_ctx_get_changing_ctx_mutex(unsigned sched_ctx_id);
  151. /* indicates wheather this worker should go to sleep or not
  152. (if it is the last one awake in a context he should better keep awake) */
  153. unsigned _starpu_sched_ctx_last_worker_awake(struct _starpu_worker *worker);
  154. /* let the appl know that the worker blocked to execute parallel code */
  155. void _starpu_sched_ctx_signal_worker_blocked(unsigned sched_ctx_id, int workerid);
  156. /* let the appl know that the worker woke up */
  157. void _starpu_sched_ctx_signal_worker_woke_up(unsigned sched_ctx_id, int workerid);
  158. /* If starpu_sched_ctx_set_context() has been called, returns the context
  159. * id set by its last call, or the id of the initial context */
  160. unsigned _starpu_sched_ctx_get_current_context();
  161. /* verify how many workers can execute a certain task */
  162. int _starpu_nworkers_able_to_execute_task(struct starpu_task *task, struct _starpu_sched_ctx *sched_ctx);
  163. void _starpu_fetch_tasks_from_empty_ctx_list(struct _starpu_sched_ctx *sched_ctx);
  164. unsigned _starpu_sched_ctx_allow_hypervisor(unsigned sched_ctx_id);
  165. #ifdef STARPU_USE_SC_HYPERVISOR
  166. /* Notifies the hypervisor that a tasks was poped from the workers' list */
  167. void _starpu_sched_ctx_post_exec_task_cb(int workerid, struct starpu_task *task, size_t data_size, uint32_t footprint);
  168. #endif //STARPU_USE_SC_HYPERVISOR
  169. #endif // __SCHED_CONTEXT_H__