workers.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <common/config.h>
  22. #include <common/timing.h>
  23. #include <common/fxt.h>
  24. #include <core/jobs.h>
  25. #include <core/perfmodel/perfmodel.h>
  26. #include <core/sched_policy.h>
  27. #include <core/topology.h>
  28. #include <core/errorcheck.h>
  29. #include <core/sched_ctx.h>
  30. #ifdef STARPU_HAVE_HWLOC
  31. #include <hwloc.h>
  32. #endif
  33. #include <drivers/cuda/driver_cuda.h>
  34. #include <drivers/opencl/driver_opencl.h>
  35. #ifdef STARPU_USE_MIC
  36. #include <drivers/mic/driver_mic_source.h>
  37. #endif /* STARPU_USE_MIC */
  38. #ifdef STARPU_USE_SCC
  39. #include <drivers/scc/driver_scc_source.h>
  40. #endif
  41. #include <drivers/cpu/driver_cpu.h>
  42. #include <datawizard/datawizard.h>
  43. #include <starpu_parameters.h>
  44. struct _starpu_worker
  45. {
  46. struct _starpu_machine_config *config;
  47. starpu_pthread_mutex_t mutex;
  48. enum starpu_worker_archtype arch; /* what is the type of worker ? */
  49. uint32_t worker_mask; /* what is the type of worker ? */
  50. enum starpu_perfmodel_archtype perf_arch; /* in case there are different models of the same arch */
  51. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  52. int mp_nodeid; /* which mp node hold the cpu/gpu/etc (-1 for this
  53. * node) */
  54. unsigned devid; /* which cpu/gpu/etc is controlled by the worker ? */
  55. int bindid; /* which cpu is the driver bound to ? (logical index) */
  56. int workerid; /* uniquely identify the worker among all processing units types */
  57. int combined_workerid; /* combined worker currently using this worker */
  58. int current_rank; /* current rank in case the worker is used in a parallel fashion */
  59. int worker_size; /* size of the worker in case we use a combined worker */
  60. starpu_pthread_cond_t started_cond; /* indicate when the worker is ready */
  61. starpu_pthread_cond_t ready_cond; /* indicate when the worker is ready */
  62. unsigned memory_node; /* which memory node is the worker associated with ? */
  63. starpu_pthread_cond_t sched_cond; /* condition variable used when the worker waits for tasks. */
  64. starpu_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. unsigned has_prev_init; /* had already been inited in another ctx */
  79. /* indicated in each ctx the workers can execute tasks on,
  80. used for overlapping ctx in order to determine on which
  81. ctx the worker is allowed to pop */
  82. unsigned active_ctx;
  83. unsigned removed_from_ctx[STARPU_NMAX_SCHED_CTXS];
  84. /* conditions variables used when parallel sections are executed in contexts */
  85. starpu_pthread_cond_t parallel_sect_cond;
  86. starpu_pthread_mutex_t parallel_sect_mutex;
  87. /* boolean indicating that workers should block in order to allow
  88. parallel sections to be executed on their allocated resources */
  89. unsigned parallel_sect;
  90. #ifdef __GLIBC__
  91. cpu_set_t initial_cpu_set;
  92. cpu_set_t current_cpu_set;
  93. #endif /* __GLIBC__ */
  94. #ifdef STARPU_HAVE_HWLOC
  95. hwloc_bitmap_t initial_hwloc_cpu_set;
  96. hwloc_bitmap_t current_hwloc_cpu_set;
  97. #endif
  98. };
  99. struct _starpu_combined_worker
  100. {
  101. enum starpu_perfmodel_archtype perf_arch; /* in case there are different models of the same arch */
  102. uint32_t worker_mask; /* what is the type of workers ? */
  103. int worker_size;
  104. unsigned memory_node; /* which memory node is associated that worker to ? */
  105. int combined_workerid[STARPU_NMAXWORKERS];
  106. #ifdef __GLIBC__
  107. cpu_set_t cpu_set;
  108. #endif /* __GLIBC__ */
  109. #ifdef STARPU_HAVE_HWLOC
  110. hwloc_bitmap_t hwloc_cpu_set;
  111. #endif
  112. };
  113. /* in case a single CPU worker may control multiple
  114. * accelerators (eg. Gordon for n SPUs) */
  115. struct _starpu_worker_set
  116. {
  117. starpu_pthread_mutex_t mutex;
  118. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  119. unsigned nworkers;
  120. unsigned started; /* Only one thread for the whole set */
  121. void *retval;
  122. struct _starpu_worker *workers;
  123. starpu_pthread_cond_t ready_cond; /* indicate when the set is ready */
  124. unsigned set_is_initialized;
  125. };
  126. struct _starpu_machine_config
  127. {
  128. struct starpu_machine_topology topology;
  129. #ifdef STARPU_HAVE_HWLOC
  130. int cpu_depth;
  131. #endif
  132. /* Where to bind workers ? */
  133. int current_bindid;
  134. /* Which GPU(s) do we use for CUDA ? */
  135. int current_cuda_gpuid;
  136. /* Which GPU(s) do we use for OpenCL ? */
  137. int current_opencl_gpuid;
  138. /* Which MIC do we use? */
  139. int current_mic_deviceid;
  140. /* Which SCC do we use? */
  141. int current_scc_deviceid;
  142. /* Basic workers : each of this worker is running its own driver and
  143. * can be combined with other basic workers. */
  144. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  145. /* Combined workers: these worker are a combination of basic workers
  146. * that can run parallel tasks together. */
  147. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  148. /* This bitmask indicates which kinds of worker are available. For
  149. * instance it is possible to test if there is a CUDA worker with
  150. * the result of (worker_mask & STARPU_CUDA). */
  151. uint32_t worker_mask;
  152. /* either the user given configuration passed to starpu_init or a default configuration */
  153. struct starpu_conf *conf;
  154. /* set to 1 if no conf has been given by the user, it
  155. * indicates the memory allocated for the default
  156. * configuration should be freed on shutdown */
  157. int default_conf;
  158. /* this flag is set until the runtime is stopped */
  159. unsigned running;
  160. /* all the sched ctx of the current instance of starpu */
  161. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS];
  162. /* this flag is set until the application is finished submitting tasks */
  163. unsigned submitting;
  164. };
  165. /* Three functions to manage argv, argc */
  166. void _starpu_set_argc_argv(int *argc, char ***argv);
  167. int *_starpu_get_argc();
  168. char ***_starpu_get_argv();
  169. /* Fill conf with environment variables */
  170. void _starpu_conf_check_environment(struct starpu_conf *conf);
  171. /* Has starpu_shutdown already been called ? */
  172. unsigned _starpu_machine_is_running(void);
  173. /* Check if there is a worker that may execute the task. */
  174. uint32_t _starpu_worker_exists(struct starpu_task *);
  175. /* Is there a worker that can execute CUDA code ? */
  176. uint32_t _starpu_can_submit_cuda_task(void);
  177. /* Is there a worker that can execute CPU code ? */
  178. uint32_t _starpu_can_submit_cpu_task(void);
  179. /* Is there a worker that can execute OpenCL code ? */
  180. uint32_t _starpu_can_submit_opencl_task(void);
  181. /* Is there a worker that can execute OpenCL code ? */
  182. uint32_t _starpu_can_submit_scc_task(void);
  183. /* Check whether there is anything that the worker should do instead of
  184. * sleeping (waiting on something to happen). */
  185. unsigned _starpu_worker_can_block(unsigned memnode);
  186. /* This function must be called to block a worker. It puts the worker in a
  187. * sleeping state until there is some event that forces the worker to wake up.
  188. * */
  189. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  190. /* The _starpu_worker structure describes all the state of a StarPU worker.
  191. * This function sets the pthread key which stores a pointer to this structure.
  192. * */
  193. void _starpu_set_local_worker_key(struct _starpu_worker *worker);
  194. /* This function initializes the current thread for the given worker */
  195. void _starpu_worker_init(struct _starpu_worker *worker, unsigned fut_key);
  196. /* Returns the _starpu_worker structure that describes the state of the
  197. * current worker. */
  198. struct _starpu_worker *_starpu_get_local_worker_key(void);
  199. /* Returns the _starpu_worker structure that describes the state of the
  200. * specified worker. */
  201. struct _starpu_worker *_starpu_get_worker_struct(unsigned id);
  202. /* Returns the starpu_sched_ctx structure that descriebes the state of the
  203. * specified ctx */
  204. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id);
  205. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  206. int _starpu_is_initialized(void);
  207. /* Returns the structure that describes the overall machine configuration (eg.
  208. * all workers and topology). */
  209. struct _starpu_machine_config *_starpu_get_machine_config(void);
  210. /* Retrieve the status which indicates what the worker is currently doing. */
  211. enum _starpu_worker_status _starpu_worker_get_status(int workerid);
  212. /* Change the status of the worker which indicates what the worker is currently
  213. * doing (eg. executing a callback). */
  214. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status);
  215. /* We keep an initial sched ctx which might be used in case no other ctx is available */
  216. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void);
  217. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  218. /* returns workers not belonging to any context, be careful no mutex is used,
  219. the list might not be updated */
  220. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  221. #endif // __WORKERS_H__