advanced-api.texi 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. @node StarPU Advanced API
  8. @chapter StarPU Advanced API
  9. @menu
  10. * Defining a new data interface::
  11. * Multiformat Data Interface::
  12. * Defining a new scheduling policy::
  13. @end menu
  14. @node Defining a new data interface
  15. @section Defining a new data interface
  16. @menu
  17. * Data Interface API:: Data Interface API
  18. * An example of data interface:: An example of data interface
  19. @end menu
  20. @node Data Interface API
  21. @subsection Data Interface API
  22. @deftp {Data Type} {struct starpu_data_interface_ops}
  23. @anchor{struct starpu_data_interface_ops}
  24. Defines the per-interface methods. TODO describe all the different fields
  25. @end deftp
  26. @deftp {Data Type} {struct starpu_data_copy_methods}
  27. Per-interface data transfer methods. TODO describe all the different fields
  28. @end deftp
  29. @node An example of data interface
  30. @subsection An example of data interface
  31. TODO
  32. See @code{src/datawizard/interfaces/vector_interface.c} for now.
  33. @node Multiformat Data Interface
  34. @section Multiformat Data Interface
  35. @deftp {Data Type} {struct starpu_multiformat_data_interface_ops}
  36. todo. The different fields are:
  37. @table @asis
  38. @item @code{cpu_elemsize} the size of each element on CPUs,
  39. @item @code{opencl_elemsize} the size of each element on OpenCL devices,
  40. @item @code{cuda_elemsize} the size of each element on CUDA devices,
  41. @item @code{cpu_to_opencl_cl} pointer to a codelet which converts from CPU to OpenCL
  42. @item @code{opencl_to_cpu_cl} pointer to a codelet which converts from OpenCL to CPU
  43. @item @code{cpu_to_cuda_cl} pointer to a codelet which converts from CPU to CUDA
  44. @item @code{cuda_to_cpu_cl} pointer to a codelet which converts from CUDA to CPU
  45. @end table
  46. @end deftp
  47. @deftypefun void starpu_multiformat_data_register (starpu_data_handle *@var{handle}, uint32_t @var{home_node}, void *@var{ptr}, uint32_t @var{nobjects}, struct starpu_multiformat_data_interface_ops *@var{format_ops});
  48. Register a piece of data that can be represented in different ways, depending upon
  49. the processing unit that manipulates it. It allows the programmer, for instance, to
  50. use an array of structures when working on a CPU, and a structure of arrays when
  51. working on a GPU.
  52. @var{nobjects} is the number of elements in the data. @var{format_ops} describes
  53. the format.
  54. @example
  55. #define NX 1024
  56. struct point array_of_structs[NX];
  57. starpu_data_handle handle;
  58. /*
  59. * The conversion of a piece of data is itself a task, though it is created,
  60. * submitted and destroyed by StarPU internals and not by the user. Therefore,
  61. * we have to define two codelets.
  62. * Note that for now the conversion from the CPU format to the GPU format has to
  63. * be executed on the GPU, and the conversion from the GPU to the CPU has to be
  64. * executed on the CPU.
  65. */
  66. #ifdef STARPU_USE_OPENCL
  67. void cpu_to_opencl_opencl_func(void *buffers[], void *args);
  68. starpu_codelet cpu_to_opencl_cl = @{
  69. .where = STARPU_OPENCL,
  70. .opencl_func = cpu_to_opencl_opencl_func,
  71. .nbuffers = 1
  72. @};
  73. void opencl_to_cpu_func(void *buffers[], void *args);
  74. starpu_codelet opencl_to_cpu_cl = @{
  75. .where = STARPU_CPU,
  76. .cpu_func = opencl_to_cpu_func,
  77. .nbuffers = 1
  78. @};
  79. #endif
  80. struct starpu_multiformat_data_interface_ops format_ops = @{
  81. #ifdef STARPU_USE_OPENCL
  82. .opencl_elemsize = 2 * sizeof(float),
  83. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  84. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  85. #endif
  86. .cpu_elemsize = 2 * sizeof(float),
  87. ...
  88. @};
  89. starpu_multiformat_data_register(handle, 0, &array_of_structs, NX, &format_ops);
  90. @end example
  91. @end deftypefun
  92. @node Defining a new scheduling policy
  93. @section Defining a new scheduling policy
  94. TODO
  95. A full example showing how to define a new scheduling policy is available in
  96. the StarPU sources in the directory @code{examples/scheduler/}.
  97. @menu
  98. * Scheduling Policy API:: Scheduling Policy API
  99. * Source code::
  100. @end menu
  101. @node Scheduling Policy API
  102. @subsection Scheduling Policy API
  103. @deftp {Data Type} {struct starpu_sched_policy}
  104. This structure contains all the methods that implement a scheduling policy. An
  105. application may specify which scheduling strategy in the @code{sched_policy}
  106. field of the @code{starpu_conf} structure passed to the @code{starpu_init}
  107. function. The different fields are:
  108. @table @asis
  109. @item @code{init_sched}
  110. Initialize the scheduling policy.
  111. @item @code{deinit_sched}
  112. Cleanup the scheduling policy.
  113. @item @code{push_task}
  114. Insert a task into the scheduler.
  115. @item @code{push_prio_task}
  116. Insert a priority task into the scheduler.
  117. @item @code{push_prio_notify}
  118. Notify the scheduler that a task was pushed on the worker. This method is
  119. called when a task that was explicitely assigned to a worker is scheduled. This
  120. method therefore permits to keep the state of of the scheduler coherent even
  121. when StarPU bypasses the scheduling strategy.
  122. @item @code{pop_task}
  123. Get a task from the scheduler. The mutex associated to the worker is already
  124. taken when this method is called. If this method is defined as @code{NULL}, the
  125. worker will only execute tasks from its local queue. In this case, the
  126. @code{push_task} method should use the @code{starpu_push_local_task} method to
  127. assign tasks to the different workers.
  128. @item @code{pop_every_task}
  129. Remove all available tasks from the scheduler (tasks are chained by the means
  130. of the prev and next fields of the starpu_task structure). The mutex associated
  131. to the worker is already taken when this method is called.
  132. @item @code{post_exec_hook} (optional)
  133. This method is called every time a task has been executed.
  134. @item @code{policy_name}
  135. Name of the policy (optional).
  136. @item @code{policy_description}
  137. Description of the policy (optionnal).
  138. @end table
  139. @end deftp
  140. @deftypefun void starpu_worker_set_sched_condition (int @var{workerid}, pthread_cond_t *@var{sched_cond}, pthread_mutex_t *@var{sched_mutex})
  141. This function specifies the condition variable associated to a worker
  142. When there is no available task for a worker, StarPU blocks this worker on a
  143. condition variable. This function specifies which condition variable (and the
  144. associated mutex) should be used to block (and to wake up) a worker. Note that
  145. multiple workers may use the same condition variable. For instance, in the case
  146. of a scheduling strategy with a single task queue, the same condition variable
  147. would be used to block and wake up all workers.
  148. The initialization method of a scheduling strategy (@code{init_sched}) must
  149. call this function once per worker.
  150. @end deftypefun
  151. @deftypefun void starpu_sched_set_min_priority (int @var{min_prio})
  152. Defines the minimum priority level supported by the scheduling policy. The
  153. default minimum priority level is the same as the default priority level which
  154. is 0 by convention. The application may access that value by calling the
  155. @code{starpu_sched_get_min_priority} function. This function should only be
  156. called from the initialization method of the scheduling policy, and should not
  157. be used directly from the application.
  158. @end deftypefun
  159. @deftypefun void starpu_sched_set_min_priority (int @var{max_prio})
  160. Defines the maximum priority level supported by the scheduling policy. The
  161. default maximum priority level is 1. The application may access that value by
  162. calling the @code{starpu_sched_get_max_priority} function. This function should
  163. only be called from the initialization method of the scheduling policy, and
  164. should not be used directly from the application.
  165. @end deftypefun
  166. @deftypefun int starpu_push_local_task (int @var{workerid}, {struct starpu_task} *@var{task}, int @var{back})
  167. The scheduling policy may put tasks directly into a worker's local queue so
  168. that it is not always necessary to create its own queue when the local queue
  169. is sufficient. If "back" not null, the task is put at the back of the queue
  170. where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  171. a FIFO ordering.
  172. @end deftypefun
  173. @node Source code
  174. @subsection Source code
  175. @cartouche
  176. @smallexample
  177. static struct starpu_sched_policy dummy_sched_policy = @{
  178. .init_sched = init_dummy_sched,
  179. .deinit_sched = deinit_dummy_sched,
  180. .push_task = push_task_dummy,
  181. .push_prio_task = NULL,
  182. .pop_task = pop_task_dummy,
  183. .post_exec_hook = NULL,
  184. .pop_every_task = NULL,
  185. .policy_name = "dummy",
  186. .policy_description = "dummy scheduling strategy"
  187. @};
  188. @end smallexample
  189. @end cartouche