advanced-examples.texi 50 KB

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