starpu_data_filters.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Antoine Lucas
  4. * Copyright (C) 2009-2012,2014,2015,2017 Université de Bordeaux
  5. * Copyright (C) 2010 Mehdi Juhoor
  6. * Copyright (C) 2010-2013,2015,2017,2018,2019 CNRS
  7. * Copyright (C) 2011 Inria
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #ifndef __STARPU_DATA_FILTERS_H__
  21. #define __STARPU_DATA_FILTERS_H__
  22. /** @defgroup API_Data_Partition Data Partition
  23. @{
  24. */
  25. #include <starpu.h>
  26. #include <stdarg.h>
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. struct starpu_data_interface_ops;
  32. /** Describe a data partitioning operation, to be given to starpu_data_partition() */
  33. struct starpu_data_filter
  34. {
  35. /**
  36. Fill the \p child_interface structure with interface information
  37. for the \p i -th child of the parent \p father_interface (among
  38. \p nparts). The \p filter structure is provided, allowing to inspect the
  39. starpu_data_filter::filter_arg and starpu_data_filter::filter_arg_ptr
  40. parameters.
  41. The details of what needs to be filled in \p child_interface vary according
  42. to the data interface, but generally speaking:
  43. <ul>
  44. <li> <c>id</c> is usually just copied over from the father,
  45. when the sub data has the same structure as the father,
  46. e.g. a subvector is a vector, a submatrix is a matrix, etc.
  47. This is however not the case for instance when dividing a
  48. BCSR matrix into its dense blocks, which then are matrices.
  49. </li>
  50. <li> <c>nx</c>, <c>ny</c> and alike are usually divided by
  51. the number of subdata, depending how the subdivision is
  52. done (e.g. nx division vs ny division for vertical matrix
  53. division vs horizontal matrix division). </li>
  54. <li> <c>ld</c> for matrix interfaces are usually just
  55. copied over: the leading dimension (ld) usually does not
  56. change. </li>
  57. <li> <c>elemsize</c> is usually just copied over. </li>
  58. <li> <c>ptr</c>, the pointer to the data, has to be
  59. computed according to \p i and the father's <c>ptr</c>, so
  60. as to point to the start of the sub data. This should
  61. however be done only if the father has <c>ptr</c> different
  62. from NULL: in the OpenCL case notably, the
  63. <c>dev_handle</c> and <c>offset</c> fields are used
  64. instead. </li>
  65. <li> <c>dev_handle</c> should be just copied over from the
  66. parent. </li>
  67. <li> <c>offset</c> has to be computed according to \p i and
  68. the father's <c>offset</c>, so as to provide the offset of
  69. the start of the sub data. This is notably used for the
  70. OpenCL case.
  71. </ul>
  72. */
  73. void (*filter_func)(void *father_interface, void *child_interface, struct starpu_data_filter *, unsigned id, unsigned nparts);
  74. unsigned nchildren; /**< Number of parts to partition the data into. */
  75. /**
  76. Return the number of children. This can be used instead of
  77. starpu_data_filter::nchildren when the number of children depends
  78. on the actual data (e.g. the number of blocks in a sparse
  79. matrix).
  80. */
  81. unsigned (*get_nchildren)(struct starpu_data_filter *, starpu_data_handle_t initial_handle);
  82. /**
  83. When children use different data interface,
  84. return which interface is used by child number \p id.
  85. */
  86. struct starpu_data_interface_ops *(*get_child_ops)(struct starpu_data_filter *, unsigned id);
  87. unsigned filter_arg; /**< Additional parameter for the filter function */
  88. /**
  89. Additional pointer parameter for
  90. the filter function, such as the
  91. sizes of the different parts. */
  92. void *filter_arg_ptr;
  93. };
  94. /** @name Basic API
  95. *
  96. * @{
  97. */
  98. /**
  99. Request the partitioning of \p initial_handle into several subdata
  100. according to the filter \p f.
  101. Here an example of how to use the function.
  102. \code{.c}
  103. struct starpu_data_filter f =
  104. {
  105. .filter_func = starpu_matrix_filter_block,
  106. .nchildren = nslicesx
  107. };
  108. starpu_data_partition(A_handle, &f);
  109. \endcode
  110. */
  111. void starpu_data_partition(starpu_data_handle_t initial_handle, struct starpu_data_filter *f);
  112. /**
  113. Unapply the filter which has been applied to \p root_data, thus
  114. unpartitioning the data. The pieces of data are collected back into
  115. one big piece in the \p gathering_node (usually ::STARPU_MAIN_RAM).
  116. Tasks working on the partitioned data will be waited for
  117. by starpu_data_unpartition().
  118. Here an example of how to use the function.
  119. \code{.c}
  120. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  121. \endcode
  122. */
  123. void starpu_data_unpartition(starpu_data_handle_t root_data, unsigned gathering_node);
  124. /**
  125. Return the \p i -th child of the given \p handle, which must have
  126. been partitionned beforehand.
  127. */
  128. starpu_data_handle_t starpu_data_get_child(starpu_data_handle_t handle, unsigned i);
  129. /**
  130. Return the number of children \p handle has been partitioned into.
  131. */
  132. int starpu_data_get_nb_children(starpu_data_handle_t handle);
  133. /**
  134. After partitioning a StarPU data by applying a filter,
  135. starpu_data_get_sub_data() can be used to get handles for each of the
  136. data portions. \p root_data is the parent data that was partitioned.
  137. \p depth is the number of filters to traverse (in case several filters
  138. have been applied, to e.g. partition in row blocks, and then in column
  139. blocks), and the subsequent parameters are the indexes. The function
  140. returns a handle to the subdata.
  141. Here an example of how to use the function.
  142. \code{.c}
  143. h = starpu_data_get_sub_data(A_handle, 1, taskx);
  144. \endcode
  145. */
  146. starpu_data_handle_t starpu_data_get_sub_data(starpu_data_handle_t root_data, unsigned depth, ... );
  147. /**
  148. Similar to starpu_data_get_sub_data() but use a \c va_list for the
  149. parameter list.
  150. */
  151. starpu_data_handle_t starpu_data_vget_sub_data(starpu_data_handle_t root_data, unsigned depth, va_list pa);
  152. /**
  153. Apply \p nfilters filters to the handle designated by \p
  154. root_handle recursively. \p nfilters pointers to variables of the
  155. type starpu_data_filter should be given.
  156. */
  157. void starpu_data_map_filters(starpu_data_handle_t root_data, unsigned nfilters, ...);
  158. /**
  159. Apply \p nfilters filters to the handle designated by
  160. \p root_handle recursively. Use a \p va_list of pointers to
  161. variables of the type starpu_data_filter.
  162. */
  163. void starpu_data_vmap_filters(starpu_data_handle_t root_data, unsigned nfilters, va_list pa);
  164. /** @} */
  165. /** @name Asynchronous API
  166. *
  167. * @{
  168. */
  169. /**
  170. Plan to partition \p initial_handle into several subdata according to
  171. the filter \p f.
  172. The handles are returned into the \p children array, which has to be
  173. the same size as the number of parts described in \p f. These handles
  174. are not immediately usable, starpu_data_partition_submit() has to be
  175. called to submit the actual partitioning.
  176. Here is an example of how to use the function:
  177. \code{.c}
  178. starpu_data_handle_t children[nslicesx];
  179. struct starpu_data_filter f =
  180. {
  181. .filter_func = starpu_matrix_filter_block,
  182. .nchildren = nslicesx
  183. };
  184. starpu_data_partition_plan(A_handle, &f, children);
  185. \endcode
  186. */
  187. void starpu_data_partition_plan(starpu_data_handle_t initial_handle, struct starpu_data_filter *f, starpu_data_handle_t *children);
  188. /**
  189. Submit the actual partitioning of \p initial_handle into the \p nparts
  190. \p children handles. This call is asynchronous, it only submits that the
  191. partitioning should be done, so that the \p children handles can now be used to
  192. submit tasks, and \p initial_handle can not be used to submit tasks any more (to
  193. guarantee coherency).
  194. For instance,
  195. \code{.c}
  196. starpu_data_partition_submit(A_handle, nslicesx, children);
  197. \endcode
  198. */
  199. void starpu_data_partition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children);
  200. /**
  201. Similar to starpu_data_partition_submit(), but do not invalidate \p
  202. initial_handle. This allows to continue using it, but the application has to be
  203. careful not to write to \p initial_handle or \p children handles, only read from
  204. them, since the coherency is otherwise not guaranteed. This thus allows to
  205. submit various tasks which concurrently read from various partitions of the data.
  206. When the application wants to write to \p initial_handle again, it should call
  207. starpu_data_unpartition_submit(), which will properly add dependencies between the
  208. reads on the \p children and the writes to be submitted.
  209. If instead the application wants to write to \p children handles, it should
  210. call starpu_data_partition_readwrite_upgrade_submit(), which will correctly add
  211. dependencies between the reads on the \p initial_handle and the writes to be
  212. submitted.
  213. */
  214. void starpu_data_partition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children);
  215. /**
  216. Assume that a partitioning of \p initial_handle has already been submited
  217. in readonly mode through starpu_data_partition_readonly_submit(), and will upgrade
  218. that partitioning into read-write mode for the \p children, by invalidating \p
  219. initial_handle, and adding the necessary dependencies.
  220. */
  221. void starpu_data_partition_readwrite_upgrade_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children);
  222. /**
  223. Assuming that \p initial_handle is partitioned into \p children,
  224. submit an unpartitionning of \p initial_handle, i.e. submit a
  225. gathering of the pieces on the requested \p gathering_node memory
  226. node, and submit an invalidation of the children.
  227. */
  228. void starpu_data_unpartition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gathering_node);
  229. void starpu_data_unpartition_submit_r(starpu_data_handle_t initial_handle, int gathering_node);
  230. /**
  231. Similar to starpu_data_partition_submit(), but do not invalidate \p
  232. initial_handle. This allows to continue using it, but the application has to be
  233. careful not to write to \p initial_handle or \p children handles, only read from
  234. them, since the coherency is otherwise not guaranteed. This thus allows to
  235. submit various tasks which concurrently read from various
  236. partitions of the data.
  237. */
  238. void starpu_data_unpartition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gathering_node);
  239. /**
  240. Clear the partition planning established between \p root_data and
  241. \p children with starpu_data_partition_plan(). This will notably
  242. submit an unregister all the \p children, which can thus not be
  243. used any more afterwards.
  244. */
  245. void starpu_data_partition_clean(starpu_data_handle_t root_data, unsigned nparts, starpu_data_handle_t *children);
  246. /**
  247. Similar to starpu_data_unpartition_submit_sequential_consistency()
  248. but allow to specify a callback function for the unpartitiong task
  249. */
  250. void starpu_data_unpartition_submit_sequential_consistency_cb(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node, int sequential_consistency, void (*callback_func)(void *), void *callback_arg);
  251. /**
  252. Similar to starpu_data_partition_submit() but also allow to specify
  253. the coherency to be used for the main data \p initial_handle
  254. through the parameter \p sequential_consistency.
  255. */
  256. void starpu_data_partition_submit_sequential_consistency(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int sequential_consistency);
  257. /**
  258. Similar to starpu_data_unpartition_submit() but also allow to specify
  259. the coherency to be used for the main data \p initial_handle
  260. through the parameter \p sequential_consistency.
  261. */
  262. void starpu_data_unpartition_submit_sequential_consistency(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gathering_node, int sequential_consistency);
  263. /**
  264. Disable the automatic partitioning of the data \p handle for which
  265. a asynchronous plan has previously been submitted
  266. */
  267. void starpu_data_partition_not_automatic(starpu_data_handle_t handle);
  268. /** @} */
  269. /** @name Predefined BCSR Filter Functions
  270. * Predefined partitioning functions for BCSR data. Examples on how to
  271. * use them are shown in \ref PartitioningData.
  272. * @{
  273. */
  274. /**
  275. Partition a block-sparse matrix into dense matrices.
  276. */
  277. void starpu_bcsr_filter_canonical_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  278. /** @} */
  279. /** @name Predefined CSR Filter Functions
  280. * Predefined partitioning functions for CSR data. Examples on how to
  281. * use them are shown in \ref PartitioningData.
  282. * @{
  283. */
  284. /**
  285. Partition a block-sparse matrix into vertical block-sparse matrices.
  286. */
  287. void starpu_csr_filter_vertical_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  288. /** @} */
  289. /** @name Predefined Matrix Filter Functions
  290. * Predefined partitioning functions for matrix
  291. * data. Examples on how to use them are shown in \ref
  292. * PartitioningData.
  293. * @{
  294. */
  295. /**
  296. Partition a dense Matrix along the x dimension, thus getting (x/\p
  297. nparts ,y) matrices. If \p nparts does not divide x, the last
  298. submatrix contains the remainder.
  299. */
  300. void starpu_matrix_filter_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  301. /**
  302. Partition a dense Matrix along the x dimension, with a
  303. shadow border <c>filter_arg_ptr</c>, thus getting ((x-2*shadow)/\p
  304. nparts +2*shadow,y) matrices. If \p nparts does not divide x-2*shadow,
  305. the last submatrix contains the remainder.
  306. <b>IMPORTANT</b>: This can
  307. only be used for read-only access, as no coherency is enforced for the
  308. shadowed parts. A usage example is available in
  309. examples/filters/shadow2d.c
  310. */
  311. void starpu_matrix_filter_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  312. /**
  313. Partition a dense Matrix along the y dimension, thus getting
  314. (x,y/\p nparts) matrices. If \p nparts does not divide y, the last
  315. submatrix contains the remainder.
  316. */
  317. void starpu_matrix_filter_vertical_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  318. /**
  319. Partition a dense Matrix along the y dimension, with a
  320. shadow border <c>filter_arg_ptr</c>, thus getting
  321. (x,(y-2*shadow)/\p nparts +2*shadow) matrices. If \p nparts does not
  322. divide y-2*shadow, the last submatrix contains the remainder.
  323. <b>IMPORTANT</b>: This can only be used for read-only access, as no
  324. coherency is enforced for the shadowed parts. A usage example is
  325. available in examples/filters/shadow2d.c
  326. */
  327. void starpu_matrix_filter_vertical_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  328. /** @} */
  329. /** @name Predefined Vector Filter Functions
  330. * Predefined partitioning functions for vector
  331. * data. Examples on how to use them are shown in \ref
  332. * PartitioningData.
  333. * @{
  334. */
  335. /**
  336. Return in \p child_interface the \p id th element of the vector
  337. represented by \p father_interface once partitioned in \p nparts chunks of
  338. equal size.
  339. */
  340. void starpu_vector_filter_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  341. /**
  342. Return in \p child_interface the \p id th element of the vector
  343. represented by \p father_interface once partitioned in \p nparts chunks of
  344. equal size with a shadow border <c>filter_arg_ptr</c>, thus getting a vector
  345. of size <c>(n-2*shadow)/nparts+2*shadow</c>. The <c>filter_arg_ptr</c> field
  346. of \p f must be the shadow size casted into \c void*.
  347. <b>IMPORTANT</b>: This can only be used for read-only access, as no coherency is
  348. enforced for the shadowed parts. An usage example is available in
  349. examples/filters/shadow.c
  350. */
  351. void starpu_vector_filter_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  352. /**
  353. Return in \p child_interface the \p id th element of the vector
  354. represented by \p father_interface once partitioned into \p nparts chunks
  355. according to the <c>filter_arg_ptr</c> field of \p f. The
  356. <c>filter_arg_ptr</c> field must point to an array of \p nparts long
  357. elements, each of which specifies the number of elements in each chunk
  358. of the partition.
  359. */
  360. void starpu_vector_filter_list_long(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  361. /**
  362. Return in \p child_interface the \p id th element of the vector
  363. represented by \p father_interface once partitioned into \p nparts chunks
  364. according to the <c>filter_arg_ptr</c> field of \p f. The
  365. <c>filter_arg_ptr</c> field must point to an array of \p nparts uint32_t
  366. elements, each of which specifies the number of elements in each chunk
  367. of the partition.
  368. */
  369. void starpu_vector_filter_list(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  370. /**
  371. Return in \p child_interface the \p id th element of the vector
  372. represented by \p father_interface once partitioned in <c>2</c> chunks of
  373. equal size, ignoring nparts. Thus, \p id must be <c>0</c> or <c>1</c>.
  374. */
  375. void starpu_vector_filter_divide_in_2(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  376. /** @} */
  377. /** @name Predefined Block Filter Functions
  378. * Predefined partitioning functions for block data. Examples on how
  379. * to use them are shown in \ref PartitioningData. An example is
  380. * available in \c examples/filters/shadow3d.c
  381. * @{
  382. */
  383. /**
  384. Partition a block along the X dimension, thus getting
  385. (x/\p nparts ,y,z) 3D matrices. If \p nparts does not divide x, the last
  386. submatrix contains the remainder.
  387. */
  388. void starpu_block_filter_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  389. /**
  390. Partition a block along the X dimension, with a
  391. shadow border <c>filter_arg_ptr</c>, thus getting
  392. ((x-2*shadow)/\p nparts +2*shadow,y,z) blocks. If \p nparts does not
  393. divide x, the last submatrix contains the remainder.
  394. <b>IMPORTANT</b>:
  395. This can only be used for read-only access, as no coherency is
  396. enforced for the shadowed parts.
  397. */
  398. void starpu_block_filter_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  399. /**
  400. Partition a block along the Y dimension, thus getting
  401. (x,y/\p nparts ,z) blocks. If \p nparts does not divide y, the last
  402. submatrix contains the remainder.
  403. */
  404. void starpu_block_filter_vertical_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  405. /**
  406. Partition a block along the Y dimension, with a
  407. shadow border <c>filter_arg_ptr</c>, thus getting
  408. (x,(y-2*shadow)/\p nparts +2*shadow,z) 3D matrices. If \p nparts does not
  409. divide y, the last submatrix contains the remainder.
  410. <b>IMPORTANT</b>:
  411. This can only be used for read-only access, as no coherency is
  412. enforced for the shadowed parts.
  413. */
  414. void starpu_block_filter_vertical_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  415. /**
  416. Partition a block along the Z dimension, thus getting
  417. (x,y,z/\p nparts) blocks. If \p nparts does not divide z, the last
  418. submatrix contains the remainder.
  419. */
  420. void starpu_block_filter_depth_block(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  421. /**
  422. Partition a block along the Z dimension, with a
  423. shadow border <c>filter_arg_ptr</c>, thus getting
  424. (x,y,(z-2*shadow)/\p nparts +2*shadow) blocks. If \p nparts does not
  425. divide z, the last submatrix contains the remainder.
  426. <b>IMPORTANT</b>:
  427. This can only be used for read-only access, as no coherency is
  428. enforced for the shadowed parts.
  429. */
  430. void starpu_block_filter_depth_block_shadow(void *father_interface, void *child_interface, struct starpu_data_filter *f, unsigned id, unsigned nparts);
  431. /** @} */
  432. #ifdef __cplusplus
  433. }
  434. #endif
  435. /** @} */
  436. #endif