workers.h 33 KB

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