330_scheduling_contexts.doxy 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. // * Copyright (C) 2009--2011 Universit@'e de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2016, 2017 CNRS
  5. * Copyright (C) 2011, 2012 INRIA
  6. * Copyright (C) 2016 Uppsala University
  7. * See the file version.doxy for copying conditions.
  8. */
  9. /*! \page SchedulingContexts Scheduling Contexts
  10. TODO: improve!
  11. \section GeneralIdeas General Ideas
  12. Scheduling contexts represent abstracts sets of workers that allow the
  13. programmers to control the distribution of computational resources
  14. (i.e. CPUs and GPUs) to concurrent kernels. The main goal is
  15. to minimize interferences between the execution of multiple parallel
  16. kernels, by partitioning the underlying pool of workers using
  17. contexts. Scheduling contexts additionally allow a user to make use of
  18. a different scheduling policy depending on the target resource set.
  19. \section CreatingAContext Creating A Context
  20. By default, the application submits tasks to an initial context, which
  21. disposes of all the computation resources available to StarPU (all
  22. the workers). If the application programmer plans to launch several
  23. kernels simultaneously, by default these kernels will be
  24. executed within this initial context, using a single scheduler
  25. policy(see \ref TaskSchedulingPolicy). Meanwhile, if the application
  26. programmer is aware of the demands of these kernels and of the
  27. specificity of the machine used to execute them, the workers can be
  28. divided between several contexts. These scheduling contexts will
  29. isolate the execution of each kernel and they will permit the use of a
  30. scheduling policy proper to each one of them.
  31. Scheduling Contexts may be created in two ways: either the programmers
  32. indicates the set of workers corresponding to each context (providing
  33. he knows the identifiers of the workers running within StarPU), or the
  34. programmer does not provide any worker list and leaves the Hypervisor
  35. assign workers to each context according to their needs (\ref
  36. SchedulingContextHypervisor).
  37. Both cases require a call to the function
  38. starpu_sched_ctx_create(), which requires as input the worker
  39. list (the exact list or a <c>NULL</c> pointer), the amount of workers
  40. (or <c>-1</c> to designate all workers on the platform) and a list of
  41. optional parameters such as the scheduling policy, terminated by a
  42. <c>0</c>. The scheduling policy can be a character list corresponding
  43. to the name of a StarPU predefined policy or the pointer to a custom
  44. policy. The function returns an identifier of the context created
  45. which you will use to indicate the context you want to submit the
  46. tasks to.
  47. \code{.c}
  48. /* the list of resources the context will manage */
  49. int workerids[3] = {1, 3, 10};
  50. /* indicate the list of workers assigned to it, the number of workers,
  51. the name of the context and the scheduling policy to be used within
  52. the context */
  53. int id_ctx = starpu_sched_ctx_create(workerids, 3, "my_ctx", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  54. /* let StarPU know that the following tasks will be submitted to this context */
  55. starpu_sched_ctx_set_task_context(id);
  56. /* submit the task to StarPU */
  57. starpu_task_submit(task);
  58. \endcode
  59. Note: Parallel greedy and parallel heft scheduling policies do not support the existence of several disjoint contexts on the machine.
  60. Combined workers are constructed depending on the entire topology of the machine, not only the one belonging to a context.
  61. \subsection CreatingAContextWithTheDefaultBehavior Creating A Context With The Default Behavior
  62. If <b>no scheduling policy</b> is specified when creating the context,
  63. it will be used as <b>another type of resource</b>: a cluster. A
  64. cluster is a context without scheduler (eventually delegated to
  65. another runtime). For more information see \ref ClusteringAMachine. It
  66. is therefore <b>mandatory</b> to stipulate a scheduler to use the
  67. contexts in this traditional way.
  68. To create a <b>context</b> with the default scheduler, that is either
  69. controlled through the environment variable <c>STARPU_SCHED</c> or the
  70. StarPU default scheduler, one can explicitly use the option <c>STARPU_SCHED_CTX_POLICY_NAME, ""</c> as in the following example:
  71. \code{.c}
  72. /* the list of resources the context will manage */
  73. int workerids[3] = {1, 3, 10};
  74. /* indicate the list of workers assigned to it, the number of workers,
  75. and use the default scheduling policy. */
  76. int id_ctx = starpu_sched_ctx_create(workerids, 3, "my_ctx", STARPU_SCHED_CTX_POLICY_NAME, "", 0);
  77. /* .... */
  78. \endcode
  79. \section CreatingAContext Creating A Context To Partition a GPU
  80. The contexts can also be used to group set of SMs of an NVIDIA GPU in order to isolate
  81. the parallel kernels and allow them to coexecution on a specified partiton of the GPU.
  82. Each context will be mapped to a stream and the user can indicate the number of SMs.
  83. The context can be added to a larger context already grouping CPU cores.
  84. This larger context can use a scheduling policy that assigns tasks to both CPUs and contexts (partitions of the GPU)
  85. based on performance models adjusted to the number of SMs.
  86. The GPU implementation of the task has to be modified accordingly and receive as a parameter the number of SMs.
  87. \code{.c}
  88. /* get the available streams (suppose we have nstreams = 2 by specifying them with STARPU_NWORKER_PER_CUDA=2 */
  89. int nstreams = starpu_worker_get_stream_workerids(gpu_devid, stream_workerids, STARPU_CUDA_WORKER);
  90. int sched_ctx[nstreams];
  91. sched_ctx[0] = starpu_sched_ctx_create(&stream_workerids[0], 1, "subctx", STARPU_SCHED_CTX_CUDA_NSMS, 6, 0);
  92. sched_ctx[1] = starpu_sched_ctx_create(&stream_workerids[1], 1, "subctx", STARPU_SCHED_CTX_CUDA_NSMS, 7, 0);
  93. int ncpus = 4;
  94. int workers[ncpus+nstreams];
  95. workers[ncpus+0] = stream_workerids[0];
  96. workers[ncpus+1] = stream_workerids[1];
  97. big_sched_ctx = starpu_sched_ctx_create(workers, ncpus+nstreams, "ctx1", STARPU_SCHED_CTX_SUB_CTXS, sched_ctxs, nstreams, STARPU_SCHED_CTX_POLICY_NAME, "dmdas", 0);
  98. starpu_task_submit_to_ctx(task, big_sched_ctx);
  99. \endcode
  100. \section ModifyingAContext Modifying A Context
  101. A scheduling context can be modified dynamically. The application may
  102. change its requirements during the execution and the programmer can
  103. add additional workers to a context or remove those no longer needed. In
  104. the following example we have two scheduling contexts
  105. <c>sched_ctx1</c> and <c>sched_ctx2</c>. After executing a part of the
  106. tasks some of the workers of <c>sched_ctx1</c> will be moved to
  107. context <c>sched_ctx2</c>.
  108. \code{.c}
  109. /* the list of ressources that context 1 will give away */
  110. int workerids[3] = {1, 3, 10};
  111. /* add the workers to context 1 */
  112. starpu_sched_ctx_add_workers(workerids, 3, sched_ctx2);
  113. /* remove the workers from context 2 */
  114. starpu_sched_ctx_remove_workers(workerids, 3, sched_ctx1);
  115. \endcode
  116. \section SubmittingTasksToAContext Submitting Tasks To A Context
  117. The application may submit tasks to several contexts either
  118. simultaneously or sequnetially. If several threads of submission
  119. are used the function starpu_sched_ctx_set_context() may be called just
  120. before starpu_task_submit(). Thus StarPU considers that
  121. the current thread will submit tasks to the coresponding context.
  122. When the application may not assign a thread of submission to each
  123. context, the id of the context must be indicated by using the
  124. function starpu_task_submit_to_ctx() or the field \ref STARPU_SCHED_CTX
  125. for starpu_task_insert().
  126. \section DeletingAContext Deleting A Context
  127. When a context is no longer needed it must be deleted. The application
  128. can indicate which context should keep the resources of a deleted one.
  129. All the tasks of the context should be executed before doing this.
  130. Thus, the programmer may use either a barrier and then delete the context
  131. directly, or just indicate
  132. that other tasks will not be submitted later on to the context (such that when
  133. the last task is executed its workers will be moved to the inheritor)
  134. and delete the context at the end of the execution (when a barrier will
  135. be used eventually).
  136. \code{.c}
  137. /* when the context 2 is deleted context 1 inherits its resources */
  138. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  139. /* submit tasks to context 2 */
  140. for (i = 0; i < ntasks; i++)
  141. starpu_task_submit_to_ctx(task[i],sched_ctx2);
  142. /* indicate that context 2 finished submitting and that */
  143. /* as soon as the last task of context 2 finished executing */
  144. /* its workers can be moved to the inheritor context */
  145. starpu_sched_ctx_finished_submit(sched_ctx1);
  146. /* wait for the tasks of both contexts to finish */
  147. starpu_task_wait_for_all();
  148. /* delete context 2 */
  149. starpu_sched_ctx_delete(sched_ctx2);
  150. /* delete context 1 */
  151. starpu_sched_ctx_delete(sched_ctx1);
  152. \endcode
  153. \section EmptyingAContext Emptying A Context
  154. A context may have no resources at the begining or at a certain
  155. moment of the execution. Task can still be submitted to these contexts
  156. and they will be executed as soon as the contexts will have resources. A list
  157. of tasks pending to be executed is kept and when workers are added to
  158. the contexts these tasks start being submitted. However, if resources
  159. are never allocated to the context the program will not terminate.
  160. If these tasks have low
  161. priority the programmer can forbid the application to submit them
  162. by calling the function starpu_sched_ctx_stop_task_submission().
  163. */