010_core.doxy 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*! \page StarPUCore StarPU Core
  17. \section CoreEntities StarPU Core Entities
  18. TODO
  19. \subsection CoreEntitiesOverview Overview
  20. Execution entities:
  21. - <b>worker</b>: A worker (see \ref CoreEntitiesWorkers, \ref
  22. CoreEntitiesWorkersAndContexts) entity is a CPU thread created by StarPU to manage
  23. one computing unit. The computing unit can be a local CPU core, an accelerator
  24. or GPU device, or --- on the master side when running in master-slave
  25. distributed mode --- a remote slave computing node. It is responsible for
  26. querying scheduling policies for tasks to execute.
  27. - <b>sched_context</b>: A scheduling context (see \ref CoreEntitiesContexts, \ref
  28. CoreEntitiesWorkersAndContexts) is a logical set of workers governed by an
  29. instance of a scheduling policy. It defines the computing units to which the
  30. scheduling policy instance may assign work entities.
  31. - <b>driver</b>: A driver is the set of hardware-dependent routines used by a
  32. worker to initialize its associated computing unit, execute work entities on
  33. it, and finalize the computing unit usage at the end of the session.
  34. Work entities:
  35. - <b>task</b>: TODO
  36. - <b>job</b>: TODO
  37. Data entities:
  38. - <b>data handle</b>: TODO
  39. - <b>data replicate</b>: TODO
  40. \subsection CoreEntitiesWorkers Workers
  41. A <b>worker</b> is a CPU thread created by StarPU. Its role is to manage one computing
  42. unit. This computing unit can be a local CPU core, in which case, the worker
  43. thread manages the actual CPU core to which it is assigned; or it can be a
  44. computing device such as a GPU or an accelerator (or even a remote computing
  45. node when StarPU is running in distributed master-slave mode.) When a worker
  46. manages a computing device, the CPU core to which the worker's thread is
  47. by default exclusively assigned to the device management work and does not
  48. participate to computation.
  49. \subsubsection CoreEntitiesWorkersStates States
  50. <b>Scheduling operations related state</b>
  51. While a worker is conducting a scheduling operations, e.g. the worker is in the
  52. process of selecting a new task to execute, flag state_sched_op_pending is set
  53. to !0, otherwise it is set to 0.
  54. While state_sched_op_pending is !0, the following exhaustive list of operations on that
  55. workers are restricted in the stated way:
  56. - adding the worker to a context is not allowed;
  57. - removing the worker from a context is not allowed;
  58. - adding the worker to a parallel task team is not allowed;
  59. - removing the worker from a parallel task team is not allowed;
  60. - querying state information about the worker is only allowed while
  61. state_relax_refcnt > 0;
  62. - in particular, querying whether the worker is blocked on a parallel team entry is only
  63. allowed while state_relax_refcnt > 0.
  64. Entering and leaving the state_sched_op_pending state is done through calls to
  65. _starpu_worker_enter_sched_op() and _starpu_worker_leave_sched_op()
  66. respectively (see these functions in use in functions _starpu_get_worker_task() and
  67. _starpu_get_multi_worker_task()). These calls ensure that any pending
  68. conflicting operation deferred while the worker was in the
  69. state_sched_op_pending state is performed in an orderly manner.
  70. <br>
  71. <b>Scheduling contexts related states</b>
  72. Flag state_changing_ctx_notice is set to !0 when a thread is about to
  73. add to a scheduling context or remove it from a scheduling context, and is
  74. currently waiting for a safe window to do, until the targeted worker is not in a
  75. scheduling operation or parallel task operation anymore. This flag set to !0 will also
  76. prevent the targeted worker to attempt a fresh scheduling operation or parallel
  77. task operation to avoid starving conditions. However, a scheduling operation
  78. that was already in process before the notice is allowed to complete.
  79. Flag state_changing_ctx_waiting is set to !0 when a scheduling context worker
  80. addition or removal involving the targeted worker is about to occur and the
  81. worker is currently performing a scheduling operation to tell the targeted
  82. worker that the initiator thread is waiting for the scheduling operation to
  83. complete and should be woken up upon completion.
  84. <br>
  85. <b>Relaxed synchronization related states</b>
  86. Any StarPU worker may participate to scheduling operations, and in this process,
  87. may be forced to observe state information from other workers.
  88. A StarPU worker thread may therefore be observed by any thread, even
  89. other StarPU workers. Since workers may observe each other in any order, it is
  90. not possible to rely exclusively on the sched_mutex of each worker to protect the
  91. observation of worker state flags by other workers, because
  92. worker A observing worker B would involve locking workers in (A B) sequence,
  93. while worker B observing worker A would involve locking workers in (B A)
  94. sequence, leading to lock inversion deadlocks.
  95. In consequence, no thread must hold more than one worker's sched_mutex at any time.
  96. Instead, workers implement a relaxed locking scheme based on the state_relax_refcnt
  97. counter, itself protected by the worker's sched_mutex. When state_relax_refcnt
  98. > 0, the targeted worker state flags may be observed, otherwise the thread attempting
  99. >the observation must repeatedly wait on the targeted worker's sched_cond
  100. >condition until state_relax_refcnt > 0.
  101. The relaxed mode, while on, can actually be seen as a transactional consistency
  102. model, where concurrent accesses are authorized and potential conflicts are
  103. resolved after the fact. When the relaxed mode is off, the consistency model
  104. becomes a mutual exclusion model, where the sched_mutex of the worker must be
  105. held in order to access or change the worker state.
  106. <br>
  107. <b>Parallel tasks related states</b>
  108. When a worker is scheduled to participate to the execution of a parallel task,
  109. it must wait for the whole team of workers participating to the execution of
  110. this task to be ready. While the worker waits for its teammates, it is not
  111. available to run other tasks or perform other operations. Such a waiting
  112. operation can therefore not start while conflicting operations such as
  113. scheduling operations and scheduling context resizing involving the worker are
  114. on-going. Conversely these operations and other may query weather the worker is
  115. blocked on a parallel task entry with starpu_worker_is_blocked_in_parallel().
  116. The starpu_worker_is_blocked_in_parallel() function is allowed to proceed while
  117. and only while state_relax_refcnt > 0. Due to the relaxed worker locking scheme,
  118. the state_blocked_in_parallel flag of the targeted worker may change after it
  119. has been observed by an observer thread. In consequence, flag
  120. state_blocked_in_parallel_observed of the targeted worker is set to 1 by the
  121. observer immediately after the observation to "taint" the targeted worker. The
  122. targeted worker will clear the state_blocked_in_parallel_observed flag tainting
  123. and defer the processing of parallel task related requests until a full
  124. scheduling operation shot completes without the
  125. state_blocked_in_parallel_observed flag being tainted again. The purpose of this
  126. tainting flag is to prevent parallel task operations to be started immediately
  127. after the observation of a transient scheduling state.
  128. Worker's management of parallel tasks is
  129. governed by the following set of state flags and counters:
  130. - state_blocked_in_parallel: set to !0 while the worker is currently blocked on a parallel
  131. task;
  132. - state_blocked_in_parallel_observed: set to !0 to taint the worker when a
  133. thread has observed the state_blocked_in_parallel flag of this worker while
  134. its state_relax_refcnt state counter was >0. Any pending request to add or
  135. remove the worker from a parallel task team will be deferred until a whole
  136. scheduling operation shot completes without being tainted again.
  137. - state_block_in_parallel_req: set to !0 when a thread is waiting on a request
  138. for the worker to be added to a parallel task team. Must be protected by the
  139. worker's sched_mutex.
  140. - state_block_in_parallel_ack: set to !0 by the worker when acknowledging a
  141. request for being added to a parallel task team. Must be protected by the
  142. worker's sched_mutex.
  143. - state_unblock_in_parallel_req: set to !0 when a thread is waiting on a request
  144. for the worker to be removed from a parallel task team. Must be protected by the
  145. worker's sched_mutex.
  146. - state_unblock_in_parallel_ack: set to !0 by the worker when acknowledging a
  147. request for being removed from a parallel task team. Must be protected by the
  148. worker's sched_mutex.
  149. - block_in_parallel_ref_count: counts the number of consecutive pending requests
  150. to enter parallel task teams. Only the first of a train of requests for
  151. entering parallel task teams triggers the transition of the
  152. state_block_in_parallel_req flag from 0 to 1. Only the last of a train of
  153. requests to leave a parallel task team triggers the transition of flag
  154. state_unblock_in_parallel_req from 0 to 1. Must be protected by the
  155. worker's sched_mutex.
  156. \subsubsection CoreEntitiesWorkersOperations Operations
  157. <b>Entry point</b>
  158. All the operations of a worker are handled in an iterative fashion, either by
  159. the application code on a thread launched by the application, or automatically
  160. by StarPU on a device-dependent CPU thread launched by StarPU. Whether a
  161. worker's operation cycle is managed automatically or
  162. not is controlled per session by the field \c not_launched_drivers of the \c
  163. starpu_conf struct, and is decided in \ref _starpu_launch_drivers() function.
  164. When managed automatically, cycles of operations for a worker are handled by the corresponding
  165. driver specific <code>_starpu_<DRV>_worker()</code> function, where \c DRV is a driver name such as
  166. cpu (\c _starpu_cpu_worker) or cuda (\c _starpu_cuda_worker), for instance.
  167. Otherwise, the application must supply a thread which will repeatedly call \ref
  168. starpu_driver_run_once() for the corresponding worker.
  169. In both cases, control is then transferred to
  170. \ref _starpu_cpu_driver_run_once() (or the corresponding driver specific func).
  171. The cycle of operations typically includes, at least, the following operations:
  172. - <b>task scheduling</b>
  173. - <b>parallel task team build-up</b>
  174. - <b>task input processing</b>
  175. - <b>data transfer processing</b>
  176. - <b>task execution</b>
  177. When the worker cycles are handled by StarPU automatically, the iterative
  178. operation processing ends when the \c running field of \c _starpu_config
  179. becomes false. This field should not be read directly, instead it should be read
  180. through the \ref _starpu_machine_is_running() function.
  181. <br>
  182. <b>Task scheduling</b>
  183. If the worker does not yet have a queued task, it calls
  184. _starpu_get_worker_task() to try and obtain a task. This may involve scheduling
  185. operations such as stealing a queued but not yet executed task from another
  186. worker. The operation may not necessarily succeed if no tasks are ready and/or
  187. suitable to run on the worker's computing unit.
  188. <br>
  189. <b>Parallel task team build-up</b>
  190. If the worker has a task ready to run and the corresponding job has a size
  191. \c >1, then the task is a parallel job and the worker must synchronize with the
  192. other workers participating to the parallel execution of the job to assign a
  193. unique rank for each worker. The synchronization is done throught the job's \c
  194. sync_mutex mutex.
  195. <br>
  196. <b>Task input processing</b>
  197. Before the task can be executed, its input data must be made available on a
  198. memory node reachable by the worker's computing unit. To do so, the worker calls
  199. \ref _starpu_fetch_task_input()
  200. <br>
  201. <b>Data transfer processing</b>
  202. TODO
  203. <br>
  204. <b>Task execution</b>
  205. Once the worker has a pending task assigned and the input data for that task are
  206. available in the memory node reachable by the worker's computing unit, the
  207. worker calls \ref _starpu_cpu_driver_execute_task() (or the corresponding driver
  208. specific function) to proceed to the execution of the task.
  209. \subsection CoreEntitiesContexts Scheduling Contexts
  210. TODO
  211. \subsection CoreEntitiesWorkersAndContexts Workers and Scheduling Contexts
  212. A worker can be assigned to one or more <b>scheduling contexts</b>. It
  213. exclusively receives tasks submitted to the scheduling context(s) it is
  214. currently assigned at the time such tasks are scheduled. A worker may add itself
  215. to or remove itself from a scheduling context.
  216. TODO
  217. */