workers.h 43 KB

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