330_scheduling_contexts.doxy 10 KB

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