workers.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  5. * Copyright (C) 2016 Uppsala University
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __WORKERS_H__
  19. #define __WORKERS_H__
  20. /** \addtogroup workers */
  21. /* @{ */
  22. #include <limits.h>
  23. #include <starpu.h>
  24. #include <common/config.h>
  25. #include <common/timing.h>
  26. #include <common/fxt.h>
  27. #include <common/thread.h>
  28. #include <common/utils.h>
  29. #include <core/jobs.h>
  30. #include <core/perfmodel/perfmodel.h>
  31. #include <core/sched_policy.h>
  32. #include <core/topology.h>
  33. #include <core/errorcheck.h>
  34. #include <core/sched_ctx.h>
  35. #include <core/sched_ctx_list.h>
  36. #include <core/simgrid.h>
  37. #ifdef STARPU_HAVE_HWLOC
  38. #include <hwloc.h>
  39. #endif
  40. #include <common/knobs.h>
  41. #include <core/drivers.h>
  42. #include <drivers/cuda/driver_cuda.h>
  43. #include <drivers/opencl/driver_opencl.h>
  44. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  45. #include <drivers/mpi/driver_mpi_source.h>
  46. #endif
  47. #include <drivers/cpu/driver_cpu.h>
  48. #include <datawizard/datawizard.h>
  49. #define STARPU_MAX_PIPELINE 4
  50. struct _starpu_ctx_change_list;
  51. /** This is initialized by _starpu_worker_init() */
  52. LIST_TYPE(_starpu_worker,
  53. struct _starpu_machine_config *config;
  54. starpu_pthread_mutex_t mutex;
  55. enum starpu_worker_archtype arch; /**< what is the type of worker ? */
  56. uint32_t worker_mask; /**< what is the type of worker ? */
  57. struct starpu_perfmodel_arch perf_arch; /**< in case there are different models of the same arch */
  58. starpu_pthread_t worker_thread; /**< the thread which runs the worker */
  59. unsigned devid; /**< which cpu/gpu/etc is controlled by the worker ? */
  60. unsigned subworkerid; /**< which sub-worker this one is for the cpu/gpu */
  61. int bindid; /**< which cpu is the driver bound to ? (logical index) */
  62. int workerid; /**< uniquely identify the worker among all processing units types */
  63. int combined_workerid; /**< combined worker currently using this worker */
  64. int current_rank; /**< current rank in case the worker is used in a parallel fashion */
  65. int worker_size; /**< size of the worker in case we use a combined worker */
  66. starpu_pthread_cond_t started_cond; /**< indicate when the worker is ready */
  67. starpu_pthread_cond_t ready_cond; /**< indicate when the worker is ready */
  68. unsigned memory_node; /**< which memory node is the worker associated with ? */
  69. unsigned numa_memory_node; /**< which numa memory node is the worker associated with? (logical index) */
  70. /**
  71. * condition variable used for passive waiting operations on worker
  72. * STARPU_PTHREAD_COND_BROADCAST must be used instead of STARPU_PTHREAD_COND_SIGNAL,
  73. * since the condition is shared for multiple purpose */
  74. starpu_pthread_cond_t sched_cond;
  75. starpu_pthread_mutex_t sched_mutex; /**< mutex protecting sched_cond */
  76. unsigned state_relax_refcnt; /**< mark scheduling sections where other workers can safely access the worker state */
  77. #ifdef STARPU_SPINLOCK_CHECK
  78. const char *relax_on_file;
  79. int relax_on_line;
  80. const char *relax_on_func;
  81. const char *relax_off_file;
  82. int relax_off_line;
  83. const char *relax_off_func;
  84. #endif
  85. unsigned state_sched_op_pending; /**< a task pop is ongoing even though sched_mutex may temporarily be unlocked */
  86. 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*/
  87. 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 */
  88. unsigned state_blocked_in_parallel; /**< worker is currently blocked on a parallel section */
  89. unsigned state_blocked_in_parallel_observed; /**< the blocked state of the worker has been observed by another worker during a relaxed section */
  90. unsigned state_block_in_parallel_req; /**< a request for state transition from unblocked to blocked is pending */
  91. unsigned state_block_in_parallel_ack; /**< a block request has been honored */
  92. unsigned state_unblock_in_parallel_req; /**< a request for state transition from blocked to unblocked is pending */
  93. unsigned state_unblock_in_parallel_ack; /**< an unblock request has been honored */
  94. /**
  95. * cumulative blocking depth
  96. * - =0 worker unblocked
  97. * - >0 worker blocked
  98. * - transition from 0 to 1 triggers a block_req
  99. * - transition from 1 to 0 triggers a unblock_req
  100. */
  101. unsigned block_in_parallel_ref_count;
  102. starpu_pthread_t thread_changing_ctx; /**< thread currently changing a sched_ctx containing the worker */
  103. /**
  104. list of deferred context changes
  105. *
  106. * when the current thread is a worker, _and_ this worker is in a
  107. * scheduling operation, new ctx changes are queued to this list for
  108. * subsequent processing once worker completes the ongoing scheduling
  109. * operation */
  110. struct _starpu_ctx_change_list ctx_change_list;
  111. struct starpu_task_prio_list local_tasks; /**< this queue contains tasks that have been explicitely submitted to that queue */
  112. struct starpu_task **local_ordered_tasks; /**< this queue contains tasks that have been explicitely submitted to that queue with an explicit order */
  113. unsigned local_ordered_tasks_size; /**< this records the size of local_ordered_tasks */
  114. unsigned current_ordered_task; /**< this records the index (within local_ordered_tasks) of the next ordered task to be executed */
  115. unsigned current_ordered_task_order; /**< this records the order of the next ordered task to be executed */
  116. struct starpu_task *current_task; /**< task currently executed by this worker (non-pipelined version) */
  117. struct starpu_task *current_tasks[STARPU_MAX_PIPELINE]; /**< tasks currently executed by this worker (pipelined version) */
  118. #ifdef STARPU_SIMGRID
  119. starpu_pthread_wait_t wait;
  120. #endif
  121. struct timespec cl_start; /**< Codelet start time of the task currently running */
  122. struct timespec cl_end; /**< Codelet end time of the last task running */
  123. unsigned char first_task; /**< Index of first task in the pipeline */
  124. unsigned char ntasks; /**< number of tasks in the pipeline */
  125. unsigned char pipeline_length; /**< number of tasks to be put in the pipeline */
  126. unsigned char pipeline_stuck; /**< whether a task prevents us from pipelining */
  127. struct _starpu_worker_set *set; /**< in case this worker belongs to a set */
  128. unsigned worker_is_running;
  129. unsigned worker_is_initialized;
  130. unsigned wait_for_worker_initialization;
  131. enum _starpu_worker_status status; /**< what is the worker doing now ? (eg. CALLBACK) */
  132. unsigned state_keep_awake; /**< !0 if a task has been pushed to the worker and the task has not yet been seen by the worker, the worker should no go to sleep before processing this task*/
  133. char name[128];
  134. char short_name[32];
  135. unsigned run_by_starpu; /**< Is this run by StarPU or directly by the application ? */
  136. struct _starpu_driver_ops *driver_ops;
  137. struct _starpu_sched_ctx_list *sched_ctx_list;
  138. int tmp_sched_ctx;
  139. unsigned nsched_ctxs; /**< the no of contexts a worker belongs to*/
  140. struct _starpu_barrier_counter tasks_barrier; /**< wait for the tasks submitted */
  141. unsigned has_prev_init; /**< had already been inited in another ctx */
  142. unsigned removed_from_ctx[STARPU_NMAX_SCHED_CTXS+1];
  143. unsigned spinning_backoff ; /**< number of cycles to pause when spinning */
  144. unsigned nb_buffers_transferred; /**< number of piece of data already send to worker */
  145. unsigned nb_buffers_totransfer; /**< number of piece of data already send to worker */
  146. struct starpu_task *task_transferring; /**< The buffers of this task are being sent */
  147. /**
  148. * indicate whether the workers shares tasks lists with other workers
  149. * in this case when removing him from a context it disapears instantly
  150. */
  151. unsigned shares_tasks_lists[STARPU_NMAX_SCHED_CTXS+1];
  152. unsigned poped_in_ctx[STARPU_NMAX_SCHED_CTXS+1]; /**< boolean to chose the next ctx a worker will pop into */
  153. /**
  154. * boolean indicating at which moment we checked all ctxs and change phase for the booleab poped_in_ctx
  155. * one for each of the 2 priorities
  156. */
  157. unsigned reverse_phase[2];
  158. unsigned pop_ctx_priority; /**< indicate which priority of ctx is currently active: the values are 0 or 1*/
  159. unsigned is_slave_somewhere; /**< bool to indicate if the worker is slave in a ctx */
  160. struct _starpu_sched_ctx *stream_ctx;
  161. #ifdef __GLIBC__
  162. cpu_set_t cpu_set;
  163. #endif /* __GLIBC__ */
  164. #ifdef STARPU_HAVE_HWLOC
  165. hwloc_bitmap_t hwloc_cpu_set;
  166. hwloc_obj_t hwloc_obj;
  167. #endif
  168. struct starpu_perf_counter_sample perf_counter_sample;
  169. int64_t __w_total_executed__value;
  170. double __w_cumul_execution_time__value;
  171. int enable_knob;
  172. int bindid_requested;
  173. /** Keep this last, to make sure to separate worker data in separate
  174. cache lines. */
  175. char padding[STARPU_CACHELINE_SIZE];
  176. );
  177. struct _starpu_combined_worker
  178. {
  179. struct starpu_perfmodel_arch perf_arch; /**< in case there are different models of the same arch */
  180. uint32_t worker_mask; /**< what is the type of workers ? */
  181. int worker_size;
  182. unsigned memory_node; /**< which memory node is associated that worker to ? */
  183. int combined_workerid[STARPU_NMAXWORKERS];
  184. #ifdef STARPU_USE_MP
  185. int count;
  186. starpu_pthread_mutex_t count_mutex;
  187. #endif
  188. #ifdef __GLIBC__
  189. cpu_set_t cpu_set;
  190. #endif /* __GLIBC__ */
  191. #ifdef STARPU_HAVE_HWLOC
  192. hwloc_bitmap_t hwloc_cpu_set;
  193. #endif
  194. /** Keep this last, to make sure to separate worker data in separate
  195. cache lines. */
  196. char padding[STARPU_CACHELINE_SIZE];
  197. };
  198. /**
  199. * in case a single CPU worker may control multiple
  200. * accelerators
  201. */
  202. struct _starpu_worker_set
  203. {
  204. starpu_pthread_mutex_t mutex;
  205. starpu_pthread_t worker_thread; /**< the thread which runs the worker */
  206. unsigned nworkers;
  207. unsigned started; /**< Only one thread for the whole set */
  208. void *retval;
  209. struct _starpu_worker *workers;
  210. starpu_pthread_cond_t ready_cond; /**< indicate when the set is ready */
  211. unsigned set_is_initialized;
  212. unsigned wait_for_set_initialization;
  213. };
  214. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  215. extern struct _starpu_worker_set mpi_worker_set[STARPU_MAXMPIDEVS];
  216. #endif
  217. struct _starpu_machine_topology
  218. {
  219. /** Total number of workers. */
  220. unsigned nworkers;
  221. /** Total number of combined workers. */
  222. unsigned ncombinedworkers;
  223. unsigned nsched_ctxs;
  224. #ifdef STARPU_HAVE_HWLOC
  225. /** Topology as detected by hwloc. */
  226. hwloc_topology_t hwtopology;
  227. #endif
  228. /** custom hwloc tree*/
  229. struct starpu_tree *tree;
  230. /** Total number of PUs (i.e. threads), as detected by the topology code. May
  231. * be different from the actual number of CPU workers.
  232. */
  233. unsigned nhwpus;
  234. /** Total number of devices, as detected. May be different from the
  235. * actual number of devices run by StarPU.
  236. */
  237. unsigned nhwdevices[STARPU_NARCH];
  238. /** Total number of worker for each device, as detected. May be different from the
  239. * actual number of workers run by StarPU.
  240. */
  241. unsigned nhwworker[STARPU_NARCH][STARPU_NMAXDEVS];
  242. /** Actual number of devices used by StarPU.
  243. */
  244. unsigned ndevices[STARPU_NARCH];
  245. /** Number of worker per device
  246. */
  247. unsigned nworker[STARPU_NARCH][STARPU_NMAXDEVS];
  248. /** Whether we should have one thread per stream */
  249. int cuda_th_per_stream;
  250. /** Whether we should have one thread per device */
  251. int cuda_th_per_dev;
  252. /** Indicates the successive logical PU identifier that should be used
  253. * to bind the workers. It is either filled according to the
  254. * user's explicit parameters (from starpu_conf) or according
  255. * to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  256. * round-robin policy is used to distributed the workers over
  257. * the cores.
  258. */
  259. unsigned workers_bindid[STARPU_NMAXWORKERS];
  260. /** Indicates the successive CUDA identifier that should be
  261. * used by the CUDA driver. It is either filled according to
  262. * the user's explicit parameters (from starpu_conf) or
  263. * according to the STARPU_WORKERS_CUDAID env. variable.
  264. * Otherwise, they are taken in ID order.
  265. */
  266. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  267. /** Indicates the successive OpenCL identifier that should be
  268. * used by the OpenCL driver. It is either filled according
  269. * to the user's explicit parameters (from starpu_conf) or
  270. * according to the STARPU_WORKERS_OPENCLID env. variable.
  271. * Otherwise, they are taken in ID order.
  272. */
  273. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  274. unsigned workers_mpi_ms_deviceid[STARPU_NMAXWORKERS];
  275. };
  276. struct _starpu_machine_config
  277. {
  278. struct _starpu_machine_topology topology;
  279. #ifdef STARPU_HAVE_HWLOC
  280. int cpu_depth;
  281. int pu_depth;
  282. #endif
  283. /** Where to bind next worker ? */
  284. int current_bindid;
  285. char currently_bound[STARPU_NMAXWORKERS];
  286. char currently_shared[STARPU_NMAXWORKERS];
  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 MPI do we use? */
  292. int current_mpi_deviceid;
  293. /** Memory node for different worker types, if only one */
  294. int arch_nodeid [STARPU_NARCH];
  295. /** Separate out previous variables from per-worker data. */
  296. char padding1[STARPU_CACHELINE_SIZE];
  297. /** Basic workers : each of this worker is running its own driver and
  298. * can be combined with other basic workers. */
  299. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  300. /** Combined workers: these worker are a combination of basic workers
  301. * that can run parallel tasks together. */
  302. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  303. starpu_pthread_mutex_t submitted_mutex;
  304. /** Separate out previous mutex from the rest of the data. */
  305. char padding2[STARPU_CACHELINE_SIZE];
  306. /** Translation table from bindid to worker IDs */
  307. struct
  308. {
  309. int *workerids;
  310. unsigned nworkers; /**< size of workerids */
  311. } *bindid_workers;
  312. unsigned nbindid; /**< size of bindid_workers */
  313. /** This bitmask indicates which kinds of worker are available. For
  314. * instance it is possible to test if there is a CUDA worker with
  315. * the result of (worker_mask & STARPU_CUDA). */
  316. uint32_t worker_mask;
  317. /** either the user given configuration passed to starpu_init or a default configuration */
  318. struct starpu_conf conf;
  319. /** this flag is set until the runtime is stopped */
  320. unsigned running;
  321. int disable_kernels;
  322. /** Number of calls to starpu_pause() - calls to starpu_resume(). When >0,
  323. * StarPU should pause. */
  324. int pause_depth;
  325. /** all the sched ctx of the current instance of starpu */
  326. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS+1];
  327. /** this flag is set until the application is finished submitting tasks */
  328. unsigned submitting;
  329. int watchdog_ok;
  330. /** When >0, StarPU should stop performance counters collection. */
  331. int perf_counter_pause_depth;
  332. };
  333. /** Provides information for a device driver */
  334. struct starpu_driver_info
  335. {
  336. const char *name_upper; /**< Name of worker type in upper case */
  337. const char *name_var; /**< Name of worker type for environment variables */
  338. const char *name_lower; /**< Name of worker type in lower case */
  339. enum starpu_node_kind memory_kind; /**< Kind of memory in device */
  340. double alpha; /**< Typical relative speed compared to a CPU core */
  341. };
  342. /** Device driver information, indexed by enum starpu_worker_archtype */
  343. extern struct starpu_driver_info starpu_driver_info[STARPU_NARCH];
  344. void starpu_driver_info_register(enum starpu_worker_archtype archtype, const struct starpu_driver_info *info);
  345. /** Provides information for a memory node driver */
  346. struct starpu_memory_driver_info
  347. {
  348. const char *name_upper; /**< Name of memory in upper case */
  349. enum starpu_worker_archtype worker_archtype; /**< Kind of device */
  350. };
  351. /** Memory driver information, indexed by enum starpu_node_kind */
  352. extern struct starpu_memory_driver_info starpu_memory_driver_info[STARPU_MAX_RAM+1];
  353. void starpu_memory_driver_info_register(enum starpu_node_kind kind, const struct starpu_memory_driver_info *info);
  354. extern int _starpu_worker_parallel_blocks;
  355. extern struct _starpu_machine_config _starpu_config STARPU_ATTRIBUTE_INTERNAL;
  356. extern int _starpu_keys_initialized STARPU_ATTRIBUTE_INTERNAL;
  357. extern starpu_pthread_key_t _starpu_worker_key STARPU_ATTRIBUTE_INTERNAL;
  358. extern starpu_pthread_key_t _starpu_worker_set_key STARPU_ATTRIBUTE_INTERNAL;
  359. /** Three functions to manage argv, argc */
  360. void _starpu_set_argc_argv(int *argc, char ***argv);
  361. int *_starpu_get_argc();
  362. char ***_starpu_get_argv();
  363. /** Fill conf with environment variables */
  364. void _starpu_conf_check_environment(struct starpu_conf *conf);
  365. /** Called by the driver when it is ready to pause */
  366. void _starpu_may_pause(void);
  367. /** Has starpu_shutdown already been called ? */
  368. static inline unsigned _starpu_machine_is_running(void)
  369. {
  370. unsigned ret;
  371. /* running is just protected by a memory barrier */
  372. STARPU_RMB();
  373. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  374. ret = _starpu_config.running;
  375. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  376. return ret;
  377. }
  378. /** initialise a worker */
  379. void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu_machine_config *pconfig);
  380. /** Check if there is a worker that may execute the task. */
  381. uint32_t _starpu_worker_exists(struct starpu_task *);
  382. /** Is there a worker that can execute CUDA code ? */
  383. uint32_t _starpu_can_submit_cuda_task(void);
  384. /** Is there a worker that can execute CPU code ? */
  385. uint32_t _starpu_can_submit_cpu_task(void);
  386. /** Is there a worker that can execute OpenCL code ? */
  387. uint32_t _starpu_can_submit_opencl_task(void);
  388. /** Check whether there is anything that the worker should do instead of
  389. * sleeping (waiting on something to happen). */
  390. unsigned _starpu_worker_can_block(unsigned memnode, struct _starpu_worker *worker);
  391. /** This function must be called to block a worker. It puts the worker in a
  392. * sleeping state until there is some event that forces the worker to wake up.
  393. * */
  394. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  395. /** This function initializes the current driver for the given worker */
  396. void _starpu_driver_start(struct _starpu_worker *worker, enum starpu_worker_archtype archtype, unsigned sync);
  397. /** This function initializes the current thread for the given worker */
  398. void _starpu_worker_start(struct _starpu_worker *worker, enum starpu_worker_archtype archtype, unsigned sync);
  399. static inline unsigned _starpu_worker_get_count(void)
  400. {
  401. return _starpu_config.topology.nworkers;
  402. }
  403. #define starpu_worker_get_count _starpu_worker_get_count
  404. /** The _starpu_worker structure describes all the state of a StarPU worker.
  405. * This function sets the pthread key which stores a pointer to this structure.
  406. * */
  407. static inline void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  408. {
  409. STARPU_ASSERT(_starpu_keys_initialized);
  410. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_key, worker);
  411. }
  412. /** Returns the _starpu_worker structure that describes the state of the
  413. * current worker. */
  414. static inline struct _starpu_worker *_starpu_get_local_worker_key(void)
  415. {
  416. if (!_starpu_keys_initialized)
  417. return NULL;
  418. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_key);
  419. }
  420. /** The _starpu_worker_set structure describes all the state of a StarPU worker_set.
  421. * This function sets the pthread key which stores a pointer to this structure.
  422. * */
  423. static inline void _starpu_set_local_worker_set_key(struct _starpu_worker_set *worker)
  424. {
  425. STARPU_ASSERT(_starpu_keys_initialized);
  426. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_set_key, worker);
  427. }
  428. /** Returns the _starpu_worker_set structure that describes the state of the
  429. * current worker_set. */
  430. static inline struct _starpu_worker_set *_starpu_get_local_worker_set_key(void)
  431. {
  432. if (!_starpu_keys_initialized)
  433. return NULL;
  434. return (struct _starpu_worker_set *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_set_key);
  435. }
  436. /** Returns the _starpu_worker structure that describes the state of the
  437. * specified worker. */
  438. static inline struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  439. {
  440. STARPU_ASSERT(id < starpu_worker_get_count());
  441. return &_starpu_config.workers[id];
  442. }
  443. /** Returns the starpu_sched_ctx structure that describes the state of the
  444. * specified ctx */
  445. static inline struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  446. {
  447. return (id > STARPU_NMAX_SCHED_CTXS) ? NULL : &_starpu_config.sched_ctxs[id];
  448. }
  449. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  450. /** Returns the structure that describes the overall machine configuration (eg.
  451. * all workers and topology). */
  452. static inline struct _starpu_machine_config *_starpu_get_machine_config(void)
  453. {
  454. return &_starpu_config;
  455. }
  456. /** Return whether kernels should be run (<=0) or not (>0) */
  457. static inline int _starpu_get_disable_kernels(void)
  458. {
  459. return _starpu_config.disable_kernels;
  460. }
  461. /** Retrieve the status which indicates what the worker is currently doing. */
  462. static inline enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  463. {
  464. return _starpu_config.workers[workerid].status;
  465. }
  466. /** Change the status of the worker which indicates what the worker is currently
  467. * doing (eg. executing a callback). */
  468. static inline void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  469. {
  470. _starpu_config.workers[workerid].status = status;
  471. }
  472. /** We keep an initial sched ctx which might be used in case no other ctx is available */
  473. static inline struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  474. {
  475. return &_starpu_config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  476. }
  477. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  478. /** returns workers not belonging to any context, be careful no mutex is used,
  479. the list might not be updated */
  480. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  481. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex)
  482. {
  483. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  484. return &w->sched_mutex == mutex;
  485. }
  486. static inline int _starpu_worker_get_nsched_ctxs(int workerid)
  487. {
  488. return _starpu_config.workers[workerid].nsched_ctxs;
  489. }
  490. /** Get the total number of sched_ctxs created till now */
  491. static inline unsigned _starpu_get_nsched_ctxs(void)
  492. {
  493. /* topology.nsched_ctxs may be increased asynchronously in sched_ctx_create */
  494. STARPU_RMB();
  495. return _starpu_config.topology.nsched_ctxs;
  496. }
  497. /** Inlined version when building the core. */
  498. static inline int _starpu_worker_get_id(void)
  499. {
  500. struct _starpu_worker * worker;
  501. worker = _starpu_get_local_worker_key();
  502. if (worker)
  503. {
  504. return worker->workerid;
  505. }
  506. else
  507. {
  508. /* there is no worker associated to that thread, perhaps it is
  509. * a thread from the application or this is some SPU worker */
  510. return -1;
  511. }
  512. }
  513. #define starpu_worker_get_id _starpu_worker_get_id
  514. /** Similar behaviour to starpu_worker_get_id() but fails when called from outside a worker */
  515. /** This returns an unsigned object on purpose, so that the caller is sure to get a positive value */
  516. static inline unsigned __starpu_worker_get_id_check(const char *f, int l)
  517. {
  518. (void) l;
  519. (void) f;
  520. int id = starpu_worker_get_id();
  521. STARPU_ASSERT_MSG(id>=0, "%s:%d Cannot be called from outside a worker\n", f, l);
  522. return id;
  523. }
  524. #define _starpu_worker_get_id_check(f,l) __starpu_worker_get_id_check(f,l)
  525. void _starpu_worker_set_stream_ctx(unsigned workerid, struct _starpu_sched_ctx *sched_ctx);
  526. struct _starpu_sched_ctx* _starpu_worker_get_ctx_stream(unsigned stream_workerid);
  527. /** Send a request to the worker to block, before a parallel task is about to
  528. * begin.
  529. *
  530. * Must be called with worker's sched_mutex held.
  531. */
  532. static inline void _starpu_worker_request_blocking_in_parallel(struct _starpu_worker * const worker)
  533. {
  534. _starpu_worker_parallel_blocks = 1;
  535. /* flush pending requests to start on a fresh transaction epoch */
  536. while (worker->state_unblock_in_parallel_req)
  537. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  538. /* announce blocking intent */
  539. STARPU_ASSERT(worker->block_in_parallel_ref_count < UINT_MAX);
  540. worker->block_in_parallel_ref_count++;
  541. if (worker->block_in_parallel_ref_count == 1)
  542. {
  543. /* only the transition from 0 to 1 triggers the block_in_parallel_req */
  544. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  545. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  546. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  547. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  548. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  549. /* trigger the block_in_parallel_req */
  550. worker->state_block_in_parallel_req = 1;
  551. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  552. #ifdef STARPU_SIMGRID
  553. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[worker->workerid]);
  554. #endif
  555. /* wait for block_in_parallel_req to be processed */
  556. while (!worker->state_block_in_parallel_ack)
  557. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  558. STARPU_ASSERT(worker->block_in_parallel_ref_count >= 1);
  559. STARPU_ASSERT(worker->state_block_in_parallel_req);
  560. STARPU_ASSERT(worker->state_blocked_in_parallel);
  561. /* reset block_in_parallel_req state flags */
  562. worker->state_block_in_parallel_req = 0;
  563. worker->state_block_in_parallel_ack = 0;
  564. /* broadcast block_in_parallel_req state flags reset */
  565. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  566. }
  567. }
  568. /** Send a request to the worker to unblock, after a parallel task is complete.
  569. *
  570. * Must be called with worker's sched_mutex held.
  571. */
  572. static inline void _starpu_worker_request_unblocking_in_parallel(struct _starpu_worker * const worker)
  573. {
  574. /* flush pending requests to start on a fresh transaction epoch */
  575. while (worker->state_block_in_parallel_req)
  576. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  577. /* unblocking may be requested unconditionnally
  578. * thus, check is unblocking is really needed */
  579. if (worker->state_blocked_in_parallel)
  580. {
  581. if (worker->block_in_parallel_ref_count == 1)
  582. {
  583. /* only the transition from 1 to 0 triggers the unblock_in_parallel_req */
  584. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  585. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  586. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  587. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  588. /* trigger the unblock_in_parallel_req */
  589. worker->state_unblock_in_parallel_req = 1;
  590. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  591. /* wait for the unblock_in_parallel_req to be processed */
  592. while (!worker->state_unblock_in_parallel_ack)
  593. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  594. STARPU_ASSERT(worker->state_unblock_in_parallel_req);
  595. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  596. /* reset unblock_in_parallel_req state flags */
  597. worker->state_unblock_in_parallel_req = 0;
  598. worker->state_unblock_in_parallel_ack = 0;
  599. /* broadcast unblock_in_parallel_req state flags reset */
  600. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  601. }
  602. /* announce unblocking complete */
  603. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  604. worker->block_in_parallel_ref_count--;
  605. }
  606. }
  607. /** Called by the the worker to process incoming requests to block or unblock on
  608. * parallel task boundaries.
  609. *
  610. * Must be called with worker's sched_mutex held.
  611. */
  612. static inline void _starpu_worker_process_block_in_parallel_requests(struct _starpu_worker * const worker)
  613. {
  614. while (worker->state_block_in_parallel_req)
  615. {
  616. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  617. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  618. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  619. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  620. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  621. /* enter effective blocked state */
  622. worker->state_blocked_in_parallel = 1;
  623. /* notify block_in_parallel_req processing */
  624. worker->state_block_in_parallel_ack = 1;
  625. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  626. /* block */
  627. while (!worker->state_unblock_in_parallel_req)
  628. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  629. STARPU_ASSERT(worker->state_blocked_in_parallel);
  630. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  631. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  632. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  633. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  634. /* leave effective blocked state */
  635. worker->state_blocked_in_parallel = 0;
  636. /* notify unblock_in_parallel_req processing */
  637. worker->state_unblock_in_parallel_ack = 1;
  638. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  639. }
  640. }
  641. #ifdef STARPU_SPINLOCK_CHECK
  642. #define _starpu_worker_enter_sched_op(worker) __starpu_worker_enter_sched_op((worker), __FILE__, __LINE__, __starpu_func__)
  643. static inline void __starpu_worker_enter_sched_op(struct _starpu_worker * const worker, const char*file, int line, const char* func)
  644. #else
  645. /** Mark the beginning of a scheduling operation by the worker. No worker
  646. * blocking operations on parallel tasks and no scheduling context change
  647. * operations must be performed on contexts containing the worker, on
  648. * contexts about to add the worker and on contexts about to remove the
  649. * worker, while the scheduling operation is in process. The sched mutex
  650. * of the worker may only be acquired permanently by another thread when
  651. * no scheduling operation is in process, or when a scheduling operation
  652. * is in process _and_ worker->state_relax_refcnt!=0. If a
  653. * scheduling operation is in process _and_
  654. * worker->state_relax_refcnt==0, a thread other than the worker
  655. * must wait on condition worker->sched_cond for
  656. * worker->state_relax_refcnt!=0 to become true, before acquiring
  657. * the worker sched mutex permanently.
  658. *
  659. * Must be called with worker's sched_mutex held.
  660. */
  661. static inline void _starpu_worker_enter_sched_op(struct _starpu_worker * const worker)
  662. #endif
  663. {
  664. STARPU_ASSERT(!worker->state_sched_op_pending);
  665. if (!worker->state_blocked_in_parallel_observed)
  666. {
  667. /* process pending block requests before entering a sched_op region */
  668. _starpu_worker_process_block_in_parallel_requests(worker);
  669. while (worker->state_changing_ctx_notice)
  670. {
  671. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  672. /* new block requests may have been triggered during the wait,
  673. * need to check again */
  674. _starpu_worker_process_block_in_parallel_requests(worker);
  675. }
  676. }
  677. else
  678. {
  679. /* if someone observed the worker state since the last call, postpone block request
  680. * processing for one sched_op turn more, because the observer will not have seen
  681. * new block requests between its observation and now.
  682. *
  683. * however, the worker still has to wait for context change operations to complete
  684. * before entering sched_op again*/
  685. while (worker->state_changing_ctx_notice)
  686. {
  687. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  688. }
  689. }
  690. /* no block request and no ctx change ahead,
  691. * enter sched_op */
  692. worker->state_sched_op_pending = 1;
  693. worker->state_blocked_in_parallel_observed = 0;
  694. worker->state_relax_refcnt = 0;
  695. #ifdef STARPU_SPINLOCK_CHECK
  696. worker->relax_on_file = file;
  697. worker->relax_on_line = line;
  698. worker->relax_on_func = func;
  699. #endif
  700. }
  701. void _starpu_worker_apply_deferred_ctx_changes(void);
  702. #ifdef STARPU_SPINLOCK_CHECK
  703. #define _starpu_worker_leave_sched_op(worker) __starpu_worker_leave_sched_op((worker), __FILE__, __LINE__, __starpu_func__)
  704. static inline void __starpu_worker_leave_sched_op(struct _starpu_worker * const worker, const char*file, int line, const char* func)
  705. #else
  706. /** Mark the end of a scheduling operation by the worker.
  707. *
  708. * Must be called with worker's sched_mutex held.
  709. */
  710. static inline void _starpu_worker_leave_sched_op(struct _starpu_worker * const worker)
  711. #endif
  712. {
  713. STARPU_ASSERT(worker->state_sched_op_pending);
  714. worker->state_relax_refcnt = 1;
  715. #ifdef STARPU_SPINLOCK_CHECK
  716. worker->relax_off_file = file;
  717. worker->relax_off_line = line;
  718. worker->relax_off_func = func;
  719. #endif
  720. worker->state_sched_op_pending = 0;
  721. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  722. _starpu_worker_apply_deferred_ctx_changes();
  723. }
  724. static inline int _starpu_worker_sched_op_pending(void)
  725. {
  726. int workerid = starpu_worker_get_id();
  727. if (workerid == -1)
  728. return 0;
  729. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  730. STARPU_ASSERT(worker != NULL);
  731. return worker->state_sched_op_pending;
  732. }
  733. /** Must be called before altering a context related to the worker
  734. * whether about adding the worker to a context, removing it from a
  735. * context or modifying the set of workers of a context of which the
  736. * worker is a member, to mark the beginning of a context change
  737. * operation. The sched mutex of the worker must be held before calling
  738. * this function.
  739. *
  740. * Must be called with worker's sched_mutex held.
  741. */
  742. static inline void _starpu_worker_enter_changing_ctx_op(struct _starpu_worker * const worker)
  743. {
  744. STARPU_ASSERT(!starpu_pthread_equal(worker->thread_changing_ctx, starpu_pthread_self()));
  745. /* flush pending requests to start on a fresh transaction epoch */
  746. while (worker->state_changing_ctx_notice)
  747. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  748. /* announce changing_ctx intent
  749. *
  750. * - an already started sched_op is allowed to complete
  751. * - no new sched_op may be started
  752. */
  753. worker->state_changing_ctx_notice = 1;
  754. worker->thread_changing_ctx = starpu_pthread_self();
  755. /* allow for an already started sched_op to complete */
  756. if (worker->state_sched_op_pending)
  757. {
  758. /* request sched_op to broadcast when way is cleared */
  759. worker->state_changing_ctx_waiting = 1;
  760. /* wait for sched_op completion */
  761. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  762. #ifdef STARPU_SIMGRID
  763. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[worker->workerid]);
  764. #endif
  765. do
  766. {
  767. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  768. }
  769. while (worker->state_sched_op_pending);
  770. /* reset flag so other sched_ops wont have to broadcast state */
  771. worker->state_changing_ctx_waiting = 0;
  772. }
  773. }
  774. /** Mark the end of a context change operation.
  775. *
  776. * Must be called with worker's sched_mutex held.
  777. */
  778. static inline void _starpu_worker_leave_changing_ctx_op(struct _starpu_worker * const worker)
  779. {
  780. worker->thread_changing_ctx = (starpu_pthread_t)0;
  781. worker->state_changing_ctx_notice = 0;
  782. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  783. }
  784. #ifdef STARPU_SPINLOCK_CHECK
  785. #define _starpu_worker_relax_on() __starpu_worker_relax_on(__FILE__, __LINE__, __starpu_func__)
  786. static inline void __starpu_worker_relax_on(const char*file, int line, const char* func)
  787. #else
  788. /** Temporarily allow other worker to access current worker state, when still scheduling,
  789. * but the scheduling has not yet been made or is already done */
  790. static inline void _starpu_worker_relax_on(void)
  791. #endif
  792. {
  793. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  794. if (worker == NULL)
  795. return;
  796. if (!worker->state_sched_op_pending)
  797. return;
  798. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  799. #ifdef STARPU_SPINLOCK_CHECK
  800. STARPU_ASSERT_MSG(worker->state_relax_refcnt<UINT_MAX, "relax last turn on in %s (%s:%d)\n", worker->relax_on_func, worker->relax_on_file, worker->relax_on_line);
  801. #else
  802. STARPU_ASSERT(worker->state_relax_refcnt<UINT_MAX);
  803. #endif
  804. worker->state_relax_refcnt++;
  805. #ifdef STARPU_SPINLOCK_CHECK
  806. worker->relax_on_file = file;
  807. worker->relax_on_line = line;
  808. worker->relax_on_func = func;
  809. #endif
  810. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  811. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  812. }
  813. #define starpu_worker_relax_on _starpu_worker_relax_on
  814. #ifdef STARPU_SPINLOCK_CHECK
  815. #define _starpu_worker_relax_on_locked(worker) __starpu_worker_relax_on_locked(worker,__FILE__, __LINE__, __starpu_func__)
  816. static inline void __starpu_worker_relax_on_locked(struct _starpu_worker *worker, const char*file, int line, const char* func)
  817. #else
  818. /** Same, but with current worker mutex already held */
  819. static inline void _starpu_worker_relax_on_locked(struct _starpu_worker *worker)
  820. #endif
  821. {
  822. if (!worker->state_sched_op_pending)
  823. return;
  824. #ifdef STARPU_SPINLOCK_CHECK
  825. STARPU_ASSERT_MSG(worker->state_relax_refcnt<UINT_MAX, "relax last turn on in %s (%s:%d)\n", worker->relax_on_func, worker->relax_on_file, worker->relax_on_line);
  826. #else
  827. STARPU_ASSERT(worker->state_relax_refcnt<UINT_MAX);
  828. #endif
  829. worker->state_relax_refcnt++;
  830. #ifdef STARPU_SPINLOCK_CHECK
  831. worker->relax_on_file = file;
  832. worker->relax_on_line = line;
  833. worker->relax_on_func = func;
  834. #endif
  835. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  836. }
  837. #ifdef STARPU_SPINLOCK_CHECK
  838. #define _starpu_worker_relax_off() __starpu_worker_relax_off(__FILE__, __LINE__, __starpu_func__)
  839. static inline void __starpu_worker_relax_off(const char*file, int line, const char* func)
  840. #else
  841. static inline void _starpu_worker_relax_off(void)
  842. #endif
  843. {
  844. int workerid = starpu_worker_get_id();
  845. if (workerid == -1)
  846. return;
  847. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  848. STARPU_ASSERT(worker != NULL);
  849. if (!worker->state_sched_op_pending)
  850. return;
  851. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  852. #ifdef STARPU_SPINLOCK_CHECK
  853. STARPU_ASSERT_MSG(worker->state_relax_refcnt>0, "relax last turn off in %s (%s:%d)\n", worker->relax_on_func, worker->relax_on_file, worker->relax_on_line);
  854. #else
  855. STARPU_ASSERT(worker->state_relax_refcnt>0);
  856. #endif
  857. worker->state_relax_refcnt--;
  858. #ifdef STARPU_SPINLOCK_CHECK
  859. worker->relax_off_file = file;
  860. worker->relax_off_line = line;
  861. worker->relax_off_func = func;
  862. #endif
  863. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  864. }
  865. #define starpu_worker_relax_off _starpu_worker_relax_off
  866. #ifdef STARPU_SPINLOCK_CHECK
  867. #define _starpu_worker_relax_off_locked() __starpu_worker_relax_off_locked(__FILE__, __LINE__, __starpu_func__)
  868. static inline void __starpu_worker_relax_off_locked(const char*file, int line, const char* func)
  869. #else
  870. static inline void _starpu_worker_relax_off_locked(void)
  871. #endif
  872. {
  873. int workerid = starpu_worker_get_id();
  874. if (workerid == -1)
  875. return;
  876. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  877. STARPU_ASSERT(worker != NULL);
  878. if (!worker->state_sched_op_pending)
  879. return;
  880. #ifdef STARPU_SPINLOCK_CHECK
  881. STARPU_ASSERT_MSG(worker->state_relax_refcnt>0, "relax last turn off in %s (%s:%d)\n", worker->relax_on_func, worker->relax_on_file, worker->relax_on_line);
  882. #else
  883. STARPU_ASSERT(worker->state_relax_refcnt>0);
  884. #endif
  885. worker->state_relax_refcnt--;
  886. #ifdef STARPU_SPINLOCK_CHECK
  887. worker->relax_off_file = file;
  888. worker->relax_off_line = line;
  889. worker->relax_off_func = func;
  890. #endif
  891. }
  892. static inline int _starpu_worker_get_relax_state(void)
  893. {
  894. int workerid = starpu_worker_get_id();
  895. if (workerid < 0)
  896. return 1;
  897. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  898. STARPU_ASSERT(worker != NULL);
  899. return worker->state_relax_refcnt != 0;
  900. }
  901. #define starpu_worker_get_relax_state _starpu_worker_get_relax_state
  902. /** lock a worker for observing contents
  903. *
  904. * notes:
  905. * - if the observed worker is not in state_relax_refcnt, the function block until the state is reached */
  906. static inline void _starpu_worker_lock(int workerid)
  907. {
  908. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  909. STARPU_ASSERT(worker != NULL);
  910. int cur_workerid = starpu_worker_get_id();
  911. if (workerid != cur_workerid)
  912. {
  913. starpu_worker_relax_on();
  914. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  915. while (!worker->state_relax_refcnt)
  916. {
  917. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  918. }
  919. }
  920. else
  921. {
  922. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  923. }
  924. }
  925. #define starpu_worker_lock _starpu_worker_lock
  926. static inline int _starpu_worker_trylock(int workerid)
  927. {
  928. struct _starpu_worker *cur_worker = _starpu_get_local_worker_key();
  929. int cur_workerid = cur_worker->workerid;
  930. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  931. STARPU_ASSERT(worker != NULL);
  932. /* Start with ourself */
  933. int ret = STARPU_PTHREAD_MUTEX_TRYLOCK_SCHED(&cur_worker->sched_mutex);
  934. if (ret)
  935. return ret;
  936. if (workerid == cur_workerid)
  937. /* We only needed to lock ourself */
  938. return 0;
  939. /* Now try to lock the other worker */
  940. ret = STARPU_PTHREAD_MUTEX_TRYLOCK_SCHED(&worker->sched_mutex);
  941. if (!ret)
  942. {
  943. /* Good, check that it is relaxed */
  944. ret = !worker->state_relax_refcnt;
  945. if (ret)
  946. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  947. }
  948. if (!ret)
  949. _starpu_worker_relax_on_locked(cur_worker);
  950. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&cur_worker->sched_mutex);
  951. return ret;
  952. }
  953. #define starpu_worker_trylock _starpu_worker_trylock
  954. static inline void _starpu_worker_unlock(int workerid)
  955. {
  956. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  957. STARPU_ASSERT(worker != NULL);
  958. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  959. int cur_workerid = starpu_worker_get_id();
  960. if (workerid != cur_workerid)
  961. {
  962. starpu_worker_relax_off();
  963. }
  964. }
  965. #define starpu_worker_unlock _starpu_worker_unlock
  966. static inline void _starpu_worker_lock_self(void)
  967. {
  968. int workerid = starpu_worker_get_id_check();
  969. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  970. STARPU_ASSERT(worker != NULL);
  971. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  972. }
  973. #define starpu_worker_lock_self _starpu_worker_lock_self
  974. static inline void _starpu_worker_unlock_self(void)
  975. {
  976. int workerid = starpu_worker_get_id_check();
  977. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  978. STARPU_ASSERT(worker != NULL);
  979. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  980. }
  981. #define starpu_worker_unlock_self _starpu_worker_unlock_self
  982. static inline int _starpu_wake_worker_relax(int workerid)
  983. {
  984. _starpu_worker_lock(workerid);
  985. int ret = starpu_wake_worker_locked(workerid);
  986. _starpu_worker_unlock(workerid);
  987. return ret;
  988. }
  989. #define starpu_wake_worker_relax _starpu_wake_worker_relax
  990. int starpu_wake_worker_relax_light(int workerid);
  991. /**
  992. * Allow a worker pulling a task it cannot execute to properly refuse it and
  993. * send it back to the scheduler.
  994. */
  995. void _starpu_worker_refuse_task(struct _starpu_worker *worker, struct starpu_task *task);
  996. void _starpu_set_catch_signals(int do_catch_signal);
  997. int _starpu_get_catch_signals(void);
  998. /** Performance Monitoring */
  999. static inline int _starpu_perf_counter_paused(void)
  1000. {
  1001. STARPU_RMB();
  1002. return STARPU_UNLIKELY(_starpu_config.perf_counter_pause_depth > 0);
  1003. }
  1004. /* @}*/
  1005. #endif // __WORKERS_H__