workers.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 CNRS
  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. #include <core/sched_ctx_list.h>
  31. #ifdef STARPU_HAVE_HWLOC
  32. #include <hwloc.h>
  33. #endif
  34. #include <drivers/cuda/driver_cuda.h>
  35. #include <drivers/opencl/driver_opencl.h>
  36. #ifdef STARPU_USE_MIC
  37. #include <drivers/mic/driver_mic_source.h>
  38. #endif /* STARPU_USE_MIC */
  39. #ifdef STARPU_USE_SCC
  40. #include <drivers/scc/driver_scc_source.h>
  41. #endif
  42. #include <drivers/cpu/driver_cpu.h>
  43. #include <datawizard/datawizard.h>
  44. #include <starpu_parameters.h>
  45. #define STARPU_MAX_PIPELINE 4
  46. /* This is initialized from in _starpu_worker_init */
  47. LIST_TYPE(_starpu_worker,
  48. struct _starpu_machine_config *config;
  49. starpu_pthread_mutex_t mutex;
  50. enum starpu_worker_archtype arch; /* what is the type of worker ? */
  51. uint32_t worker_mask; /* what is the type of worker ? */
  52. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  53. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  54. unsigned devid; /* which cpu/gpu/etc is controlled by the worker ? */
  55. unsigned subworkerid; /* which sub-worker this one is for the cpu/gpu */
  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. starpu_pthread_cond_t started_cond; /* indicate when the worker is ready */
  62. starpu_pthread_cond_t ready_cond; /* indicate when the worker is ready */
  63. unsigned memory_node; /* which memory node is the worker associated with ? */
  64. starpu_pthread_cond_t sched_cond; /* condition variable used when the worker waits for tasks. */
  65. starpu_pthread_mutex_t sched_mutex; /* mutex protecting sched_cond */
  66. struct starpu_task_list local_tasks; /* this queue contains tasks that have been explicitely submitted to that queue */
  67. struct starpu_task **local_ordered_tasks; /* this queue contains tasks that have been explicitely submitted to that queue with an explicit order */
  68. unsigned local_ordered_tasks_size; /* this records the size of local_ordered_tasks */
  69. unsigned current_ordered_task; /* this records the index (within local_ordered_tasks) of the next ordered task to be executed */
  70. unsigned current_ordered_task_order; /* this records the order of the next ordered task to be executed */
  71. struct starpu_task *current_task; /* task currently executed by this worker (non-pipelined version) */
  72. struct starpu_task *current_tasks[STARPU_MAX_PIPELINE]; /* tasks currently executed by this worker (pipelined version) */
  73. unsigned char first_task; /* Index of first task in the pipeline */
  74. unsigned char ntasks; /* number of tasks in the pipeline */
  75. unsigned char pipeline_length; /* number of tasks to be put in the pipeline */
  76. unsigned char pipeline_stuck; /* whether a task prevents us from pipelining */
  77. struct _starpu_worker_set *set; /* in case this worker belongs to a set */
  78. unsigned worker_is_running;
  79. unsigned worker_is_initialized;
  80. enum _starpu_worker_status status; /* what is the worker doing now ? (eg. CALLBACK) */
  81. char name[64];
  82. char short_name[10];
  83. unsigned run_by_starpu; /* Is this run by StarPU or directly by the application ? */
  84. struct _starpu_sched_ctx_list *sched_ctx_list;
  85. int tmp_sched_ctx;
  86. unsigned nsched_ctxs; /* the no of contexts a worker belongs to*/
  87. struct _starpu_barrier_counter tasks_barrier; /* wait for the tasks submitted */
  88. unsigned has_prev_init; /* had already been inited in another ctx */
  89. unsigned removed_from_ctx[STARPU_NMAX_SCHED_CTXS];
  90. unsigned spinning_backoff ; /* number of cycles to pause when spinning */
  91. /* indicate whether the workers shares tasks lists with other workers*/
  92. /* in this case when removing him from a context it disapears instantly */
  93. unsigned shares_tasks_lists[STARPU_NMAX_SCHED_CTXS];
  94. /* boolean to chose the next ctx a worker will pop into */
  95. unsigned poped_in_ctx[STARPU_NMAX_SCHED_CTXS];
  96. /* boolean indicating at which moment we checked all ctxs and change phase for the booleab poped_in_ctx*/
  97. /* one for each of the 2 priorities*/
  98. unsigned reverse_phase[2];
  99. /* indicate which priority of ctx is currently active: the values are 0 or 1*/
  100. unsigned pop_ctx_priority;
  101. /* flag to know if sched_mutex is locked or not */
  102. unsigned sched_mutex_locked;
  103. /* bool to indicate if the worker is blocked in a ctx */
  104. unsigned blocked;
  105. /* bool to indicate if the worker is slave in a ctx */
  106. unsigned is_slave_somewhere;
  107. #ifdef __GLIBC__
  108. cpu_set_t cpu_set;
  109. #endif /* __GLIBC__ */
  110. #ifdef STARPU_HAVE_HWLOC
  111. hwloc_bitmap_t hwloc_cpu_set;
  112. #endif
  113. );
  114. struct _starpu_combined_worker
  115. {
  116. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  117. uint32_t worker_mask; /* what is the type of workers ? */
  118. int worker_size;
  119. unsigned memory_node; /* which memory node is associated that worker to ? */
  120. int combined_workerid[STARPU_NMAXWORKERS];
  121. #ifdef STARPU_USE_MP
  122. int count;
  123. starpu_pthread_mutex_t count_mutex;
  124. #endif
  125. #ifdef __GLIBC__
  126. cpu_set_t cpu_set;
  127. #endif /* __GLIBC__ */
  128. #ifdef STARPU_HAVE_HWLOC
  129. hwloc_bitmap_t hwloc_cpu_set;
  130. #endif
  131. };
  132. /* in case a single CPU worker may control multiple
  133. * accelerators (eg. Gordon for n SPUs) */
  134. struct _starpu_worker_set
  135. {
  136. starpu_pthread_mutex_t mutex;
  137. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  138. unsigned nworkers;
  139. unsigned started; /* Only one thread for the whole set */
  140. void *retval;
  141. struct _starpu_worker *workers;
  142. starpu_pthread_cond_t ready_cond; /* indicate when the set is ready */
  143. unsigned set_is_initialized;
  144. };
  145. struct _starpu_machine_topology
  146. {
  147. /* Total number of workers. */
  148. unsigned nworkers;
  149. /* Total number of combined workers. */
  150. unsigned ncombinedworkers;
  151. unsigned nsched_ctxs;
  152. #ifdef STARPU_HAVE_HWLOC
  153. /* Topology as detected by hwloc. */
  154. hwloc_topology_t hwtopology;
  155. #endif
  156. /* custom hwloc tree*/
  157. struct starpu_tree *tree;
  158. /* Total number of CPUs, as detected by the topology code. May
  159. * be different from the actual number of CPU workers.
  160. */
  161. unsigned nhwcpus;
  162. /* Total number of PUs, as detected by the topology code. May
  163. * be different from the actual number of PU workers.
  164. */
  165. unsigned nhwpus;
  166. /* Total number of CUDA devices, as detected. May be different
  167. * from the actual number of CUDA workers.
  168. */
  169. unsigned nhwcudagpus;
  170. /* Total number of OpenCL devices, as detected. May be
  171. * different from the actual number of OpenCL workers.
  172. */
  173. unsigned nhwopenclgpus;
  174. /* Total number of SCC cores, as detected. May be different
  175. * from the actual number of core workers.
  176. */
  177. unsigned nhwscc;
  178. /* Actual number of CPU workers used by StarPU. */
  179. unsigned ncpus;
  180. /* Actual number of CUDA workers used by StarPU. */
  181. unsigned ncudagpus;
  182. /* Actual number of OpenCL workers used by StarPU. */
  183. unsigned nopenclgpus;
  184. /* Actual number of SCC workers used by StarPU. */
  185. unsigned nsccdevices;
  186. /* Topology of MP nodes (mainly MIC and SCC) as well as necessary
  187. * objects to communicate with them. */
  188. unsigned nhwmicdevices;
  189. unsigned nmicdevices;
  190. unsigned nhwmiccores[STARPU_MAXMICDEVS]; // Each MIC node has its set of cores.
  191. unsigned nmiccores[STARPU_MAXMICDEVS];
  192. /* Indicates the successive logical PU identifier that should be used
  193. * to bind the workers. It is either filled according to the
  194. * user's explicit parameters (from starpu_conf) or according
  195. * to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  196. * round-robin policy is used to distributed the workers over
  197. * the cores.
  198. */
  199. unsigned workers_bindid[STARPU_NMAXWORKERS];
  200. /* Indicates the successive CUDA identifier that should be
  201. * used by the CUDA driver. It is either filled according to
  202. * the user's explicit parameters (from starpu_conf) or
  203. * according to the STARPU_WORKERS_CUDAID env. variable.
  204. * Otherwise, they are taken in ID order.
  205. */
  206. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  207. /* Indicates the successive OpenCL identifier that should be
  208. * used by the OpenCL driver. It is either filled according
  209. * to the user's explicit parameters (from starpu_conf) or
  210. * according to the STARPU_WORKERS_OPENCLID env. variable.
  211. * Otherwise, they are taken in ID order.
  212. */
  213. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  214. /** Indicates the successive MIC devices that should be used
  215. * by the MIC driver. It is either filled according to the
  216. * user's explicit parameters (from starpu_conf) or according
  217. * to the STARPU_WORKERS_MICID env. variable. Otherwise, they
  218. * are taken in ID order. */
  219. /* TODO */
  220. /* unsigned workers_mic_deviceid[STARPU_NMAXWORKERS]; */
  221. /* Which SCC(s) do we use ? */
  222. /* Indicates the successive SCC devices that should be used by
  223. * the SCC driver. It is either filled according to the
  224. * user's explicit parameters (from starpu_conf) or according
  225. * to the STARPU_WORKERS_SCCID env. variable. Otherwise, they
  226. * are taken in ID order.
  227. */
  228. unsigned workers_scc_deviceid[STARPU_NMAXWORKERS];
  229. };
  230. struct _starpu_machine_config
  231. {
  232. struct _starpu_machine_topology topology;
  233. #ifdef STARPU_HAVE_HWLOC
  234. int cpu_depth;
  235. int pu_depth;
  236. #endif
  237. /* Where to bind workers ? */
  238. int current_bindid;
  239. /* Which GPU(s) do we use for CUDA ? */
  240. int current_cuda_gpuid;
  241. /* Which GPU(s) do we use for OpenCL ? */
  242. int current_opencl_gpuid;
  243. /* Which MIC do we use? */
  244. int current_mic_deviceid;
  245. /* Which SCC do we use? */
  246. int current_scc_deviceid;
  247. /* Memory node for cpus, if only one */
  248. int cpus_nodeid;
  249. /* Memory node for CUDA, if only one */
  250. int cuda_nodeid;
  251. /* Memory node for OpenCL, if only one */
  252. int opencl_nodeid;
  253. /* Memory node for MIC, if only one */
  254. int mic_nodeid;
  255. /* Memory node for SCC, if only one */
  256. int scc_nodeid;
  257. /* Basic workers : each of this worker is running its own driver and
  258. * can be combined with other basic workers. */
  259. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  260. /* Combined workers: these worker are a combination of basic workers
  261. * that can run parallel tasks together. */
  262. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  263. /* This bitmask indicates which kinds of worker are available. For
  264. * instance it is possible to test if there is a CUDA worker with
  265. * the result of (worker_mask & STARPU_CUDA). */
  266. uint32_t worker_mask;
  267. /* either the user given configuration passed to starpu_init or a default configuration */
  268. struct starpu_conf *conf;
  269. /* set to 1 if no conf has been given by the user, it
  270. * indicates the memory allocated for the default
  271. * configuration should be freed on shutdown */
  272. int default_conf;
  273. /* this flag is set until the runtime is stopped */
  274. unsigned running;
  275. /* Number of calls to starpu_pause() - calls to starpu_resume(). When >0,
  276. * StarPU should pause. */
  277. int pause_depth;
  278. /* all the sched ctx of the current instance of starpu */
  279. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS];
  280. /* this flag is set until the application is finished submitting tasks */
  281. unsigned submitting;
  282. int watchdog_ok;
  283. starpu_pthread_mutex_t submitted_mutex;
  284. };
  285. /* Three functions to manage argv, argc */
  286. void _starpu_set_argc_argv(int *argc, char ***argv);
  287. int *_starpu_get_argc();
  288. char ***_starpu_get_argv();
  289. /* Fill conf with environment variables */
  290. void _starpu_conf_check_environment(struct starpu_conf *conf);
  291. /* Called by the driver when it is ready to pause */
  292. void _starpu_may_pause(void);
  293. /* Has starpu_shutdown already been called ? */
  294. unsigned _starpu_machine_is_running(void);
  295. /* Check if there is a worker that may execute the task. */
  296. uint32_t _starpu_worker_exists(struct starpu_task *);
  297. /* Is there a worker that can execute CUDA code ? */
  298. uint32_t _starpu_can_submit_cuda_task(void);
  299. /* Is there a worker that can execute CPU code ? */
  300. uint32_t _starpu_can_submit_cpu_task(void);
  301. /* Is there a worker that can execute OpenCL code ? */
  302. uint32_t _starpu_can_submit_opencl_task(void);
  303. /* Is there a worker that can execute OpenCL code ? */
  304. uint32_t _starpu_can_submit_scc_task(void);
  305. /* Check whether there is anything that the worker should do instead of
  306. * sleeping (waiting on something to happen). */
  307. unsigned _starpu_worker_can_block(unsigned memnode, struct _starpu_worker *worker);
  308. /* This function must be called to block a worker. It puts the worker in a
  309. * sleeping state until there is some event that forces the worker to wake up.
  310. * */
  311. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  312. /* This function initializes the current driver for the given worker */
  313. void _starpu_driver_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  314. /* This function initializes the current thread for the given worker */
  315. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  316. /* The _starpu_worker structure describes all the state of a StarPU worker.
  317. * This function sets the pthread key which stores a pointer to this structure.
  318. * */
  319. void _starpu_set_local_worker_key(struct _starpu_worker *worker);
  320. /* Returns the _starpu_worker structure that describes the state of the
  321. * current worker. */
  322. struct _starpu_worker *_starpu_get_local_worker_key(void);
  323. /* The _starpu_worker_set structure describes all the state of a StarPU worker_set.
  324. * This function sets the pthread key which stores a pointer to this structure.
  325. * */
  326. void _starpu_set_local_worker_set_key(struct _starpu_worker_set *worker_set);
  327. /* Returns the _starpu_worker_set structure that describes the state of the
  328. * current worker_set. */
  329. struct _starpu_worker_set *_starpu_get_local_worker_set_key(void);
  330. /* Returns the _starpu_worker structure that describes the state of the
  331. * specified worker. */
  332. struct _starpu_worker *_starpu_get_worker_struct(unsigned id);
  333. /* Returns the starpu_sched_ctx structure that descriebes the state of the
  334. * specified ctx */
  335. struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id);
  336. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  337. int _starpu_is_initialized(void);
  338. /* Returns the structure that describes the overall machine configuration (eg.
  339. * all workers and topology). */
  340. struct _starpu_machine_config *_starpu_get_machine_config(void);
  341. /* Return whether kernels should be run (<=0) or not (>0) */
  342. int _starpu_get_disable_kernels(void);
  343. /* Retrieve the status which indicates what the worker is currently doing. */
  344. enum _starpu_worker_status _starpu_worker_get_status(int workerid);
  345. /* Change the status of the worker which indicates what the worker is currently
  346. * doing (eg. executing a callback). */
  347. void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status);
  348. /* We keep an initial sched ctx which might be used in case no other ctx is available */
  349. struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void);
  350. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  351. /* returns workers not belonging to any context, be careful no mutex is used,
  352. the list might not be updated */
  353. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  354. /* geet starpu workerids corresponding to the os physical id bindid */
  355. int _starpu_worker_get_workerids(int bindid, int *workerids);
  356. /* if the current worker has the lock release it */
  357. void _starpu_unlock_mutex_if_prev_locked();
  358. /* if we prev released the lock relock it */
  359. void _starpu_relock_mutex_if_prev_locked();
  360. void _starpu_worker_set_flag_sched_mutex_locked(int workerid, unsigned flag);
  361. unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex);
  362. int _starpu_worker_get_nsched_ctxs(int workerid);
  363. #endif // __WORKERS_H__