workers.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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 CPU cores, as detected by the topology code. May
  235. * be different from the actual number of CPU workers.
  236. */
  237. unsigned nhwcpus;
  238. /** Total number of PUs (i.e. threads), as detected by the topology code. May
  239. * be different from the actual number of PU workers.
  240. */
  241. unsigned nhwpus;
  242. /** Total number of CUDA devices, as detected. May be different
  243. * from the actual number of CUDA workers.
  244. */
  245. unsigned nhwcudagpus;
  246. /** Total number of OpenCL devices, as detected. May be
  247. * different from the actual number of OpenCL workers.
  248. */
  249. unsigned nhwopenclgpus;
  250. /** Total number of MPI nodes, as detected. May be different
  251. * from the actual number of node workers.
  252. */
  253. unsigned nhwmpi;
  254. /** Actual number of CPU workers used by StarPU. */
  255. unsigned ncpus;
  256. /** Actual number of CUDA GPUs used by StarPU. */
  257. unsigned ncudagpus;
  258. unsigned nworkerpercuda;
  259. int cuda_th_per_stream;
  260. int cuda_th_per_dev;
  261. /** Actual number of OpenCL workers used by StarPU. */
  262. unsigned nopenclgpus;
  263. /** Actual number of MPI workers used by StarPU. */
  264. unsigned nmpidevices;
  265. unsigned nhwmpidevices;
  266. unsigned nhwmpicores[STARPU_MAXMPIDEVS]; /**< Each MPI node has its set of cores. */
  267. unsigned nmpicores[STARPU_MAXMPIDEVS];
  268. /** Topology of MP nodes (MIC) as well as necessary
  269. * objects to communicate with them. */
  270. unsigned nhwmicdevices;
  271. unsigned nmicdevices;
  272. unsigned nhwmiccores[STARPU_MAXMICDEVS]; /**< Each MIC node has its set of cores. */
  273. unsigned nmiccores[STARPU_MAXMICDEVS];
  274. /** Indicates the successive logical PU identifier that should be used
  275. * to bind the workers. It is either filled according to the
  276. * user's explicit parameters (from starpu_conf) or according
  277. * to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  278. * round-robin policy is used to distributed the workers over
  279. * the cores.
  280. */
  281. unsigned workers_bindid[STARPU_NMAXWORKERS];
  282. /** Indicates the successive CUDA identifier that should be
  283. * used by the CUDA driver. It is either filled according to
  284. * the user's explicit parameters (from starpu_conf) or
  285. * according to the STARPU_WORKERS_CUDAID env. variable.
  286. * Otherwise, they are taken in ID order.
  287. */
  288. unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS];
  289. /** Indicates the successive OpenCL identifier that should be
  290. * used by the OpenCL driver. It is either filled according
  291. * to the user's explicit parameters (from starpu_conf) or
  292. * according to the STARPU_WORKERS_OPENCLID env. variable.
  293. * Otherwise, they are taken in ID order.
  294. */
  295. unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS];
  296. /*** Indicates the successive MIC devices that should be used
  297. * by the MIC driver. It is either filled according to the
  298. * user's explicit parameters (from starpu_conf) or according
  299. * to the STARPU_WORKERS_MICID env. variable. Otherwise, they
  300. * are taken in ID order. */
  301. /** TODO */
  302. /** unsigned workers_mic_deviceid[STARPU_NMAXWORKERS]; */
  303. unsigned workers_mpi_ms_deviceid[STARPU_NMAXWORKERS];
  304. };
  305. struct _starpu_machine_config
  306. {
  307. struct _starpu_machine_topology topology;
  308. #ifdef STARPU_HAVE_HWLOC
  309. int cpu_depth;
  310. int pu_depth;
  311. #endif
  312. /** Where to bind next worker ? */
  313. int current_bindid;
  314. char currently_bound[STARPU_NMAXWORKERS];
  315. char currently_shared[STARPU_NMAXWORKERS];
  316. /** Which GPU(s) do we use for CUDA ? */
  317. int current_cuda_gpuid;
  318. /** Which GPU(s) do we use for OpenCL ? */
  319. int current_opencl_gpuid;
  320. /** Which MIC do we use? */
  321. int current_mic_deviceid;
  322. /** Which MPI do we use? */
  323. int current_mpi_deviceid;
  324. /** Memory node for cpus, if only one */
  325. int cpus_nodeid;
  326. /** Memory node for CUDA, if only one */
  327. int cuda_nodeid;
  328. /** Memory node for OpenCL, if only one */
  329. int opencl_nodeid;
  330. /** Memory node for MIC, if only one */
  331. int mic_nodeid;
  332. /** Memory node for MPI, if only one */
  333. int mpi_nodeid;
  334. /** Separate out previous variables from per-worker data. */
  335. char padding1[STARPU_CACHELINE_SIZE];
  336. /** Basic workers : each of this worker is running its own driver and
  337. * can be combined with other basic workers. */
  338. struct _starpu_worker workers[STARPU_NMAXWORKERS];
  339. /** Combined workers: these worker are a combination of basic workers
  340. * that can run parallel tasks together. */
  341. struct _starpu_combined_worker combined_workers[STARPU_NMAX_COMBINEDWORKERS];
  342. starpu_pthread_mutex_t submitted_mutex;
  343. /** Separate out previous mutex from the rest of the data. */
  344. char padding2[STARPU_CACHELINE_SIZE];
  345. /** Translation table from bindid to worker IDs */
  346. struct
  347. {
  348. int *workerids;
  349. unsigned nworkers; /**< size of workerids */
  350. } *bindid_workers;
  351. unsigned nbindid; /**< size of bindid_workers */
  352. /** This bitmask indicates which kinds of worker are available. For
  353. * instance it is possible to test if there is a CUDA worker with
  354. * the result of (worker_mask & STARPU_CUDA). */
  355. uint32_t worker_mask;
  356. /** either the user given configuration passed to starpu_init or a default configuration */
  357. struct starpu_conf conf;
  358. /** this flag is set until the runtime is stopped */
  359. unsigned running;
  360. int disable_kernels;
  361. /** Number of calls to starpu_pause() - calls to starpu_resume(). When >0,
  362. * StarPU should pause. */
  363. int pause_depth;
  364. /** all the sched ctx of the current instance of starpu */
  365. struct _starpu_sched_ctx sched_ctxs[STARPU_NMAX_SCHED_CTXS+1];
  366. /** this flag is set until the application is finished submitting tasks */
  367. unsigned submitting;
  368. int watchdog_ok;
  369. /** When >0, StarPU should stop performance counters collection. */
  370. int perf_counter_pause_depth;
  371. };
  372. /** Provides information for a device driver */
  373. struct starpu_driver_info {
  374. const char *name_upper; /**< Name of worker type in upper case */
  375. const char *name_var; /**< Name of worker type for environment variables */
  376. const char *name_lower; /**< Name of worker type in lower case */
  377. enum starpu_node_kind memory_kind; /**< Kind of memory in device */
  378. double alpha; /**< Typical relative speed compared to a CPU core */
  379. };
  380. /** Device driver information, indexed by enum starpu_worker_archtype */
  381. extern struct starpu_driver_info starpu_driver_info[STARPU_NARCH];
  382. void starpu_driver_info_register(enum starpu_worker_archtype archtype, const struct starpu_driver_info *info);
  383. /** Provides information for a memory node driver */
  384. struct starpu_memory_driver_info {
  385. const char *name_upper; /**< Name of memory in upper case */
  386. enum starpu_worker_archtype worker_archtype; /**< Kind of device */
  387. };
  388. /** Memory driver information, indexed by enum starpu_node_kind */
  389. extern struct starpu_memory_driver_info starpu_memory_driver_info[STARPU_MAX_RAM+1];
  390. void starpu_memory_driver_info_register(enum starpu_node_kind kind, const struct starpu_memory_driver_info *info);
  391. extern int _starpu_worker_parallel_blocks;
  392. extern struct _starpu_machine_config _starpu_config STARPU_ATTRIBUTE_INTERNAL;
  393. extern int _starpu_keys_initialized STARPU_ATTRIBUTE_INTERNAL;
  394. extern starpu_pthread_key_t _starpu_worker_key STARPU_ATTRIBUTE_INTERNAL;
  395. extern starpu_pthread_key_t _starpu_worker_set_key STARPU_ATTRIBUTE_INTERNAL;
  396. /** Three functions to manage argv, argc */
  397. void _starpu_set_argc_argv(int *argc, char ***argv);
  398. int *_starpu_get_argc();
  399. char ***_starpu_get_argv();
  400. /** Fill conf with environment variables */
  401. void _starpu_conf_check_environment(struct starpu_conf *conf);
  402. /** Called by the driver when it is ready to pause */
  403. void _starpu_may_pause(void);
  404. /** Has starpu_shutdown already been called ? */
  405. static inline unsigned _starpu_machine_is_running(void)
  406. {
  407. unsigned ret;
  408. /* running is just protected by a memory barrier */
  409. STARPU_RMB();
  410. ANNOTATE_HAPPENS_AFTER(&_starpu_config.running);
  411. ret = _starpu_config.running;
  412. ANNOTATE_HAPPENS_BEFORE(&_starpu_config.running);
  413. return ret;
  414. }
  415. /** initialise a worker */
  416. void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu_machine_config *pconfig);
  417. /** Check if there is a worker that may execute the task. */
  418. uint32_t _starpu_worker_exists(struct starpu_task *);
  419. /** Is there a worker that can execute CUDA code ? */
  420. uint32_t _starpu_can_submit_cuda_task(void);
  421. /** Is there a worker that can execute CPU code ? */
  422. uint32_t _starpu_can_submit_cpu_task(void);
  423. /** Is there a worker that can execute OpenCL code ? */
  424. uint32_t _starpu_can_submit_opencl_task(void);
  425. /** Check whether there is anything that the worker should do instead of
  426. * sleeping (waiting on something to happen). */
  427. unsigned _starpu_worker_can_block(unsigned memnode, struct _starpu_worker *worker);
  428. /** This function must be called to block a worker. It puts the worker in a
  429. * sleeping state until there is some event that forces the worker to wake up.
  430. * */
  431. void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex);
  432. /** This function initializes the current driver for the given worker */
  433. void _starpu_driver_start(struct _starpu_worker *worker, enum starpu_worker_archtype archtype, unsigned sync);
  434. /** This function initializes the current thread for the given worker */
  435. void _starpu_worker_start(struct _starpu_worker *worker, enum starpu_worker_archtype archtype, unsigned sync);
  436. static inline unsigned _starpu_worker_get_count(void)
  437. {
  438. return _starpu_config.topology.nworkers;
  439. }
  440. #define starpu_worker_get_count _starpu_worker_get_count
  441. /** The _starpu_worker structure describes all the state of a StarPU worker.
  442. * This function sets the pthread key which stores a pointer to this structure.
  443. * */
  444. static inline void _starpu_set_local_worker_key(struct _starpu_worker *worker)
  445. {
  446. STARPU_ASSERT(_starpu_keys_initialized);
  447. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_key, worker);
  448. }
  449. /** Returns the _starpu_worker structure that describes the state of the
  450. * current worker. */
  451. static inline struct _starpu_worker *_starpu_get_local_worker_key(void)
  452. {
  453. if (!_starpu_keys_initialized)
  454. return NULL;
  455. return (struct _starpu_worker *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_key);
  456. }
  457. /** The _starpu_worker_set structure describes all the state of a StarPU worker_set.
  458. * This function sets the pthread key which stores a pointer to this structure.
  459. * */
  460. static inline void _starpu_set_local_worker_set_key(struct _starpu_worker_set *worker)
  461. {
  462. STARPU_ASSERT(_starpu_keys_initialized);
  463. STARPU_PTHREAD_SETSPECIFIC(_starpu_worker_set_key, worker);
  464. }
  465. /** Returns the _starpu_worker_set structure that describes the state of the
  466. * current worker_set. */
  467. static inline struct _starpu_worker_set *_starpu_get_local_worker_set_key(void)
  468. {
  469. if (!_starpu_keys_initialized)
  470. return NULL;
  471. return (struct _starpu_worker_set *) STARPU_PTHREAD_GETSPECIFIC(_starpu_worker_set_key);
  472. }
  473. /** Returns the _starpu_worker structure that describes the state of the
  474. * specified worker. */
  475. static inline struct _starpu_worker *_starpu_get_worker_struct(unsigned id)
  476. {
  477. STARPU_ASSERT(id < starpu_worker_get_count());
  478. return &_starpu_config.workers[id];
  479. }
  480. /** Returns the starpu_sched_ctx structure that describes the state of the
  481. * specified ctx */
  482. static inline struct _starpu_sched_ctx *_starpu_get_sched_ctx_struct(unsigned id)
  483. {
  484. return (id > STARPU_NMAX_SCHED_CTXS) ? NULL : &_starpu_config.sched_ctxs[id];
  485. }
  486. struct _starpu_combined_worker *_starpu_get_combined_worker_struct(unsigned id);
  487. /** Returns the structure that describes the overall machine configuration (eg.
  488. * all workers and topology). */
  489. static inline struct _starpu_machine_config *_starpu_get_machine_config(void)
  490. {
  491. return &_starpu_config;
  492. }
  493. /** Return whether kernels should be run (<=0) or not (>0) */
  494. static inline int _starpu_get_disable_kernels(void)
  495. {
  496. return _starpu_config.disable_kernels;
  497. }
  498. /** Retrieve the status which indicates what the worker is currently doing. */
  499. static inline enum _starpu_worker_status _starpu_worker_get_status(int workerid)
  500. {
  501. return _starpu_config.workers[workerid].status;
  502. }
  503. /** Change the status of the worker which indicates what the worker is currently
  504. * doing (eg. executing a callback). */
  505. static inline void _starpu_worker_set_status(int workerid, enum _starpu_worker_status status)
  506. {
  507. _starpu_config.workers[workerid].status = status;
  508. }
  509. /** We keep an initial sched ctx which might be used in case no other ctx is available */
  510. static inline struct _starpu_sched_ctx* _starpu_get_initial_sched_ctx(void)
  511. {
  512. return &_starpu_config.sched_ctxs[STARPU_GLOBAL_SCHED_CTX];
  513. }
  514. int starpu_worker_get_nids_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  515. /** returns workers not belonging to any context, be careful no mutex is used,
  516. the list might not be updated */
  517. int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, int *workerids, int maxsize);
  518. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex)
  519. {
  520. struct _starpu_worker *w = _starpu_get_worker_struct(workerid);
  521. return &w->sched_mutex == mutex;
  522. }
  523. static inline int _starpu_worker_get_nsched_ctxs(int workerid)
  524. {
  525. return _starpu_config.workers[workerid].nsched_ctxs;
  526. }
  527. /** Get the total number of sched_ctxs created till now */
  528. static inline unsigned _starpu_get_nsched_ctxs(void)
  529. {
  530. /* topology.nsched_ctxs may be increased asynchronously in sched_ctx_create */
  531. STARPU_RMB();
  532. return _starpu_config.topology.nsched_ctxs;
  533. }
  534. /** Inlined version when building the core. */
  535. static inline int _starpu_worker_get_id(void)
  536. {
  537. struct _starpu_worker * worker;
  538. worker = _starpu_get_local_worker_key();
  539. if (worker)
  540. {
  541. return worker->workerid;
  542. }
  543. else
  544. {
  545. /* there is no worker associated to that thread, perhaps it is
  546. * a thread from the application or this is some SPU worker */
  547. return -1;
  548. }
  549. }
  550. #define starpu_worker_get_id _starpu_worker_get_id
  551. /** Similar behaviour to starpu_worker_get_id() but fails when called from outside a worker */
  552. /** This returns an unsigned object on purpose, so that the caller is sure to get a positive value */
  553. static inline unsigned __starpu_worker_get_id_check(const char *f, int l)
  554. {
  555. (void) l;
  556. (void) f;
  557. int id = starpu_worker_get_id();
  558. STARPU_ASSERT_MSG(id>=0, "%s:%d Cannot be called from outside a worker\n", f, l);
  559. return id;
  560. }
  561. #define _starpu_worker_get_id_check(f,l) __starpu_worker_get_id_check(f,l)
  562. enum starpu_node_kind _starpu_worker_get_node_kind(enum starpu_worker_archtype type);
  563. void _starpu_worker_set_stream_ctx(unsigned workerid, struct _starpu_sched_ctx *sched_ctx);
  564. struct _starpu_sched_ctx* _starpu_worker_get_ctx_stream(unsigned stream_workerid);
  565. /** Send a request to the worker to block, before a parallel task is about to
  566. * begin.
  567. *
  568. * Must be called with worker's sched_mutex held.
  569. */
  570. static inline void _starpu_worker_request_blocking_in_parallel(struct _starpu_worker * const worker)
  571. {
  572. _starpu_worker_parallel_blocks = 1;
  573. /* flush pending requests to start on a fresh transaction epoch */
  574. while (worker->state_unblock_in_parallel_req)
  575. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  576. /* announce blocking intent */
  577. STARPU_ASSERT(worker->block_in_parallel_ref_count < UINT_MAX);
  578. worker->block_in_parallel_ref_count++;
  579. if (worker->block_in_parallel_ref_count == 1)
  580. {
  581. /* only the transition from 0 to 1 triggers the block_in_parallel_req */
  582. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  583. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  584. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  585. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  586. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  587. /* trigger the block_in_parallel_req */
  588. worker->state_block_in_parallel_req = 1;
  589. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  590. #ifdef STARPU_SIMGRID
  591. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[worker->workerid]);
  592. #endif
  593. /* wait for block_in_parallel_req to be processed */
  594. while (!worker->state_block_in_parallel_ack)
  595. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  596. STARPU_ASSERT(worker->block_in_parallel_ref_count >= 1);
  597. STARPU_ASSERT(worker->state_block_in_parallel_req);
  598. STARPU_ASSERT(worker->state_blocked_in_parallel);
  599. /* reset block_in_parallel_req state flags */
  600. worker->state_block_in_parallel_req = 0;
  601. worker->state_block_in_parallel_ack = 0;
  602. /* broadcast block_in_parallel_req state flags reset */
  603. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  604. }
  605. }
  606. /** Send a request to the worker to unblock, after a parallel task is complete.
  607. *
  608. * Must be called with worker's sched_mutex held.
  609. */
  610. static inline void _starpu_worker_request_unblocking_in_parallel(struct _starpu_worker * const worker)
  611. {
  612. /* flush pending requests to start on a fresh transaction epoch */
  613. while (worker->state_block_in_parallel_req)
  614. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  615. /* unblocking may be requested unconditionnally
  616. * thus, check is unblocking is really needed */
  617. if (worker->state_blocked_in_parallel)
  618. {
  619. if (worker->block_in_parallel_ref_count == 1)
  620. {
  621. /* only the transition from 1 to 0 triggers the unblock_in_parallel_req */
  622. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  623. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  624. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  625. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  626. /* trigger the unblock_in_parallel_req */
  627. worker->state_unblock_in_parallel_req = 1;
  628. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  629. /* wait for the unblock_in_parallel_req to be processed */
  630. while (!worker->state_unblock_in_parallel_ack)
  631. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  632. STARPU_ASSERT(worker->state_unblock_in_parallel_req);
  633. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  634. /* reset unblock_in_parallel_req state flags */
  635. worker->state_unblock_in_parallel_req = 0;
  636. worker->state_unblock_in_parallel_ack = 0;
  637. /* broadcast unblock_in_parallel_req state flags reset */
  638. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  639. }
  640. /* announce unblocking complete */
  641. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  642. worker->block_in_parallel_ref_count--;
  643. }
  644. }
  645. /** Called by the the worker to process incoming requests to block or unblock on
  646. * parallel task boundaries.
  647. *
  648. * Must be called with worker's sched_mutex held.
  649. */
  650. static inline void _starpu_worker_process_block_in_parallel_requests(struct _starpu_worker * const worker)
  651. {
  652. while (worker->state_block_in_parallel_req)
  653. {
  654. STARPU_ASSERT(!worker->state_blocked_in_parallel);
  655. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  656. STARPU_ASSERT(!worker->state_unblock_in_parallel_req);
  657. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  658. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  659. /* enter effective blocked state */
  660. worker->state_blocked_in_parallel = 1;
  661. /* notify block_in_parallel_req processing */
  662. worker->state_block_in_parallel_ack = 1;
  663. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  664. /* block */
  665. while (!worker->state_unblock_in_parallel_req)
  666. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  667. STARPU_ASSERT(worker->state_blocked_in_parallel);
  668. STARPU_ASSERT(!worker->state_block_in_parallel_req);
  669. STARPU_ASSERT(!worker->state_block_in_parallel_ack);
  670. STARPU_ASSERT(!worker->state_unblock_in_parallel_ack);
  671. STARPU_ASSERT(worker->block_in_parallel_ref_count > 0);
  672. /* leave effective blocked state */
  673. worker->state_blocked_in_parallel = 0;
  674. /* notify unblock_in_parallel_req processing */
  675. worker->state_unblock_in_parallel_ack = 1;
  676. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  677. }
  678. }
  679. #ifdef STARPU_SPINLOCK_CHECK
  680. #define _starpu_worker_enter_sched_op(worker) __starpu_worker_enter_sched_op((worker), __FILE__, __LINE__, __starpu_func__)
  681. static inline void __starpu_worker_enter_sched_op(struct _starpu_worker * const worker, const char*file, int line, const char* func)
  682. #else
  683. /** Mark the beginning of a scheduling operation by the worker. No worker
  684. * blocking operations on parallel tasks and no scheduling context change
  685. * operations must be performed on contexts containing the worker, on
  686. * contexts about to add the worker and on contexts about to remove the
  687. * worker, while the scheduling operation is in process. The sched mutex
  688. * of the worker may only be acquired permanently by another thread when
  689. * no scheduling operation is in process, or when a scheduling operation
  690. * is in process _and_ worker->state_relax_refcnt!=0. If a
  691. * scheduling operation is in process _and_
  692. * worker->state_relax_refcnt==0, a thread other than the worker
  693. * must wait on condition worker->sched_cond for
  694. * worker->state_relax_refcnt!=0 to become true, before acquiring
  695. * the worker sched mutex permanently.
  696. *
  697. * Must be called with worker's sched_mutex held.
  698. */
  699. static inline void _starpu_worker_enter_sched_op(struct _starpu_worker * const worker)
  700. #endif
  701. {
  702. STARPU_ASSERT(!worker->state_sched_op_pending);
  703. if (!worker->state_blocked_in_parallel_observed)
  704. {
  705. /* process pending block requests before entering a sched_op region */
  706. _starpu_worker_process_block_in_parallel_requests(worker);
  707. while (worker->state_changing_ctx_notice)
  708. {
  709. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  710. /* new block requests may have been triggered during the wait,
  711. * need to check again */
  712. _starpu_worker_process_block_in_parallel_requests(worker);
  713. }
  714. }
  715. else
  716. {
  717. /* if someone observed the worker state since the last call, postpone block request
  718. * processing for one sched_op turn more, because the observer will not have seen
  719. * new block requests between its observation and now.
  720. *
  721. * however, the worker still has to wait for context change operations to complete
  722. * before entering sched_op again*/
  723. while (worker->state_changing_ctx_notice)
  724. {
  725. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  726. }
  727. }
  728. /* no block request and no ctx change ahead,
  729. * enter sched_op */
  730. worker->state_sched_op_pending = 1;
  731. worker->state_blocked_in_parallel_observed = 0;
  732. worker->state_relax_refcnt = 0;
  733. #ifdef STARPU_SPINLOCK_CHECK
  734. worker->relax_on_file = file;
  735. worker->relax_on_line = line;
  736. worker->relax_on_func = func;
  737. #endif
  738. }
  739. void _starpu_worker_apply_deferred_ctx_changes(void);
  740. #ifdef STARPU_SPINLOCK_CHECK
  741. #define _starpu_worker_leave_sched_op(worker) __starpu_worker_leave_sched_op((worker), __FILE__, __LINE__, __starpu_func__)
  742. static inline void __starpu_worker_leave_sched_op(struct _starpu_worker * const worker, const char*file, int line, const char* func)
  743. #else
  744. /** Mark the end of a scheduling operation by the worker.
  745. *
  746. * Must be called with worker's sched_mutex held.
  747. */
  748. static inline void _starpu_worker_leave_sched_op(struct _starpu_worker * const worker)
  749. #endif
  750. {
  751. STARPU_ASSERT(worker->state_sched_op_pending);
  752. worker->state_relax_refcnt = 1;
  753. #ifdef STARPU_SPINLOCK_CHECK
  754. worker->relax_off_file = file;
  755. worker->relax_off_line = line;
  756. worker->relax_off_func = func;
  757. #endif
  758. worker->state_sched_op_pending = 0;
  759. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  760. _starpu_worker_apply_deferred_ctx_changes();
  761. }
  762. static inline int _starpu_worker_sched_op_pending(void)
  763. {
  764. int workerid = starpu_worker_get_id();
  765. if (workerid == -1)
  766. return 0;
  767. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  768. STARPU_ASSERT(worker != NULL);
  769. return worker->state_sched_op_pending;
  770. }
  771. /** Must be called before altering a context related to the worker
  772. * whether about adding the worker to a context, removing it from a
  773. * context or modifying the set of workers of a context of which the
  774. * worker is a member, to mark the beginning of a context change
  775. * operation. The sched mutex of the worker must be held before calling
  776. * this function.
  777. *
  778. * Must be called with worker's sched_mutex held.
  779. */
  780. static inline void _starpu_worker_enter_changing_ctx_op(struct _starpu_worker * const worker)
  781. {
  782. STARPU_ASSERT(!starpu_pthread_equal(worker->thread_changing_ctx, starpu_pthread_self()));
  783. /* flush pending requests to start on a fresh transaction epoch */
  784. while (worker->state_changing_ctx_notice)
  785. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  786. /* announce changing_ctx intent
  787. *
  788. * - an already started sched_op is allowed to complete
  789. * - no new sched_op may be started
  790. */
  791. worker->state_changing_ctx_notice = 1;
  792. worker->thread_changing_ctx = starpu_pthread_self();
  793. /* allow for an already started sched_op to complete */
  794. if (worker->state_sched_op_pending)
  795. {
  796. /* request sched_op to broadcast when way is cleared */
  797. worker->state_changing_ctx_waiting = 1;
  798. /* wait for sched_op completion */
  799. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  800. #ifdef STARPU_SIMGRID
  801. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[worker->workerid]);
  802. #endif
  803. do
  804. {
  805. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  806. }
  807. while (worker->state_sched_op_pending);
  808. /* reset flag so other sched_ops wont have to broadcast state */
  809. worker->state_changing_ctx_waiting = 0;
  810. }
  811. }
  812. /** Mark the end of a context change operation.
  813. *
  814. * Must be called with worker's sched_mutex held.
  815. */
  816. static inline void _starpu_worker_leave_changing_ctx_op(struct _starpu_worker * const worker)
  817. {
  818. worker->thread_changing_ctx = (starpu_pthread_t)0;
  819. worker->state_changing_ctx_notice = 0;
  820. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  821. }
  822. #ifdef STARPU_SPINLOCK_CHECK
  823. #define _starpu_worker_relax_on() __starpu_worker_relax_on(__FILE__, __LINE__, __starpu_func__)
  824. static inline void __starpu_worker_relax_on(const char*file, int line, const char* func)
  825. #else
  826. /** Temporarily allow other worker to access current worker state, when still scheduling,
  827. * but the scheduling has not yet been made or is already done */
  828. static inline void _starpu_worker_relax_on(void)
  829. #endif
  830. {
  831. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  832. if (worker == NULL)
  833. return;
  834. if (!worker->state_sched_op_pending)
  835. return;
  836. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  837. #ifdef STARPU_SPINLOCK_CHECK
  838. 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);
  839. #else
  840. STARPU_ASSERT(worker->state_relax_refcnt<UINT_MAX);
  841. #endif
  842. worker->state_relax_refcnt++;
  843. #ifdef STARPU_SPINLOCK_CHECK
  844. worker->relax_on_file = file;
  845. worker->relax_on_line = line;
  846. worker->relax_on_func = func;
  847. #endif
  848. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  849. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  850. }
  851. #define starpu_worker_relax_on _starpu_worker_relax_on
  852. #ifdef STARPU_SPINLOCK_CHECK
  853. #define _starpu_worker_relax_on_locked(worker) __starpu_worker_relax_on_locked(worker,__FILE__, __LINE__, __starpu_func__)
  854. static inline void __starpu_worker_relax_on_locked(struct _starpu_worker *worker, const char*file, int line, const char* func)
  855. #else
  856. /** Same, but with current worker mutex already held */
  857. static inline void _starpu_worker_relax_on_locked(struct _starpu_worker *worker)
  858. #endif
  859. {
  860. if (!worker->state_sched_op_pending)
  861. return;
  862. #ifdef STARPU_SPINLOCK_CHECK
  863. 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);
  864. #else
  865. STARPU_ASSERT(worker->state_relax_refcnt<UINT_MAX);
  866. #endif
  867. worker->state_relax_refcnt++;
  868. #ifdef STARPU_SPINLOCK_CHECK
  869. worker->relax_on_file = file;
  870. worker->relax_on_line = line;
  871. worker->relax_on_func = func;
  872. #endif
  873. STARPU_PTHREAD_COND_BROADCAST(&worker->sched_cond);
  874. }
  875. #ifdef STARPU_SPINLOCK_CHECK
  876. #define _starpu_worker_relax_off() __starpu_worker_relax_off(__FILE__, __LINE__, __starpu_func__)
  877. static inline void __starpu_worker_relax_off(const char*file, int line, const char* func)
  878. #else
  879. static inline void _starpu_worker_relax_off(void)
  880. #endif
  881. {
  882. int workerid = starpu_worker_get_id();
  883. if (workerid == -1)
  884. return;
  885. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  886. STARPU_ASSERT(worker != NULL);
  887. if (!worker->state_sched_op_pending)
  888. return;
  889. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  890. #ifdef STARPU_SPINLOCK_CHECK
  891. 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);
  892. #else
  893. STARPU_ASSERT(worker->state_relax_refcnt>0);
  894. #endif
  895. worker->state_relax_refcnt--;
  896. #ifdef STARPU_SPINLOCK_CHECK
  897. worker->relax_off_file = file;
  898. worker->relax_off_line = line;
  899. worker->relax_off_func = func;
  900. #endif
  901. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  902. }
  903. #define starpu_worker_relax_off _starpu_worker_relax_off
  904. #ifdef STARPU_SPINLOCK_CHECK
  905. #define _starpu_worker_relax_off_locked() __starpu_worker_relax_off_locked(__FILE__, __LINE__, __starpu_func__)
  906. static inline void __starpu_worker_relax_off_locked(const char*file, int line, const char* func)
  907. #else
  908. static inline void _starpu_worker_relax_off_locked(void)
  909. #endif
  910. {
  911. int workerid = starpu_worker_get_id();
  912. if (workerid == -1)
  913. return;
  914. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  915. STARPU_ASSERT(worker != NULL);
  916. if (!worker->state_sched_op_pending)
  917. return;
  918. #ifdef STARPU_SPINLOCK_CHECK
  919. 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);
  920. #else
  921. STARPU_ASSERT(worker->state_relax_refcnt>0);
  922. #endif
  923. worker->state_relax_refcnt--;
  924. #ifdef STARPU_SPINLOCK_CHECK
  925. worker->relax_off_file = file;
  926. worker->relax_off_line = line;
  927. worker->relax_off_func = func;
  928. #endif
  929. }
  930. static inline int _starpu_worker_get_relax_state(void)
  931. {
  932. int workerid = starpu_worker_get_id();
  933. if (workerid < 0)
  934. return 1;
  935. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  936. STARPU_ASSERT(worker != NULL);
  937. return worker->state_relax_refcnt != 0;
  938. }
  939. #define starpu_worker_get_relax_state _starpu_worker_get_relax_state
  940. /** lock a worker for observing contents
  941. *
  942. * notes:
  943. * - if the observed worker is not in state_relax_refcnt, the function block until the state is reached */
  944. static inline void _starpu_worker_lock(int workerid)
  945. {
  946. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  947. STARPU_ASSERT(worker != NULL);
  948. int cur_workerid = starpu_worker_get_id();
  949. if (workerid != cur_workerid)
  950. {
  951. starpu_worker_relax_on();
  952. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  953. while (!worker->state_relax_refcnt)
  954. {
  955. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  956. }
  957. }
  958. else
  959. {
  960. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  961. }
  962. }
  963. #define starpu_worker_lock _starpu_worker_lock
  964. static inline int _starpu_worker_trylock(int workerid)
  965. {
  966. struct _starpu_worker *cur_worker = _starpu_get_local_worker_key();
  967. int cur_workerid = cur_worker->workerid;
  968. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  969. STARPU_ASSERT(worker != NULL);
  970. /* Start with ourself */
  971. int ret = STARPU_PTHREAD_MUTEX_TRYLOCK_SCHED(&cur_worker->sched_mutex);
  972. if (ret)
  973. return ret;
  974. if (workerid == cur_workerid)
  975. /* We only needed to lock ourself */
  976. return 0;
  977. /* Now try to lock the other worker */
  978. ret = STARPU_PTHREAD_MUTEX_TRYLOCK_SCHED(&worker->sched_mutex);
  979. if (!ret)
  980. {
  981. /* Good, check that it is relaxed */
  982. ret = !worker->state_relax_refcnt;
  983. if (ret)
  984. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  985. }
  986. if (!ret)
  987. _starpu_worker_relax_on_locked(cur_worker);
  988. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&cur_worker->sched_mutex);
  989. return ret;
  990. }
  991. #define starpu_worker_trylock _starpu_worker_trylock
  992. static inline void _starpu_worker_unlock(int workerid)
  993. {
  994. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  995. STARPU_ASSERT(worker != NULL);
  996. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  997. int cur_workerid = starpu_worker_get_id();
  998. if (workerid != cur_workerid)
  999. {
  1000. starpu_worker_relax_off();
  1001. }
  1002. }
  1003. #define starpu_worker_unlock _starpu_worker_unlock
  1004. static inline void _starpu_worker_lock_self(void)
  1005. {
  1006. int workerid = starpu_worker_get_id_check();
  1007. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1008. STARPU_ASSERT(worker != NULL);
  1009. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  1010. }
  1011. #define starpu_worker_lock_self _starpu_worker_lock_self
  1012. static inline void _starpu_worker_unlock_self(void)
  1013. {
  1014. int workerid = starpu_worker_get_id_check();
  1015. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1016. STARPU_ASSERT(worker != NULL);
  1017. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  1018. }
  1019. #define starpu_worker_unlock_self _starpu_worker_unlock_self
  1020. static inline int _starpu_wake_worker_relax(int workerid)
  1021. {
  1022. _starpu_worker_lock(workerid);
  1023. int ret = starpu_wake_worker_locked(workerid);
  1024. _starpu_worker_unlock(workerid);
  1025. return ret;
  1026. }
  1027. #define starpu_wake_worker_relax _starpu_wake_worker_relax
  1028. int starpu_wake_worker_relax_light(int workerid);
  1029. /**
  1030. * Allow a worker pulling a task it cannot execute to properly refuse it and
  1031. * send it back to the scheduler.
  1032. */
  1033. void _starpu_worker_refuse_task(struct _starpu_worker *worker, struct starpu_task *task);
  1034. void _starpu_set_catch_signals(int do_catch_signal);
  1035. int _starpu_get_catch_signals(void);
  1036. /** Performance Monitoring */
  1037. static inline int _starpu_perf_counter_paused(void)
  1038. {
  1039. STARPU_RMB();
  1040. return STARPU_UNLIKELY(_starpu_config.perf_counter_pause_depth > 0);
  1041. }
  1042. /* @}*/
  1043. #endif // __WORKERS_H__