workers.h 15 KB

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