workers.h 43 KB

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