advanced-api.texi 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. Per-interface data transfer methods.
  27. @table @asis
  28. @item @code{void (*register_data_handle)(starpu_data_handle_t handle, uint32_t home_node, void *data_interface)}
  29. Register an existing interface into a data handle.
  30. @item @code{starpu_ssize_t (*allocate_data_on_node)(void *data_interface, uint32_t node)}
  31. Allocate data for the interface on a given node.
  32. @item @code{ void (*free_data_on_node)(void *data_interface, uint32_t node)}
  33. Free data of the interface on a given node.
  34. @item @code{ const struct starpu_data_copy_methods *copy_methods}
  35. ram/cuda/spu/opencl synchronous and asynchronous transfer methods.
  36. @item @code{ void * (*handle_to_pointer)(starpu_data_handle_t handle, uint32_t node)}
  37. Return the current pointer (if any) for the handle on the given node.
  38. @item @code{ size_t (*get_size)(starpu_data_handle_t handle)}
  39. Return an estimation of the size of data, for performance models.
  40. @item @code{ uint32_t (*footprint)(starpu_data_handle_t handle)}
  41. Return a 32bit footprint which characterizes the data size.
  42. @item @code{ int (*compare)(void *data_interface_a, void *data_interface_b)}
  43. Compare the data size of two interfaces.
  44. @item @code{ void (*display)(starpu_data_handle_t handle, FILE *f)}
  45. Dump the sizes of a handle to a file.
  46. @item @code{ int (*convert_to_gordon)(void *data_interface, uint64_t *ptr, gordon_strideSize_t *ss)}
  47. Convert the data size to the spu size format. If no SPUs are used, this field can be seto NULL.
  48. @item @code{enum starpu_data_interface_id interfaceid}
  49. An identifier that is unique to each interface.
  50. @item @code{size_t interface_size}
  51. The size of the interface data descriptor.
  52. @end table
  53. @end deftp
  54. @deftp {Data Type} {struct starpu_data_copy_methods}
  55. Defines the per-interface methods.
  56. @table @asis
  57. @item @code{int @{ram,cuda,opencl,spu@}_to_@{ram,cuda,opencl,spu@}(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)}
  58. These 16 functions define how to copy data from the @var{src_interface}
  59. interface on the @var{src_node} node to the @var{dst_interface} interface
  60. on the @var{dst_node} node. They return 0 on success.
  61. @item @code{int (*ram_to_cuda_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)}
  62. Define how to copy data from the @var{src_interface} interface on the
  63. @var{src_node} node (in RAM) to the @var{dst_interface} interface on the
  64. @var{dst_node} node (on a CUDA device), using the given @var{stream}. Return 0
  65. on success.
  66. @item @code{int (*cuda_to_ram_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)}
  67. Define how to copy data from the @var{src_interface} interface on the
  68. @var{src_node} node (on a CUDA device) to the @var{dst_interface} interface on the
  69. @var{dst_node} node (in RAM), using the given @var{stream}. Return 0
  70. on success.
  71. @item @code{int (*cuda_to_cuda_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, cudaStream_t stream)}
  72. Define how to copy data from the @var{src_interface} interface on the
  73. @var{src_node} node (on a CUDA device) to the @var{dst_interface} interface on
  74. the @var{dst_node} node (on another CUDA device), using the given @var{stream}.
  75. Return 0 on success.
  76. @item @code{int (*ram_to_opencl_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event)}
  77. Define how to copy data from the @var{src_interface} interface on the
  78. @var{src_node} node (in RAM) to the @var{dst_interface} interface on the
  79. @var{dst_node} node (on an OpenCL device), using @var{event}, a pointer to a
  80. cl_event. Return 0 on success.
  81. @item @code{int (*opencl_to_ram_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event)}
  82. Define how to copy data from the @var{src_interface} interface on the
  83. @var{src_node} node (on an OpenCL device) to the @var{dst_interface} interface
  84. on the @var{dst_node} node (in RAM), using the given @var{event}, a pointer to
  85. a cl_event. Return 0 on success.
  86. @item @code{int (*opencl_to_opencl_async)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, /* cl_event * */ void *event)}
  87. Define how to copy data from the @var{src_interface} interface on the
  88. @var{src_node} node (on an OpenCL device) to the @var{dst_interface} interface
  89. on the @var{dst_node} node (on another OpenCL device), using the given
  90. @var{event}, a pointer to a cl_event. Return 0 on success.
  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. Compute the CRC of a byte buffer seeded by the inputcrc "current
  95. state". The return value should be considered as the new "current
  96. state" for future CRC computation. This is used for computing data size
  97. footprint.
  98. @end deftypefun
  99. @deftypefun uint32_t starpu_crc32_be (uint32_t @var{input}, uint32_t @var{inputcrc})
  100. Compute the CRC of a 32bit number seeded by the inputcrc "current
  101. state". The return value should be considered as the new "current
  102. state" for future CRC computation. This is used for computing data size
  103. footprint.
  104. @end deftypefun
  105. @deftypefun uint32_t starpu_crc32_string ({char *}@var{str}, uint32_t @var{inputcrc})
  106. Compute the CRC of a string seeded by the inputcrc "current state".
  107. The return value should be considered as the new "current state" for
  108. future CRC computation. This is used for computing data size footprint.
  109. @end deftypefun
  110. @node An example of data interface
  111. @subsection An example of data interface
  112. @deftypefun int starpu_data_interface_get_next_id ()
  113. Returns the next available id for a newly created data interface.
  114. @end deftypefun
  115. Let's define a new data interface to manage complex numbers.
  116. @cartouche
  117. @smallexample
  118. /* interface for complex numbers */
  119. struct starpu_complex_interface
  120. @{
  121. double *real;
  122. double *imaginary;
  123. int nx;
  124. @};
  125. @end smallexample
  126. @end cartouche
  127. Registering such a data to StarPU is easily done using the function
  128. @code{starpu_data_register} (@pxref{Basic Data Library API}). The last
  129. parameter of the function, @code{interface_complex_ops}, will be
  130. described below.
  131. @cartouche
  132. @smallexample
  133. void starpu_complex_data_register(starpu_data_handle_t *handle,
  134. uint32_t home_node, double *real, double *imaginary, int nx)
  135. @{
  136. struct starpu_complex_interface complex =
  137. @{
  138. .real = real,
  139. .imaginary = imaginary,
  140. .nx = nx
  141. @};
  142. if (interface_complex_ops.interfaceid == -1)
  143. @{
  144. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  145. @}
  146. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  147. @}
  148. @end smallexample
  149. @end cartouche
  150. Different operations need to be defined for a data interface through
  151. the type @code{struct starpu_data_interface_ops} (@pxref{Data
  152. Interface API}). We only define here the basic operations needed to
  153. run simple applications. The source code for the different functions
  154. can be found in the file
  155. @code{examples/interface/complex_interface.c}.
  156. @cartouche
  157. @smallexample
  158. static struct starpu_data_interface_ops interface_complex_ops =
  159. @{
  160. .register_data_handle = complex_register_data_handle,
  161. .allocate_data_on_node = complex_allocate_data_on_node,
  162. .copy_methods = &complex_copy_methods,
  163. .get_size = complex_get_size,
  164. .footprint = complex_footprint,
  165. .interfaceid = -1,
  166. .interface_size = sizeof(struct starpu_complex_interface),
  167. @};
  168. @end smallexample
  169. @end cartouche
  170. Functions need to be defined to access the different fields of the
  171. complex interface from a StarPU data handle.
  172. @cartouche
  173. @smallexample
  174. double *starpu_complex_get_real(starpu_data_handle_t handle)
  175. @{
  176. struct starpu_complex_interface *complex_interface =
  177. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  178. return complex_interface->real;
  179. @}
  180. double *starpu_complex_get_imaginary(starpu_data_handle_t handle);
  181. int starpu_complex_get_nx(starpu_data_handle_t handle);
  182. @end smallexample
  183. @end cartouche
  184. Similar functions need to be defined to access the different fields of the
  185. complex interface from a @code{void *} pointer to be used within codelet
  186. implemetations.
  187. @cartouche
  188. @smallexample
  189. #define STARPU_COMPLEX_GET_REAL(interface) \
  190. (((struct starpu_complex_interface *)(interface))->real)
  191. #define STARPU_COMPLEX_GET_IMAGINARY(interface) \
  192. (((struct starpu_complex_interface *)(interface))->imaginary)
  193. #define STARPU_COMPLEX_GET_NX(interface) \
  194. (((struct starpu_complex_interface *)(interface))->nx)
  195. @end smallexample
  196. @end cartouche
  197. Complex data interfaces can then be registered to StarPU.
  198. @cartouche
  199. @smallexample
  200. double real = 45.0;
  201. double imaginary = 12.0;
  202. starpu_complex_data_register(&handle1, 0, &real, &imaginary, 1);
  203. starpu_insert_task(&cl_display, STARPU_R, handle1, 0);
  204. @end smallexample
  205. @end cartouche
  206. and used by codelets.
  207. @cartouche
  208. @smallexample
  209. void display_complex_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  210. @{
  211. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  212. double *real = STARPU_COMPLEX_GET_REAL(descr[0]);
  213. double *imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  214. int i;
  215. for(i=0 ; i<nx ; i++)
  216. @{
  217. fprintf(stderr, "Complex[%d] = %3.2f + %3.2f i\n", i, real[i], imaginary[i]);
  218. @}
  219. @}
  220. @end smallexample
  221. @end cartouche
  222. The whole code for this complex data interface is available in the
  223. directory @code{examples/interface/}.
  224. @node Multiformat Data Interface
  225. @section Multiformat Data Interface
  226. @deftp {Data Type} {struct starpu_multiformat_data_interface_ops}
  227. The different fields are:
  228. @table @asis
  229. @item @code{size_t cpu_elemsize}
  230. the size of each element on CPUs,
  231. @item @code{size_t opencl_elemsize}
  232. the size of each element on OpenCL devices,
  233. @item @code{struct starpu_codelet *cpu_to_opencl_cl}
  234. pointer to a codelet which converts from CPU to OpenCL
  235. @item @code{struct starpu_codelet *opencl_to_cpu_cl}
  236. pointer to a codelet which converts from OpenCL to CPU
  237. @item @code{size_t cuda_elemsize}
  238. the size of each element on CUDA devices,
  239. @item @code{struct starpu_codelet *cpu_to_cuda_cl}
  240. pointer to a codelet which converts from CPU to CUDA
  241. @item @code{struct starpu_codelet *cuda_to_cpu_cl}
  242. pointer to a codelet which converts from CUDA to CPU
  243. @end table
  244. @end deftp
  245. @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})
  246. Register a piece of data that can be represented in different ways, depending upon
  247. the processing unit that manipulates it. It allows the programmer, for instance, to
  248. use an array of structures when working on a CPU, and a structure of arrays when
  249. working on a GPU.
  250. @var{nobjects} is the number of elements in the data. @var{format_ops} describes
  251. the format.
  252. @end deftypefun
  253. @defmac STARPU_MULTIFORMAT_GET_CPU_PTR ({void *}@var{interface})
  254. returns the local pointer to the data with CPU format.
  255. @end defmac
  256. @defmac STARPU_MULTIFORMAT_GET_CUDA_PTR ({void *}@var{interface})
  257. returns the local pointer to the data with CUDA format.
  258. @end defmac
  259. @defmac STARPU_MULTIFORMAT_GET_OPENCL_PTR ({void *}@var{interface})
  260. returns the local pointer to the data with OpenCL format.
  261. @end defmac
  262. @defmac STARPU_MULTIFORMAT_GET_NX ({void *}@var{interface})
  263. returns the number of elements in the data.
  264. @end defmac
  265. @node Task Bundles
  266. @section Task Bundles
  267. @deftp {Data Type} {starpu_task_bundle_t}
  268. Opaque structure describing a list of tasks that should be scheduled
  269. on the same worker whenever it's possible. It must be considered as a
  270. hint given to the scheduler as there is no guarantee that they will be
  271. executed on the same worker.
  272. @end deftp
  273. @deftypefun void starpu_task_bundle_create ({starpu_task_bundle_t *}@var{bundle})
  274. Factory function creating and initializing @var{bundle}, when the call returns, memory needed is allocated and @var{bundle} is ready to use.
  275. @end deftypefun
  276. @deftypefun int starpu_task_bundle_insert (starpu_task_bundle_t @var{bundle}, {struct starpu_task *}@var{task})
  277. 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}.
  278. This function mustn't be called if @var{bundle} is already closed and/or @var{task} is already submitted.
  279. @end deftypefun
  280. @deftypefun int starpu_task_bundle_remove (starpu_task_bundle_t @var{bundle}, {struct starpu_task *}@var{task})
  281. Remove @var{task} from @var{bundle}.
  282. Of course @var{task} must have been previously inserted @var{bundle}.
  283. 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.
  284. @end deftypefun
  285. @deftypefun void starpu_task_bundle_close (starpu_task_bundle_t @var{bundle})
  286. 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.
  287. @end deftypefun
  288. @node Task Lists
  289. @section Task Lists
  290. @deftp {Data Type} {struct starpu_task_list}
  291. Stores a double-chained list of tasks
  292. @end deftp
  293. @deftypefun void starpu_task_list_init ({struct starpu_task_list *}@var{list})
  294. Initialize a list structure
  295. @end deftypefun
  296. @deftypefun void starpu_task_list_push_front ({struct starpu_task_list *}@var{list}, {struct starpu_task *}@var{task})
  297. Push a task at the front of a list
  298. @end deftypefun
  299. @deftypefun void starpu_task_list_push_back ({struct starpu_task_list *}@var{list}, {struct starpu_task *}@var{task})
  300. Push a task at the back of a list
  301. @end deftypefun
  302. @deftypefun {struct starpu_task *} starpu_task_list_front ({struct starpu_task_list *}@var{list})
  303. Get the front of the list (without removing it)
  304. @end deftypefun
  305. @deftypefun {struct starpu_task *} starpu_task_list_back ({struct starpu_task_list *}@var{list})
  306. Get the back of the list (without removing it)
  307. @end deftypefun
  308. @deftypefun int starpu_task_list_empty ({struct starpu_task_list *}@var{list})
  309. Test if a list is empty
  310. @end deftypefun
  311. @deftypefun void starpu_task_list_erase ({struct starpu_task_list *}@var{list}, {struct starpu_task *}@var{task})
  312. Remove an element from the list
  313. @end deftypefun
  314. @deftypefun {struct starpu_task *} starpu_task_list_pop_front ({struct starpu_task_list *}@var{list})
  315. Remove the element at the front of the list
  316. @end deftypefun
  317. @deftypefun {struct starpu_task *} starpu_task_list_pop_back ({struct starpu_task_list *}@var{list})
  318. Remove the element at the back of the list
  319. @end deftypefun
  320. @deftypefun {struct starpu_task *} starpu_task_list_begin ({struct starpu_task_list *}@var{list})
  321. Get the first task of the list.
  322. @end deftypefun
  323. @deftypefun {struct starpu_task *} starpu_task_list_end ({struct starpu_task_list *}@var{list})
  324. Get the end of the list.
  325. @end deftypefun
  326. @deftypefun {struct starpu_task *} starpu_task_list_next ({struct starpu_task *}@var{task})
  327. Get the next task of the list. This is not erase-safe.
  328. @end deftypefun
  329. @node Using Parallel Tasks
  330. @section Using Parallel Tasks
  331. Workers are grouped considering the topology of the machine in order to permit the opaque execution of parallel tasks.
  332. @deftp {Data Type} {struct starpu_machine_topology}
  333. @table @asis
  334. @item @code{unsigned nworkers}
  335. Total number of workers.
  336. @item @code{unsigned ncombinedworkers}
  337. Total number of combined workers.
  338. @item @code{hwloc_topology_t hwtopology}
  339. Topology as detected by hwloc.
  340. To maintain ABI compatibility when hwloc is not available, the field
  341. is replaced with @code{void *dummy}
  342. @item @code{unsigned nhwcpus}
  343. Total number of CPUs, as detected by the topology code. May be different from
  344. the actual number of CPU workers.
  345. @item @code{unsigned nhwcudagpus}
  346. Total number of CUDA devices, as detected. May be different from the actual
  347. number of CUDA workers.
  348. @item @code{unsigned nhwopenclgpus}
  349. Total number of OpenCL devices, as detected. May be different from the actual
  350. number of CUDA workers.
  351. @item @code{unsigned ncpus}
  352. Actual number of CPU workers used by StarPU.
  353. @item @code{unsigned ncudagpus}
  354. Actual number of CUDA workers used by StarPU.
  355. @item @code{unsigned nopenclgpus}
  356. Actual number of OpenCL workers used by StarPU.
  357. @item @code{unsigned ngordon_spus}
  358. Actual number of Gordon workers used by StarPU.
  359. @item @code{unsigned workers_bindid[STARPU_NMAXWORKERS]}
  360. Indicates the successive cpu identifier that should be used to bind the
  361. workers. It is either filled according to the user's explicit
  362. parameters (from starpu_conf) or according to the STARPU_WORKERS_CPUID env.
  363. variable. Otherwise, a round-robin policy is used to distributed the workers
  364. over the cpus.
  365. @item @code{unsigned workers_cuda_gpuid[STARPU_NMAXWORKERS]}
  366. Indicates the successive cpu identifier that should be used by the CUDA
  367. driver. It is either filled according to the user's explicit parameters (from
  368. starpu_conf) or according to the STARPU_WORKERS_CUDAID env. variable. Otherwise,
  369. they are taken in ID order.
  370. @item @code{unsigned workers_opencl_gpuid[STARPU_NMAXWORKERS]}
  371. Indicates the successive cpu identifier that should be used by the OpenCL
  372. driver. It is either filled according to the user's explicit parameters (from
  373. starpu_conf) or according to the STARPU_WORKERS_OPENCLID env. variable. Otherwise,
  374. they are taken in ID order.
  375. @end table
  376. @end deftp
  377. @deftypefun int starpu_combined_worker_get_size (void)
  378. Return the size of the current combined worker, i.e. the total number of cpus
  379. running the same task in the case of SPMD parallel tasks, or the total number
  380. of threads that the task is allowed to start in the case of FORKJOIN parallel
  381. tasks.
  382. @end deftypefun
  383. @deftypefun int starpu_combined_worker_get_rank (void)
  384. Return the rank of the current thread within the combined worker. Can only be
  385. used in FORKJOIN parallel tasks, to know which part of the task to work on.
  386. @end deftypefun
  387. Most of these are used for schedulers which support parallel tasks.
  388. @deftypefun unsigned starpu_combined_worker_get_count (void)
  389. Return the number of different combined workers.
  390. @end deftypefun
  391. @deftypefun int starpu_combined_worker_get_id (void)
  392. Return the identifier of the current combined worker.
  393. @end deftypefun
  394. @deftypefun int starpu_combined_worker_assign_workerid (int @var{nworkers}, int @var{workerid_array}[])
  395. Register a new combined worker and get its identifier
  396. @end deftypefun
  397. @deftypefun int starpu_combined_worker_get_description (int @var{workerid}, {int *}@var{worker_size}, {int **}@var{combined_workerid})
  398. Get the description of a combined worker
  399. @end deftypefun
  400. @deftypefun int starpu_combined_worker_can_execute_task (unsigned @var{workerid}, {struct starpu_task *}@var{task}, unsigned @var{nimpl})
  401. Variant of starpu_worker_can_execute_task compatible with combined workers
  402. @end deftypefun
  403. @node Scheduling Contexts
  404. @section Scheduling Contexts
  405. StarPU permits on one hand grouping workers in combined workers in order to execute a parallel task and on the other hand grouping tasks in bundles that will be executed by a single specified worker.
  406. Scheduling contexts are different, they represent abstracts sets of workers that allow the programmers to control the distribution of computational resources (i.e. CPUs and
  407. GPUs) to concurrent parallel kernels. The main goal is to minimize interferences between the execution of multiple parallel kernels, by partitioning the underlying pool of workers using contexts.
  408. Scheduling contexts can be created, deleted and modified dynamically.
  409. @menu
  410. * starpu_create_sched_ctx:: Create a scheduling context
  411. * starpu_delete_sched_ctx:: Delete a scheduling context
  412. * starpu_add_workers_to_sched_ctx:: Add workers to scheduling context at runtime
  413. * starpu_remove_workers_from_sched_ctx:: Remove workers from scheduling context at runtime
  414. * starpu_get_workers_of_sched_ctx:: Get the workers assigned to a scheduling context
  415. @end menu
  416. @deftypefun unsigned starpu_create_sched_ctx (const char *@var{policy_name}, int *@var{workerids_ctx}, int @var{nworkers_ctx}, const char *@var{sched_ctx_name})
  417. This function creates a scheduling context which uses the scheduling policy indicated in the first argument and assigns the workers indicated in the second argument to execute the tasks submitted to it.
  418. The return value represents the identifier of the context that has just been created. It will be further used to indicate the context the tasks will be submitted to. The return value should be at most @code{STARPU_NMAX_SCHED_CTXS}.
  419. @end deftypefun
  420. @deftypefun void starpu_delete_sched_ctx (unsigned @var{sched_ctx_id}, unsigned @var{inheritor_sched_ctx_id})
  421. This function deletes the scheduling context indicated by the first argument and lets the scheduling context indicated in the second argument take over its workers.
  422. @end deftypefun
  423. @deftypefun void starpu_add_workers_to_sched_ctx (int *@var{workerids_ctx}, int @var{nworkers_ctx}, unsigned @var{sched_ctx})
  424. This function adds dynamically the workers indicated in the first argument to the context indicated in the last argument. The last argument cannot be greater than @code{STARPU_NMAX_SCHED_CTXS}.
  425. @end deftypefun
  426. @deftypefun void starpu_remove_workers_from_sched_ctx (int *@var{workerids_ctx}, int @var{nworkers_ctx}, unsigned @var{sched_ctx})
  427. This function removes the workers indicated in the first argument from the context indicated in the last argument. The last argument cannot be greater than @code{STARPU_NMAX_SCHED_CTXS}.
  428. @end deftypefun
  429. A scheduling context manages a collection of workers that can be memorized using different data structures. Thus, a generic structure is available in order to simplify the choice of its type.
  430. Only the list data structure is available but further data structures(like tree) implementations are foreseen.
  431. @deftp {Data Type} {struct worker_collection}
  432. @table @asis
  433. @item @code{void *workerids}
  434. The workerids managed by the collection
  435. @item @code{unsigned nworkers}
  436. The number of workerids
  437. @item @code{pthread_key_t cursor_key} (optional)
  438. The cursor needed to iterate the collection (depending on the data structure)
  439. @item @code{int type}
  440. The type of structure (currently WORKER_LIST is the only one available)
  441. @item @code{unsigned (*has_next)(struct worker_collection *workers)}
  442. Checks if there is a next worker
  443. @item @code{int (*get_next)(struct worker_collection *workers)}
  444. Gets the next worker
  445. @item @code{int (*add)(struct worker_collection *workers, int worker)}
  446. Adds a worker to the collection
  447. @item @code{int (*remove)(struct worker_collection *workers, int worker)}
  448. Removes a worker from the collection
  449. @item @code{void* (*init)(struct worker_collection *workers)}
  450. Initialize the collection
  451. @item @code{void (*deinit)(struct worker_collection *workers)}
  452. Deinitialize the colection
  453. @item @code{void (*init_cursor)(struct worker_collection *workers)} (optional)
  454. Initialize the cursor if there is one
  455. @item @code{void (*deinit_cursor)(struct worker_collection *workers)} (optional)
  456. Deinitialize the cursor if there is one
  457. @end table
  458. @end deftp
  459. @deftypefun struct worker_collection* starpu_create_worker_collection_for_sched_ctx(unsigned @var{sched_ctx_id}, int @var{type})
  460. Creates a worker collection of the type indicated by the last parameter for the context specified through the first parameter
  461. @end deftypefun
  462. @deftypefun void starpu_delete_worker_collection_for_sched_ctx(unsigned @var{sched_ctx_id})
  463. Deletes the worker collection of the specified scheduling context
  464. @end deftypefun
  465. @deftypefun struct worker_collection* starpu_get_worker_collection_of_sched_ctx(unsigned @var{sched_ctx_id})
  466. Returns the worker collection managed by the indicated context
  467. @end deftypefun
  468. @deftypefun pthread_mutex_t* starpu_get_changing_ctx_mutex(unsigned @var{sched_ctx_id})
  469. @end deftypefun
  470. @deftypefun void starpu_set_sched_ctx(unsigned *@var{sched_ctx})
  471. Sets the scheduling context the task will be submitted to
  472. @end deftypefun
  473. @deftypefun unsigned starpu_get_sched_ctx(void)
  474. Returns the scheduling contexts the tasks are currently submitted to
  475. @end deftypefun
  476. @deftypefun unsigned starpu_get_nworkers_of_sched_ctx(unsigned @var{sched_ctx})
  477. Returns the number of workers managed by the specified contexts
  478. (Usually needed to verify if it manages any workers or if it should be blocked)
  479. @end deftypefun
  480. @deftypefun unsigned starpu_get_nshared_workers(unsigned @var{sched_ctx_id}, unsigned @var{sched_ctx_id2})
  481. Returns the number of workers shared by two contexts
  482. @end deftypefun
  483. @node Defining a new scheduling policy
  484. @section Defining a new scheduling policy
  485. TODO
  486. A full example showing how to define a new scheduling policy is available in
  487. the StarPU sources in the directory @code{examples/scheduler/}.
  488. @menu
  489. * Scheduling Policy API:: Scheduling Policy API
  490. * Source code::
  491. @end menu
  492. @node Scheduling Policy API
  493. @subsection Scheduling Policy API
  494. While StarPU comes with a variety of scheduling policies (@pxref{Task
  495. scheduling policy}), it may sometimes be desirable to implement custom
  496. policies to address specific problems. The API described below allows
  497. users to write their own scheduling policy.
  498. @deftp {Data Type} {struct starpu_sched_policy}
  499. This structure contains all the methods that implement a scheduling policy. An
  500. application may specify which scheduling strategy in the @code{sched_policy}
  501. field of the @code{starpu_conf} structure passed to the @code{starpu_init}
  502. function. The different fields are:
  503. @table @asis
  504. @item @code{void (*init_sched)(unsigned sched_ctx_id)}
  505. Initialize the scheduling policy.
  506. @item @code{void (*deinit_sched)(unsigned sched_ctx_id)}
  507. Cleanup the scheduling policy.
  508. @item @code{int (*push_task)(struct starpu_task *)}
  509. Insert a task into the scheduler.
  510. @item @code{void (*push_task_notify)(struct starpu_task *, int workerid)}
  511. Notify the scheduler that a task was pushed on a given worker. This method is
  512. called when a task that was explicitely assigned to a worker becomes ready and
  513. is about to be executed by the worker. This method therefore permits to keep
  514. the state of of the scheduler coherent even when StarPU bypasses the scheduling
  515. strategy.
  516. @item @code{struct starpu_task *(*pop_task)(unsigned sched_ctx_id)} (optional)
  517. Get a task from the scheduler. The mutex associated to the worker is already
  518. taken when this method is called. If this method is defined as @code{NULL}, the
  519. worker will only execute tasks from its local queue. In this case, the
  520. @code{push_task} method should use the @code{starpu_push_local_task} method to
  521. assign tasks to the different workers.
  522. @item @code{struct starpu_task *(*pop_every_task)(unsigned sched_ctx_id)}
  523. Remove all available tasks from the scheduler (tasks are chained by the means
  524. of the prev and next fields of the starpu_task structure). The mutex associated
  525. to the worker is already taken when this method is called. This is currently
  526. only used by the Gordon driver.
  527. @item @code{void (*pre_exec_hook)(struct starpu_task *)} (optional)
  528. This method is called every time a task is starting.
  529. @item @code{void (*post_exec_hook)(struct starpu_task *)} (optional)
  530. This method is called every time a task has been executed.
  531. @item @code{void (*add_workers)(unsigned sched_ctx_id, int *workerids, unsigned nworkers)}
  532. Initialize scheduling structures corresponding to each worker used by the policy.
  533. @item @code{void (*remove_workers)(unsigned sched_ctx_id, int *workerids, unsigned nworkers)}
  534. Deinitialize scheduling structures corresponding to each worker used by the policy.
  535. @item @code{const char *policy_name} (optional)
  536. Name of the policy.
  537. @item @code{const char *policy_description} (optional)
  538. Description of the policy.
  539. @end table
  540. @end deftp
  541. @deftypefun void starpu_worker_set_sched_condition (unsigned @var{sched_ctx_id}, int @var{workerid}, pthread_cond_t *@var{sched_cond}, pthread_mutex_t *@var{sched_mutex})
  542. This function specifies the condition variable associated to a worker per context
  543. When there is no available task for a worker, StarPU blocks this worker on a
  544. condition variable. This function specifies which condition variable (and the
  545. associated mutex) should be used to block (and to wake up) a worker. Note that
  546. multiple workers may use the same condition variable. For instance, in the case
  547. of a scheduling strategy with a single task queue, the same condition variable
  548. would be used to block and wake up all workers.
  549. The initialization method of a scheduling strategy (@code{init_sched}) must
  550. call this function once per worker.
  551. @end deftypefun
  552. @deftypefun void starpu_worker_get_sched_condition (unsigned @var{sched_ctx_id}, int @var{workerid}, pthread_cond_t **@var{sched_cond}, pthread_mutex_t **@var{sched_mutex})
  553. This function returns the condition variables associated to a worker in a context
  554. It is used in the policy to access to the local queue of the worker
  555. @end deftypefun
  556. @deftypefun void starpu_set_sched_ctx_policy_data(unsigned @var{sched_ctx}, void* @var{policy_data})
  557. Each scheduling policy uses some specific data (queues, variables, additional condition variables).
  558. It is memorize through a local structure. This function assigns it to a scheduling context.
  559. @end deftypefun
  560. @deftypefun void* starpu_get_sched_ctx_policy_data(unsigned @var{sched_ctx})
  561. Returns the policy data previously assigned to a context
  562. @end deftypefun
  563. @deftypefun void starpu_sched_set_min_priority (int @var{min_prio})
  564. Defines the minimum priority level supported by the scheduling policy. The
  565. default minimum priority level is the same as the default priority level which
  566. is 0 by convention. The application may access that value by calling the
  567. @code{starpu_sched_get_min_priority} function. This function should only be
  568. called from the initialization method of the scheduling policy, and should not
  569. be used directly from the application.
  570. @end deftypefun
  571. @deftypefun void starpu_sched_set_max_priority (int @var{max_prio})
  572. Defines the maximum priority level supported by the scheduling policy. The
  573. default maximum priority level is 1. The application may access that value by
  574. calling the @code{starpu_sched_get_max_priority} function. This function should
  575. only be called from the initialization method of the scheduling policy, and
  576. should not be used directly from the application.
  577. @end deftypefun
  578. @deftypefun int starpu_sched_get_min_priority (void)
  579. Returns the current minimum priority level supported by the
  580. scheduling policy
  581. @end deftypefun
  582. @deftypefun int starpu_sched_get_max_priority (void)
  583. Returns the current maximum priority level supported by the
  584. scheduling policy
  585. @end deftypefun
  586. @deftypefun int starpu_push_local_task (int @var{workerid}, {struct starpu_task} *@var{task}, int @var{back})
  587. The scheduling policy may put tasks directly into a worker's local queue so
  588. that it is not always necessary to create its own queue when the local queue
  589. is sufficient. If @var{back} not null, @var{task} is put at the back of the queue
  590. where the worker will pop tasks first. Setting @var{back} to 0 therefore ensures
  591. a FIFO ordering.
  592. @end deftypefun
  593. @deftypefun int starpu_worker_can_execute_task (unsigned @var{workerid}, {struct starpu_task *}@var{task}, unsigned {nimpl})
  594. 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.
  595. @end deftypefun
  596. @deftypefun double starpu_timing_now (void)
  597. Return the current date in µs
  598. @end deftypefun
  599. @deftypefun double starpu_task_expected_length ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  600. Returns expected task duration in µs
  601. @end deftypefun
  602. @deftypefun double starpu_worker_get_relative_speedup ({enum starpu_perf_archtype} @var{perf_archtype})
  603. Returns an estimated speedup factor relative to CPU speed
  604. @end deftypefun
  605. @deftypefun double starpu_task_expected_data_transfer_time (uint32_t @var{memory_node}, {struct starpu_task *}@var{task})
  606. Returns expected data transfer time in µs
  607. @end deftypefun
  608. @deftypefun double starpu_data_expected_transfer_time (starpu_data_handle_t @var{handle}, unsigned @var{memory_node}, {enum starpu_access_mode} @var{mode})
  609. Predict the transfer time (in µs) to move a handle to a memory node
  610. @end deftypefun
  611. @deftypefun double starpu_task_expected_power ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned @var{nimpl})
  612. Returns expected power consumption in J
  613. @end deftypefun
  614. @deftypefun double starpu_task_expected_conversion_time ({struct starpu_task *}@var{task}, {enum starpu_perf_archtype} @var{arch}, unsigned {nimpl})
  615. Returns expected conversion time in ms (multiformat interface only)
  616. @end deftypefun
  617. @node Source code
  618. @subsection Source code
  619. @cartouche
  620. @smallexample
  621. static struct starpu_sched_policy dummy_sched_policy = @{
  622. .init_sched = init_dummy_sched,
  623. .deinit_sched = deinit_dummy_sched,
  624. .add_workers = dummy_sched_add_workers,
  625. .remove_workers = dummy_sched_remove_workers,
  626. .push_task = push_task_dummy,
  627. .push_prio_task = NULL,
  628. .pop_task = pop_task_dummy,
  629. .post_exec_hook = NULL,
  630. .pop_every_task = NULL,
  631. .policy_name = "dummy",
  632. .policy_description = "dummy scheduling strategy"
  633. @};
  634. @end smallexample
  635. @end cartouche
  636. @node Expert mode
  637. @section Expert mode
  638. @deftypefun void starpu_wake_all_blocked_workers (void)
  639. Wake all the workers, so they can inspect data requests and task submissions
  640. again.
  641. @end deftypefun
  642. @deftypefun int starpu_progression_hook_register (unsigned (*@var{func})(void *arg), void *@var{arg})
  643. Register a progression hook, to be called when workers are idle.
  644. @end deftypefun
  645. @deftypefun void starpu_progression_hook_deregister (int @var{hook_id})
  646. Unregister a given progression hook.
  647. @end deftypefun