07data_management.doxy 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2009--2011 Universit@'e de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
  6. * See the file version.doxy for copying conditions.
  7. */
  8. /*! \page DataManagement Data Management
  9. intro qui parle de coherency entre autres
  10. \section DataManagement Data Management
  11. When the application allocates data, whenever possible it should use
  12. the starpu_malloc() function, which will ask CUDA or OpenCL to make
  13. the allocation itself and pin the corresponding allocated memory, or to use the
  14. starpu_memory_pin() function to pin memory allocated by other ways, such as local arrays. This
  15. is needed to permit asynchronous data transfer, i.e. permit data
  16. transfer to overlap with computations. Otherwise, the trace will show
  17. that the <c>DriverCopyAsync</c> state takes a lot of time, this is
  18. because CUDA or OpenCL then reverts to synchronous transfers.
  19. By default, StarPU leaves replicates of data wherever they were used, in case they
  20. will be re-used by other tasks, thus saving the data transfer time. When some
  21. task modifies some data, all the other replicates are invalidated, and only the
  22. processing unit which ran that task will have a valid replicate of the data. If the application knows
  23. that this data will not be re-used by further tasks, it should advise StarPU to
  24. immediately replicate it to a desired list of memory nodes (given through a
  25. bitmask). This can be understood like the write-through mode of CPU caches.
  26. \code{.c}
  27. starpu_data_set_wt_mask(img_handle, 1<<0);
  28. \endcode
  29. will for instance request to always automatically transfer a replicate into the
  30. main memory (node <c>0</c>), as bit <c>0</c> of the write-through bitmask is being set.
  31. \code{.c}
  32. starpu_data_set_wt_mask(img_handle, ~0U);
  33. \endcode
  34. will request to always automatically broadcast the updated data to all memory
  35. nodes.
  36. Setting the write-through mask to <c>~0U</c> can also be useful to make sure all
  37. memory nodes always have a copy of the data, so that it is never evicted when
  38. memory gets scarse.
  39. Implicit data dependency computation can become expensive if a lot
  40. of tasks access the same piece of data. If no dependency is required
  41. on some piece of data (e.g. because it is only accessed in read-only
  42. mode, or because write accesses are actually commutative), use the
  43. function starpu_data_set_sequential_consistency_flag() to disable
  44. implicit dependencies on that data.
  45. In the same vein, accumulation of results in the same data can become a
  46. bottleneck. The use of the mode ::STARPU_REDUX permits to optimize such
  47. accumulation (see \ref DataReduction). To a lesser extent, the use of
  48. the flag ::STARPU_COMMUTE keeps the bottleneck, but at least permits
  49. the accumulation to happen in any order.
  50. Applications often need a data just for temporary results. In such a case,
  51. registration can be made without an initial value, for instance this produces a vector data:
  52. \code{.c}
  53. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  54. \endcode
  55. StarPU will then allocate the actual buffer only when it is actually needed,
  56. e.g. directly on the GPU without allocating in main memory.
  57. In the same vein, once the temporary results are not useful any more, the
  58. data should be thrown away. If the handle is not to be reused, it can be
  59. unregistered:
  60. \code{.c}
  61. starpu_data_unregister_submit(handle);
  62. \endcode
  63. actual unregistration will be done after all tasks working on the handle
  64. terminate.
  65. If the handle is to be reused, instead of unregistering it, it can simply be invalidated:
  66. \code{.c}
  67. starpu_data_invalidate_submit(handle);
  68. \endcode
  69. the buffers containing the current value will then be freed, and reallocated
  70. only when another task writes some value to the handle.
  71. \section DataPrefetch Data Prefetch
  72. The scheduling policies <c>heft</c>, <c>dmda</c> and <c>pheft</c>
  73. perform data prefetch (see \ref STARPU_PREFETCH):
  74. as soon as a scheduling decision is taken for a task, requests are issued to
  75. transfer its required data to the target processing unit, if needed, so that
  76. when the processing unit actually starts the task, its data will hopefully be
  77. already available and it will not have to wait for the transfer to finish.
  78. The application may want to perform some manual prefetching, for several reasons
  79. such as excluding initial data transfers from performance measurements, or
  80. setting up an initial statically-computed data distribution on the machine
  81. before submitting tasks, which will thus guide StarPU toward an initial task
  82. distribution (since StarPU will try to avoid further transfers).
  83. This can be achieved by giving the function starpu_data_prefetch_on_node() the
  84. handle and the desired target memory node. The
  85. starpu_data_idle_prefetch_on_node() variant can be used to issue the transfer
  86. only when the bus is idle.
  87. Conversely, one can advise StarPU that some data will not be useful in the
  88. close future by calling starpu_data_wont_use. StarPU will then write its value
  89. back to its home node, and evict it from GPUs when room is needed.
  90. \section PartitioningData Partitioning Data
  91. An existing piece of data can be partitioned in sub parts to be used by different tasks, for instance:
  92. \code{.c}
  93. int vector[NX];
  94. starpu_data_handle_t handle;
  95. /* Declare data to StarPU */
  96. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector,
  97. NX, sizeof(vector[0]));
  98. /* Partition the vector in PARTS sub-vectors */
  99. struct starpu_data_filter f =
  100. {
  101. .filter_func = starpu_vector_filter_block,
  102. .nchildren = PARTS
  103. };
  104. starpu_data_partition(handle, &f);
  105. \endcode
  106. The task submission then uses the function starpu_data_get_sub_data()
  107. to retrieve the sub-handles to be passed as tasks parameters.
  108. \code{.c}
  109. /* Submit a task on each sub-vector */
  110. for (i=0; i<starpu_data_get_nb_children(handle); i++) {
  111. /* Get subdata number i (there is only 1 dimension) */
  112. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  113. struct starpu_task *task = starpu_task_create();
  114. task->handles[0] = sub_handle;
  115. task->cl = &cl;
  116. task->synchronous = 1;
  117. task->cl_arg = &factor;
  118. task->cl_arg_size = sizeof(factor);
  119. starpu_task_submit(task);
  120. }
  121. \endcode
  122. Partitioning can be applied several times, see
  123. <c>examples/basic_examples/mult.c</c> and <c>examples/filters/</c>.
  124. Wherever the whole piece of data is already available, the partitioning will
  125. be done in-place, i.e. without allocating new buffers but just using pointers
  126. inside the existing copy. This is particularly important to be aware of when
  127. using OpenCL, where the kernel parameters are not pointers, but handles. The
  128. kernel thus needs to be also passed the offset within the OpenCL buffer:
  129. \code{.c}
  130. void opencl_func(void *buffers[], void *cl_arg)
  131. {
  132. cl_mem vector = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  133. unsigned offset = STARPU_BLOCK_GET_OFFSET(buffers[0]);
  134. ...
  135. clSetKernelArg(kernel, 0, sizeof(vector), &vector);
  136. clSetKernelArg(kernel, 1, sizeof(offset), &offset);
  137. ...
  138. }
  139. \endcode
  140. And the kernel has to shift from the pointer passed by the OpenCL driver:
  141. \code{.c}
  142. __kernel void opencl_kernel(__global int *vector, unsigned offset)
  143. {
  144. block = (__global void *)block + offset;
  145. ...
  146. }
  147. \endcode
  148. StarPU provides various interfaces and filters for matrices, vectors, etc.,
  149. but applications can also write their own data interfaces and filters, see
  150. <c>examples/interface</c> and <c>examples/filters/custom_mf</c> for an example.
  151. \section DataReduction Data Reduction
  152. In various cases, some piece of data is used to accumulate intermediate
  153. results. For instances, the dot product of a vector, maximum/minimum finding,
  154. the histogram of a photograph, etc. When these results are produced along the
  155. whole machine, it would not be efficient to accumulate them in only one place,
  156. incurring data transmission each and access concurrency.
  157. StarPU provides a mode ::STARPU_REDUX, which permits to optimize
  158. that case: it will allocate a buffer on each memory node, and accumulate
  159. intermediate results there. When the data is eventually accessed in the normal
  160. mode ::STARPU_R, StarPU will collect the intermediate results in just one
  161. buffer.
  162. For this to work, the user has to use the function
  163. starpu_data_set_reduction_methods() to declare how to initialize these
  164. buffers, and how to assemble partial results.
  165. For instance, <c>cg</c> uses that to optimize its dot product: it first defines
  166. the codelets for initialization and reduction:
  167. \code{.c}
  168. struct starpu_codelet bzero_variable_cl =
  169. {
  170. .cpu_funcs = { bzero_variable_cpu },
  171. .cpu_funcs_name = { "bzero_variable_cpu" },
  172. .cuda_funcs = { bzero_variable_cuda },
  173. .nbuffers = 1,
  174. }
  175. static void accumulate_variable_cpu(void *descr[], void *cl_arg)
  176. {
  177. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  178. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  179. *v_dst = *v_dst + *v_src;
  180. }
  181. static void accumulate_variable_cuda(void *descr[], void *cl_arg)
  182. {
  183. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  184. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  185. cublasaxpy(1, (double)1.0, v_src, 1, v_dst, 1);
  186. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  187. }
  188. struct starpu_codelet accumulate_variable_cl =
  189. {
  190. .cpu_funcs = { accumulate_variable_cpu },
  191. .cpu_funcs_name = { "accumulate_variable_cpu" },
  192. .cuda_funcs = { accumulate_variable_cuda },
  193. .nbuffers = 1,
  194. }
  195. \endcode
  196. and attaches them as reduction methods for its handle <c>dtq</c>:
  197. \code{.c}
  198. starpu_variable_data_register(&dtq_handle, -1, NULL, sizeof(type));
  199. starpu_data_set_reduction_methods(dtq_handle,
  200. &accumulate_variable_cl, &bzero_variable_cl);
  201. \endcode
  202. and <c>dtq_handle</c> can now be used in mode ::STARPU_REDUX for the
  203. dot products with partitioned vectors:
  204. \code{.c}
  205. for (b = 0; b < nblocks; b++)
  206. starpu_task_insert(&dot_kernel_cl,
  207. STARPU_REDUX, dtq_handle,
  208. STARPU_R, starpu_data_get_sub_data(v1, 1, b),
  209. STARPU_R, starpu_data_get_sub_data(v2, 1, b),
  210. 0);
  211. \endcode
  212. During registration, we have here provided <c>NULL</c>, i.e. there is
  213. no initial value to be taken into account during reduction. StarPU
  214. will thus only take into account the contributions from the tasks
  215. <c>dot_kernel_cl</c>. Also, it will not allocate any memory for
  216. <c>dtq_handle</c> before tasks <c>dot_kernel_cl</c> are ready to run.
  217. If another dot product has to be performed, one could unregister
  218. <c>dtq_handle</c>, and re-register it. But one can also call
  219. starpu_data_invalidate_submit() with the parameter <c>dtq_handle</c>,
  220. which will clear all data from the handle, thus resetting it back to
  221. the initial status <c>register(NULL)</c>.
  222. The example <c>cg</c> also uses reduction for the blocked gemv kernel,
  223. leading to yet more relaxed dependencies and more parallelism.
  224. ::STARPU_REDUX can also be passed to starpu_mpi_task_insert() in the MPI
  225. case. That will however not produce any MPI communication, but just pass
  226. ::STARPU_REDUX to the underlying starpu_task_insert(). It is up to the
  227. application to call starpu_mpi_redux_data(), which posts tasks that will
  228. reduce the partial results among MPI nodes into the MPI node which owns the
  229. data. For instance, some hypothetical application which collects partial results
  230. into data <c>res</c>, then uses it for other computation, before looping again
  231. with a new reduction:
  232. \code{.c}
  233. for (i = 0; i < 100; i++) {
  234. starpu_mpi_task_insert(MPI_COMM_WORLD, &init_res, STARPU_W, res, 0);
  235. starpu_mpi_task_insert(MPI_COMM_WORLD, &work, STARPU_RW, A,
  236. STARPU_R, B, STARPU_REDUX, res, 0);
  237. starpu_mpi_redux_data(MPI_COMM_WORLD, res);
  238. starpu_mpi_task_insert(MPI_COMM_WORLD, &work2, STARPU_RW, B, STARPU_R, res, 0);
  239. }
  240. \endcode
  241. \section TemporaryBuffers Temporary Buffers
  242. There are two kinds of temporary buffers: temporary data which just pass results
  243. from a task to another, and scratch data which are needed only internally by
  244. tasks.
  245. \subsection TemporaryData Temporary Data
  246. Data can sometimes be entirely produced by a task, and entirely consumed by
  247. another task, without the need for other parts of the application to access
  248. it. In such case, registration can be done without prior allocation, by using
  249. the special memory node number <c>-1</c>, and passing a zero pointer. StarPU will
  250. actually allocate memory only when the task creating the content gets scheduled,
  251. and destroy it on unregistration.
  252. In addition to that, it can be tedious for the application to have to unregister
  253. the data, since it will not use its content anyway. The unregistration can be
  254. done lazily by using the function starpu_data_unregister_submit(),
  255. which will record that no more tasks accessing the handle will be submitted, so
  256. that it can be freed as soon as the last task accessing it is over.
  257. The following code examplifies both points: it registers the temporary
  258. data, submits three tasks accessing it, and records the data for automatic
  259. unregistration.
  260. \code{.c}
  261. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  262. starpu_task_insert(&produce_data, STARPU_W, handle, 0);
  263. starpu_task_insert(&compute_data, STARPU_RW, handle, 0);
  264. starpu_task_insert(&summarize_data, STARPU_R, handle, STARPU_W, result_handle, 0);
  265. starpu_data_unregister_submit(handle);
  266. \endcode
  267. The application may also want to see the temporary data initialized
  268. on the fly before being used by the task. This can be done by using
  269. starpu_data_set_reduction_methods() to set an initialization codelet (no redux
  270. codelet is needed).
  271. \subsection ScratchData Scratch Data
  272. Some kernels sometimes need temporary data to achieve the computations, i.e. a
  273. workspace. The application could allocate it at the start of the codelet
  274. function, and free it at the end, but that would be costly. It could also
  275. allocate one buffer per worker (similarly to \ref
  276. HowToInitializeAComputationLibraryOnceForEachWorker), but that would
  277. make them systematic and permanent. A more optimized way is to use
  278. the data access mode ::STARPU_SCRATCH, as examplified below, which
  279. provides per-worker buffers without content consistency.
  280. \code{.c}
  281. starpu_vector_data_register(&workspace, -1, 0, sizeof(float));
  282. for (i = 0; i < N; i++)
  283. starpu_task_insert(&compute, STARPU_R, input[i],
  284. STARPU_SCRATCH, workspace, STARPU_W, output[i], 0);
  285. \endcode
  286. StarPU will make sure that the buffer is allocated before executing the task,
  287. and make this allocation per-worker: for CPU workers, notably, each worker has
  288. its own buffer. This means that each task submitted above will actually have its
  289. own workspace, which will actually be the same for all tasks running one after
  290. the other on the same worker. Also, if for instance GPU memory becomes scarce,
  291. StarPU will notice that it can free such buffers easily, since the content does
  292. not matter.
  293. The example <c>examples/pi</c> uses scratches for some temporary buffer.
  294. \section TheMultiformatInterface The Multiformat Interface
  295. It may be interesting to represent the same piece of data using two different
  296. data structures: one that would only be used on CPUs, and one that would only
  297. be used on GPUs. This can be done by using the multiformat interface. StarPU
  298. will be able to convert data from one data structure to the other when needed.
  299. Note that the scheduler <c>dmda</c> is the only one optimized for this
  300. interface. The user must provide StarPU with conversion codelets:
  301. \snippet multiformat.c To be included. You should update doxygen if you see this text.
  302. Kernels can be written almost as for any other interface. Note that
  303. ::STARPU_MULTIFORMAT_GET_CPU_PTR shall only be used for CPU kernels. CUDA kernels
  304. must use ::STARPU_MULTIFORMAT_GET_CUDA_PTR, and OpenCL kernels must use
  305. ::STARPU_MULTIFORMAT_GET_OPENCL_PTR. ::STARPU_MULTIFORMAT_GET_NX may
  306. be used in any kind of kernel.
  307. \code{.c}
  308. static void
  309. multiformat_scal_cpu_func(void *buffers[], void *args)
  310. {
  311. struct point *aos;
  312. unsigned int n;
  313. aos = STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  314. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  315. ...
  316. }
  317. extern "C" void multiformat_scal_cuda_func(void *buffers[], void *_args)
  318. {
  319. unsigned int n;
  320. struct struct_of_arrays *soa;
  321. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  322. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  323. ...
  324. }
  325. \endcode
  326. A full example may be found in <c>examples/basic_examples/multiformat.c</c>.
  327. \section DefiningANewDataInterface Defining A New Data Interface
  328. Let's define a new data interface to manage complex numbers.
  329. \code{.c}
  330. /* interface for complex numbers */
  331. struct starpu_complex_interface
  332. {
  333. double *real;
  334. double *imaginary;
  335. int nx;
  336. };
  337. \endcode
  338. Registering such a data to StarPU is easily done using the function
  339. starpu_data_register(). The last
  340. parameter of the function, <c>interface_complex_ops</c>, will be
  341. described below.
  342. \code{.c}
  343. void starpu_complex_data_register(starpu_data_handle_t *handle,
  344. unsigned home_node, double *real, double *imaginary, int nx)
  345. {
  346. struct starpu_complex_interface complex =
  347. {
  348. .real = real,
  349. .imaginary = imaginary,
  350. .nx = nx
  351. };
  352. if (interface_complex_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  353. {
  354. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  355. }
  356. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  357. }
  358. \endcode
  359. Different operations need to be defined for a data interface through
  360. the type starpu_data_interface_ops. We only define here the basic
  361. operations needed to run simple applications. The source code for the
  362. different functions can be found in the file
  363. <c>examples/interface/complex_interface.c</c>.
  364. \code{.c}
  365. static struct starpu_data_interface_ops interface_complex_ops =
  366. {
  367. .register_data_handle = complex_register_data_handle,
  368. .allocate_data_on_node = complex_allocate_data_on_node,
  369. .copy_methods = &complex_copy_methods,
  370. .get_size = complex_get_size,
  371. .footprint = complex_footprint,
  372. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  373. .interface_size = sizeof(struct starpu_complex_interface),
  374. };
  375. \endcode
  376. Functions need to be defined to access the different fields of the
  377. complex interface from a StarPU data handle.
  378. \code{.c}
  379. double *starpu_complex_get_real(starpu_data_handle_t handle)
  380. {
  381. struct starpu_complex_interface *complex_interface =
  382. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  383. return complex_interface->real;
  384. }
  385. double *starpu_complex_get_imaginary(starpu_data_handle_t handle);
  386. int starpu_complex_get_nx(starpu_data_handle_t handle);
  387. \endcode
  388. Similar functions need to be defined to access the different fields of the
  389. complex interface from a <c>void *</c> pointer to be used within codelet
  390. implemetations.
  391. \snippet complex.c To be included. You should update doxygen if you see this text.
  392. Complex data interfaces can then be registered to StarPU.
  393. \code{.c}
  394. double real = 45.0;
  395. double imaginary = 12.0;starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  396. starpu_task_insert(&cl_display, STARPU_R, handle1, 0);
  397. \endcode
  398. and used by codelets.
  399. \code{.c}
  400. void display_complex_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  401. {
  402. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  403. double *real = STARPU_COMPLEX_GET_REAL(descr[0]);
  404. double *imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  405. int i;
  406. for(i=0 ; i<nx ; i++)
  407. {
  408. fprintf(stderr, "Complex[%d] = %3.2f + %3.2f i\n", i, real[i], imaginary[i]);
  409. }
  410. }
  411. \endcode
  412. The whole code for this complex data interface is available in the
  413. directory <c>examples/interface/</c>.
  414. \section SpecifyingATargetNode Specifying a target node for task data
  415. When executing a task on a GPU for instance, StarPU would normally copy all the
  416. needed data for the tasks on the embedded memory of the GPU. It may however
  417. happen that the task kernel would rather have some of the datas kept in the
  418. main memory instead of copied in the GPU, a pivoting vector for instance.
  419. This can be achieved by setting the starpu_codelet::specific_nodes flag to
  420. 1, and then fill the starpu_codelet::nodes array (or starpu_codelet::dyn_nodes when
  421. starpu_codelet::nbuffers is greater than STARPU_NMAXBUFS) with the node numbers
  422. where data should be copied to, or -1 to let StarPU copy it to the memory node
  423. where the task will be executed. For instance, with the following codelet:
  424. \code{.c}
  425. struct starpu_codelet cl =
  426. {
  427. .cuda_funcs = { kernel },
  428. .nbuffers = 2,
  429. .modes = {STARPU_RW, STARPU_RW},
  430. .specific_nodes = 1,
  431. .nodes = {STARPU_MAIN_RAM, -1},
  432. };
  433. \endcode
  434. the first data of the task will be kept in the main memory, while the second
  435. data will be copied to the CUDA GPU as usual.
  436. */