workers.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <starpu.h>
  22. #include <common/config.h>
  23. #include <common/timing.h>
  24. #include <common/fxt.h>
  25. #include <common/thread.h>
  26. #include <core/jobs.h>
  27. #include <core/perfmodel/perfmodel.h>
  28. #include <core/sched_policy.h>
  29. #include <core/topology.h>
  30. #include <core/errorcheck.h>
  31. #include <core/sched_ctx.h>
  32. #include <core/sched_ctx_list.h>
  33. #ifdef STARPU_HAVE_HWLOC
  34. #include <hwloc.h>
  35. #endif
  36. #include <core/drivers.h>
  37. #include <drivers/cuda/driver_cuda.h>
  38. #include <drivers/opencl/driver_opencl.h>
  39. #ifdef STARPU_USE_MIC
  40. #include <drivers/mic/driver_mic_source.h>
  41. #endif /* STARPU_USE_MIC */
  42. #ifdef STARPU_USE_SCC
  43. #include <drivers/scc/driver_scc_source.h>
  44. #endif
  45. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  46. #include <drivers/mpi/driver_mpi_source.h>
  47. #endif
  48. #include <drivers/cpu/driver_cpu.h>
  49. #include <datawizard/datawizard.h>
  50. #include <starpu_parameters.h>
  51. #define STARPU_MAX_PIPELINE 4
  52. enum initialization { UNINITIALIZED = 0, CHANGING, INITIALIZED };
  53. /* This is initialized from in _starpu_worker_init */
  54. LIST_TYPE(_starpu_worker,
  55. struct _starpu_machine_config *config;
  56. starpu_pthread_mutex_t mutex;
  57. enum starpu_worker_archtype arch; /* what is the type of worker ? */
  58. uint32_t worker_mask; /* what is the type of worker ? */
  59. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  60. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  61. unsigned devid; /* which cpu/gpu/etc is controlled by the worker ? */
  62. unsigned subworkerid; /* which sub-worker this one is for the cpu/gpu */
  63. int bindid; /* which cpu is the driver bound to ? (logical index) */
  64. int workerid; /* uniquely identify the worker among all processing units types */
  65. int combined_workerid; /* combined worker currently using this worker */
  66. int current_rank; /* current rank in case the worker is used in a parallel fashion */
  67. int worker_size; /* size of the worker in case we use a combined worker */
  68. starpu_pthread_cond_t started_cond; /* indicate when the worker is ready */
  69. starpu_pthread_cond_t ready_cond; /* indicate when the worker is ready */
  70. unsigned memory_node; /* which memory node is the worker associated with ? */
  71. starpu_pthread_cond_t sched_cond; /* condition variable used when the worker waits for tasks. */
  72. starpu_pthread_mutex_t sched_mutex; /* mutex protecting sched_cond */
  73. int state_sched_op_pending:1; /* a task pop is ongoing even though sched_mutex may temporarily be unlocked */
  74. int state_changing_ctx_waiting:1; /* a thread is waiting for transient operations such as pop to complete before acquiring sched_mutex and modifying the worker ctx*/
  75. struct starpu_task_list local_tasks; /* this queue contains tasks that have been explicitely submitted to that queue */
  76. struct starpu_task **local_ordered_tasks; /* this queue contains tasks that have been explicitely submitted to that queue with an explicit order */
  77. unsigned local_ordered_tasks_size; /* this records the size of local_ordered_tasks */
  78. unsigned current_ordered_task; /* this records the index (within local_ordered_tasks) of the next ordered task to be executed */
  79. unsigned current_ordered_task_order; /* this records the order of the next ordered task to be executed */
  80. struct starpu_task *current_task; /* task currently executed by this worker (non-pipelined version) */
  81. struct starpu_task *current_tasks[STARPU_MAX_PIPELINE]; /* tasks currently executed by this worker (pipelined version) */
  82. #ifdef STARPU_SIMGRID
  83. starpu_pthread_wait_t wait;
  84. #endif
  85. unsigned char first_task; /* Index of first task in the pipeline */
  86. unsigned char ntasks; /* number of tasks in the pipeline */
  87. unsigned char pipeline_length; /* number of tasks to be put in the pipeline */
  88. unsigned char pipeline_stuck; /* whether a task prevents us from pipelining */
  89. struct _starpu_worker_set *set; /* in case this worker belongs to a set */
  90. unsigned worker_is_running;
  91. unsigned worker_is_initialized;
  92. enum _starpu_worker_status status; /* what is the worker doing now ? (eg. CALLBACK) */
  93. char name[64];
  94. char short_name[10];
  95. unsigned run_by_starpu; /* Is this run by StarPU or directly by the application ? */
  96. struct _starpu_driver_ops *driver_ops;
  97. struct _starpu_sched_ctx_list *sched_ctx_list;
  98. int tmp_sched_ctx;
  99. unsigned nsched_ctxs; /* the no of contexts a worker belongs to*/
  100. struct _starpu_barrier_counter tasks_barrier; /* wait for the tasks submitted */
  101. unsigned has_prev_init; /* had already been inited in another ctx */
  102. unsigned removed_from_ctx[STARPU_NMAX_SCHED_CTXS+1];
  103. unsigned spinning_backoff ; /* number of cycles to pause when spinning */
  104. unsigned nb_buffers_transferred; /* number of piece of data already send to worker */
  105. unsigned nb_buffers_totransfer; /* number of piece of data already send to worker */
  106. struct starpu_task *task_transferring; /* The buffers of this task are being sent */
  107. /* indicate whether the workers shares tasks lists with other workers*/
  108. /* in this case when removing him from a context it disapears instantly */
  109. unsigned shares_tasks_lists[STARPU_NMAX_SCHED_CTXS+1];
  110. /* boolean to chose the next ctx a worker will pop into */
  111. unsigned poped_in_ctx[STARPU_NMAX_SCHED_CTXS+1];
  112. /* boolean indicating at which moment we checked all ctxs and change phase for the booleab poped_in_ctx*/
  113. /* one for each of the 2 priorities*/
  114. unsigned reverse_phase[2];
  115. /* indicate which priority of ctx is currently active: the values are 0 or 1*/
  116. unsigned pop_ctx_priority;
  117. /* sched mutex local worker locking depth */
  118. unsigned sched_mutex_depth;
  119. /* bool to indicate if the worker is blocked in a ctx */
  120. unsigned blocked;
  121. /* bool to indicate if the worker is slave in a ctx */
  122. unsigned is_slave_somewhere;
  123. struct _starpu_sched_ctx *stream_ctx;
  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. struct _starpu_combined_worker
  132. {
  133. struct starpu_perfmodel_arch perf_arch; /* in case there are different models of the same arch */
  134. uint32_t worker_mask; /* what is the type of workers ? */
  135. int worker_size;
  136. unsigned memory_node; /* which memory node is associated that worker to ? */
  137. int combined_workerid[STARPU_NMAXWORKERS];
  138. #ifdef STARPU_USE_MP
  139. int count;
  140. starpu_pthread_mutex_t count_mutex;
  141. #endif
  142. #ifdef __GLIBC__
  143. cpu_set_t cpu_set;
  144. #endif /* __GLIBC__ */
  145. #ifdef STARPU_HAVE_HWLOC
  146. hwloc_bitmap_t hwloc_cpu_set;
  147. #endif
  148. };
  149. /* in case a single CPU worker may control multiple
  150. * accelerators (eg. Gordon for n SPUs) */
  151. struct _starpu_worker_set
  152. {
  153. starpu_pthread_mutex_t mutex;
  154. starpu_pthread_t worker_thread; /* the thread which runs the worker */
  155. unsigned nworkers;
  156. unsigned started; /* Only one thread for the whole set */
  157. void *retval;
  158. struct _starpu_worker *workers;
  159. starpu_pthread_cond_t ready_cond; /* indicate when the set is ready */
  160. unsigned set_is_initialized;
  161. };
  162. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  163. extern struct _starpu_worker_set mpi_worker_set[STARPU_MAXMPIDEVS];
  164. #endif
  165. struct _starpu_machine_topology
  166. {
  167. /* Total number of workers. */
  168. unsigned nworkers;
  169. /* Total number of combined workers. */
  170. unsigned ncombinedworkers;
  171. unsigned nsched_ctxs;
  172. #ifdef STARPU_HAVE_HWLOC
  173. /* Topology as detected by hwloc. */
  174. hwloc_topology_t hwtopology;
  175. #endif
  176. /* custom hwloc tree*/
  177. struct starpu_tree *tree;
  178. /* Total number of CPUs, as detected by the topology code. May
  179. * be different from the actual number of CPU workers.
  180. */
  181. unsigned nhwcpus;
  182. /* Total number of PUs, as detected by the topology code. May
  183. * be different from the actual number of PU workers.
  184. */
  185. unsigned nhwpus;
  186. /* Total number of CUDA devices, as detected. May be different
  187. * from the actual number of CUDA workers.
  188. */
  189. unsigned nhwcudagpus;
  190. /* Total number of OpenCL devices, as detected. May be
  191. * different from the actual number of OpenCL workers.
  192. */
  193. unsigned nhwopenclgpus;
  194. /* Total number of SCC cores, as detected. May be different
  195. * from the actual number of core workers.
  196. */
  197. unsigned nhwscc;
  198. /* Total number of MPI nodes, as detected. May be different
  199. * from the actual number of node workers.
  200. */
  201. unsigned nhwmpi;
  202. /* Actual number of CPU workers used by StarPU. */
  203. unsigned ncpus;
  204. /* Actual number of CUDA GPUs used by StarPU. */
  205. unsigned ncudagpus;
  206. unsigned nworkerpercuda;
  207. int cuda_th_per_stream;
  208. int cuda_th_per_dev;
  209. /* Actual number of OpenCL workers used by StarPU. */
  210. unsigned nopenclgpus;
  211. /* Actual number of SCC workers used by StarPU. */
  212. unsigned nsccdevices;
  213. /* Actual number of MPI workers used by StarPU. */
  214. unsigned nmpidevices;
  215. unsigned nhwmpidevices;
  216. unsigned nhwmpicores[STARPU_MAXMPIDEVS]; // Each MPI node has its set of cores.
  217. unsigned nmpicores[STARPU_MAXMPIDEVS];
  218. /* Topology of MP nodes (mainly MIC and SCC) as well as necessary
  219. * objects to communicate with them. */
  220. unsigned nhwmicdevices;
  221. unsigned nmicdevices;
  222. unsigned nhwmiccores[STARPU_MAXMICDEVS]; // Each MIC node has its set of cores.
  223. unsigned nmiccores[STARPU_MAXMICDEVS];
  224. /* Indicates the successive logical PU identifier that should be used
  225. * to bind the workers. It is either filled according to the
  226. * user's explicit parameters (from starpu_conf) or according
  227. * to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  228. * round-robin policy is used to distributed the workers over
  229. * the cores.
  230. */
  231. unsigned workers_bindid[STARPU_NMAXWORKERS];
  232. /* Indicates the successive CUDA identifier that should be
  233. * used by the CUDA driver. It is either filled according to
  234. * the user's explicit parameters (from starpu_conf) or
  235. * according to the STARPU_WORKERS_CUDAID env. variable.
  236. * Otherwise, they are taken in ID order.
  237. */
  238. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  239. /* Indicates the successive OpenCL identifier that should be
  240. * used by the OpenCL driver. It is either filled according
  241. * to the user's explicit parameters (from starpu_conf) or
  242. * according to the STARPU_WORKERS_OPENCLID env. variable.
  243. * Otherwise, they are taken in ID order.
  244. */
  245. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  246. /** Indicates the successive MIC devices that should be used
  247. * by the MIC driver. It is either filled according to the
  248. * user's explicit parameters (from starpu_conf) or according
  249. * to the STARPU_WORKERS_MICID env. variable. Otherwise, they
  250. * are taken in ID order. */
  251. /* TODO */
  252. /* unsigned workers_mic_deviceid[STARPU_NMAXWORKERS]; */
  253. /* Which SCC(s) do we use ? */
  254. /* Indicates the successive SCC devices that should be used by
  255. * the SCC driver. It is either filled according to the
  256. * user's explicit parameters (from starpu_conf) or according
  257. * to the STARPU_WORKERS_SCCID env. variable. Otherwise, they
  258. * are taken in ID order.
  259. */
  260. unsigned workers_scc_deviceid[STARPU_NMAXWORKERS];
  261. unsigned workers_mpi_ms_deviceid[STARPU_NMAXWORKERS];
  262. };
  263. struct _starpu_machine_config
  264. {
  265. struct _starpu_machine_topology topology;
  266. #ifdef STARPU_HAVE_HWLOC
  267. int cpu_depth;
  268. int pu_depth;
  269. #endif
  270. /* Where to bind workers ? */
  271. int current_bindid;
  272. /* Which GPU(s) do we use for CUDA ? */
  273. int current_cuda_gpuid;
  274. /* Which GPU(s) do we use for OpenCL ? */
  275. int current_opencl_gpuid;
  276. /* Which MIC do we use? */
  277. int current_mic_deviceid;
  278. /* Which SCC do we use? */
  279. int current_scc_deviceid;
  280. /* Which MPI do we use? */
  281. int current_mpi_deviceid;
  282. /* Memory node for cpus, if only one */
  283. int cpus_nodeid;
  284. /* Memory node for CUDA, if only one */
  285. int cuda_nodeid;
  286. /* Memory node for OpenCL, if only one */
  287. int opencl_nodeid;
  288. /* Memory node for MIC, if only one */
  289. int mic_nodeid;
  290. /* Memory node for SCC, if only one */
  291. int scc_nodeid;
  292. /* Memory node for MPI, if only one */
  293. int mpi_nodeid;
  294. /* Basic workers : each of this worker is running its own driver and
  295. * can be combined with other basic workers. */
  296. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  297. /* Combined workers: these worker are a combination of basic workers
  298. * that can run parallel tasks together. */
  299. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  300. /* Translation table from bindid to worker IDs */
  301. struct {
  302. int *workerids;
  303. unsigned nworkers; /* size of workerids */
  304. } *bindid_workers;
  305. unsigned nbindid; /* size of bindid_workers */
  306. /* This bitmask indicates which kinds of worker are available. For
  307. * instance it is possible to test if there is a CUDA worker with
  308. * the result of (worker_mask & STARPU_CUDA). */
  309. uint32_t worker_mask;
  310. /* either the user given configuration passed to starpu_init or a default configuration */
  311. struct starpu_conf conf;
  312. /* this flag is set until the runtime is stopped */
  313. unsigned running;
  314. int disable_kernels;
  315. /* Number of calls to starpu_pause() - calls to starpu_resume(). When >0,
  316. * StarPU should pause. */
  317. int pause_depth;
  318. /* all the sched ctx of the current instance of starpu */
  319. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS+1];
  320. /* this flag is set until the application is finished submitting tasks */
  321. unsigned submitting;
  322. int watchdog_ok;
  323. starpu_pthread_mutex_t submitted_mutex;
  324. };
  325. extern struct _starpu_machine_config _starpu_config STARPU_ATTRIBUTE_INTERNAL;
  326. extern int _starpu_keys_initialized STARPU_ATTRIBUTE_INTERNAL;
  327. extern starpu_pthread_key_t _starpu_worker_key STARPU_ATTRIBUTE_INTERNAL;
  328. extern starpu_pthread_key_t _starpu_worker_set_key STARPU_ATTRIBUTE_INTERNAL;
  329. /* Three functions to manage argv, argc */
  330. void _starpu_set_argc_argv(int *argc, char ***argv);
  331. int *_starpu_get_argc();
  332. char ***_starpu_get_argv();
  333. /* Fill conf with environment variables */
  334. void _starpu_conf_check_environment(struct starpu_conf *conf);
  335. /* Called by the driver when it is ready to pause */
  336. void _starpu_may_pause(void);
  337. /* Has starpu_shutdown already been called ? */
  338. static inline unsigned _starpu_machine_is_running(void)
  339. {
  340. unsigned ret;
  341. /* running is just protected by a memory barrier */
  342. STARPU_RMB();
  343. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  344. ret = _starpu_config.running;
  345. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  346. return ret;
  347. }
  348. /* Check if there is a worker that may execute the task. */
  349. uint32_t _starpu_worker_exists(struct starpu_task *);
  350. /* Is there a worker that can execute CUDA code ? */
  351. uint32_t _starpu_can_submit_cuda_task(void);
  352. /* Is there a worker that can execute CPU code ? */
  353. uint32_t _starpu_can_submit_cpu_task(void);
  354. /* Is there a worker that can execute OpenCL code ? */
  355. uint32_t _starpu_can_submit_opencl_task(void);
  356. /* Is there a worker that can execute OpenCL code ? */
  357. uint32_t _starpu_can_submit_scc_task(void);
  358. /* Check whether there is anything that the worker should do instead of
  359. * sleeping (waiting on something to happen). */
  360. unsigned _starpu_worker_can_block(unsigned memnode, struct _starpu_worker *worker);
  361. /* This function must be called to block a worker. It puts the worker in a
  362. * sleeping state until there is some event that forces the worker to wake up.
  363. * */
  364. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  365. /* This function initializes the current driver for the given worker */
  366. void _starpu_driver_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  367. /* This function initializes the current thread for the given worker */
  368. void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key, unsigned sync);
  369. static inline unsigned _starpu_worker_get_count(void)
  370. {
  371. return _starpu_config.topology.nworkers;
  372. }
  373. #define starpu_worker_get_count _starpu_worker_get_count
  374. /* The _starpu_worker structure describes all the state of a StarPU worker.
  375. * This function sets the pthread key which stores a pointer to this structure.
  376. * */
  377. static inline void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  378. {
  379. STARPU_ASSERT(_starpu_keys_initialized);
  380. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_key, worker);
  381. }
  382. /* Returns the _starpu_worker structure that describes the state of the
  383. * current worker. */
  384. static inline struct _starpu_worker *_starpu_get_local_worker_key(void)
  385. {
  386. if (!_starpu_keys_initialized)
  387. return NULL;
  388. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_key);
  389. }
  390. /* The _starpu_worker_set structure describes all the state of a StarPU worker_set.
  391. * This function sets the pthread key which stores a pointer to this structure.
  392. * */
  393. static inline void _starpu_set_local_worker_set_key(struct _starpu_worker_set *worker)
  394. {
  395. STARPU_ASSERT(_starpu_keys_initialized);
  396. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_set_key, worker);
  397. }
  398. /* Returns the _starpu_worker_set structure that describes the state of the
  399. * current worker_set. */
  400. static inline struct _starpu_worker_set *_starpu_get_local_worker_set_key(void)
  401. {
  402. if (!_starpu_keys_initialized)
  403. return NULL;
  404. return (struct _starpu_worker_set *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_set_key);
  405. }
  406. /* Returns the _starpu_worker structure that describes the state of the
  407. * specified worker. */
  408. static inline struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  409. {
  410. STARPU_ASSERT(id < starpu_worker_get_count());
  411. return &_starpu_config.workers[id];
  412. }
  413. /* Returns the starpu_sched_ctx structure that descriebes the state of the
  414. * specified ctx */
  415. static inline struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  416. {
  417. if(id == STARPU_NMAX_SCHED_CTXS) return NULL;
  418. return &_starpu_config.sched_ctxs[id];
  419. }
  420. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  421. int _starpu_is_initialized(void);
  422. /* Returns the structure that describes the overall machine configuration (eg.
  423. * all workers and topology). */
  424. static inline struct _starpu_machine_config *_starpu_get_machine_config(void)
  425. {
  426. return &_starpu_config;
  427. }
  428. /* Return whether kernels should be run (<=0) or not (>0) */
  429. static inline int _starpu_get_disable_kernels(void)
  430. {
  431. return _starpu_config.disable_kernels;
  432. }
  433. /* Retrieve the status which indicates what the worker is currently doing. */
  434. static inline enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  435. {
  436. return _starpu_config.workers[workerid].status;
  437. }
  438. /* Change the status of the worker which indicates what the worker is currently
  439. * doing (eg. executing a callback). */
  440. static inline void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  441. {
  442. _starpu_config.workers[workerid].status = status;
  443. }
  444. /* We keep an initial sched ctx which might be used in case no other ctx is available */
  445. static inline struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  446. {
  447. return &_starpu_config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  448. }
  449. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  450. /* returns workers not belonging to any context, be careful no mutex is used,
  451. the list might not be updated */
  452. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  453. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex)
  454. {
  455. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  456. return &w->sched_mutex == mutex;
  457. }
  458. static inline int _starpu_worker_get_nsched_ctxs(int workerid)
  459. {
  460. return _starpu_config.workers[workerid].nsched_ctxs;
  461. }
  462. /* Get the total number of sched_ctxs created till now */
  463. static inline unsigned _starpu_get_nsched_ctxs(void)
  464. {
  465. return _starpu_config.topology.nsched_ctxs;
  466. }
  467. /* Inlined version when building the core. */
  468. static inline int _starpu_worker_get_id(void)
  469. {
  470. struct _starpu_worker * worker;
  471. worker = _starpu_get_local_worker_key();
  472. if (worker)
  473. {
  474. return worker->workerid;
  475. }
  476. else
  477. {
  478. /* there is no worker associated to that thread, perhaps it is
  479. * a thread from the application or this is some SPU worker */
  480. return -1;
  481. }
  482. }
  483. #define starpu_worker_get_id _starpu_worker_get_id
  484. /* Similar behaviour to starpu_worker_get_id() but fails when called from outside a worker */
  485. /* This returns an unsigned object on purpose, so that the caller is sure to get a positive value */
  486. static inline unsigned __starpu_worker_get_id_check(const char *f, int l)
  487. {
  488. (void) l;
  489. (void) f;
  490. int id = starpu_worker_get_id();
  491. STARPU_ASSERT_MSG(id>=0, "%s:%d Cannot be called from outside a worker\n", f, l);
  492. return id;
  493. }
  494. #define _starpu_worker_get_id_check(f,l) __starpu_worker_get_id_check(f,l)
  495. void _starpu_worker_set_stream_ctx(unsigned workerid, struct _starpu_sched_ctx *sched_ctx);
  496. struct _starpu_sched_ctx* _starpu_worker_get_ctx_stream(unsigned stream_workerid);
  497. /* Must be called with worker's sched_mutex held.
  498. * Mark the beginning of a scheduling operation during which the sched_mutex
  499. * lock may be temporarily released, but the scheduling context of the worker
  500. * should not be modified */
  501. static inline void _starpu_worker_enter_transient_sched_op(struct _starpu_worker * const worker)
  502. {
  503. worker->state_sched_op_pending = 1;
  504. }
  505. /* Must be called with worker's sched_mutex held.
  506. * Mark the end of a scheduling operation, and notify potential waiters that
  507. * scheduling context changes can safely be performed again.
  508. */
  509. static inline void _starpu_worker_leave_transient_sched_op(struct _starpu_worker * const worker)
  510. {
  511. worker->state_sched_op_pending = 0;
  512. if (worker->state_changing_ctx_waiting)
  513. /* cond_broadcast is required over cond_signal since
  514. * the condition is share for multiple purpose */
  515. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  516. }
  517. /* Must be called with worker's sched_mutex held.
  518. * Passively wait until state_sched_op_pending is cleared.
  519. */
  520. static inline void _starpu_worker_wait_for_transient_sched_op_completion(struct _starpu_worker * const worker)
  521. {
  522. if (worker->state_sched_op_pending)
  523. {
  524. worker->state_changing_ctx_waiting = 1;
  525. do
  526. {
  527. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  528. }
  529. while (worker->state_sched_op_pending);
  530. worker->state_changing_ctx_waiting = 0;
  531. }
  532. }
  533. #endif // __WORKERS_H__