workers.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016, 2017 CNRS
  5. * Copyright (C) 2011, 2016 INRIA
  6. * Copyright (C) 2016 Uppsala University
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #ifndef __WORKERS_H__
  20. #define __WORKERS_H__
  21. #include <limits.h>
  22. #include <starpu.h>
  23. #include <common/config.h>
  24. #include <common/timing.h>
  25. #include <common/fxt.h>
  26. #include <common/thread.h>
  27. #include <core/jobs.h>
  28. #include <core/perfmodel/perfmodel.h>
  29. #include <core/sched_policy.h>
  30. #include <core/topology.h>
  31. #include <core/errorcheck.h>
  32. #include <core/sched_ctx.h>
  33. #include <core/sched_ctx_list.h>
  34. #ifdef STARPU_HAVE_HWLOC
  35. #include <hwloc.h>
  36. #endif
  37. #include <core/drivers.h>
  38. #include <drivers/cuda/driver_cuda.h>
  39. #include <drivers/opencl/driver_opencl.h>
  40. #ifdef STARPU_USE_MIC
  41. #include <drivers/mic/driver_mic_source.h>
  42. #endif /* STARPU_USE_MIC */
  43. #ifdef STARPU_USE_SCC
  44. #include <drivers/scc/driver_scc_source.h>
  45. #endif
  46. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  47. #include <drivers/mpi/driver_mpi_source.h>
  48. #endif
  49. #include <drivers/cpu/driver_cpu.h>
  50. #include <datawizard/datawizard.h>
  51. #include <starpu_parameters.h>
  52. #define STARPU_MAX_PIPELINE 4
  53. enum initialization { UNINITIALIZED = 0, CHANGING, INITIALIZED };
  54. /* This is initialized from in _starpu_worker_init */
  55. LIST_TYPE(_starpu_worker,
  56. struct _starpu_machine_config *config;
  57. starpu_pthread_mutex_t mutex;
  58. enum starpu_worker_archtype arch; /* what is the type of worker ? */
  59. uint32_t worker_mask; /* what is the type of worker ? */
  60. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  61. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  62. unsigned devid; /* which cpu/gpu/etc is controlled by the worker ? */
  63. unsigned subworkerid; /* which sub-worker this one is for the cpu/gpu */
  64. int bindid; /* which cpu is the driver bound to ? (logical index) */
  65. int workerid; /* uniquely identify the worker among all processing units types */
  66. int combined_workerid; /* combined worker currently using this worker */
  67. int current_rank; /* current rank in case the worker is used in a parallel fashion */
  68. int worker_size; /* size of the worker in case we use a combined worker */
  69. starpu_pthread_cond_t started_cond; /* indicate when the worker is ready */
  70. starpu_pthread_cond_t ready_cond; /* indicate when the worker is ready */
  71. unsigned memory_node; /* which memory node is the worker associated with ? */
  72. /* condition variable used for passive waiting operations on worker
  73. * STARPU_PTHREAD_COND_BROADCAST must be used instead of STARPU_PTHREAD_COND_SIGNAL,
  74. * since the condition is shared for multiple purpose */
  75. starpu_pthread_cond_t sched_cond;
  76. starpu_pthread_mutex_t sched_mutex; /* mutex protecting sched_cond */
  77. int state_safe_for_observation:1; /* mark scheduling sections where other workers can safely access the worker state */
  78. int state_sched_op_pending:1; /* a task pop is ongoing even though sched_mutex may temporarily be unlocked */
  79. int state_changing_ctx_waiting:1; /* a thread is waiting for operations such as pop to complete before acquiring sched_mutex and modifying the worker ctx*/
  80. int state_changing_ctx_notice:1; /* the worker ctx is about to change or being changed, wait for flag to be cleared before starting new scheduling operations */
  81. int state_blocked_in_parallel:1; /* worker is currently blocked on a parallel section */
  82. int state_block_in_parallel_req:1; /* a request for state transition from unblocked to blocked is pending */
  83. int state_block_in_parallel_ack:1; /* a block request has been honored */
  84. int state_unblock_in_parallel_req:1; /* a request for state transition from blocked to unblocked is pending */
  85. int state_unblock_in_parallel_ack:1; /* an unblock request has been honored */
  86. /* cumulative blocking depth
  87. * - =0 worker unblocked
  88. * - >0 worker blocked
  89. * - transition from 0 to 1 triggers a block_req
  90. * - transition from 1 to 0 triggers a unblock_req
  91. */
  92. unsigned block_in_parallel_ref_count;
  93. struct starpu_task_list local_tasks; /* this queue contains tasks that have been explicitely submitted to that queue */
  94. struct starpu_task **local_ordered_tasks; /* this queue contains tasks that have been explicitely submitted to that queue with an explicit order */
  95. unsigned local_ordered_tasks_size; /* this records the size of local_ordered_tasks */
  96. unsigned current_ordered_task; /* this records the index (within local_ordered_tasks) of the next ordered task to be executed */
  97. unsigned current_ordered_task_order; /* this records the order of the next ordered task to be executed */
  98. struct starpu_task *current_task; /* task currently executed by this worker (non-pipelined version) */
  99. struct starpu_task *current_tasks[STARPU_MAX_PIPELINE]; /* tasks currently executed by this worker (pipelined version) */
  100. #ifdef STARPU_SIMGRID
  101. starpu_pthread_wait_t wait;
  102. #endif
  103. unsigned char first_task; /* Index of first task in the pipeline */
  104. unsigned char ntasks; /* number of tasks in the pipeline */
  105. unsigned char pipeline_length; /* number of tasks to be put in the pipeline */
  106. unsigned char pipeline_stuck; /* whether a task prevents us from pipelining */
  107. struct _starpu_worker_set *set; /* in case this worker belongs to a set */
  108. unsigned worker_is_running;
  109. unsigned worker_is_initialized;
  110. enum _starpu_worker_status status; /* what is the worker doing now ? (eg. CALLBACK) */
  111. char name[64];
  112. char short_name[10];
  113. unsigned run_by_starpu; /* Is this run by StarPU or directly by the application ? */
  114. struct _starpu_driver_ops *driver_ops;
  115. struct _starpu_sched_ctx_list *sched_ctx_list;
  116. int tmp_sched_ctx;
  117. unsigned nsched_ctxs; /* the no of contexts a worker belongs to*/
  118. struct _starpu_barrier_counter tasks_barrier; /* wait for the tasks submitted */
  119. unsigned has_prev_init; /* had already been inited in another ctx */
  120. unsigned removed_from_ctx[STARPU_NMAX_SCHED_CTXS+1];
  121. unsigned spinning_backoff ; /* number of cycles to pause when spinning */
  122. unsigned nb_buffers_transferred; /* number of piece of data already send to worker */
  123. unsigned nb_buffers_totransfer; /* number of piece of data already send to worker */
  124. struct starpu_task *task_transferring; /* The buffers of this task are being sent */
  125. /* indicate whether the workers shares tasks lists with other workers*/
  126. /* in this case when removing him from a context it disapears instantly */
  127. unsigned shares_tasks_lists[STARPU_NMAX_SCHED_CTXS+1];
  128. /* boolean to chose the next ctx a worker will pop into */
  129. unsigned poped_in_ctx[STARPU_NMAX_SCHED_CTXS+1];
  130. /* boolean indicating at which moment we checked all ctxs and change phase for the booleab poped_in_ctx*/
  131. /* one for each of the 2 priorities*/
  132. unsigned reverse_phase[2];
  133. /* indicate which priority of ctx is currently active: the values are 0 or 1*/
  134. unsigned pop_ctx_priority;
  135. /* sched mutex local worker locking depth */
  136. unsigned sched_mutex_depth;
  137. /* bool to indicate if the worker is slave in a ctx */
  138. unsigned is_slave_somewhere;
  139. struct _starpu_sched_ctx *stream_ctx;
  140. #ifdef __GLIBC__
  141. cpu_set_t cpu_set;
  142. #endif /* __GLIBC__ */
  143. #ifdef STARPU_HAVE_HWLOC
  144. hwloc_bitmap_t hwloc_cpu_set;
  145. #endif
  146. );
  147. struct _starpu_combined_worker
  148. {
  149. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  150. uint32_t worker_mask; /* what is the type of workers ? */
  151. int worker_size;
  152. unsigned memory_node; /* which memory node is associated that worker to ? */
  153. int combined_workerid[STARPU_NMAXWORKERS];
  154. #ifdef STARPU_USE_MP
  155. int count;
  156. starpu_pthread_mutex_t count_mutex;
  157. #endif
  158. #ifdef __GLIBC__
  159. cpu_set_t cpu_set;
  160. #endif /* __GLIBC__ */
  161. #ifdef STARPU_HAVE_HWLOC
  162. hwloc_bitmap_t hwloc_cpu_set;
  163. #endif
  164. };
  165. /* in case a single CPU worker may control multiple
  166. * accelerators (eg. Gordon for n SPUs) */
  167. struct _starpu_worker_set
  168. {
  169. starpu_pthread_mutex_t mutex;
  170. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  171. unsigned nworkers;
  172. unsigned started; /* Only one thread for the whole set */
  173. void *retval;
  174. struct _starpu_worker *workers;
  175. starpu_pthread_cond_t ready_cond; /* indicate when the set is ready */
  176. unsigned set_is_initialized;
  177. };
  178. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  179. extern struct _starpu_worker_set mpi_worker_set[STARPU_MAXMPIDEVS];
  180. #endif
  181. struct _starpu_machine_topology
  182. {
  183. /* Total number of workers. */
  184. unsigned nworkers;
  185. /* Total number of combined workers. */
  186. unsigned ncombinedworkers;
  187. unsigned nsched_ctxs;
  188. #ifdef STARPU_HAVE_HWLOC
  189. /* Topology as detected by hwloc. */
  190. hwloc_topology_t hwtopology;
  191. #endif
  192. /* custom hwloc tree*/
  193. struct starpu_tree *tree;
  194. /* Total number of CPUs, as detected by the topology code. May
  195. * be different from the actual number of CPU workers.
  196. */
  197. unsigned nhwcpus;
  198. /* Total number of PUs, as detected by the topology code. May
  199. * be different from the actual number of PU workers.
  200. */
  201. unsigned nhwpus;
  202. /* Total number of CUDA devices, as detected. May be different
  203. * from the actual number of CUDA workers.
  204. */
  205. unsigned nhwcudagpus;
  206. /* Total number of OpenCL devices, as detected. May be
  207. * different from the actual number of OpenCL workers.
  208. */
  209. unsigned nhwopenclgpus;
  210. /* Total number of SCC cores, as detected. May be different
  211. * from the actual number of core workers.
  212. */
  213. unsigned nhwscc;
  214. /* Total number of MPI nodes, as detected. May be different
  215. * from the actual number of node workers.
  216. */
  217. unsigned nhwmpi;
  218. /* Actual number of CPU workers used by StarPU. */
  219. unsigned ncpus;
  220. /* Actual number of CUDA GPUs used by StarPU. */
  221. unsigned ncudagpus;
  222. unsigned nworkerpercuda;
  223. int cuda_th_per_stream;
  224. int cuda_th_per_dev;
  225. /* Actual number of OpenCL workers used by StarPU. */
  226. unsigned nopenclgpus;
  227. /* Actual number of SCC workers used by StarPU. */
  228. unsigned nsccdevices;
  229. /* Actual number of MPI workers used by StarPU. */
  230. unsigned nmpidevices;
  231. unsigned nhwmpidevices;
  232. unsigned nhwmpicores[STARPU_MAXMPIDEVS]; // Each MPI node has its set of cores.
  233. unsigned nmpicores[STARPU_MAXMPIDEVS];
  234. /* Topology of MP nodes (mainly MIC and SCC) as well as necessary
  235. * objects to communicate with them. */
  236. unsigned nhwmicdevices;
  237. unsigned nmicdevices;
  238. unsigned nhwmiccores[STARPU_MAXMICDEVS]; // Each MIC node has its set of cores.
  239. unsigned nmiccores[STARPU_MAXMICDEVS];
  240. /* Indicates the successive logical PU identifier that should be used
  241. * to bind the workers. It is either filled according to the
  242. * user's explicit parameters (from starpu_conf) or according
  243. * to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  244. * round-robin policy is used to distributed the workers over
  245. * the cores.
  246. */
  247. unsigned workers_bindid[STARPU_NMAXWORKERS];
  248. /* Indicates the successive CUDA identifier that should be
  249. * used by the CUDA driver. It is either filled according to
  250. * the user's explicit parameters (from starpu_conf) or
  251. * according to the STARPU_WORKERS_CUDAID env. variable.
  252. * Otherwise, they are taken in ID order.
  253. */
  254. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  255. /* Indicates the successive OpenCL identifier that should be
  256. * used by the OpenCL driver. It is either filled according
  257. * to the user's explicit parameters (from starpu_conf) or
  258. * according to the STARPU_WORKERS_OPENCLID env. variable.
  259. * Otherwise, they are taken in ID order.
  260. */
  261. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  262. /** Indicates the successive MIC devices that should be used
  263. * by the MIC driver. It is either filled according to the
  264. * user's explicit parameters (from starpu_conf) or according
  265. * to the STARPU_WORKERS_MICID env. variable. Otherwise, they
  266. * are taken in ID order. */
  267. /* TODO */
  268. /* unsigned workers_mic_deviceid[STARPU_NMAXWORKERS]; */
  269. /* Which SCC(s) do we use ? */
  270. /* Indicates the successive SCC devices that should be used by
  271. * the SCC driver. It is either filled according to the
  272. * user's explicit parameters (from starpu_conf) or according
  273. * to the STARPU_WORKERS_SCCID env. variable. Otherwise, they
  274. * are taken in ID order.
  275. */
  276. unsigned workers_scc_deviceid[STARPU_NMAXWORKERS];
  277. unsigned workers_mpi_ms_deviceid[STARPU_NMAXWORKERS];
  278. };
  279. struct _starpu_machine_config
  280. {
  281. struct _starpu_machine_topology topology;
  282. #ifdef STARPU_HAVE_HWLOC
  283. int cpu_depth;
  284. int pu_depth;
  285. #endif
  286. /* Where to bind workers ? */
  287. int current_bindid;
  288. /* Which GPU(s) do we use for CUDA ? */
  289. int current_cuda_gpuid;
  290. /* Which GPU(s) do we use for OpenCL ? */
  291. int current_opencl_gpuid;
  292. /* Which MIC do we use? */
  293. int current_mic_deviceid;
  294. /* Which SCC do we use? */
  295. int current_scc_deviceid;
  296. /* Which MPI do we use? */
  297. int current_mpi_deviceid;
  298. /* Memory node for cpus, if only one */
  299. int cpus_nodeid;
  300. /* Memory node for CUDA, if only one */
  301. int cuda_nodeid;
  302. /* Memory node for OpenCL, if only one */
  303. int opencl_nodeid;
  304. /* Memory node for MIC, if only one */
  305. int mic_nodeid;
  306. /* Memory node for SCC, if only one */
  307. int scc_nodeid;
  308. /* Memory node for MPI, if only one */
  309. int mpi_nodeid;
  310. /* Basic workers : each of this worker is running its own driver and
  311. * can be combined with other basic workers. */
  312. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  313. /* Combined workers: these worker are a combination of basic workers
  314. * that can run parallel tasks together. */
  315. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  316. /* Translation table from bindid to worker IDs */
  317. struct {
  318. int *workerids;
  319. unsigned nworkers; /* size of workerids */
  320. } *bindid_workers;
  321. unsigned nbindid; /* size of bindid_workers */
  322. /* This bitmask indicates which kinds of worker are available. For
  323. * instance it is possible to test if there is a CUDA worker with
  324. * the result of (worker_mask & STARPU_CUDA). */
  325. uint32_t worker_mask;
  326. /* either the user given configuration passed to starpu_init or a default configuration */
  327. struct starpu_conf conf;
  328. /* this flag is set until the runtime is stopped */
  329. unsigned running;
  330. int disable_kernels;
  331. /* Number of calls to starpu_pause() - calls to starpu_resume(). When >0,
  332. * StarPU should pause. */
  333. int pause_depth;
  334. /* all the sched ctx of the current instance of starpu */
  335. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS+1];
  336. /* this flag is set until the application is finished submitting tasks */
  337. unsigned submitting;
  338. int watchdog_ok;
  339. starpu_pthread_mutex_t submitted_mutex;
  340. };
  341. extern struct _starpu_machine_config _starpu_config STARPU_ATTRIBUTE_INTERNAL;
  342. extern int _starpu_keys_initialized STARPU_ATTRIBUTE_INTERNAL;
  343. extern starpu_pthread_key_t _starpu_worker_key STARPU_ATTRIBUTE_INTERNAL;
  344. extern starpu_pthread_key_t _starpu_worker_set_key STARPU_ATTRIBUTE_INTERNAL;
  345. /* Three functions to manage argv, argc */
  346. void _starpu_set_argc_argv(int *argc, char ***argv);
  347. int *_starpu_get_argc();
  348. char ***_starpu_get_argv();
  349. /* Fill conf with environment variables */
  350. void _starpu_conf_check_environment(struct starpu_conf *conf);
  351. /* Called by the driver when it is ready to pause */
  352. void _starpu_may_pause(void);
  353. /* Has starpu_shutdown already been called ? */
  354. static inline unsigned _starpu_machine_is_running(void)
  355. {
  356. unsigned ret;
  357. /* running is just protected by a memory barrier */
  358. STARPU_RMB();
  359. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  360. ret = _starpu_config.running;
  361. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  362. return ret;
  363. }
  364. /* Check if there is a worker that may execute the task. */
  365. uint32_t _starpu_worker_exists(struct starpu_task *);
  366. /* Is there a worker that can execute CUDA code ? */
  367. uint32_t _starpu_can_submit_cuda_task(void);
  368. /* Is there a worker that can execute CPU code ? */
  369. uint32_t _starpu_can_submit_cpu_task(void);
  370. /* Is there a worker that can execute OpenCL code ? */
  371. uint32_t _starpu_can_submit_opencl_task(void);
  372. /* Is there a worker that can execute OpenCL code ? */
  373. uint32_t _starpu_can_submit_scc_task(void);
  374. /* Check whether there is anything that the worker should do instead of
  375. * sleeping (waiting on something to happen). */
  376. unsigned _starpu_worker_can_block(unsigned memnode, struct _starpu_worker *worker);
  377. /* This function must be called to block a worker. It puts the worker in a
  378. * sleeping state until there is some event that forces the worker to wake up.
  379. * */
  380. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  381. /* This function initializes the current driver for the given worker */
  382. void _starpu_driver_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  383. /* This function initializes the current thread for the given worker */
  384. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  385. static inline unsigned _starpu_worker_get_count(void)
  386. {
  387. return _starpu_config.topology.nworkers;
  388. }
  389. #define starpu_worker_get_count _starpu_worker_get_count
  390. /* The _starpu_worker structure describes all the state of a StarPU worker.
  391. * This function sets the pthread key which stores a pointer to this structure.
  392. * */
  393. static inline void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  394. {
  395. STARPU_ASSERT(_starpu_keys_initialized);
  396. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_key, worker);
  397. }
  398. /* Returns the _starpu_worker structure that describes the state of the
  399. * current worker. */
  400. static inline struct _starpu_worker *_starpu_get_local_worker_key(void)
  401. {
  402. if (!_starpu_keys_initialized)
  403. return NULL;
  404. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_key);
  405. }
  406. /* The _starpu_worker_set structure describes all the state of a StarPU worker_set.
  407. * This function sets the pthread key which stores a pointer to this structure.
  408. * */
  409. static inline void _starpu_set_local_worker_set_key(struct _starpu_worker_set *worker)
  410. {
  411. STARPU_ASSERT(_starpu_keys_initialized);
  412. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_set_key, worker);
  413. }
  414. /* Returns the _starpu_worker_set structure that describes the state of the
  415. * current worker_set. */
  416. static inline struct _starpu_worker_set *_starpu_get_local_worker_set_key(void)
  417. {
  418. if (!_starpu_keys_initialized)
  419. return NULL;
  420. return (struct _starpu_worker_set *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_set_key);
  421. }
  422. /* Returns the _starpu_worker structure that describes the state of the
  423. * specified worker. */
  424. static inline struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  425. {
  426. STARPU_ASSERT(id < starpu_worker_get_count());
  427. return &_starpu_config.workers[id];
  428. }
  429. /* Returns the starpu_sched_ctx structure that describes the state of the
  430. * specified ctx */
  431. static inline struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  432. {
  433. if(id == STARPU_NMAX_SCHED_CTXS) return NULL;
  434. return &_starpu_config.sched_ctxs[id];
  435. }
  436. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  437. int _starpu_is_initialized(void);
  438. /* Returns the structure that describes the overall machine configuration (eg.
  439. * all workers and topology). */
  440. static inline struct _starpu_machine_config *_starpu_get_machine_config(void)
  441. {
  442. return &_starpu_config;
  443. }
  444. /* Return whether kernels should be run (<=0) or not (>0) */
  445. static inline int _starpu_get_disable_kernels(void)
  446. {
  447. return _starpu_config.disable_kernels;
  448. }
  449. /* Retrieve the status which indicates what the worker is currently doing. */
  450. static inline enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  451. {
  452. return _starpu_config.workers[workerid].status;
  453. }
  454. /* Change the status of the worker which indicates what the worker is currently
  455. * doing (eg. executing a callback). */
  456. static inline void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  457. {
  458. _starpu_config.workers[workerid].status = status;
  459. }
  460. /* We keep an initial sched ctx which might be used in case no other ctx is available */
  461. static inline struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  462. {
  463. return &_starpu_config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  464. }
  465. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  466. /* returns workers not belonging to any context, be careful no mutex is used,
  467. the list might not be updated */
  468. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  469. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex)
  470. {
  471. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  472. return &w->sched_mutex == mutex;
  473. }
  474. static inline int _starpu_worker_get_nsched_ctxs(int workerid)
  475. {
  476. return _starpu_config.workers[workerid].nsched_ctxs;
  477. }
  478. /* Get the total number of sched_ctxs created till now */
  479. static inline unsigned _starpu_get_nsched_ctxs(void)
  480. {
  481. return _starpu_config.topology.nsched_ctxs;
  482. }
  483. /* Inlined version when building the core. */
  484. static inline int _starpu_worker_get_id(void)
  485. {
  486. struct _starpu_worker * worker;
  487. worker = _starpu_get_local_worker_key();
  488. if (worker)
  489. {
  490. return worker->workerid;
  491. }
  492. else
  493. {
  494. /* there is no worker associated to that thread, perhaps it is
  495. * a thread from the application or this is some SPU worker */
  496. return -1;
  497. }
  498. }
  499. #define starpu_worker_get_id _starpu_worker_get_id
  500. /* Similar behaviour to starpu_worker_get_id() but fails when called from outside a worker */
  501. /* This returns an unsigned object on purpose, so that the caller is sure to get a positive value */
  502. static inline unsigned __starpu_worker_get_id_check(const char *f, int l)
  503. {
  504. (void) l;
  505. (void) f;
  506. int id = starpu_worker_get_id();
  507. STARPU_ASSERT_MSG(id>=0, "%s:%d Cannot be called from outside a worker\n", f, l);
  508. return id;
  509. }
  510. #define _starpu_worker_get_id_check(f,l) __starpu_worker_get_id_check(f,l)
  511. void _starpu_worker_set_stream_ctx(unsigned workerid, struct _starpu_sched_ctx *sched_ctx);
  512. struct _starpu_sched_ctx* _starpu_worker_get_ctx_stream(unsigned stream_workerid);
  513. /* Must be called with worker's sched_mutex held.
  514. */
  515. static inline void _starpu_worker_request_blocking_in_parallel(struct _starpu_worker * const worker)
  516. {
  517. /* flush pending requests to start on a fresh transaction epoch */
  518. while (worker->state_unblock_in_parallel_req)
  519. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  520. /* announce blocking intent */
  521. STARPU_ASSERT(worker->block_in_parallel_ref_count < UINT_MAX);
  522. worker->block_in_parallel_ref_count++;
  523. if (worker->block_in_parallel_ref_count == 1)
  524. {
  525. /* only the transition from 0 to 1 triggers the block_in_parallel_req */
  526. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  527. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  528. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  529. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  530. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  531. /* trigger the block_in_parallel_req */
  532. worker->state_block_in_parallel_req = 1;
  533. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  534. /* wait for block_in_parallel_req to be processed */
  535. while (!worker->state_block_in_parallel_ack)
  536. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  537. STARPU_ASSERT(worker->block_in_parallel_ref_count >= 1);
  538. STARPU_ASSERT(worker->state_block_in_parallel_req);
  539. STARPU_ASSERT(worker->state_blocked_in_parallel);
  540. /* reset block_in_parallel_req state flags */
  541. worker->state_block_in_parallel_req = 0;
  542. worker->state_block_in_parallel_ack = 0;
  543. /* broadcast block_in_parallel_req state flags reset */
  544. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  545. }
  546. }
  547. /* Must be called with worker's sched_mutex held.
  548. */
  549. static inline void _starpu_worker_request_unblocking_in_parallel(struct _starpu_worker * const worker)
  550. {
  551. /* flush pending requests to start on a fresh transaction epoch */
  552. while (worker->state_block_in_parallel_req)
  553. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  554. /* unblocking may be requested unconditionnally
  555. * thus, check is unblocking is really needed */
  556. if (worker->state_blocked_in_parallel)
  557. {
  558. if (worker->block_in_parallel_ref_count == 1)
  559. {
  560. /* only the transition from 1 to 0 triggers the unblock_in_parallel_req */
  561. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  562. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  563. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  564. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  565. /* trigger the unblock_in_parallel_req */
  566. worker->state_unblock_in_parallel_req = 1;
  567. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  568. /* wait for the unblock_in_parallel_req to be processed */
  569. while (!worker->state_unblock_in_parallel_ack)
  570. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  571. STARPU_ASSERT(worker->state_unblock_in_parallel_req);
  572. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  573. /* reset unblock_in_parallel_req state flags */
  574. worker->state_unblock_in_parallel_req = 0;
  575. worker->state_unblock_in_parallel_ack = 0;
  576. /* broadcast unblock_in_parallel_req state flags reset */
  577. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  578. }
  579. /* announce unblocking complete */
  580. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  581. worker->block_in_parallel_ref_count--;
  582. }
  583. }
  584. /* Must be called with worker's sched_mutex held.
  585. */
  586. static inline void _starpu_worker_process_block_in_parallel_requests(struct _starpu_worker * const worker)
  587. {
  588. while (worker->state_block_in_parallel_req)
  589. {
  590. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  591. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  592. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  593. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  594. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  595. /* enter effective blocked state */
  596. worker->state_blocked_in_parallel = 1;
  597. /* notify block_in_parallel_req processing */
  598. worker->state_block_in_parallel_ack = 1;
  599. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  600. /* block */
  601. while (!worker->state_unblock_in_parallel_req)
  602. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  603. STARPU_ASSERT(worker->state_blocked_in_parallel);
  604. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  605. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  606. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  607. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  608. /* leave effective blocked state */
  609. worker->state_blocked_in_parallel = 0;
  610. /* notify unblock_in_parallel_req processing */
  611. worker->state_unblock_in_parallel_ack = 1;
  612. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  613. }
  614. }
  615. /* Must be called with worker's sched_mutex held.
  616. * Mark the beginning of a scheduling operation during which the sched_mutex
  617. * lock may be temporarily released, but the scheduling context of the worker
  618. * should not be modified */
  619. static inline void _starpu_worker_enter_sched_op(struct _starpu_worker * const worker)
  620. {
  621. /* process pending block requests before entering a sched_op region */
  622. _starpu_worker_process_block_in_parallel_requests(worker);
  623. while (worker->state_changing_ctx_notice)
  624. {
  625. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  626. /* new block requests may have been triggered during the wait,
  627. * need to check again */
  628. _starpu_worker_process_block_in_parallel_requests(worker);
  629. }
  630. /* no block request and no ctx change ahead,
  631. * enter sched_op */
  632. worker->state_safe_for_observation = 0;
  633. worker->state_sched_op_pending = 1;
  634. }
  635. /* Must be called with worker's sched_mutex held.
  636. * Mark the end of a scheduling operation, and notify potential waiters that
  637. * scheduling context changes can safely be performed again.
  638. */
  639. static inline void _starpu_worker_leave_sched_op(struct _starpu_worker * const worker)
  640. {
  641. worker->state_safe_for_observation = 1;
  642. worker->state_sched_op_pending = 0;
  643. if (worker->state_changing_ctx_waiting)
  644. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  645. }
  646. /* Must be called with worker's sched_mutex held.
  647. */
  648. static inline void _starpu_worker_enter_changing_ctx_op(struct _starpu_worker * const worker)
  649. {
  650. /* flush pending requests to start on a fresh transaction epoch */
  651. while (worker->state_changing_ctx_notice)
  652. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  653. /* announce changing_ctx intent
  654. *
  655. * - an already started sched_op is allowed to complete
  656. * - no new sched_op may be started
  657. */
  658. worker->state_changing_ctx_notice = 1;
  659. /* allow for an already started sched_op to complete */
  660. if (worker->state_sched_op_pending)
  661. {
  662. /* request sched_op to broadcast when way is cleared */
  663. worker->state_changing_ctx_waiting = 1;
  664. /* wait for sched_op completion */
  665. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  666. do
  667. {
  668. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  669. }
  670. while (worker->state_sched_op_pending);
  671. /* reset flag so other sched_ops wont have to broadcast state */
  672. worker->state_changing_ctx_waiting = 0;
  673. }
  674. }
  675. /* Must be called with worker's sched_mutex held.
  676. */
  677. static inline void _starpu_worker_leave_changing_ctx_op(struct _starpu_worker * const worker)
  678. {
  679. worker->state_changing_ctx_notice = 0;
  680. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  681. }
  682. /* lock a worker for observing contents
  683. *
  684. * notes:
  685. * - if the observed worker is not in state_safe_for_observation, the function block until the state is reached */
  686. static inline void _starpu_worker_lock_for_observation(int workerid)
  687. {
  688. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  689. STARPU_ASSERT(worker != NULL);
  690. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  691. if (workerid != starpu_worker_get_id())
  692. {
  693. while (!worker->state_safe_for_observation)
  694. {
  695. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  696. }
  697. }
  698. }
  699. static inline void _starpu_worker_unlock_for_observation(int workerid)
  700. {
  701. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  702. STARPU_ASSERT(worker != NULL);
  703. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  704. }
  705. /* Temporarily allow other worker to access current worker state, when still scheduling,
  706. * but the scheduling has not yet been made or is already done */
  707. static inline void _starpu_worker_enter_section_safe_for_observation(void)
  708. {
  709. int workerid = starpu_worker_get_id();
  710. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  711. STARPU_ASSERT(worker != NULL);
  712. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  713. STARPU_ASSERT(!worker->state_safe_for_observation);
  714. worker->state_safe_for_observation = 1;
  715. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  716. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  717. }
  718. static inline void _starpu_worker_leave_section_safe_for_observation(void)
  719. {
  720. int workerid = starpu_worker_get_id();
  721. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  722. STARPU_ASSERT(worker != NULL);
  723. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  724. STARPU_ASSERT(worker->state_safe_for_observation);
  725. worker->state_safe_for_observation = 0;
  726. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  727. }
  728. #endif // __WORKERS_H__