advanced-api.texi 15 KB

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