workers.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 INRIA
  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 __WORKERS_H__
  19. #define __WORKERS_H__
  20. #include <starpu.h>
  21. #include <starpu_scheduler.h>
  22. #include <common/config.h>
  23. #include <pthread.h>
  24. #include <common/timing.h>
  25. #include <common/fxt.h>
  26. #include <core/jobs.h>
  27. #include <core/perfmodel/perfmodel.h>
  28. #include <core/sched_policy.h>
  29. #include <core/topology.h>
  30. #include <core/errorcheck.h>
  31. #include <core/sched_ctx.h>
  32. #ifdef STARPU_HAVE_HWLOC
  33. #include <hwloc.h>
  34. #endif
  35. #ifdef STARPU_USE_CUDA
  36. #include <drivers/cuda/driver_cuda.h>
  37. #endif
  38. #ifdef STARPU_USE_OPENCL
  39. #include <drivers/opencl/driver_opencl.h>
  40. #endif
  41. #ifdef STARPU_USE_GORDON
  42. #include <drivers/gordon/driver_gordon.h>
  43. #endif
  44. #include <drivers/cpu/driver_cpu.h>
  45. #include <datawizard/datawizard.h>
  46. #include <starpu_parameters.h>
  47. struct _starpu_worker
  48. {
  49. struct _starpu_machine_config *config;
  50. pthread_mutex_t mutex;
  51. enum starpu_archtype arch; /* what is the type of worker ? */
  52. uint32_t worker_mask; /* what is the type of worker ? */
  53. enum starpu_perf_archtype perf_arch; /* in case there are different models of the same arch */
  54. pthread_t worker_thread; /* the thread which runs the worker */
  55. unsigned devid; /* which cpu/gpu/etc is controlled by the worker ? */
  56. int bindid; /* which cpu is the driver bound to ? (logical index) */
  57. int workerid; /* uniquely identify the worker among all processing units types */
  58. int combined_workerid; /* combined worker currently using this worker */
  59. int current_rank; /* current rank in case the worker is used in a parallel fashion */
  60. int worker_size; /* size of the worker in case we use a combined worker */
  61. pthread_cond_t ready_cond; /* indicate when the worker is ready */
  62. unsigned memory_node; /* which memory node is the worker associated with ? */
  63. pthread_cond_t sched_cond; /* condition variable used when the worker waits for tasks. */
  64. pthread_mutex_t sched_mutex; /* mutex protecting sched_cond */
  65. struct starpu_task_list local_tasks; /* this queue contains tasks that have been explicitely submitted to that queue */
  66. struct starpu_task *current_task; /* task currently executed by this worker */
  67. struct _starpu_worker_set *set; /* in case this worker belongs to a set */
  68. struct _starpu_job_list *terminated_jobs; /* list of pending jobs which were executed */
  69. unsigned worker_is_running;
  70. unsigned worker_is_initialized;
  71. enum _starpu_worker_status status; /* what is the worker doing now ? (eg. CALLBACK) */
  72. char name[64];
  73. char short_name[10];
  74. unsigned run_by_starpu; /* Is this run by StarPU or directly by the application ? */
  75. struct _starpu_sched_ctx **sched_ctx;
  76. unsigned nsched_ctxs; /* the no of contexts a worker belongs to*/
  77. struct _starpu_barrier_counter tasks_barrier; /* wait for the tasks submitted */
  78. struct starpu_task *tasks[STARPU_NMAX_SCHED_CTXS];
  79. unsigned has_prev_init; /* had already been inited in another ctx */
  80. /* indicated in each ctx the workers can execute tasks on,
  81. used for overlapping ctx in order to determine on which
  82. ctx the worker is allowed to pop */
  83. unsigned active_ctx;
  84. #ifdef __GLIBC__
  85. cpu_set_t initial_cpu_set;
  86. cpu_set_t current_cpu_set;
  87. #endif /* __GLIBC__ */
  88. #ifdef STARPU_HAVE_HWLOC
  89. hwloc_bitmap_t initial_hwloc_cpu_set;
  90. hwloc_bitmap_t current_hwloc_cpu_set;
  91. #endif
  92. };
  93. struct _starpu_combined_worker
  94. {
  95. enum starpu_perf_archtype perf_arch; /* in case there are different models of the same arch */
  96. uint32_t worker_mask; /* what is the type of workers ? */
  97. int worker_size;
  98. unsigned memory_node; /* which memory node is associated that worker to ? */
  99. int combined_workerid[STARPU_NMAXWORKERS];
  100. #ifdef __GLIBC__
  101. cpu_set_t cpu_set;
  102. #endif /* __GLIBC__ */
  103. #ifdef STARPU_HAVE_HWLOC
  104. hwloc_bitmap_t hwloc_cpu_set;
  105. #endif
  106. };
  107. /* in case a single CPU worker may control multiple
  108. * accelerators (eg. Gordon for n SPUs) */
  109. struct _starpu_worker_set
  110. {
  111. pthread_mutex_t mutex;
  112. pthread_t worker_thread; /* the thread which runs the worker */
  113. unsigned nworkers;
  114. unsigned joined; /* only one thread may call pthread_join*/
  115. void *retval;
  116. struct _starpu_worker *workers;
  117. pthread_cond_t ready_cond; /* indicate when the set is ready */
  118. unsigned set_is_initialized;
  119. };
  120. struct _starpu_machine_config
  121. {
  122. struct starpu_machine_topology topology;
  123. #ifdef STARPU_HAVE_HWLOC
  124. int cpu_depth;
  125. #endif
  126. /* Where to bind workers ? */
  127. int current_bindid;
  128. /* Which GPU(s) do we use for CUDA ? */
  129. int current_cuda_gpuid;
  130. /* Which GPU(s) do we use for OpenCL ? */
  131. int current_opencl_gpuid;
  132. /* Basic workers : each of this worker is running its own driver and
  133. * can be combined with other basic workers. */
  134. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  135. /* Combined workers: these worker are a combination of basic workers
  136. * that can run parallel tasks together. */
  137. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  138. /* This bitmask indicates which kinds of worker are available. For
  139. * instance it is possible to test if there is a CUDA worker with
  140. * the result of (worker_mask & STARPU_CUDA). */
  141. uint32_t worker_mask;
  142. /* either the user given configuration passed to starpu_init or a default configuration */
  143. struct starpu_conf *conf;
  144. /* set to 1 if no conf has been given by the user, it
  145. * indicates the memory allocated for the default
  146. * configuration should be freed on shutdown */
  147. int default_conf;
  148. /* this flag is set until the runtime is stopped */
  149. unsigned running;
  150. /* all the sched ctx of the current instance of starpu */
  151. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS];
  152. /* this flag is set until the application is finished submitting tasks */
  153. unsigned submitting;
  154. };
  155. /* Has starpu_shutdown already been called ? */
  156. unsigned _starpu_machine_is_running(void);
  157. /* Check if there is a worker that may execute the task. */
  158. uint32_t _starpu_worker_exists(struct starpu_task *);
  159. /* Is there a worker that can execute CUDA code ? */
  160. uint32_t _starpu_can_submit_cuda_task(void);
  161. /* Is there a worker that can execute CPU code ? */
  162. uint32_t _starpu_can_submit_cpu_task(void);
  163. /* Is there a worker that can execute OpenCL code ? */
  164. uint32_t _starpu_can_submit_opencl_task(void);
  165. /* Check whether there is anything that the worker should do instead of
  166. * sleeping (waiting on something to happen). */
  167. unsigned _starpu_worker_can_block(unsigned memnode);
  168. /* This function must be called to block a worker. It puts the worker in a
  169. * sleeping state until there is some event that forces the worker to wake up.
  170. * */
  171. void _starpu_block_worker(int workerid, pthread_cond_t *cond, pthread_mutex_t *mutex);
  172. /* The _starpu_worker structure describes all the state of a StarPU worker.
  173. * This function sets the pthread key which stores a pointer to this structure.
  174. * */
  175. void _starpu_set_local_worker_key(struct _starpu_worker *worker);
  176. /* This function initializes the current thread for the given worker */
  177. void _starpu_worker_init(struct _starpu_worker *worker, unsigned fut_key);
  178. /* Returns the _starpu_worker structure that describes the state of the
  179. * current worker. */
  180. struct _starpu_worker *_starpu_get_local_worker_key(void);
  181. /* Returns the _starpu_worker structure that describes the state of the
  182. * specified worker. */
  183. struct _starpu_worker *_starpu_get_worker_struct(unsigned id);
  184. /* Returns the starpu_sched_ctx structure that descriebes the state of the
  185. * specified ctx */
  186. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id);
  187. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  188. int _starpu_is_initialized(void);
  189. /* Returns the structure that describes the overall machine configuration (eg.
  190. * all workers and topology). */
  191. struct _starpu_machine_config *_starpu_get_machine_config(void);
  192. /* Retrieve the status which indicates what the worker is currently doing. */
  193. enum _starpu_worker_status _starpu_worker_get_status(int workerid);
  194. /* Change the status of the worker which indicates what the worker is currently
  195. * doing (eg. executing a callback). */
  196. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status);
  197. /* TODO move */
  198. unsigned _starpu_execute_registered_progression_hooks(void);
  199. /* We keep an initial sched ctx which might be used in case no other ctx is available */
  200. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void);
  201. int starpu_worker_get_nids_by_type(enum starpu_archtype type, int *workerids, int maxsize);
  202. /* returns workers not belonging to any context, be careful no mutex is used,
  203. the list might not be updated */
  204. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_archtype type, int *workerids, int maxsize);
  205. #endif // __WORKERS_H__