330_scheduling_contexts.doxy 7.5 KB

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