310_data_management.doxy 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2019 CNRS
  4. * Copyright (C) 2009-2011,2014-2019 Université de Bordeaux
  5. * Copyright (C) 2011,2012 Inria
  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. /*! \page DataManagement Data Management
  19. TODO: intro which mentions consistency among other things
  20. \section DataInterface Data Interface
  21. StarPU provides several data interfaces for programmers to describe
  22. the data layout of their application. There are predefined interfaces
  23. already available in StarPU. Users can define new data interfaces as
  24. explained in \ref DefiningANewDataInterface. All functions provided by
  25. StarPU are documented in \ref API_Data_Interfaces. You will find a
  26. short list below.
  27. \subsection VariableDataInterface Variable Data Interface
  28. A variable is a given-size byte element, typically a scalar. Here an
  29. example of how to register a variable data to StarPU by using
  30. starpu_variable_data_register().
  31. \code{.c}
  32. float var = 42.0;
  33. starpu_data_handle_t var_handle;
  34. starpu_variable_data_register(&var_handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(var));
  35. \endcode
  36. \subsection VectorDataInterface Vector Data Interface
  37. A vector is a fixed number of elements of a given size. Here an
  38. example of how to register a vector data to StarPU by using
  39. starpu_vector_data_register().
  40. \code{.c}
  41. float vector[NX];
  42. starpu_data_handle_t vector_handle;
  43. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  44. \endcode
  45. \subsection MatrixDataInterface Matrix Data Interface
  46. To register 2-D matrices with a potential padding, one can use the
  47. matrix data interface. Here an example of how to register a matrix
  48. data to StarPU by using starpu_matrix_data_register().
  49. \code{.c}
  50. float *matrix;
  51. starpu_data_handle_t matrix_handle;
  52. matrix = (float*)malloc(width * height * sizeof(float));
  53. starpu_matrix_data_register(&matrix_handle, STARPU_MAIN_RAM, (uintptr_t)matrix, width, width, height, sizeof(float));
  54. \endcode
  55. \subsection BlockDataInterface Block Data Interface
  56. To register 3-D blocks with potential paddings on Y and Z dimensions,
  57. one can use the block data interface. Here an example of how to
  58. register a block data to StarPU by using starpu_block_data_register().
  59. \code{.c}
  60. float *block;
  61. starpu_data_handle_t block_handle;
  62. block = (float*)malloc(nx*ny*nz*sizeof(float));
  63. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM, (uintptr_t)block, nx, nx*ny, nx, ny, nz, sizeof(float));
  64. \endcode
  65. \subsection BCSRDataInterface BCSR Data Interface
  66. BCSR (Blocked Compressed Sparse Row Representation) sparse matrix data
  67. can be registered to StarPU using the bcsr data interface. Here an
  68. example on how to do so by using starpu_bcsr_data_register().
  69. \code{.c}
  70. /*
  71. * We use the following matrix:
  72. *
  73. * +----------------+
  74. * | 0 1 0 0 |
  75. * | 2 3 0 0 |
  76. * | 4 5 8 9 |
  77. * | 6 7 10 11 |
  78. * +----------------+
  79. *
  80. * nzval = [0, 1, 2, 3] ++ [4, 5, 6, 7] ++ [8, 9, 10, 11]
  81. * colind = [0, 0, 1]
  82. * rowptr = [0, 1, 3]
  83. * r = c = 2
  84. */
  85. /* Size of the blocks */
  86. int R = 2;
  87. int C = 2;
  88. int NROWS = 2;
  89. int NNZ_BLOCKS = 3; /* out of 4 */
  90. int NZVAL_SIZE = (R*C*NNZ_BLOCKS);
  91. int nzval[NZVAL_SIZE] =
  92. {
  93. 0, 1, 2, 3, /* First block */
  94. 4, 5, 6, 7, /* Second block */
  95. 8, 9, 10, 11 /* Third block */
  96. };
  97. uint32_t colind[NNZ_BLOCKS] =
  98. {
  99. 0, /* block-column index for first block in nzval */
  100. 0, /* block-column index for second block in nzval */
  101. 1 /* block-column index for third block in nzval */
  102. };
  103. uint32_t rowptr[NROWS+1] =
  104. {
  105. 0, / * block-index in nzval of the first block of the first row. */
  106. 1, / * block-index in nzval of the first block of the second row. */
  107. NNZ_BLOCKS /* number of blocks, to allow an easier element's access for the kernels */
  108. };
  109. starpu_data_handle_t bcsr_handle;
  110. starpu_bcsr_data_register(&bcsr_handle,
  111. STARPU_MAIN_RAM,
  112. NNZ_BLOCKS,
  113. NROWS,
  114. (uintptr_t) nzval,
  115. colind,
  116. rowptr,
  117. 0, /* firstentry */
  118. R,
  119. C,
  120. sizeof(nzval[0]));
  121. \endcode
  122. StarPU provides an example on how to deal with such matrices in
  123. <c>examples/spmv</c>.
  124. \subsection CSRDataInterface CSR Data Interface
  125. TODO
  126. \subsection VariableSizeDataInterface Data Interface with Variable Size
  127. Tasks are actually allowed to change the size of data interfaces.
  128. The simplest case is just changing the amount of data actually used within the
  129. allocated buffer. This is for instance implemented for the matrix interface: one
  130. can set the new NX/NY values with STARPU_MATRIX_SET_NX(), STARPU_MATRIX_SET_NY(), and STARPU_MATRIX_SET_LD()
  131. at the end of the task implementation. Data transfers achieved by StarPU will
  132. then use these values instead of the whole allocated size. The values of course
  133. need to be set within the original allocation. To reserve room for increasing
  134. the NX/NY values, one can use starpu_matrix_data_register_allocsize() instead of
  135. starpu_matrix_data_register(), to specify the allocation size to be used instead
  136. of the default NX*NY*ELEMSIZE. To support this, the data interface
  137. has to implement the starpu_data_interface_ops::alloc_footprint and
  138. starpu_data_interface_ops::alloc_compare methods, for proper StarPU allocation
  139. management.
  140. A more involved case is changing the amount of allocated data.
  141. The task implementation can just reallocate the buffer during its execution, and
  142. set the proper new values in the interface structure, e.g. nx, ny, ld, etc. so
  143. that the StarPU core knows the new data layout. The starpu_data_interface_ops
  144. structure however then needs to have the starpu_data_interface_ops::dontcache
  145. field set to 1, to prevent StarPU from trying to perform any cached allocation,
  146. since the allocated size will vary. An example is available in
  147. <c>tests/datawizard/variable_size.c</c>
  148. \section DataManagement Data Management
  149. When the application allocates data, whenever possible it should use
  150. the starpu_malloc() function, which will ask CUDA or OpenCL to make
  151. the allocation itself and pin the corresponding allocated memory, or to use the
  152. starpu_memory_pin() function to pin memory allocated by other ways, such as local arrays. This
  153. is needed to permit asynchronous data transfer, i.e. permit data
  154. transfer to overlap with computations. Otherwise, the trace will show
  155. that the <c>DriverCopyAsync</c> state takes a lot of time, this is
  156. because CUDA or OpenCL then reverts to synchronous transfers.
  157. The application can provide its own allocation function by calling
  158. starpu_malloc_set_hooks(). StarPU will then use them for all data handle
  159. allocations in the main memory.
  160. By default, StarPU leaves replicates of data wherever they were used, in case they
  161. will be re-used by other tasks, thus saving the data transfer time. When some
  162. task modifies some data, all the other replicates are invalidated, and only the
  163. processing unit which ran this task will have a valid replicate of the data. If the application knows
  164. that this data will not be re-used by further tasks, it should advise StarPU to
  165. immediately replicate it to a desired list of memory nodes (given through a
  166. bitmask). This can be understood like the write-through mode of CPU caches.
  167. \code{.c}
  168. starpu_data_set_wt_mask(img_handle, 1<<0);
  169. \endcode
  170. will for instance request to always automatically transfer a replicate into the
  171. main memory (node <c>0</c>), as bit <c>0</c> of the write-through bitmask is being set.
  172. \code{.c}
  173. starpu_data_set_wt_mask(img_handle, ~0U);
  174. \endcode
  175. will request to always automatically broadcast the updated data to all memory
  176. nodes.
  177. Setting the write-through mask to <c>~0U</c> can also be useful to make sure all
  178. memory nodes always have a copy of the data, so that it is never evicted when
  179. memory gets scarse.
  180. Implicit data dependency computation can become expensive if a lot
  181. of tasks access the same piece of data. If no dependency is required
  182. on some piece of data (e.g. because it is only accessed in read-only
  183. mode, or because write accesses are actually commutative), use the
  184. function starpu_data_set_sequential_consistency_flag() to disable
  185. implicit dependencies on this data.
  186. In the same vein, accumulation of results in the same data can become a
  187. bottleneck. The use of the mode ::STARPU_REDUX permits to optimize such
  188. accumulation (see \ref DataReduction). To a lesser extent, the use of
  189. the flag ::STARPU_COMMUTE keeps the bottleneck (see \ref DataCommute), but at least permits
  190. the accumulation to happen in any order.
  191. Applications often need a data just for temporary results. In such a case,
  192. registration can be made without an initial value, for instance this produces a vector data:
  193. \code{.c}
  194. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  195. \endcode
  196. StarPU will then allocate the actual buffer only when it is actually needed,
  197. e.g. directly on the GPU without allocating in main memory.
  198. In the same vein, once the temporary results are not useful any more, the
  199. data should be thrown away. If the handle is not to be reused, it can be
  200. unregistered:
  201. \code{.c}
  202. starpu_data_unregister_submit(handle);
  203. \endcode
  204. actual unregistration will be done after all tasks working on the handle
  205. terminate.
  206. If the handle is to be reused, instead of unregistering it, it can simply be invalidated:
  207. \code{.c}
  208. starpu_data_invalidate_submit(handle);
  209. \endcode
  210. the buffers containing the current value will then be freed, and reallocated
  211. only when another task writes some value to the handle.
  212. \section DataPrefetch Data Prefetch
  213. The scheduling policies <c>heft</c>, <c>dmda</c> and <c>pheft</c>
  214. perform data prefetch (see \ref STARPU_PREFETCH):
  215. as soon as a scheduling decision is taken for a task, requests are issued to
  216. transfer its required data to the target processing unit, if needed, so that
  217. when the processing unit actually starts the task, its data will hopefully be
  218. already available and it will not have to wait for the transfer to finish.
  219. The application may want to perform some manual prefetching, for several reasons
  220. such as excluding initial data transfers from performance measurements, or
  221. setting up an initial statically-computed data distribution on the machine
  222. before submitting tasks, which will thus guide StarPU toward an initial task
  223. distribution (since StarPU will try to avoid further transfers).
  224. This can be achieved by giving the function starpu_data_prefetch_on_node() the
  225. handle and the desired target memory node. The
  226. starpu_data_idle_prefetch_on_node() variant can be used to issue the transfer
  227. only when the bus is idle.
  228. Conversely, one can advise StarPU that some data will not be useful in the
  229. close future by calling starpu_data_wont_use(). StarPU will then write its value
  230. back to its home node, and evict it from GPUs when room is needed.
  231. \section PartitioningData Partitioning Data
  232. An existing piece of data can be partitioned in sub parts to be used by different tasks, for instance:
  233. \code{.c}
  234. #define NX 1048576
  235. #define PARTS 16
  236. int vector[NX];
  237. starpu_data_handle_t handle;
  238. /* Declare data to StarPU */
  239. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  240. /* Partition the vector in PARTS sub-vectors */
  241. struct starpu_data_filter f =
  242. {
  243. .filter_func = starpu_vector_filter_block,
  244. .nchildren = PARTS
  245. };
  246. starpu_data_partition(handle, &f);
  247. \endcode
  248. The task submission then uses the function starpu_data_get_sub_data()
  249. to retrieve the sub-handles to be passed as tasks parameters.
  250. \code{.c}
  251. /* Submit a task on each sub-vector */
  252. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  253. {
  254. /* Get subdata number i (there is only 1 dimension) */
  255. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  256. struct starpu_task *task = starpu_task_create();
  257. task->handles[0] = sub_handle;
  258. task->cl = &cl;
  259. task->synchronous = 1;
  260. task->cl_arg = &factor;
  261. task->cl_arg_size = sizeof(factor);
  262. starpu_task_submit(task);
  263. }
  264. \endcode
  265. Partitioning can be applied several times, see
  266. <c>examples/basic_examples/mult.c</c> and <c>examples/filters/</c>.
  267. Wherever the whole piece of data is already available, the partitioning will
  268. be done in-place, i.e. without allocating new buffers but just using pointers
  269. inside the existing copy. This is particularly important to be aware of when
  270. using OpenCL, where the kernel parameters are not pointers, but \c cl_mem handles. The
  271. kernel thus needs to be also passed the offset within the OpenCL buffer:
  272. \code{.c}
  273. void opencl_func(void *buffers[], void *cl_arg)
  274. {
  275. cl_mem vector = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  276. unsigned offset = STARPU_BLOCK_GET_OFFSET(buffers[0]);
  277. ...
  278. clSetKernelArg(kernel, 0, sizeof(vector), &vector);
  279. clSetKernelArg(kernel, 1, sizeof(offset), &offset);
  280. ...
  281. }
  282. \endcode
  283. And the kernel has to shift from the pointer passed by the OpenCL driver:
  284. \code{.c}
  285. __kernel void opencl_kernel(__global int *vector, unsigned offset)
  286. {
  287. block = (__global void *)block + offset;
  288. ...
  289. }
  290. \endcode
  291. When the sub-data is not of the same type as the original data, the
  292. starpu_data_filter::get_child_ops field needs to be set appropriately for StarPU
  293. to know which type should be used.
  294. StarPU provides various interfaces and filters for matrices, vectors, etc.,
  295. but applications can also write their own data interfaces and filters, see
  296. <c>examples/interface</c> and <c>examples/filters/custom_mf</c> for an example,
  297. and see \ref DefiningANewDataInterface and \ref DefiningANewDataFilter
  298. for documentation.
  299. \section AsynchronousPartitioning Asynchronous Partitioning
  300. The partitioning functions described in the previous section are synchronous:
  301. starpu_data_partition() and starpu_data_unpartition() both wait for all the tasks
  302. currently working on the data. This can be a bottleneck for the application.
  303. An asynchronous API also exists, it works only on handles with sequential
  304. consistency. The principle is to first plan the partitioning, which returns
  305. data handles of the partition, which are not functional yet. When submitting
  306. tasks, one can mix using the handles of the partition, of the whole data. One
  307. can even partition recursively and mix using handles at different levels of the
  308. recursion. Of course, StarPU will have to introduce coherency synchronization.
  309. <c>fmultiple_submit_implicit</c> is a complete example using this technique.
  310. One can also look at <c>fmultiple_submit_readonly</c> which contains the
  311. explicit coherency synchronization which are automatically introduced by StarPU
  312. for <c>fmultiple_submit_implicit</c>.
  313. In short, we first register a matrix and plan the partitioning:
  314. \code{.c}
  315. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  316. struct starpu_data_filter f_vert =
  317. {
  318. .filter_func = starpu_matrix_filter_block,
  319. .nchildren = PARTS
  320. };
  321. starpu_data_partition_plan(handle, &f_vert, vert_handle);
  322. \endcode
  323. starpu_data_partition_plan() returns the handles for the partition in <c>vert_handle</c>.
  324. One can then submit tasks working on the main handle, and tasks working on
  325. <c>vert_handle</c> handles. Between using the main handle and <c>vert_handle</c>
  326. handles, StarPU will automatically call starpu_data_partition_submit() and
  327. starpu_data_unpartition_submit().
  328. All this code is asynchronous, just submitting which tasks, partitioning and
  329. unpartitioning will be done at runtime.
  330. Planning several partitioning of the same data is also possible, StarPU will
  331. unpartition and repartition as needed when mixing accesses of different
  332. partitions. If data access is done in read-only mode, StarPU will allow the
  333. different partitioning to coexist. As soon as a data is accessed in read-write
  334. mode, StarPU will automatically unpartition everything and activate only the
  335. partitioning leading to the data being written to.
  336. For instance, for a stencil application, one can split a subdomain into
  337. its interior and halos, and then just submit a task updating the whole
  338. subdomain, then submit MPI sends/receives to update the halos, then submit
  339. again a task updating the whole subdomain, etc. and StarPU will automatically
  340. partition/unpartition each time.
  341. \section ManualPartitioning Manual Partitioning
  342. One can also handle partitioning by hand, by registering several views on the
  343. same piece of data. The idea is then to manage the coherency of the various
  344. views through the common buffer in the main memory.
  345. <c>fmultiple_manual</c> is a complete example using this technique.
  346. In short, we first register the same matrix several times:
  347. \code{.c}
  348. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  349. for (i = 0; i < PARTS; i++)
  350. starpu_matrix_data_register(&vert_handle[i], STARPU_MAIN_RAM, (uintptr_t)&matrix[0][i*(NX/PARTS)], NX, NX/PARTS, NY, sizeof(matrix[0][0]));
  351. \endcode
  352. Since StarPU is not aware that the two handles are actually pointing to the same
  353. data, we have a danger of inadvertently submitting tasks to both views, which
  354. will bring a mess since StarPU will not guarantee any coherency between the two
  355. views. To make sure we don't do this, we invalidate the view that we will not
  356. use:
  357. \code{.c}
  358. for (i = 0; i < PARTS; i++)
  359. starpu_data_invalidate(vert_handle[i]);
  360. \endcode
  361. Then we can safely work on <c>handle</c>.
  362. When we want to switch to the vertical slice view, all we need to do is bring
  363. coherency between them by running an empty task on the home node of the data:
  364. \code{.c}
  365. void empty(void *buffers[], void *cl_arg)
  366. { }
  367. struct starpu_codelet cl_switch =
  368. {
  369. .cpu_funcs = {empty},
  370. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  371. };
  372. ret = starpu_task_insert(&cl_switch, STARPU_RW, handle,
  373. STARPU_W, vert_handle[0],
  374. STARPU_W, vert_handle[1],
  375. 0);
  376. \endcode
  377. The execution of the <c>switch</c> task will get back the matrix data into the
  378. main memory, and thus the vertical slices will get the updated value there.
  379. Again, we prefer to make sure that we don't accidentally access the matrix through the whole-matrix handle:
  380. \code{.c}
  381. starpu_data_invalidate_submit(handle);
  382. \endcode
  383. And now we can start using vertical slices, etc.
  384. \section DefiningANewDataFilter Defining A New Data Filter
  385. StarPU provides a series of predefined filters in \ref API_Data_Partition, but
  386. additional filters can be defined by the application. The principle is that the
  387. filter function just fills the memory location of the <c>i-th</c> subpart of a data.
  388. Examples are provided in <c>src/datawizard/interfaces/*_filters.c</c>,
  389. and see \ref starpu_data_filter::filter_func for the details.
  390. The starpu_filter_nparts_compute_chunk_size_and_offset() helper can be used to
  391. compute the division of pieces of data.
  392. \section DataReduction Data Reduction
  393. In various cases, some piece of data is used to accumulate intermediate
  394. results. For instances, the dot product of a vector, maximum/minimum finding,
  395. the histogram of a photograph, etc. When these results are produced along the
  396. whole machine, it would not be efficient to accumulate them in only one place,
  397. incurring data transmission each and access concurrency.
  398. StarPU provides a mode ::STARPU_REDUX, which permits to optimize
  399. this case: it will allocate a buffer on each memory node, and accumulate
  400. intermediate results there. When the data is eventually accessed in the normal
  401. mode ::STARPU_R, StarPU will collect the intermediate results in just one
  402. buffer.
  403. For this to work, the user has to use the function
  404. starpu_data_set_reduction_methods() to declare how to initialize these
  405. buffers, and how to assemble partial results.
  406. For instance, <c>cg</c> uses that to optimize its dot product: it first defines
  407. the codelets for initialization and reduction:
  408. \code{.c}
  409. struct starpu_codelet bzero_variable_cl =
  410. {
  411. .cpu_funcs = { bzero_variable_cpu },
  412. .cpu_funcs_name = { "bzero_variable_cpu" },
  413. .cuda_funcs = { bzero_variable_cuda },
  414. .nbuffers = 1,
  415. }
  416. static void accumulate_variable_cpu(void *descr[], void *cl_arg)
  417. {
  418. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  419. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  420. *v_dst = *v_dst + *v_src;
  421. }
  422. static void accumulate_variable_cuda(void *descr[], void *cl_arg)
  423. {
  424. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  425. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  426. cublasaxpy(1, (double)1.0, v_src, 1, v_dst, 1);
  427. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  428. }
  429. struct starpu_codelet accumulate_variable_cl =
  430. {
  431. .cpu_funcs = { accumulate_variable_cpu },
  432. .cpu_funcs_name = { "accumulate_variable_cpu" },
  433. .cuda_funcs = { accumulate_variable_cuda },
  434. .nbuffers = 1,
  435. }
  436. \endcode
  437. and attaches them as reduction methods for its handle <c>dtq</c>:
  438. \code{.c}
  439. starpu_variable_data_register(&dtq_handle, -1, NULL, sizeof(type));
  440. starpu_data_set_reduction_methods(dtq_handle, &accumulate_variable_cl, &bzero_variable_cl);
  441. \endcode
  442. and <c>dtq_handle</c> can now be used in mode ::STARPU_REDUX for the
  443. dot products with partitioned vectors:
  444. \code{.c}
  445. for (b = 0; b < nblocks; b++)
  446. starpu_task_insert(&dot_kernel_cl,
  447. STARPU_REDUX, dtq_handle,
  448. STARPU_R, starpu_data_get_sub_data(v1, 1, b),
  449. STARPU_R, starpu_data_get_sub_data(v2, 1, b),
  450. 0);
  451. \endcode
  452. During registration, we have here provided <c>NULL</c>, i.e. there is
  453. no initial value to be taken into account during reduction. StarPU
  454. will thus only take into account the contributions from the tasks
  455. <c>dot_kernel_cl</c>. Also, it will not allocate any memory for
  456. <c>dtq_handle</c> before tasks <c>dot_kernel_cl</c> are ready to run.
  457. If another dot product has to be performed, one could unregister
  458. <c>dtq_handle</c>, and re-register it. But one can also call
  459. starpu_data_invalidate_submit() with the parameter <c>dtq_handle</c>,
  460. which will clear all data from the handle, thus resetting it back to
  461. the initial status <c>register(NULL)</c>.
  462. The example <c>cg</c> also uses reduction for the blocked gemv kernel,
  463. leading to yet more relaxed dependencies and more parallelism.
  464. ::STARPU_REDUX can also be passed to starpu_mpi_task_insert() in the MPI
  465. case. This will however not produce any MPI communication, but just pass
  466. ::STARPU_REDUX to the underlying starpu_task_insert(). It is up to the
  467. application to call starpu_mpi_redux_data(), which posts tasks which will
  468. reduce the partial results among MPI nodes into the MPI node which owns the
  469. data. For instance, some hypothetical application which collects partial results
  470. into data <c>res</c>, then uses it for other computation, before looping again
  471. with a new reduction:
  472. \code{.c}
  473. for (i = 0; i < 100; i++)
  474. {
  475. starpu_mpi_task_insert(MPI_COMM_WORLD, &init_res, STARPU_W, res, 0);
  476. starpu_mpi_task_insert(MPI_COMM_WORLD, &work, STARPU_RW, A, STARPU_R, B, STARPU_REDUX, res, 0);
  477. starpu_mpi_redux_data(MPI_COMM_WORLD, res);
  478. starpu_mpi_task_insert(MPI_COMM_WORLD, &work2, STARPU_RW, B, STARPU_R, res, 0);
  479. }
  480. \endcode
  481. \section DataCommute Commute Data Access
  482. By default, the implicit dependencies computed from data access use the
  483. sequential semantic. Notably, write accesses are always serialized in the order
  484. of submission. In some applicative cases, the write contributions can actually
  485. be performed in any order without affecting the eventual result. In this case
  486. it is useful to drop the strictly sequential semantic, to improve parallelism
  487. by allowing StarPU to reorder the write accesses. This can be done by using
  488. the ::STARPU_COMMUTE data access flag. Accesses without this flag will however
  489. properly be serialized against accesses with this flag. For instance:
  490. \code{.c}
  491. starpu_task_insert(&cl1, STARPU_R, h, STARPU_RW, handle, 0);
  492. starpu_task_insert(&cl2, STARPU_R, handle1, STARPU_RW|STARPU_COMMUTE, handle, 0);
  493. starpu_task_insert(&cl2, STARPU_R, handle2, STARPU_RW|STARPU_COMMUTE, handle, 0);
  494. starpu_task_insert(&cl3, STARPU_R, g, STARPU_RW, handle, 0);
  495. \endcode
  496. The two tasks running <c>cl2</c> will be able to commute: depending on whether the
  497. value of <c>handle1</c> or <c>handle2</c> becomes available first, the corresponding task
  498. running <c>cl2</c> will start first. The task running <c>cl1</c> will however always be run
  499. before them, and the task running <c>cl3</c> will always be run after them.
  500. If a lot of tasks use the commute access on the same set of data and a lot of
  501. them are ready at the same time, it may become interesting to use an arbiter,
  502. see \ref ConcurrentDataAccess.
  503. \section ConcurrentDataAccess Concurrent Data Accesses
  504. When several tasks are ready and will work on several data, StarPU is faced with
  505. the classical Dining Philosophers problem, and has to determine the order in
  506. which it will run the tasks.
  507. Data accesses usually use sequential ordering, so data accesses are usually
  508. already serialized, and thus by default StarPU uses the Dijkstra solution which
  509. scales very well in terms of overhead: tasks will just acquire data one by one
  510. by data handle pointer value order.
  511. When sequential ordering is disabled or the ::STARPU_COMMUTE flag is used, there
  512. may be a lot of concurrent accesses to the same data, and the Dijkstra solution
  513. gets only poor parallelism, typically in some pathological cases which do happen
  514. in various applications. In this case, one can use a data access arbiter, which
  515. implements the classical centralized solution for the Dining Philosophers
  516. problem. This is more expensive in terms of overhead since it is centralized,
  517. but it opportunistically gets a lot of parallelism. The centralization can also
  518. be avoided by using several arbiters, thus separating sets of data for which
  519. arbitration will be done. If a task accesses data from different arbiters, it
  520. will acquire them arbiter by arbiter, in arbiter pointer value order.
  521. See the <c>tests/datawizard/test_arbiter.cpp</c> example.
  522. Arbiters however do not support the ::STARPU_REDUX flag yet.
  523. \section TemporaryBuffers Temporary Buffers
  524. There are two kinds of temporary buffers: temporary data which just pass results
  525. from a task to another, and scratch data which are needed only internally by
  526. tasks.
  527. \subsection TemporaryData Temporary Data
  528. Data can sometimes be entirely produced by a task, and entirely consumed by
  529. another task, without the need for other parts of the application to access
  530. it. In such case, registration can be done without prior allocation, by using
  531. the special memory node number <c>-1</c>, and passing a zero pointer. StarPU will
  532. actually allocate memory only when the task creating the content gets scheduled,
  533. and destroy it on unregistration.
  534. In addition to this, it can be tedious for the application to have to unregister
  535. the data, since it will not use its content anyway. The unregistration can be
  536. done lazily by using the function starpu_data_unregister_submit(),
  537. which will record that no more tasks accessing the handle will be submitted, so
  538. that it can be freed as soon as the last task accessing it is over.
  539. The following code examplifies both points: it registers the temporary
  540. data, submits three tasks accessing it, and records the data for automatic
  541. unregistration.
  542. \code{.c}
  543. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  544. starpu_task_insert(&produce_data, STARPU_W, handle, 0);
  545. starpu_task_insert(&compute_data, STARPU_RW, handle, 0);
  546. starpu_task_insert(&summarize_data, STARPU_R, handle, STARPU_W, result_handle, 0);
  547. starpu_data_unregister_submit(handle);
  548. \endcode
  549. The application may also want to see the temporary data initialized
  550. on the fly before being used by the task. This can be done by using
  551. starpu_data_set_reduction_methods() to set an initialization codelet (no redux
  552. codelet is needed).
  553. \subsection ScratchData Scratch Data
  554. Some kernels sometimes need temporary data to achieve the computations, i.e. a
  555. workspace. The application could allocate it at the start of the codelet
  556. function, and free it at the end, but this would be costly. It could also
  557. allocate one buffer per worker (similarly to \ref HowToInitializeAComputationLibraryOnceForEachWorker),
  558. but this would
  559. make them systematic and permanent. A more optimized way is to use
  560. the data access mode ::STARPU_SCRATCH, as examplified below, which
  561. provides per-worker buffers without content consistency. The buffer is
  562. registered only once, using memory node <c>-1</c>, i.e. the application didn't allocate
  563. memory for it, and StarPU will allocate it on demand at task execution.
  564. \code{.c}
  565. starpu_vector_data_register(&workspace, -1, 0, sizeof(float));
  566. for (i = 0; i < N; i++)
  567. starpu_task_insert(&compute, STARPU_R, input[i], STARPU_SCRATCH, workspace, STARPU_W, output[i], 0);
  568. \endcode
  569. StarPU will make sure that the buffer is allocated before executing the task,
  570. and make this allocation per-worker: for CPU workers, notably, each worker has
  571. its own buffer. This means that each task submitted above will actually have its
  572. own workspace, which will actually be the same for all tasks running one after
  573. the other on the same worker. Also, if for instance memory becomes scarce,
  574. StarPU will notice that it can free such buffers easily, since the content does
  575. not matter.
  576. The example <c>examples/pi</c> uses scratches for some temporary buffer.
  577. \section TheMultiformatInterface The Multiformat Interface
  578. It may be interesting to represent the same piece of data using two different
  579. data structures: one only used on CPUs, and one only used on GPUs.
  580. This can be done by using the multiformat interface. StarPU
  581. will be able to convert data from one data structure to the other when needed.
  582. Note that the scheduler <c>dmda</c> is the only one optimized for this
  583. interface. The user must provide StarPU with conversion codelets:
  584. \snippet multiformat.c To be included. You should update doxygen if you see this text.
  585. Kernels can be written almost as for any other interface. Note that
  586. ::STARPU_MULTIFORMAT_GET_CPU_PTR shall only be used for CPU kernels. CUDA kernels
  587. must use ::STARPU_MULTIFORMAT_GET_CUDA_PTR, and OpenCL kernels must use
  588. ::STARPU_MULTIFORMAT_GET_OPENCL_PTR. ::STARPU_MULTIFORMAT_GET_NX may
  589. be used in any kind of kernel.
  590. \code{.c}
  591. static void
  592. multiformat_scal_cpu_func(void *buffers[], void *args)
  593. {
  594. struct point *aos;
  595. unsigned int n;
  596. aos = STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  597. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  598. ...
  599. }
  600. extern "C" void multiformat_scal_cuda_func(void *buffers[], void *_args)
  601. {
  602. unsigned int n;
  603. struct struct_of_arrays *soa;
  604. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  605. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  606. ...
  607. }
  608. \endcode
  609. A full example may be found in <c>examples/basic_examples/multiformat.c</c>.
  610. \section DefiningANewDataInterface Defining A New Data Interface
  611. Let's define a new data interface to manage complex numbers.
  612. \code{.c}
  613. /* interface for complex numbers */
  614. struct starpu_complex_interface
  615. {
  616. double *real;
  617. double *imaginary;
  618. int nx;
  619. };
  620. \endcode
  621. Registering such a data to StarPU is easily done using the function
  622. starpu_data_register(). The last
  623. parameter of the function, <c>interface_complex_ops</c>, will be
  624. described below.
  625. \code{.c}
  626. void starpu_complex_data_register(starpu_data_handle_t *handle,
  627. unsigned home_node, double *real, double *imaginary, int nx)
  628. {
  629. struct starpu_complex_interface complex =
  630. {
  631. .real = real,
  632. .imaginary = imaginary,
  633. .nx = nx
  634. };
  635. if (interface_complex_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  636. {
  637. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  638. }
  639. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  640. }
  641. \endcode
  642. The <c>starpu_complex_interface</c> structure is here used just to store the
  643. parameters that the user provided to <c>starpu_complex_data_register</c>.
  644. starpu_data_register() will first allocate the handle, and
  645. then pass the <c>starpu_complex_interface</c> structure to the
  646. starpu_data_interface_ops::register_data_handle method, which records them
  647. within the data handle (it is called once per node by starpu_data_register()).
  648. Different operations need to be defined for a data interface through
  649. the type starpu_data_interface_ops. We only define here the basic
  650. operations needed to run simple applications. The source code for the
  651. different functions can be found in the file
  652. <c>examples/interface/complex_interface.c</c>, the details of the hooks to be
  653. provided are documented in \ref starpu_data_interface_ops .
  654. \code{.c}
  655. static struct starpu_data_interface_ops interface_complex_ops =
  656. {
  657. .register_data_handle = complex_register_data_handle,
  658. .allocate_data_on_node = complex_allocate_data_on_node,
  659. .copy_methods = &complex_copy_methods,
  660. .get_size = complex_get_size,
  661. .footprint = complex_footprint,
  662. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  663. .interface_size = sizeof(struct starpu_complex_interface),
  664. };
  665. \endcode
  666. Functions need to be defined to access the different fields of the
  667. complex interface from a StarPU data handle.
  668. \code{.c}
  669. double *starpu_complex_get_real(starpu_data_handle_t handle)
  670. {
  671. struct starpu_complex_interface *complex_interface =
  672. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  673. return complex_interface->real;
  674. }
  675. double *starpu_complex_get_imaginary(starpu_data_handle_t handle);
  676. int starpu_complex_get_nx(starpu_data_handle_t handle);
  677. \endcode
  678. Similar functions need to be defined to access the different fields of the
  679. complex interface from a <c>void *</c> pointer to be used within codelet
  680. implemetations.
  681. \snippet complex.c To be included. You should update doxygen if you see this text.
  682. Complex data interfaces can then be registered to StarPU.
  683. \code{.c}
  684. double real = 45.0;
  685. double imaginary = 12.0;
  686. starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  687. starpu_task_insert(&cl_display, STARPU_R, handle1, 0);
  688. \endcode
  689. and used by codelets.
  690. \code{.c}
  691. void display_complex_codelet(void *descr[], void *_args)
  692. {
  693. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  694. double *real = STARPU_COMPLEX_GET_REAL(descr[0]);
  695. double *imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  696. int i;
  697. for(i=0 ; i<nx ; i++)
  698. {
  699. fprintf(stderr, "Complex[%d] = %3.2f + %3.2f i\n", i, real[i], imaginary[i]);
  700. }
  701. }
  702. \endcode
  703. The whole code for this complex data interface is available in the
  704. directory <c>examples/interface/</c>.
  705. \section SpecifyingATargetNode Specifying A Target Node For Task Data
  706. When executing a task on a GPU for instance, StarPU would normally copy all the
  707. needed data for the tasks on the embedded memory of the GPU. It may however
  708. happen that the task kernel would rather have some of the datas kept in the
  709. main memory instead of copied in the GPU, a pivoting vector for instance.
  710. This can be achieved by setting the starpu_codelet::specific_nodes flag to
  711. <c>1</c>, and then fill the starpu_codelet::nodes array (or starpu_codelet::dyn_nodes when
  712. starpu_codelet::nbuffers is greater than \ref STARPU_NMAXBUFS) with the node numbers
  713. where data should be copied to, or ::STARPU_SPECIFIC_NODE_LOCAL to let
  714. StarPU copy it to the memory node where the task will be executed.
  715. ::STARPU_SPECIFIC_NODE_CPU can also be used to request data to be
  716. put in CPU-accessible memory (and let StarPU choose the NUMA node).
  717. ::STARPU_SPECIFIC_NODE_FAST and ::STARPU_SPECIFIC_NODE_SLOW can also be
  718. used
  719. For instance,
  720. with the following codelet:
  721. \code{.c}
  722. struct starpu_codelet cl =
  723. {
  724. .cuda_funcs = { kernel },
  725. .nbuffers = 2,
  726. .modes = {STARPU_RW, STARPU_RW},
  727. .specific_nodes = 1,
  728. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  729. };
  730. \endcode
  731. the first data of the task will be kept in the CPU memory, while the second
  732. data will be copied to the CUDA GPU as usual. A working example is available in
  733. <c>tests/datawizard/specific_node.c</c>
  734. With the following codelet:
  735. \code{.c}
  736. struct starpu_codelet cl =
  737. {
  738. .cuda_funcs = { kernel },
  739. .nbuffers = 2,
  740. .modes = {STARPU_RW, STARPU_RW},
  741. .specific_nodes = 1,
  742. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_SLOW},
  743. };
  744. \endcode
  745. The first data will be copied into fast (but probably size-limited) local memory
  746. while the second data will be left in slow (but large) memory. This makes sense
  747. when the kernel does not make so many accesses to the second data, and thus data
  748. being remote e.g. over a PCI bus is not a performance problem, and avoids
  749. filling the fast local memory with data which does not need the performance.
  750. */