advanced-api.texi 20 KB

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