starpu_task_util.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2014 Inria
  4. * Copyright (C) 2010-2019 CNRS
  5. * Copyright (C) 2010-2015,2018-2019 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __STARPU_TASK_UTIL_H__
  19. #define __STARPU_TASK_UTIL_H__
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <starpu.h>
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. /**
  30. @defgroup API_Insert_Task Task Insert Utility
  31. @{
  32. */
  33. /* NOTE: when adding a value here, please make sure to update both
  34. * src/util/starpu_task_insert_utils.c (in two places) and
  35. * mpi/src/starpu_mpi_task_insert.c and mpi/src/starpu_mpi_task_insert_fortran.c */
  36. #define STARPU_MODE_SHIFT 17
  37. /**
  38. Used when calling starpu_task_insert(), must be followed by a
  39. pointer to a constant value and the size of the constant
  40. */
  41. #define STARPU_VALUE (1<<STARPU_MODE_SHIFT)
  42. /**
  43. Used when calling starpu_task_insert(), must be followed by a
  44. pointer to a callback function
  45. */
  46. #define STARPU_CALLBACK (2<<STARPU_MODE_SHIFT)
  47. /**
  48. Used when calling starpu_task_insert(), must be followed by two
  49. pointers: one to a callback function, and the other to be given as
  50. an argument to the callback function; this is equivalent to using
  51. both ::STARPU_CALLBACK and ::STARPU_CALLBACK_WITH_ARG.
  52. */
  53. #define STARPU_CALLBACK_WITH_ARG (3<<STARPU_MODE_SHIFT)
  54. /**
  55. Used when calling starpu_task_insert(), must be followed by a
  56. pointer to be given as an argument to the callback function
  57. */
  58. #define STARPU_CALLBACK_ARG (4<<STARPU_MODE_SHIFT)
  59. /**
  60. Used when calling starpu_task_insert(), must
  61. be followed by a integer defining a priority level
  62. */
  63. #define STARPU_PRIORITY (5<<STARPU_MODE_SHIFT)
  64. /**
  65. \ingroup API_MPI_Support
  66. Used when calling starpu_mpi_task_insert(), must be followed by a
  67. integer value which specified the node on which to execute the
  68. codelet.
  69. */
  70. #define STARPU_EXECUTE_ON_NODE (6<<STARPU_MODE_SHIFT)
  71. /**
  72. \ingroup API_MPI_Support
  73. Used when calling starpu_mpi_task_insert(), must be followed by a
  74. data handle to specify that the node owning the given data will
  75. execute the codelet.
  76. */
  77. #define STARPU_EXECUTE_ON_DATA (7<<STARPU_MODE_SHIFT)
  78. #define STARPU_DATA_ARRAY (8<<STARPU_MODE_SHIFT)
  79. #define STARPU_DATA_MODE_ARRAY (9<<STARPU_MODE_SHIFT)
  80. /**
  81. Used when calling starpu_task_insert(), must be followed by a tag.
  82. */
  83. #define STARPU_TAG (10<<STARPU_MODE_SHIFT)
  84. #define STARPU_HYPERVISOR_TAG (11<<STARPU_MODE_SHIFT)
  85. /**
  86. Used when calling starpu_task_insert(), must be followed by an
  87. amount of floating point operations, as a double. Users <b>MUST</b>
  88. explicitly cast into double, otherwise parameter passing will not
  89. work.
  90. */
  91. #define STARPU_FLOPS (12<<STARPU_MODE_SHIFT)
  92. /**
  93. Used when calling starpu_task_insert(), must be followed by the id
  94. of the scheduling context to which to submit the task to.
  95. */
  96. #define STARPU_SCHED_CTX (13<<STARPU_MODE_SHIFT)
  97. #define STARPU_PROLOGUE_CALLBACK (14<<STARPU_MODE_SHIFT)
  98. #define STARPU_PROLOGUE_CALLBACK_ARG (15<<STARPU_MODE_SHIFT)
  99. #define STARPU_PROLOGUE_CALLBACK_POP (16<<STARPU_MODE_SHIFT)
  100. #define STARPU_PROLOGUE_CALLBACK_POP_ARG (17<<STARPU_MODE_SHIFT)
  101. /**
  102. Used when calling starpu_task_insert(), must be followed by an
  103. integer value specifying the worker on which to execute the task
  104. (as specified by starpu_task::execute_on_a_specific_worker)
  105. */
  106. #define STARPU_EXECUTE_ON_WORKER (18<<STARPU_MODE_SHIFT)
  107. /**
  108. Used when calling starpu_task_insert(), must be followed by an
  109. unsigned long long value specifying the mask of worker on which to execute
  110. the task (as specified by starpu_task::where)
  111. */
  112. #define STARPU_EXECUTE_WHERE (19<<STARPU_MODE_SHIFT)
  113. /**
  114. Used when calling starpu_task_insert(), must be followed by a tag
  115. stored in starpu_task::tag_id. Leave starpu_task::use_tag as 0.
  116. */
  117. #define STARPU_TAG_ONLY (20<<STARPU_MODE_SHIFT)
  118. #define STARPU_POSSIBLY_PARALLEL (21<<STARPU_MODE_SHIFT)
  119. /**
  120. used when calling starpu_task_insert(), must be
  121. followed by an integer value specifying the worker order in which
  122. to execute the tasks (as specified by starpu_task::workerorder)
  123. */
  124. #define STARPU_WORKER_ORDER (22<<STARPU_MODE_SHIFT)
  125. /**
  126. \ingroup API_MPI_Support
  127. Used when calling starpu_mpi_task_insert(), must be followed by a
  128. identifier to a node selection policy. This is needed when several
  129. nodes own data in ::STARPU_W mode.
  130. */
  131. #define STARPU_NODE_SELECTION_POLICY (23<<STARPU_MODE_SHIFT)
  132. /**
  133. Used when calling starpu_task_insert(), must be followed by a
  134. char * stored in starpu_task::name.
  135. */
  136. #define STARPU_NAME (24<<STARPU_MODE_SHIFT)
  137. /**
  138. Used when calling starpu_task_insert(), must be followed by a
  139. memory buffer containing the arguments to be given to the task, and
  140. by the size of the arguments. The memory buffer should be the
  141. result of a previous call to starpu_codelet_pack_args(), and will
  142. be freed (i.e. starpu_task::cl_arg_free will be set to 1)
  143. */
  144. #define STARPU_CL_ARGS (25<<STARPU_MODE_SHIFT)
  145. /**
  146. Used when calling starpu_task_insert(), similarly to
  147. ::STARPU_CL_ARGS, must be followed by a memory buffer containing
  148. the arguments to be given to the task, and by the size of the
  149. arguments. The memory buffer should be the result of a previous
  150. call to starpu_codelet_pack_args(), and will NOT be freed (i.e.
  151. starpu_task::cl_arg_free will be set to 0)
  152. */
  153. #define STARPU_CL_ARGS_NFREE (26<<STARPU_MODE_SHIFT)
  154. /**
  155. Used when calling starpu_task_insert(), must be followed by a
  156. number of tasks as int, and an array containing these tasks. The
  157. function starpu_task_declare_deps_array() will be called with the
  158. given values.
  159. */
  160. #define STARPU_TASK_DEPS_ARRAY (27<<STARPU_MODE_SHIFT)
  161. /**
  162. Used when calling starpu_task_insert(), must be followed by an
  163. integer representing a color
  164. */
  165. #define STARPU_TASK_COLOR (28<<STARPU_MODE_SHIFT)
  166. /**
  167. Used when calling starpu_task_insert(), must be followed by an
  168. array of characters representing the sequential consistency for
  169. each buffer of the task.
  170. */
  171. #define STARPU_HANDLES_SEQUENTIAL_CONSISTENCY (29<<STARPU_MODE_SHIFT)
  172. /**
  173. Used when calling starpu_task_insert(), must be followed by an
  174. integer stating if the task is synchronous or not
  175. */
  176. #define STARPU_TASK_SYNCHRONOUS (30<<STARPU_MODE_SHIFT)
  177. /**
  178. Used when calling starpu_task_insert(), must be followed by a
  179. number of tasks as int, and an array containing these tasks. The
  180. function starpu_task_declare_end_deps_array() will be called with
  181. the given values.
  182. */
  183. #define STARPU_TASK_END_DEPS_ARRAY (31<<STARPU_MODE_SHIFT)
  184. /**
  185. Used when calling starpu_task_insert(), must be followed by an
  186. integer which will be given to starpu_task_end_dep_add()
  187. */
  188. #define STARPU_TASK_END_DEP (32<<STARPU_MODE_SHIFT)
  189. #define STARPU_SHIFTED_MODE_MAX (33<<STARPU_MODE_SHIFT)
  190. /**
  191. Set the given \p task corresponding to \p cl with the following arguments.
  192. The argument list must be zero-terminated. The arguments
  193. following the codelet are the same as the ones for the function
  194. starpu_task_insert().
  195. If some arguments of type ::STARPU_VALUE are given, the parameter
  196. starpu_task::cl_arg_free will be set to 1.
  197. */
  198. int starpu_task_set(struct starpu_task *task, struct starpu_codelet *cl, ...);
  199. /**
  200. Create a task corresponding to \p cl with the following arguments.
  201. The argument list must be zero-terminated. The arguments
  202. following the codelet are the same as the ones for the function
  203. starpu_task_insert().
  204. If some arguments of type ::STARPU_VALUE are given, the parameter
  205. starpu_task::cl_arg_free will be set to 1.
  206. */
  207. struct starpu_task *starpu_task_build(struct starpu_codelet *cl, ...);
  208. /**
  209. Create and submit a task corresponding to \p cl with the following
  210. given arguments. The argument list must be zero-terminated.
  211. The arguments following the codelet can be of the following types:
  212. <ul>
  213. <li> ::STARPU_R, ::STARPU_W, ::STARPU_RW, ::STARPU_SCRATCH,
  214. ::STARPU_REDUX an access mode followed by a data handle;
  215. <li> ::STARPU_DATA_ARRAY followed by an array of data handles and
  216. its number of elements;
  217. <li> ::STARPU_DATA_MODE_ARRAY followed by an array of struct
  218. starpu_data_descr, i.e data handles with their associated access
  219. modes, and its number of elements;
  220. <li> ::STARPU_EXECUTE_ON_WORKER, ::STARPU_WORKER_ORDER followed by
  221. an integer value specifying the worker on which to execute the task
  222. (as specified by starpu_task::execute_on_a_specific_worker)
  223. <li> the specific values ::STARPU_VALUE, ::STARPU_CALLBACK,
  224. ::STARPU_CALLBACK_ARG, ::STARPU_CALLBACK_WITH_ARG,
  225. ::STARPU_PRIORITY, ::STARPU_TAG, ::STARPU_TAG_ONLY, ::STARPU_FLOPS,
  226. ::STARPU_SCHED_CTX, ::STARPU_CL_ARGS, ::STARPU_CL_ARGS_NFREE,
  227. ::STARPU_TASK_DEPS_ARRAY, ::STARPU_TASK_COLOR,
  228. ::STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, ::STARPU_TASK_SYNCHRONOUS,
  229. ::STARPU_TASK_END_DEP followed by the appropriated objects as
  230. defined elsewhere.
  231. </ul>
  232. When using ::STARPU_DATA_ARRAY, the access mode of the data handles
  233. is not defined, it will be taken from the codelet
  234. starpu_codelet::modes or starpu_codelet::dyn_modes field. One
  235. should use ::STARPU_DATA_MODE_ARRAY to define the data handles
  236. along with the access modes.
  237. Parameters to be passed to the codelet implementation are defined
  238. through the type ::STARPU_VALUE. The function
  239. starpu_codelet_unpack_args() must be called within the codelet implementation to retrieve them.
  240. */
  241. int starpu_task_insert(struct starpu_codelet *cl, ...);
  242. /**
  243. Similar to starpu_task_insert(). Kept to avoid breaking old codes.
  244. */
  245. int starpu_insert_task(struct starpu_codelet *cl, ...);
  246. /**
  247. Assuming that there are already \p current_buffer data handles
  248. passed to the task, and if *allocated_buffers is not 0, the
  249. <c>task->dyn_handles</c> array has size \p *allocated_buffers, this
  250. function makes room for \p room other data handles, allocating or
  251. reallocating <c>task->dyn_handles</c> as necessary and updating \p
  252. *allocated_buffers accordingly. One can thus start with
  253. *allocated_buffers equal to 0 and current_buffer equal to 0, then
  254. make room by calling this function, then store handles with
  255. STARPU_TASK_SET_HANDLE(), make room again with this function, store
  256. yet more handles, etc.
  257. */
  258. void starpu_task_insert_data_make_room(struct starpu_codelet *cl, struct starpu_task *task, int *allocated_buffers, int current_buffer, int room);
  259. /**
  260. Store data handle \p handle into task \p task with mode \p
  261. arg_type, updating \p *allocated_buffers and \p *current_buffer
  262. accordingly.
  263. */
  264. void starpu_task_insert_data_process_arg(struct starpu_codelet *cl, struct starpu_task *task, int *allocated_buffers, int *current_buffer, int arg_type, starpu_data_handle_t handle);
  265. /**
  266. Store \p nb_handles data handles \p handles into task \p task,
  267. updating \p *allocated_buffers and \p *current_buffer accordingly.
  268. */
  269. void starpu_task_insert_data_process_array_arg(struct starpu_codelet *cl, struct starpu_task *task, int *allocated_buffers, int *current_buffer, int nb_handles, starpu_data_handle_t *handles);
  270. /**
  271. Store \p nb_descrs data handles described by \p descrs into task \p
  272. task, updating \p *allocated_buffers and \p *current_buffer
  273. accordingly.
  274. */
  275. void starpu_task_insert_data_process_mode_array_arg(struct starpu_codelet *cl, struct starpu_task *task, int *allocated_buffers, int *current_buffer, int nb_descrs, struct starpu_data_descr *descrs);
  276. /**
  277. Pack arguments of type ::STARPU_VALUE into a buffer which can be
  278. given to a codelet and later unpacked with the function
  279. starpu_codelet_unpack_args().
  280. Instead of calling starpu_codelet_pack_args(), one can also call
  281. starpu_codelet_pack_arg_init(), then starpu_codelet_pack_arg() for
  282. each data, then starpu_codelet_pack_arg_fini().
  283. */
  284. void starpu_codelet_pack_args(void **arg_buffer, size_t *arg_buffer_size, ...);
  285. struct starpu_codelet_pack_arg_data
  286. {
  287. char *arg_buffer;
  288. size_t arg_buffer_size;
  289. size_t current_offset;
  290. int nargs;
  291. };
  292. /**
  293. Initialize struct starpu_codelet_pack_arg before calling
  294. starpu_codelet_pack_arg() and starpu_codelet_pack_arg_fini(). This
  295. will simply initialize the content of the structure.
  296. */
  297. void starpu_codelet_pack_arg_init(struct starpu_codelet_pack_arg_data *state);
  298. /**
  299. Pack one argument into struct starpu_codelet_pack_arg \p state.
  300. That structure has to be initialized before with
  301. starpu_codelet_pack_arg_init(), and after all
  302. starpu_codelet_pack_arg() calls performed,
  303. starpu_codelet_pack_arg_fini() has to be used to get the \p cl_arg
  304. and \p cl_arg_size to be put in the task.
  305. */
  306. void starpu_codelet_pack_arg(struct starpu_codelet_pack_arg_data *state, const void *ptr, size_t ptr_size);
  307. /**
  308. Finish packing data, after calling starpu_codelet_pack_arg_init()
  309. once and starpu_codelet_pack_arg() several times.
  310. */
  311. void starpu_codelet_pack_arg_fini(struct starpu_codelet_pack_arg_data *state, void **cl_arg, size_t *cl_arg_size);
  312. /**
  313. Retrieve the arguments of type ::STARPU_VALUE associated to a
  314. task automatically created using the function starpu_task_insert(). If
  315. any parameter's value is 0, unpacking will stop there and ignore the remaining
  316. parameters.
  317. */
  318. void starpu_codelet_unpack_args(void *cl_arg, ...);
  319. /**
  320. Similar to starpu_codelet_unpack_args(), but if any parameter is 0,
  321. copy the part of \p cl_arg that has not been read in \p buffer
  322. which can then be used in a later call to one of the unpack
  323. functions.
  324. */
  325. void starpu_codelet_unpack_args_and_copyleft(void *cl_arg, void *buffer, size_t buffer_size, ...);
  326. /** @} */
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #endif /* __STARPU_TASK_UTIL_H__ */