workers.h 43 KB

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