workers.h 42 KB

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