advanced-api.texi 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * Task Bundles::
  11. * Defining a new data interface::
  12. * Multiformat Data Interface::
  13. * Defining a new scheduling policy::
  14. @end menu
  15. @node Task Bundles
  16. @section Task Bundles
  17. @deftp {DataType} {struct starpu_task_bundle}
  18. The task bundle structure describes a list of tasks that should be
  19. scheduled together whenever possible. The different fields are:
  20. @table @asis
  21. @item @code{mutex} Mutex protecting the bundle
  22. @item @code{int previous_workerid} last worker previously assigned a task from the bundle (-1 if none)
  23. @item @code{struct starpu_task_bundle_entry *list} list of tasks
  24. @item @code{int destroy} If this flag is set, the bundle structure is automatically free'd when the bundle is deinitialized.
  25. @item @code{int closed} Is the bundle closed ?
  26. @end table
  27. @end deftp
  28. @deftypefun void starpu_task_bundle_init ({struct starpu_task_bundle *}@var{bundle})
  29. Initialize a task bundle
  30. @end deftypefun
  31. @deftypefun void starpu_task_bundle_deinit ({struct starpu_task_bundle *}@var{bundle})
  32. Deinitialize a bundle. In case the destroy flag is set, the bundle
  33. structure is freed too.
  34. @end deftypefun
  35. @deftypefun int starpu_task_bundle_insert ({struct starpu_task_bundle *}@var{bundle}, {struct starpu_task *}@var{task})
  36. Insert a task into a bundle.
  37. @end deftypefun
  38. @deftypefun int starpu_task_bundle_remove ({struct starpu_task_bundle *}@var{bundle}, {struct starpu_task *}@var{task})
  39. Remove a task from a bundle. This method must be called with
  40. bundle->mutex hold. This function returns 0 if the task was found,
  41. -ENOENT if the element was not found, 1 if the element is found and if
  42. the list was deinitialized because it became empty.
  43. @end deftypefun
  44. @deftypefun void starpu_task_bundle_close ({struct starpu_task_bundle *}@var{bundle});
  45. Close a bundle. No task can be added to a closed bundle. A closed
  46. bundle automatically gets deinitialized when it becomes empty.
  47. @end deftypefun
  48. @deftypefun double starpu_task_bundle_expected_length ({struct starpu_task_bundle *}@var{bundle}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  49. Return the expected duration of the entire task bundle in µs.
  50. @end deftypefun
  51. @deftypefun double starpu_task_bundle_expected_data_transfer_time ({struct starpu_task_bundle *}@var{bundle}, unsigned {memory_node})
  52. Return the time (in µs) expected to transfer all data used within the bundle
  53. @end deftypefun
  54. @deftypefun double starpu_task_bundle_expected_power ({struct starpu_task_bundle *}@var{bundle}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  55. Return the expected power consumption of the entire task bundle in J.
  56. @end deftypefun
  57. @node Defining a new data interface
  58. @section Defining a new data interface
  59. @menu
  60. * Data Interface API:: Data Interface API
  61. * An example of data interface:: An example of data interface
  62. @end menu
  63. @node Data Interface API
  64. @subsection Data Interface API
  65. @deftp {Data Type} {struct starpu_data_interface_ops}
  66. @anchor{struct starpu_data_interface_ops}
  67. Defines the per-interface methods. TODO describe all the different fields
  68. @end deftp
  69. @deftp {Data Type} {struct starpu_data_copy_methods}
  70. Per-interface data transfer methods. TODO describe all the different fields
  71. @end deftp
  72. @node An example of data interface
  73. @subsection An example of data interface
  74. TODO
  75. See @code{src/datawizard/interfaces/vector_interface.c} for now.
  76. @node Multiformat Data Interface
  77. @section Multiformat Data Interface
  78. @deftp {Data Type} {struct starpu_multiformat_data_interface_ops}
  79. todo. The different fields are:
  80. @table @asis
  81. @item @code{cpu_elemsize} the size of each element on CPUs,
  82. @item @code{opencl_elemsize} the size of each element on OpenCL devices,
  83. @item @code{cuda_elemsize} the size of each element on CUDA devices,
  84. @item @code{cpu_to_opencl_cl} pointer to a codelet which converts from CPU to OpenCL
  85. @item @code{opencl_to_cpu_cl} pointer to a codelet which converts from OpenCL to CPU
  86. @item @code{cpu_to_cuda_cl} pointer to a codelet which converts from CPU to CUDA
  87. @item @code{cuda_to_cpu_cl} pointer to a codelet which converts from CUDA to CPU
  88. @end table
  89. @end deftp
  90. @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});
  91. Register a piece of data that can be represented in different ways, depending upon
  92. the processing unit that manipulates it. It allows the programmer, for instance, to
  93. use an array of structures when working on a CPU, and a structure of arrays when
  94. working on a GPU.
  95. @var{nobjects} is the number of elements in the data. @var{format_ops} describes
  96. the format.
  97. @example
  98. #define NX 1024
  99. struct point array_of_structs[NX];
  100. starpu_data_handle handle;
  101. /*
  102. * The conversion of a piece of data is itself a task, though it is created,
  103. * submitted and destroyed by StarPU internals and not by the user. Therefore,
  104. * we have to define two codelets.
  105. * Note that for now the conversion from the CPU format to the GPU format has to
  106. * be executed on the GPU, and the conversion from the GPU to the CPU has to be
  107. * executed on the CPU.
  108. */
  109. #ifdef STARPU_USE_OPENCL
  110. void cpu_to_opencl_opencl_func(void *buffers[], void *args);
  111. starpu_codelet cpu_to_opencl_cl = @{
  112. .where = STARPU_OPENCL,
  113. .opencl_func = cpu_to_opencl_opencl_func,
  114. .nbuffers = 1
  115. @};
  116. void opencl_to_cpu_func(void *buffers[], void *args);
  117. starpu_codelet opencl_to_cpu_cl = @{
  118. .where = STARPU_CPU,
  119. .cpu_func = opencl_to_cpu_func,
  120. .nbuffers = 1
  121. @};
  122. #endif
  123. struct starpu_multiformat_data_interface_ops format_ops = @{
  124. #ifdef STARPU_USE_OPENCL
  125. .opencl_elemsize = 2 * sizeof(float),
  126. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  127. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  128. #endif
  129. .cpu_elemsize = 2 * sizeof(float),
  130. ...
  131. @};
  132. starpu_multiformat_data_register(handle, 0, &array_of_structs, NX, &format_ops);
  133. @end example
  134. @end deftypefun
  135. @node Defining a new scheduling policy
  136. @section Defining a new scheduling policy
  137. TODO
  138. A full example showing how to define a new scheduling policy is available in
  139. the StarPU sources in the directory @code{examples/scheduler/}.
  140. @menu
  141. * Scheduling Policy API:: Scheduling Policy API
  142. * Source code::
  143. @end menu
  144. @node Scheduling Policy API
  145. @subsection Scheduling Policy API
  146. @deftp {Data Type} {struct starpu_sched_policy}
  147. This structure contains all the methods that implement a scheduling policy. An
  148. application may specify which scheduling strategy in the @code{sched_policy}
  149. field of the @code{starpu_conf} structure passed to the @code{starpu_init}
  150. function. The different fields are:
  151. @table @asis
  152. @item @code{init_sched}
  153. Initialize the scheduling policy.
  154. @item @code{deinit_sched}
  155. Cleanup the scheduling policy.
  156. @item @code{push_task}
  157. Insert a task into the scheduler.
  158. @item @code{push_prio_task}
  159. Insert a priority task into the scheduler.
  160. @item @code{push_prio_notify}
  161. Notify the scheduler that a task was pushed on the worker. This method is
  162. called when a task that was explicitely assigned to a worker is scheduled. This
  163. method therefore permits to keep the state of of the scheduler coherent even
  164. when StarPU bypasses the scheduling strategy.
  165. @item @code{pop_task}
  166. Get a task from the scheduler. The mutex associated to the worker is already
  167. taken when this method is called. If this method is defined as @code{NULL}, the
  168. worker will only execute tasks from its local queue. In this case, the
  169. @code{push_task} method should use the @code{starpu_push_local_task} method to
  170. assign tasks to the different workers.
  171. @item @code{pop_every_task}
  172. Remove all available tasks from the scheduler (tasks are chained by the means
  173. of the prev and next fields of the starpu_task structure). The mutex associated
  174. to the worker is already taken when this method is called.
  175. @item @code{post_exec_hook} (optional)
  176. This method is called every time a task has been executed.
  177. @item @code{policy_name}
  178. Name of the policy (optional).
  179. @item @code{policy_description}
  180. Description of the policy (optionnal).
  181. @end table
  182. @end deftp
  183. @deftypefun void starpu_worker_set_sched_condition (int @var{workerid}, pthread_cond_t *@var{sched_cond}, pthread_mutex_t *@var{sched_mutex})
  184. This function specifies the condition variable associated to a worker
  185. When there is no available task for a worker, StarPU blocks this worker on a
  186. condition variable. This function specifies which condition variable (and the
  187. associated mutex) should be used to block (and to wake up) a worker. Note that
  188. multiple workers may use the same condition variable. For instance, in the case
  189. of a scheduling strategy with a single task queue, the same condition variable
  190. would be used to block and wake up all workers.
  191. The initialization method of a scheduling strategy (@code{init_sched}) must
  192. call this function once per worker.
  193. @end deftypefun
  194. @deftypefun void starpu_sched_set_min_priority (int @var{min_prio})
  195. Defines the minimum priority level supported by the scheduling policy. The
  196. default minimum priority level is the same as the default priority level which
  197. is 0 by convention. The application may access that value by calling the
  198. @code{starpu_sched_get_min_priority} function. This function should only be
  199. called from the initialization method of the scheduling policy, and should not
  200. be used directly from the application.
  201. @end deftypefun
  202. @deftypefun void starpu_sched_set_min_priority (int @var{max_prio})
  203. Defines the maximum priority level supported by the scheduling policy. The
  204. default maximum priority level is 1. The application may access that value by
  205. calling the @code{starpu_sched_get_max_priority} function. This function should
  206. only be called from the initialization method of the scheduling policy, and
  207. should not be used directly from the application.
  208. @end deftypefun
  209. @deftypefun int starpu_push_local_task (int @var{workerid}, {struct starpu_task} *@var{task}, int @var{back})
  210. The scheduling policy may put tasks directly into a worker's local queue so
  211. that it is not always necessary to create its own queue when the local queue
  212. is sufficient. If "back" not null, the task is put at the back of the queue
  213. where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  214. a FIFO ordering.
  215. @end deftypefun
  216. @deftypefun int starpu_worker_may_execute_task (unsigned @var{workerid}, {struct starpu_task *}@var{task}, unsigned {nimpl})
  217. Check if the worker specified by workerid can execute the codelet.
  218. @end deftypefun
  219. @deftypefun double starpu_timing_now (void)
  220. Return the current date
  221. @end deftypefun
  222. @deftypefun double starpu_task_expected_length ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  223. Returns expected task duration in µs
  224. @end deftypefun
  225. @deftypefun double starpu_worker_get_relative_speedup ({enum starpu_perf_archtype} @var{perf_archtype})
  226. Returns an estimated speedup factor relative to CPU speed
  227. @end deftypefun
  228. @deftypefun double starpu_task_expected_data_transfer_time (uint32_t @var{memory_node}, {struct starpu_task *}@var{task})
  229. Returns expected data transfer time in µs
  230. @end deftypefun
  231. @deftypefun double starpu_data_expected_transfer_time (starpu_data_handle @var{handle}, unsigned @var{memory_node}, starpu_access_mode @var{mode})
  232. Predict the transfer time (in µs) to move a handle to a memory node
  233. @end deftypefun
  234. @deftypefun double starpu_task_expected_power ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  235. Returns expected power consumption in J
  236. @end deftypefun
  237. @deftypefun double starpu_task_expected_conversion_time ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned {nimpl})
  238. Returns expected conversion time in ms (multiformat interface only)
  239. @end deftypefun
  240. @node Source code
  241. @subsection Source code
  242. @cartouche
  243. @smallexample
  244. static struct starpu_sched_policy dummy_sched_policy = @{
  245. .init_sched = init_dummy_sched,
  246. .deinit_sched = deinit_dummy_sched,
  247. .push_task = push_task_dummy,
  248. .push_prio_task = NULL,
  249. .pop_task = pop_task_dummy,
  250. .post_exec_hook = NULL,
  251. .pop_every_task = NULL,
  252. .policy_name = "dummy",
  253. .policy_description = "dummy scheduling strategy"
  254. @};
  255. @end smallexample
  256. @end cartouche