advanced-examples.texi 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2009--2011 Universit@'e de Bordeaux 1
  4. @c Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. @menu
  8. * Using multiple implementations of a codelet::
  9. * Enabling implementation according to capabilities::
  10. * Task and Worker Profiling::
  11. * Partitioning Data::
  12. * Performance model example::
  13. * Theoretical lower bound on execution time::
  14. * Insert Task Utility::
  15. * Data reduction::
  16. * Temporary buffers::
  17. * Parallel Tasks::
  18. * Debugging::
  19. * The multiformat interface::
  20. * On-GPU rendering::
  21. * More examples:: More examples shipped with StarPU
  22. @end menu
  23. @node Using multiple implementations of a codelet
  24. @section Using multiple implementations of a codelet
  25. One may want to write multiple implementations of a codelet for a single type of
  26. device and let StarPU choose which one to run. As an example, we will show how
  27. to use SSE to scale a vector. The codelet can be written as follows:
  28. @cartouche
  29. @smallexample
  30. #include <xmmintrin.h>
  31. void scal_sse_func(void *buffers[], void *cl_arg)
  32. @{
  33. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  34. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]);
  35. unsigned int n_iterations = n/4;
  36. if (n % 4 != 0)
  37. n_iterations++;
  38. __m128 *VECTOR = (__m128*) vector;
  39. __m128 factor __attribute__((aligned(16)));
  40. factor = _mm_set1_ps(*(float *) cl_arg);
  41. unsigned int i;
  42. for (i = 0; i < n_iterations; i++)
  43. VECTOR[i] = _mm_mul_ps(factor, VECTOR[i]);
  44. @}
  45. @end smallexample
  46. @end cartouche
  47. @cartouche
  48. @smallexample
  49. struct starpu_codelet cl = @{
  50. .where = STARPU_CPU,
  51. .cpu_funcs = @{ scal_cpu_func, scal_sse_func, NULL @},
  52. .nbuffers = 1,
  53. .modes = @{ STARPU_RW @}
  54. @};
  55. @end smallexample
  56. @end cartouche
  57. Schedulers which are multi-implementation aware (only @code{dmda}, @code{heft}
  58. and @code{pheft} for now) will use the performance models of all the
  59. implementations it was given, and pick the one that seems to be the fastest.
  60. @node Enabling implementation according to capabilities
  61. @section Enabling implementation according to capabilities
  62. Some implementations may not run on some devices. For instance, some CUDA
  63. devices do not support double floating point precision, and thus the kernel
  64. execution would just fail; or the device may not have enough shared memory for
  65. the implementation being used. The @code{can_execute} field of the @code{struct
  66. starpu_codelet} structure permits to express this. For instance:
  67. @cartouche
  68. @smallexample
  69. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  70. @{
  71. const struct cudaDeviceProp *props;
  72. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  73. return 1;
  74. /* Cuda device */
  75. props = starpu_cuda_get_device_properties(workerid);
  76. if (props->major >= 2 || props->minor >= 3)
  77. /* At least compute capability 1.3, supports doubles */
  78. return 1;
  79. /* Old card, does not support doubles */
  80. return 0;
  81. @}
  82. struct starpu_codelet cl = @{
  83. .where = STARPU_CPU|STARPU_CUDA,
  84. .can_execute = can_execute,
  85. .cpu_funcs = @{ cpu_func, NULL @},
  86. .cuda_funcs = @{ gpu_func, NULL @}
  87. .nbuffers = 1,
  88. .modes = @{ STARPU_RW @}
  89. @};
  90. @end smallexample
  91. @end cartouche
  92. This can be essential e.g. when running on a machine which mixes various models
  93. of CUDA devices, to take benefit from the new models without crashing on old models.
  94. Note: the @code{can_execute} function is called by the scheduler each time it
  95. tries to match a task with a worker, and should thus be very fast. The
  96. @code{starpu_cuda_get_device_properties} provides a quick access to CUDA
  97. properties of CUDA devices to achieve such efficiency.
  98. Another example is compiling CUDA code for various compute capabilities,
  99. resulting with two CUDA functions, e.g. @code{scal_gpu_13} for compute capability
  100. 1.3, and @code{scal_gpu_20} for compute capability 2.0. Both functions can be
  101. provided to StarPU by using @code{cuda_funcs}, and @code{can_execute} can then be
  102. used to rule out the @code{scal_gpu_20} variant on a CUDA device which
  103. will not be able to execute it:
  104. @cartouche
  105. @smallexample
  106. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  107. @{
  108. const struct cudaDeviceProp *props;
  109. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  110. return 1;
  111. /* Cuda device */
  112. if (nimpl == 0)
  113. /* Trying to execute the 1.3 capability variant, we assume it is ok in all cases. */
  114. return 1;
  115. /* Trying to execute the 2.0 capability variant, check that the card can do it. */
  116. props = starpu_cuda_get_device_properties(workerid);
  117. if (props->major >= 2 || props->minor >= 0)
  118. /* At least compute capability 2.0, can run it */
  119. return 1;
  120. /* Old card, does not support 2.0, will not be able to execute the 2.0 variant. */
  121. return 0;
  122. @}
  123. struct starpu_codelet cl = @{
  124. .where = STARPU_CPU|STARPU_CUDA,
  125. .can_execute = can_execute,
  126. .cpu_funcs = @{ cpu_func, NULL @},
  127. .cuda_funcs = @{ scal_gpu_13, scal_gpu_20, NULL @},
  128. .nbuffers = 1,
  129. .modes = @{ STARPU_RW @}
  130. @};
  131. @end smallexample
  132. @end cartouche
  133. Note: the most generic variant should be provided first, as some schedulers are
  134. not able to try the different variants.
  135. @node Task and Worker Profiling
  136. @section Task and Worker Profiling
  137. A full example showing how to use the profiling API is available in
  138. the StarPU sources in the directory @code{examples/profiling/}.
  139. @cartouche
  140. @smallexample
  141. struct starpu_task *task = starpu_task_create();
  142. task->cl = &cl;
  143. task->synchronous = 1;
  144. /* We will destroy the task structure by hand so that we can
  145. * query the profiling info before the task is destroyed. */
  146. task->destroy = 0;
  147. /* Submit and wait for completion (since synchronous was set to 1) */
  148. starpu_task_submit(task);
  149. /* The task is finished, get profiling information */
  150. struct starpu_task_profiling_info *info = task->profiling_info;
  151. /* How much time did it take before the task started ? */
  152. double delay += starpu_timing_timespec_delay_us(&info->submit_time, &info->start_time);
  153. /* How long was the task execution ? */
  154. double length += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  155. /* We don't need the task structure anymore */
  156. starpu_task_destroy(task);
  157. @end smallexample
  158. @end cartouche
  159. @cartouche
  160. @smallexample
  161. /* Display the occupancy of all workers during the test */
  162. int worker;
  163. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  164. @{
  165. struct starpu_worker_profiling_info worker_info;
  166. int ret = starpu_worker_get_profiling_info(worker, &worker_info);
  167. STARPU_ASSERT(!ret);
  168. double total_time = starpu_timing_timespec_to_us(&worker_info.total_time);
  169. double executing_time = starpu_timing_timespec_to_us(&worker_info.executing_time);
  170. double sleeping_time = starpu_timing_timespec_to_us(&worker_info.sleeping_time);
  171. float executing_ratio = 100.0*executing_time/total_time;
  172. float sleeping_ratio = 100.0*sleeping_time/total_time;
  173. char workername[128];
  174. starpu_worker_get_name(worker, workername, 128);
  175. fprintf(stderr, "Worker %s:\n", workername);
  176. fprintf(stderr, "\ttotal time: %.2lf ms\n", total_time*1e-3);
  177. fprintf(stderr, "\texec time: %.2lf ms (%.2f %%)\n", executing_time*1e-3,
  178. executing_ratio);
  179. fprintf(stderr, "\tblocked time: %.2lf ms (%.2f %%)\n", sleeping_time*1e-3,
  180. sleeping_ratio);
  181. @}
  182. @end smallexample
  183. @end cartouche
  184. @node Partitioning Data
  185. @section Partitioning Data
  186. An existing piece of data can be partitioned in sub parts to be used by different tasks, for instance:
  187. @cartouche
  188. @smallexample
  189. int vector[NX];
  190. starpu_data_handle_t handle;
  191. /* Declare data to StarPU */
  192. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  193. /* Partition the vector in PARTS sub-vectors */
  194. starpu_filter f =
  195. @{
  196. .filter_func = starpu_block_filter_func_vector,
  197. .nchildren = PARTS
  198. @};
  199. starpu_data_partition(handle, &f);
  200. @end smallexample
  201. @end cartouche
  202. The task submission then uses @code{starpu_data_get_sub_data} to retrieve the
  203. sub-handles to be passed as tasks parameters.
  204. @cartouche
  205. @smallexample
  206. /* Submit a task on each sub-vector */
  207. for (i=0; i<starpu_data_get_nb_children(handle); i++) @{
  208. /* Get subdata number i (there is only 1 dimension) */
  209. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  210. struct starpu_task *task = starpu_task_create();
  211. task->handles[0] = sub_handle;
  212. task->cl = &cl;
  213. task->synchronous = 1;
  214. task->cl_arg = &factor;
  215. task->cl_arg_size = sizeof(factor);
  216. starpu_task_submit(task);
  217. @}
  218. @end smallexample
  219. @end cartouche
  220. Partitioning can be applied several times, see
  221. @code{examples/basic_examples/mult.c} and @code{examples/filters/}.
  222. Wherever the whole piece of data is already available, the partitioning will
  223. be done in-place, i.e. without allocating new buffers but just using pointers
  224. inside the existing copy. This is particularly important to be aware of when
  225. using OpenCL, where the kernel parameters are not pointers, but handles. The
  226. kernel thus needs to be also passed the offset within the OpenCL buffer:
  227. @cartouche
  228. @smallexample
  229. void opencl_func(void *buffers[], void *cl_arg)
  230. @{
  231. cl_mem vector = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  232. unsigned offset = STARPU_BLOCK_GET_OFFSET(buffers[0]);
  233. ...
  234. clSetKernelArg(kernel, 0, sizeof(vector), &vector);
  235. clSetKernelArg(kernel, 1, sizeof(offset), &offset);
  236. ...
  237. @}
  238. @end smallexample
  239. @end cartouche
  240. And the kernel has to shift from the pointer passed by the OpenCL driver:
  241. @cartouche
  242. @smallexample
  243. __kernel void opencl_kernel(__global int *vector, unsigned offset)
  244. @{
  245. block = (__global void *)block + offset;
  246. ...
  247. @}
  248. @end smallexample
  249. @end cartouche
  250. StarPU provides various interfaces and filters for matrices, vectors, etc.,
  251. but applications can also write their own data interfaces and filters, see
  252. @code{examples/interface} and @code{examples/filters/custom_mf} for an example.
  253. @node Performance model example
  254. @section Performance model example
  255. To achieve good scheduling, StarPU scheduling policies need to be able to
  256. estimate in advance the duration of a task. This is done by giving to codelets
  257. a performance model, by defining a @code{starpu_perfmodel} structure and
  258. providing its address in the @code{model} field of the @code{struct starpu_codelet}
  259. structure. The @code{symbol} and @code{type} fields of @code{starpu_perfmodel}
  260. are mandatory, to give a name to the model, and the type of the model, since
  261. there are several kinds of performance models. For compatibility, make sure to
  262. initialize the whole structure to zero, either by using explicit memset, or by
  263. letting the compiler implicitly do it as examplified below.
  264. @itemize
  265. @item
  266. Measured at runtime (@code{STARPU_HISTORY_BASED} model type). This assumes that for a
  267. given set of data input/output sizes, the performance will always be about the
  268. same. This is very true for regular kernels on GPUs for instance (<0.1% error),
  269. and just a bit less true on CPUs (~=1% error). This also assumes that there are
  270. few different sets of data input/output sizes. StarPU will then keep record of
  271. the average time of previous executions on the various processing units, and use
  272. it as an estimation. History is done per task size, by using a hash of the input
  273. and ouput sizes as an index.
  274. It will also save it in @code{~/.starpu/sampling/codelets}
  275. for further executions, and can be observed by using the
  276. @code{starpu_perfmodel_display} command, or drawn by using
  277. the @code{starpu_perfmodel_plot} (@pxref{Performance model calibration}). The
  278. models are indexed by machine name. To
  279. share the models between machines (e.g. for a homogeneous cluster), use
  280. @code{export STARPU_HOSTNAME=some_global_name}. Measurements are only done
  281. when using a task scheduler which makes use of it, such as @code{heft} or
  282. @code{dmda}. Measurements can also be provided explicitly by the application, by
  283. using the @code{starpu_perfmodel_update_history} function.
  284. The following is a small code example.
  285. If e.g. the code is recompiled with other compilation options, or several
  286. variants of the code are used, the symbol string should be changed to reflect
  287. that, in order to recalibrate a new model from zero. The symbol string can even
  288. be constructed dynamically at execution time, as long as this is done before
  289. submitting any task using it.
  290. @cartouche
  291. @smallexample
  292. static struct starpu_perfmodel mult_perf_model = @{
  293. .type = STARPU_HISTORY_BASED,
  294. .symbol = "mult_perf_model"
  295. @};
  296. struct starpu_codelet cl = @{
  297. .where = STARPU_CPU,
  298. .cpu_funcs = @{ cpu_mult, NULL @},
  299. .nbuffers = 3,
  300. .modes = @{ STARPU_R, STARPU_R, STARPU_W @},
  301. /* for the scheduling policy to be able to use performance models */
  302. .model = &mult_perf_model
  303. @};
  304. @end smallexample
  305. @end cartouche
  306. @item
  307. Measured at runtime and refined by regression (@code{STARPU_*REGRESSION_BASED}
  308. model type). This still assumes performance regularity, but works
  309. with various data input sizes, by applying regression over observed
  310. execution times. STARPU_REGRESSION_BASED uses an a*n^b regression
  311. form, STARPU_NL_REGRESSION_BASED uses an a*n^b+c (more precise than
  312. STARPU_REGRESSION_BASED, but costs a lot more to compute).
  313. For instance,
  314. @code{tests/perfmodels/regression_based.c} uses a regression-based performance
  315. model for the @code{memset} operation.
  316. Of course, the application has to issue
  317. tasks with varying size so that the regression can be computed. StarPU will not
  318. trust the regression unless there is at least 10% difference between the minimum
  319. and maximum observed input size. It can be useful to set the
  320. @code{STARPU_CALIBRATE} environment variable to @code{1} and run the application
  321. on varying input sizes, so as to feed the performance model for a variety of
  322. inputs, or to provide the measurements explictly by using
  323. @code{starpu_perfmodel_update_history}. The @code{starpu_perfmodel_display} and
  324. @code{starpu_perfmodel_plot}
  325. tools can be used to observe how much the performance model is calibrated (@pxref{Performance model calibration}); when
  326. their output look good, @code{STARPU_CALIBRATE} can be reset to @code{0} to let
  327. StarPU use the resulting performance model without recording new measures. If
  328. the data input sizes vary a lot, it is really important to set
  329. @code{STARPU_CALIBRATE} to @code{0}, otherwise StarPU will continue adding the
  330. measures, and result with a very big performance model, which will take time a
  331. lot of time to load and save.
  332. For non-linear regression, since computing it
  333. is quite expensive, it is only done at termination of the application. This
  334. means that the first execution of the application will use only history-based
  335. performance model to perform scheduling, without using regression.
  336. @item
  337. Provided as an estimation from the application itself (@code{STARPU_COMMON} model type and @code{cost_function} field),
  338. see for instance
  339. @code{examples/common/blas_model.h} and @code{examples/common/blas_model.c}.
  340. @item
  341. Provided explicitly by the application (@code{STARPU_PER_ARCH} model type): the
  342. @code{.per_arch[arch][nimpl].cost_function} fields have to be filled with pointers to
  343. functions which return the expected duration of the task in micro-seconds, one
  344. per architecture.
  345. @end itemize
  346. For the @code{STARPU_HISTORY_BASED} and @code{STARPU_*REGRESSION_BASE},
  347. the total size of task data (both input and output) is used as an index by
  348. default. The @code{size_base} field of @code{struct starpu_perfmodel} however
  349. permits the application to override that, when for instance some of the data
  350. do not matter for task cost (e.g. mere reference table), or when using sparse
  351. structures (in which case it is the number of non-zeros which matter), or when
  352. there is some hidden parameter such as the number of iterations, etc.
  353. How to use schedulers which can benefit from such performance model is explained
  354. in @ref{Task scheduling policy}.
  355. The same can be done for task power consumption estimation, by setting the
  356. @code{power_model} field the same way as the @code{model} field. Note: for
  357. now, the application has to give to the power consumption performance model
  358. a name which is different from the execution time performance model.
  359. The application can request time estimations from the StarPU performance
  360. models by filling a task structure as usual without actually submitting
  361. it. The data handles can be created by calling @code{starpu_data_register}
  362. functions with a @code{NULL} pointer (and need to be unregistered as usual)
  363. and the desired data sizes. The @code{starpu_task_expected_length} and
  364. @code{starpu_task_expected_power} functions can then be called to get an
  365. estimation of the task duration on a given arch. @code{starpu_task_destroy}
  366. needs to be called to destroy the dummy task afterwards. See
  367. @code{tests/perfmodels/regression_based.c} for an example.
  368. @node Theoretical lower bound on execution time
  369. @section Theoretical lower bound on execution time
  370. For kernels with history-based performance models (and provided that they are completely calibrated), StarPU can very easily provide a theoretical lower
  371. bound for the execution time of a whole set of tasks. See for
  372. instance @code{examples/lu/lu_example.c}: before submitting tasks,
  373. call @code{starpu_bound_start}, and after complete execution, call
  374. @code{starpu_bound_stop}. @code{starpu_bound_print_lp} or
  375. @code{starpu_bound_print_mps} can then be used to output a Linear Programming
  376. problem corresponding to the schedule of your tasks. Run it through
  377. @code{lp_solve} or any other linear programming solver, and that will give you a
  378. lower bound for the total execution time of your tasks. If StarPU was compiled
  379. with the glpk library installed, @code{starpu_bound_compute} can be used to
  380. solve it immediately and get the optimized minimum, in ms. Its @code{integer}
  381. parameter allows to decide whether integer resolution should be computed
  382. and returned too.
  383. The @code{deps} parameter tells StarPU whether to take tasks and implicit data
  384. dependencies into account. It must be understood that the linear programming
  385. problem size is quadratic with the number of tasks and thus the time to solve it
  386. will be very long, it could be minutes for just a few dozen tasks. You should
  387. probably use @code{lp_solve -timeout 1 test.pl -wmps test.mps} to convert the
  388. problem to MPS format and then use a better solver, @code{glpsol} might be
  389. better than @code{lp_solve} for instance (the @code{--pcost} option may be
  390. useful), but sometimes doesn't manage to converge. @code{cbc} might look
  391. slower, but it is parallel. Be sure to try at least all the @code{-B} options
  392. of @code{lp_solve}. For instance, we often just use
  393. @code{lp_solve -cc -B1 -Bb -Bg -Bp -Bf -Br -BG -Bd -Bs -BB -Bo -Bc -Bi} , and
  394. the @code{-gr} option can also be quite useful.
  395. Setting @code{deps} to 0 will only take into account the actual computations
  396. on processing units. It however still properly takes into account the varying
  397. performances of kernels and processing units, which is quite more accurate than
  398. just comparing StarPU performances with the fastest of the kernels being used.
  399. The @code{prio} parameter tells StarPU whether to simulate taking into account
  400. the priorities as the StarPU scheduler would, i.e. schedule prioritized
  401. tasks before less prioritized tasks, to check to which extend this results
  402. to a less optimal solution. This increases even more computation time.
  403. Note that for simplicity, all this however doesn't take into account data
  404. transfers, which are assumed to be completely overlapped.
  405. @node Insert Task Utility
  406. @section Insert Task Utility
  407. StarPU provides the wrapper function @code{starpu_insert_task} to ease
  408. the creation and submission of tasks.
  409. @deftypefun int starpu_insert_task (struct starpu_codelet *@var{cl}, ...)
  410. Create and submit a task corresponding to @var{cl} with the following
  411. arguments. The argument list must be zero-terminated.
  412. The arguments following the codelets can be of the following types:
  413. @itemize
  414. @item
  415. @code{STARPU_R}, @code{STARPU_W}, @code{STARPU_RW}, @code{STARPU_SCRATCH}, @code{STARPU_REDUX} an access mode followed by a data handle;
  416. @item
  417. @code{STARPU_DATA_ARRAY} followed by an array of data handles and its number of elements;
  418. @item
  419. the specific values @code{STARPU_VALUE}, @code{STARPU_CALLBACK},
  420. @code{STARPU_CALLBACK_ARG}, @code{STARPU_CALLBACK_WITH_ARG},
  421. @code{STARPU_PRIORITY}, followed by the appropriated objects as
  422. defined below.
  423. @end itemize
  424. When using @code{STARPU_DATA_ARRAY}, the access mode of the data
  425. handles is not defined.
  426. Parameters to be passed to the codelet implementation are defined
  427. through the type @code{STARPU_VALUE}. The function
  428. @code{starpu_codelet_unpack_args} must be called within the codelet
  429. implementation to retrieve them.
  430. @end deftypefun
  431. @defmac STARPU_VALUE
  432. this macro is used when calling @code{starpu_insert_task}, and must be
  433. followed by a pointer to a constant value and the size of the constant
  434. @end defmac
  435. @defmac STARPU_CALLBACK
  436. this macro is used when calling @code{starpu_insert_task}, and must be
  437. followed by a pointer to a callback function
  438. @end defmac
  439. @defmac STARPU_CALLBACK_ARG
  440. this macro is used when calling @code{starpu_insert_task}, and must be
  441. followed by a pointer to be given as an argument to the callback
  442. function
  443. @end defmac
  444. @defmac STARPU_CALLBACK_WITH_ARG
  445. this macro is used when calling @code{starpu_insert_task}, and must be
  446. followed by two pointers: one to a callback function, and the other to
  447. be given as an argument to the callback function; this is equivalent
  448. to using both @code{STARPU_CALLBACK} and
  449. @code{STARPU_CALLBACK_WITH_ARG}
  450. @end defmac
  451. @defmac STARPU_PRIORITY
  452. this macro is used when calling @code{starpu_insert_task}, and must be
  453. followed by a integer defining a priority level
  454. @end defmac
  455. @deftypefun void starpu_codelet_pack_args ({char **}@var{arg_buffer}, {size_t *}@var{arg_buffer_size}, ...)
  456. Pack arguments of type @code{STARPU_VALUE} into a buffer which can be
  457. given to a codelet and later unpacked with the function
  458. @code{starpu_codelet_unpack_args} defined below.
  459. @end deftypefun
  460. @deftypefun void starpu_codelet_unpack_args ({void *}@var{cl_arg}, ...)
  461. Retrieve the arguments of type @code{STARPU_VALUE} associated to a
  462. task automatically created using the function
  463. @code{starpu_insert_task} defined above.
  464. @end deftypefun
  465. Here the implementation of the codelet:
  466. @smallexample
  467. void func_cpu(void *descr[], void *_args)
  468. @{
  469. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  470. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  471. int ifactor;
  472. float ffactor;
  473. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  474. *x0 = *x0 * ifactor;
  475. *x1 = *x1 * ffactor;
  476. @}
  477. struct starpu_codelet mycodelet = @{
  478. .where = STARPU_CPU,
  479. .cpu_funcs = @{ func_cpu, NULL @},
  480. .nbuffers = 2,
  481. .modes = @{ STARPU_RW, STARPU_RW @}
  482. @};
  483. @end smallexample
  484. And the call to the @code{starpu_insert_task} wrapper:
  485. @smallexample
  486. starpu_insert_task(&mycodelet,
  487. STARPU_VALUE, &ifactor, sizeof(ifactor),
  488. STARPU_VALUE, &ffactor, sizeof(ffactor),
  489. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  490. 0);
  491. @end smallexample
  492. The call to @code{starpu_insert_task} is equivalent to the following
  493. code:
  494. @smallexample
  495. struct starpu_task *task = starpu_task_create();
  496. task->cl = &mycodelet;
  497. task->handles[0] = data_handles[0];
  498. task->handles[1] = data_handles[1];
  499. char *arg_buffer;
  500. size_t arg_buffer_size;
  501. starpu_codelet_pack_args(&arg_buffer, &arg_buffer_size,
  502. STARPU_VALUE, &ifactor, sizeof(ifactor),
  503. STARPU_VALUE, &ffactor, sizeof(ffactor),
  504. 0);
  505. task->cl_arg = arg_buffer;
  506. task->cl_arg_size = arg_buffer_size;
  507. int ret = starpu_task_submit(task);
  508. @end smallexample
  509. Here a similar call using @code{STARPU_DATA_ARRAY}.
  510. @smallexample
  511. starpu_insert_task(&mycodelet,
  512. STARPU_DATA_ARRAY, data_handles, 2,
  513. STARPU_VALUE, &ifactor, sizeof(ifactor),
  514. STARPU_VALUE, &ffactor, sizeof(ffactor),
  515. 0);
  516. @end smallexample
  517. If some part of the task insertion depends on the value of some computation,
  518. the @code{STARPU_DATA_ACQUIRE_CB} macro can be very convenient. For
  519. instance, assuming that the index variable @code{i} was registered as handle
  520. @code{i_handle}:
  521. @smallexample
  522. /* Compute which portion we will work on, e.g. pivot */
  523. starpu_insert_task(&which_index, STARPU_W, i_handle, 0);
  524. /* And submit the corresponding task */
  525. STARPU_DATA_ACQUIRE_CB(i_handle, STARPU_R, starpu_insert_task(&work, STARPU_RW, A_handle[i], 0));
  526. @end smallexample
  527. The @code{STARPU_DATA_ACQUIRE_CB} macro submits an asynchronous request for
  528. acquiring data @code{i} for the main application, and will execute the code
  529. given as third parameter when it is acquired. In other words, as soon as the
  530. value of @code{i} computed by the @code{which_index} codelet can be read, the
  531. portion of code passed as third parameter of @code{STARPU_DATA_ACQUIRE_CB} will
  532. be executed, and is allowed to read from @code{i} to use it e.g. as an
  533. index. Note that this macro is only avaible when compiling StarPU with
  534. the compiler @code{gcc}.
  535. @node Data reduction
  536. @section Data reduction
  537. In various cases, some piece of data is used to accumulate intermediate
  538. results. For instances, the dot product of a vector, maximum/minimum finding,
  539. the histogram of a photograph, etc. When these results are produced along the
  540. whole machine, it would not be efficient to accumulate them in only one place,
  541. incurring data transmission each and access concurrency.
  542. StarPU provides a @code{STARPU_REDUX} mode, which permits to optimize
  543. that case: it will allocate a buffer on each memory node, and accumulate
  544. intermediate results there. When the data is eventually accessed in the normal
  545. @code{STARPU_R} mode, StarPU will collect the intermediate results in just one
  546. buffer.
  547. For this to work, the user has to use the
  548. @code{starpu_data_set_reduction_methods} to declare how to initialize these
  549. buffers, and how to assemble partial results.
  550. For instance, @code{cg} uses that to optimize its dot product: it first defines
  551. the codelets for initialization and reduction:
  552. @smallexample
  553. struct starpu_codelet bzero_variable_cl =
  554. @{
  555. .cpu_funcs = @{ bzero_variable_cpu, NULL @},
  556. .cuda_funcs = @{ bzero_variable_cuda, NULL @},
  557. .nbuffers = 1,
  558. @}
  559. static void accumulate_variable_cpu(void *descr[], void *cl_arg)
  560. @{
  561. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  562. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  563. *v_dst = *v_dst + *v_src;
  564. @}
  565. static void accumulate_variable_cuda(void *descr[], void *cl_arg)
  566. @{
  567. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  568. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  569. cublasaxpy(1, (double)1.0, v_src, 1, v_dst, 1);
  570. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  571. @}
  572. struct starpu_codelet accumulate_variable_cl =
  573. @{
  574. .cpu_funcs = @{ accumulate_variable_cpu, NULL @},
  575. .cuda_funcs = @{ accumulate_variable_cuda, NULL @},
  576. .nbuffers = 1,
  577. @}
  578. @end smallexample
  579. and attaches them as reduction methods for its dtq handle:
  580. @smallexample
  581. starpu_data_set_reduction_methods(dtq_handle,
  582. &accumulate_variable_cl, &bzero_variable_cl);
  583. @end smallexample
  584. and dtq_handle can now be used in @code{STARPU_REDUX} mode for the dot products
  585. with partitioned vectors:
  586. @smallexample
  587. int dots(starpu_data_handle_t v1, starpu_data_handle_t v2,
  588. starpu_data_handle_t s, unsigned nblocks)
  589. @{
  590. starpu_insert_task(&bzero_variable_cl, STARPU_W, s, 0);
  591. for (b = 0; b < nblocks; b++)
  592. starpu_insert_task(&dot_kernel_cl,
  593. STARPU_RW, s,
  594. STARPU_R, starpu_data_get_sub_data(v1, 1, b),
  595. STARPU_R, starpu_data_get_sub_data(v2, 1, b),
  596. 0);
  597. @}
  598. @end smallexample
  599. The @code{cg} example also uses reduction for the blocked gemv kernel, leading
  600. to yet more relaxed dependencies and more parallelism.
  601. @node Temporary buffers
  602. @section Temporary buffers
  603. There are two kinds of temporary buffers: temporary data which just pass results
  604. from a task to another, and scratch data which are needed only internally by
  605. tasks.
  606. @subsection Temporary data
  607. Data can sometimes be entirely produced by a task, and entirely consumed by
  608. another task, without the need for other parts of the application to access
  609. it. In such case, registration can be done without prior allocation, by using
  610. the special -1 memory node number, and passing a zero pointer. StarPU will
  611. actually allocate memory only when the task creating the content gets scheduled,
  612. and destroy it on unregistration.
  613. In addition to that, it can be tedious for the application to have to unregister
  614. the data, since it will not use its content anyway. The unregistration can be
  615. done lazily by using the @code{starpu_data_unregister_submit(handle)} function,
  616. which will record that no more tasks accessing the handle will be submitted, so
  617. that it can be freed as soon as the last task accessing it is over.
  618. The following code examplifies both points: it registers the temporary
  619. data, submits three tasks accessing it, and records the data for automatic
  620. unregistration.
  621. @smallexample
  622. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  623. starpu_insert_task(&produce_data, STARPU_W, handle, 0);
  624. starpu_insert_task(&compute_data, STARPU_RW, handle, 0);
  625. starpu_insert_task(&summarize_data, STARPU_R, handle, STARPU_W, result_handle, 0);
  626. starpu_data_unregister_submit(handle);
  627. @end smallexample
  628. @subsection Scratch data
  629. Some kernels sometimes need temporary data to achieve the computations, i.e. a
  630. workspace. The application could allocate it at the start of the codelet
  631. function, and free it at the end, but that would be costly. It could also
  632. allocate one buffer per worker (similarly to @ref{Per-worker library
  633. initialization }), but that would make them systematic and permanent. A more
  634. optimized way is to use the SCRATCH data access mode, as examplified below,
  635. which provides per-worker buffers without content consistency.
  636. @smallexample
  637. starpu_vector_data_register(&workspace, -1, 0, sizeof(float));
  638. for (i = 0; i < N; i++)
  639. starpu_insert_task(&compute, STARPU_R, input[i], STARPU_SCRATCH, workspace, STARPU_W, output[i], 0);
  640. @end smallexample
  641. StarPU will make sure that the buffer is allocated before executing the task,
  642. and make this allocation per-worker: for CPU workers, notably, each worker has
  643. its own buffer. This means that each task submitted above will actually have its
  644. own workspace, which will actually be the same for all tasks running one after
  645. the other on the same worker. Also, if for instance GPU memory becomes scarce,
  646. StarPU will notice that it can free such buffers easily, since the content does
  647. not matter.
  648. @node Parallel Tasks
  649. @section Parallel Tasks
  650. StarPU can leverage existing parallel computation libraries by the means of
  651. parallel tasks. A parallel task is a task which gets worked on by a set of CPUs
  652. (called a parallel or combined worker) at the same time, by using an existing
  653. parallel CPU implementation of the computation to be achieved. This can also be
  654. useful to improve the load balance between slow CPUs and fast GPUs: since CPUs
  655. work collectively on a single task, the completion time of tasks on CPUs become
  656. comparable to the completion time on GPUs, thus relieving from granularity
  657. discrepancy concerns. Hwloc support needs to be enabled to get good performance,
  658. otherwise StarPU will not know how to better group cores.
  659. Two modes of execution exist to accomodate with existing usages.
  660. @subsection Fork-mode parallel tasks
  661. In the Fork mode, StarPU will call the codelet function on one
  662. of the CPUs of the combined worker. The codelet function can use
  663. @code{starpu_combined_worker_get_size()} to get the number of threads it is
  664. allowed to start to achieve the computation. The CPU binding mask for the whole
  665. set of CPUs is already enforced, so that threads created by the function will
  666. inherit the mask, and thus execute where StarPU expected, the OS being in charge
  667. of choosing how to schedule threads on the corresponding CPUs. The application
  668. can also choose to bind threads by hand, using e.g. sched_getaffinity to know
  669. the CPU binding mask that StarPU chose.
  670. For instance, using OpenMP (full source is available in
  671. @code{examples/openmp/vector_scal.c}):
  672. @example
  673. void scal_cpu_func(void *buffers[], void *_args)
  674. @{
  675. unsigned i;
  676. float *factor = _args;
  677. struct starpu_vector_interface *vector = buffers[0];
  678. unsigned n = STARPU_VECTOR_GET_NX(vector);
  679. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  680. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  681. for (i = 0; i < n; i++)
  682. val[i] *= *factor;
  683. @}
  684. static struct starpu_codelet cl =
  685. @{
  686. .modes = @{ STARPU_RW @},
  687. .where = STARPU_CPU,
  688. .type = STARPU_FORKJOIN,
  689. .max_parallelism = INT_MAX,
  690. .cpu_funcs = @{scal_cpu_func, NULL@},
  691. .nbuffers = 1,
  692. @};
  693. @end example
  694. Other examples include for instance calling a BLAS parallel CPU implementation
  695. (see @code{examples/mult/xgemm.c}).
  696. @subsection SPMD-mode parallel tasks
  697. In the SPMD mode, StarPU will call the codelet function on
  698. each CPU of the combined worker. The codelet function can use
  699. @code{starpu_combined_worker_get_size()} to get the total number of CPUs
  700. involved in the combined worker, and thus the number of calls that are made in
  701. parallel to the function, and @code{starpu_combined_worker_get_rank()} to get
  702. the rank of the current CPU within the combined worker. For instance:
  703. @example
  704. static void func(void *buffers[], void *args)
  705. @{
  706. unsigned i;
  707. float *factor = _args;
  708. struct starpu_vector_interface *vector = buffers[0];
  709. unsigned n = STARPU_VECTOR_GET_NX(vector);
  710. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  711. /* Compute slice to compute */
  712. unsigned m = starpu_combined_worker_get_size();
  713. unsigned j = starpu_combined_worker_get_rank();
  714. unsigned slice = (n+m-1)/m;
  715. for (i = j * slice; i < (j+1) * slice && i < n; i++)
  716. val[i] *= *factor;
  717. @}
  718. static struct starpu_codelet cl =
  719. @{
  720. .modes = @{ STARPU_RW @},
  721. .where = STARP_CPU,
  722. .type = STARPU_SPMD,
  723. .max_parallelism = INT_MAX,
  724. .cpu_funcs = @{ func, NULL @},
  725. .nbuffers = 1,
  726. @}
  727. @end example
  728. Of course, this trivial example will not really benefit from parallel task
  729. execution, and was only meant to be simple to understand. The benefit comes
  730. when the computation to be done is so that threads have to e.g. exchange
  731. intermediate results, or write to the data in a complex but safe way in the same
  732. buffer.
  733. @subsection Parallel tasks performance
  734. To benefit from parallel tasks, a parallel-task-aware StarPU scheduler has to
  735. be used. When exposed to codelets with a Fork or SPMD flag, the @code{pheft}
  736. (parallel-heft) and @code{pgreedy} (parallel greedy) schedulers will indeed also
  737. try to execute tasks with several CPUs. It will automatically try the various
  738. available combined worker sizes and thus be able to avoid choosing a large
  739. combined worker if the codelet does not actually scale so much.
  740. @subsection Combined worker sizes
  741. By default, StarPU creates combined workers according to the architecture
  742. structure as detected by hwloc. It means that for each object of the hwloc
  743. topology (NUMA node, socket, cache, ...) a combined worker will be created. If
  744. some nodes of the hierarchy have a big arity (e.g. many cores in a socket
  745. without a hierarchy of shared caches), StarPU will create combined workers of
  746. intermediate sizes.
  747. The user can give some hints to StarPU about combined workers sizes to favor.
  748. This can be done by using the environment variables @code{STARPU_MIN_WORKERSIZE}
  749. and @code{STARPU_MAX_WORKERSIZE}. When set, they will force StarPU to create the
  750. biggest combined workers possible without overstepping the defined boundaries.
  751. However, StarPU will create the remaining combined workers without abiding by
  752. the rules if not possible.
  753. For example : if the user specifies a minimum and maximum combined workers size
  754. of 3 on a machine containing 8 CPUs, StarPU will create a combined worker of
  755. size 2 beside the combined workers of size 3.
  756. The combined workers actually produced can be seen in the output of the
  757. @code{starpu_machine_display} tool (the @code{STARPU_SCHED} environment variable
  758. has to be set to a combined worker-aware scheduler such as @code{pheft} or
  759. @code{pgreedy}).
  760. @subsection Concurrent parallel tasks
  761. Unfortunately, many environments and librairies do not support concurrent
  762. calls.
  763. For instance, most OpenMP implementations (including the main ones) do not
  764. support concurrent @code{pragma omp parallel} statements without nesting them in
  765. another @code{pragma omp parallel} statement, but StarPU does not yet support
  766. creating its CPU workers by using such pragma.
  767. Other parallel libraries are also not safe when being invoked concurrently
  768. from different threads, due to the use of global variables in their sequential
  769. sections for instance.
  770. The solution is then to use only one combined worker at a time. This can be
  771. done by setting @code{single_combined_worker} to 1 in the @code{starpu_conf}
  772. structure, or setting the @code{STARPU_SINGLE_COMBINED_WORKER} environment
  773. variable to 1. StarPU will then run only one parallel task at a time.
  774. @node Debugging
  775. @section Debugging
  776. StarPU provides several tools to help debugging aplications. Execution traces
  777. can be generated and displayed graphically, see @ref{Generating traces}. Some
  778. gdb helpers are also provided to show the whole StarPU state:
  779. @smallexample
  780. (gdb) source tools/gdbinit
  781. (gdb) help starpu
  782. @end smallexample
  783. @node The multiformat interface
  784. @section The multiformat interface
  785. It may be interesting to represent the same piece of data using two different
  786. data structures: one that would only be used on CPUs, and one that would only
  787. be used on GPUs. This can be done by using the multiformat interface. StarPU
  788. will be able to convert data from one data structure to the other when needed.
  789. Note that the heft scheduler is the only one optimized for this interface. The
  790. user must provide StarPU with conversion codelets:
  791. @cartouche
  792. @smallexample
  793. #define NX 1024
  794. struct point array_of_structs[NX];
  795. starpu_data_handle_t handle;
  796. /*
  797. * The conversion of a piece of data is itself a task, though it is created,
  798. * submitted and destroyed by StarPU internals and not by the user. Therefore,
  799. * we have to define two codelets.
  800. * Note that for now the conversion from the CPU format to the GPU format has to
  801. * be executed on the GPU, and the conversion from the GPU to the CPU has to be
  802. * executed on the CPU.
  803. */
  804. #ifdef STARPU_USE_OPENCL
  805. void cpu_to_opencl_opencl_func(void *buffers[], void *args);
  806. struct starpu_codelet cpu_to_opencl_cl = @{
  807. .where = STARPU_OPENCL,
  808. .opencl_funcs = @{ cpu_to_opencl_opencl_func, NULL @},
  809. .nbuffers = 1,
  810. .modes = @{ STARPU_RW @}
  811. @};
  812. void opencl_to_cpu_func(void *buffers[], void *args);
  813. struct starpu_codelet opencl_to_cpu_cl = @{
  814. .where = STARPU_CPU,
  815. .cpu_funcs = @{ opencl_to_cpu_func, NULL @},
  816. .nbuffers = 1,
  817. .modes = @{ STARPU_RW @}
  818. @};
  819. #endif
  820. struct starpu_multiformat_data_interface_ops format_ops = @{
  821. #ifdef STARPU_USE_OPENCL
  822. .opencl_elemsize = 2 * sizeof(float),
  823. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  824. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  825. #endif
  826. .cpu_elemsize = 2 * sizeof(float),
  827. ...
  828. @};
  829. starpu_multiformat_data_register(handle, 0, &array_of_structs, NX, &format_ops);
  830. @end smallexample
  831. @end cartouche
  832. Kernels can be written almost as for any other interface. Note that
  833. STARPU_MULTIFORMAT_GET_CPU_PTR shall only be used for CPU kernels. CUDA kernels
  834. must use STARPU_MULTIFORMAT_GET_CUDA_PTR, and OpenCL kernels must use
  835. STARPU_MULTIFORMAT_GET_OPENCL_PTR. STARPU_MULTIFORMAT_GET_NX may be used in any
  836. kind of kernel.
  837. @cartouche
  838. @smallexample
  839. static void
  840. multiformat_scal_cpu_func(void *buffers[], void *args)
  841. @{
  842. struct point *aos;
  843. unsigned int n;
  844. aos = STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  845. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  846. ...
  847. @}
  848. extern "C" void multiformat_scal_cuda_func(void *buffers[], void *_args)
  849. @{
  850. unsigned int n;
  851. struct struct_of_arrays *soa;
  852. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  853. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  854. ...
  855. @}
  856. @end smallexample
  857. @end cartouche
  858. A full example may be found in @code{examples/basic_examples/multiformat.c}.
  859. @node On-GPU rendering
  860. @section On-GPU rendering
  861. Graphical-oriented applications need to draw the result of their computations,
  862. typically on the very GPU where these happened. Technologies such as OpenGL/CUDA
  863. interoperability permit to let CUDA directly work on the OpenGL buffers, making
  864. them thus immediately ready for drawing, by mapping OpenGL buffer, textures or
  865. renderbuffer objects into CUDA. CUDA however imposes some technical
  866. constraints: peer memcpy has to be disabled, and the thread that runs OpenGL has
  867. to be the one that runs CUDA computations for that GPU.
  868. To achieve this with StarPU, pass the @code{--disable-cuda-memcpy-peer} option
  869. to @code{./configure} (TODO: make it dynamic), OpenGL/GLUT has to be initialized
  870. first, and the interoperability mode has to
  871. be enabled by using the @code{cuda_opengl_interoperability} field of the
  872. @code{starpu_conf} structure, and the driver loop has to be run by
  873. the application, by using the @code{not_launched_drivers} field of
  874. @code{starpu_conf} to prevent StarPU from running it in a separate thread, and
  875. by using @code{starpu_driver_run} to run the loop. The @code{gl_interop} and
  876. @code{gl_interop_idle} examples shows how it articulates in a simple case, where
  877. rendering is done in task callbacks. The former uses @code{glutMainLoopEvent}
  878. to make GLUT progress from the StarPU driver loop, while the latter uses
  879. @code{glutIdleFunc} to make StarPU progress from the GLUT main loop.
  880. Then, to use an OpenGL buffer as a CUDA data, StarPU simply needs to be given
  881. the CUDA pointer at registration, for instance:
  882. @cartouche
  883. @smallexample
  884. /* Get the CUDA worker id */
  885. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  886. if (starpu_worker_get_type(workerid) == STARPU_CUDA_WORKER)
  887. break;
  888. /* Build a CUDA pointer pointing at the OpenGL buffer */
  889. cudaGraphicsResourceGetMappedPointer((void**)&output, &num_bytes, resource);
  890. /* And register it to StarPU */
  891. starpu_vector_data_register(&handle, starpu_worker_get_memory_node(workerid), output, num_bytes / sizeof(float4), sizeof(float4));
  892. /* The handle can now be used as usual */
  893. starpu_insert_task(&cl, STARPU_RW, handle, 0);
  894. /* ... */
  895. /* This gets back data into the OpenGL buffer */
  896. starpu_data_unregister(handle);
  897. @end smallexample
  898. @end cartouche
  899. and display it e.g. in the callback function.
  900. @node More examples
  901. @section More examples
  902. More examples are available in the StarPU sources in the @code{examples/}
  903. directory. Simple examples include:
  904. @table @asis
  905. @item @code{incrementer/}:
  906. Trivial incrementation test.
  907. @item @code{basic_examples/}:
  908. Simple documented Hello world (as shown in @ref{Hello World}), vector/scalar product (as shown
  909. in @ref{Vector Scaling on an Hybrid CPU/GPU Machine}), matrix
  910. product examples (as shown in @ref{Performance model example}), an example using the blocked matrix data
  911. interface, an example using the variable data interface, and an example
  912. using different formats on CPUs and GPUs.
  913. @item @code{matvecmult/}:
  914. OpenCL example from NVidia, adapted to StarPU.
  915. @item @code{axpy/}:
  916. AXPY CUBLAS operation adapted to StarPU.
  917. @item @code{fortran/}:
  918. Example of Fortran bindings.
  919. @end table
  920. More advanced examples include:
  921. @table @asis
  922. @item @code{filters/}:
  923. Examples using filters, as shown in @ref{Partitioning Data}.
  924. @item @code{lu/}:
  925. LU matrix factorization, see for instance @code{xlu_implicit.c}
  926. @item @code{cholesky/}:
  927. Cholesky matrix factorization, see for instance @code{cholesky_implicit.c}.
  928. @end table