sched_ctx.texi 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2009--2011 Universit@'e de Bordeaux 1
  4. @c Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011--2013 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. TODO: improve!
  8. @menu
  9. * General Idea::
  10. * Create a Context::
  11. * Modify a Context::
  12. * Delete a Context::
  13. * Empty Context::
  14. * Contexts Sharing Workers::
  15. @end menu
  16. @node General Idea
  17. @section General Idea
  18. Scheduling contexts represent abstracts sets of workers that allow the programmers to control the distribution of computational resources (i.e. CPUs and
  19. GPUs) to concurrent parallel kernels. The main goal is to minimize interferences between the execution of multiple parallel kernels, by partitioning the underlying pool of workers using contexts.
  20. @node Create a Context
  21. @section Create a Context
  22. By default, the application submits tasks to an initial context, which disposes of all the computation ressources available to StarPU (all the workers).
  23. If the application programmer plans to launch several parallel kernels simultaneusly, by default these kernels will be executed within this initial context, using a single scheduler policy(@pxref{Task scheduling policy}).
  24. Meanwhile, if the application programmer is aware of the demands of these kernels and of the specificity of the machine used to execute them, the workers can be divided between several contexts.
  25. These scheduling contexts will isolate the execution of each kernel and they will permit the use of a scheduling policy proper to each one of them.
  26. In order to create the contexts, you have to know the indentifiers of the workers running within StarPU.
  27. By passing a set of workers together with the scheduling policy to the function @code{starpu_sched_ctx_create}, you will get an identifier of the context created which you will use to indicate the context you want to submit the tasks to.
  28. @cartouche
  29. @smallexample
  30. /* @b{the list of ressources the context will manage} */
  31. int workerids[3] = @{1, 3, 10@};
  32. /* @b{indicate the scheduling policy to be used within the context, the list of
  33. workers assigned to it, the number of workers, the name of the context} */
  34. int id_ctx = starpu_sched_ctx_create("dmda", workerids, 3, "my_ctx");
  35. /* @b{let StarPU know that the folowing tasks will be submitted to this context} */
  36. starpu_sched_ctx_set_task_context(id);
  37. /* @b{submit the task to StarPU} */
  38. starpu_task_submit(task);
  39. @end smallexample
  40. @end cartouche
  41. Note: Parallel greedy and parallel heft scheduling policies do not support the existence of several disjoint contexts on the machine.
  42. Combined workers are constructed depending on the entire topology of the machine, not only the one belonging to a context.
  43. @node Modify a Context
  44. @section Modify a Context
  45. A scheduling context can be modified dynamically. The applications may change its requirements during the execution and the programmer can add additional workers to a context or remove if no longer needed.
  46. In the following example we have two scheduling contexts @code{sched_ctx1} and @code{sched_ctx2}. After executing a part of the tasks some of the workers of @code{sched_ctx1} will be moved to context @code{sched_ctx2}.
  47. @cartouche
  48. @smallexample
  49. /* @b{the list of ressources that context 1 will give away} */
  50. int workerids[3] = @{1, 3, 10@};
  51. /* @b{add the workers to context 1} */
  52. starpu_sched_ctx_add_workers(workerids, 3, sched_ctx2);
  53. /* @b{remove the workers from context 2} */
  54. starpu_sched_ctx_remove_workers(workerids, 3, sched_ctx1);
  55. @end smallexample
  56. @end cartouche
  57. @node Delete a Context
  58. @section Delete a Context
  59. When a context is no longer needed it must be deleted. The application can indicate which context should keep the resources of a deleted one.
  60. All the tasks of the context should be executed before doing this. If the application need to avoid a barrier before moving the resources from the deleted context to the inheritor one, the application can just indicate
  61. when the last task was submitted. Thus, when this last task was submitted the resources will be move, but the context should still be deleted at some point of the application.
  62. @cartouche
  63. @smallexample
  64. /* @b{when the context 2 will be deleted context 1 will be keep its resources} */
  65. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  66. /* @b{submit tasks to context 2} */
  67. for (i = 0; i < ntasks; i++)
  68. starpu_task_submit_to_ctx(task[i],sched_ctx2);
  69. /* @b{indicate that context 2 finished submitting and that } */
  70. /* @b{as soon as the last task of context 2 finished executing } */
  71. /* @b{its workers can be mobed to the inheritor context} */
  72. starpu_sched_ctx_finished_submit(sched_ctx1);
  73. /* @b{wait for the tasks of both contexts to finish} */
  74. starpu_task_wait_for_all();
  75. /* @b{delete context 2} */
  76. starpu_sched_ctx_delete(sched_ctx2);
  77. /* @b{delete context 1} */
  78. starpu_sched_ctx_delete(sched_ctx1);
  79. @end smallexample
  80. @end cartouche
  81. @node Empty Context
  82. @section Empty Context
  83. A context may not have any resources at the begining or at a certain moment of the execution. Task can still be submitted to these contexts and they will execute them as soon as they will have resources.
  84. A list of tasks pending to be executed is kept and when workers are added to the contexts the tasks are submitted. However, if no resources are allocated the program will not terminate.
  85. If these tasks have not much priority the programmer can forbid the application to submitted them by calling the function @code{starpu_sched_ctx_stop_task_submission}.
  86. @node Contexts Sharing Workers
  87. @section Contexts Sharing Workers
  88. Contexts may share workers when a single context cannot execute efficiently enough alone on these workers or when the application decides to express a hierarchy of contexts. The workers apply
  89. an alogrithm of ``Round-Robin'' to chose the context on which they will ``pop'' next. By using the function @code{void starpu_sched_ctx_set_turn_to_other_ctx(int workerid, unsigned sched_ctx_id)}
  90. the programmer can impose the @code{workerid} to ``pop'' in the context @code{sched_ctx_id} next.