workers.h 42 KB

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