advanced-api.texi 18 KB

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