advanced-examples.texi 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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_profiling_task_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_profiling_worker_info worker_info;
  170. int ret = starpu_profiling_worker_get_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. For @code{lp_solve}, be sure to try at least all the
  410. @code{-B} options. For instance, we often just use @code{lp_solve -cc -B1 -Bb
  411. -Bg -Bp -Bf -Br -BG -Bd -Bs -BB -Bo -Bc -Bi} , and the @code{-gr} option can
  412. also be quite useful. The resulting schedule can be observed by using the
  413. @code{starpu_lp2paje} tool, which converts it into the Paje format.
  414. Data transfer time can only be taken into account when @code{deps} is set. Only
  415. data transfers inferred from implicit data dependencies between tasks are taken
  416. into account. Other data transfers are assumed to be completely overlapped.
  417. Setting @code{deps} to 0 will only take into account the actual computations
  418. on processing units. It however still properly takes into account the varying
  419. performances of kernels and processing units, which is quite more accurate than
  420. just comparing StarPU performances with the fastest of the kernels being used.
  421. The @code{prio} parameter tells StarPU whether to simulate taking into account
  422. the priorities as the StarPU scheduler would, i.e. schedule prioritized
  423. tasks before less prioritized tasks, to check to which extend this results
  424. to a less optimal solution. This increases even more computation time.
  425. @node Insert Task Utility
  426. @section Insert Task Utility
  427. StarPU provides the wrapper function @code{starpu_insert_task} to ease
  428. the creation and submission of tasks. See the definition of the
  429. functions in @ref{Insert Task}.
  430. Here the implementation of the codelet:
  431. @cartouche
  432. @smallexample
  433. void func_cpu(void *descr[], void *_args)
  434. @{
  435. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  436. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  437. int ifactor;
  438. float ffactor;
  439. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  440. *x0 = *x0 * ifactor;
  441. *x1 = *x1 * ffactor;
  442. @}
  443. struct starpu_codelet mycodelet = @{
  444. .where = STARPU_CPU,
  445. .cpu_funcs = @{ func_cpu, NULL @},
  446. .nbuffers = 2,
  447. .modes = @{ STARPU_RW, STARPU_RW @}
  448. @};
  449. @end smallexample
  450. @end cartouche
  451. And the call to the @code{starpu_insert_task} wrapper:
  452. @cartouche
  453. @smallexample
  454. starpu_insert_task(&mycodelet,
  455. STARPU_VALUE, &ifactor, sizeof(ifactor),
  456. STARPU_VALUE, &ffactor, sizeof(ffactor),
  457. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  458. 0);
  459. @end smallexample
  460. @end cartouche
  461. The call to @code{starpu_insert_task} is equivalent to the following
  462. code:
  463. @cartouche
  464. @smallexample
  465. struct starpu_task *task = starpu_task_create();
  466. task->cl = &mycodelet;
  467. task->handles[0] = data_handles[0];
  468. task->handles[1] = data_handles[1];
  469. char *arg_buffer;
  470. size_t arg_buffer_size;
  471. starpu_codelet_pack_args(&arg_buffer, &arg_buffer_size,
  472. STARPU_VALUE, &ifactor, sizeof(ifactor),
  473. STARPU_VALUE, &ffactor, sizeof(ffactor),
  474. 0);
  475. task->cl_arg = arg_buffer;
  476. task->cl_arg_size = arg_buffer_size;
  477. int ret = starpu_task_submit(task);
  478. @end smallexample
  479. @end cartouche
  480. Here a similar call using @code{STARPU_DATA_ARRAY}.
  481. @cartouche
  482. @smallexample
  483. starpu_insert_task(&mycodelet,
  484. STARPU_DATA_ARRAY, data_handles, 2,
  485. STARPU_VALUE, &ifactor, sizeof(ifactor),
  486. STARPU_VALUE, &ffactor, sizeof(ffactor),
  487. 0);
  488. @end smallexample
  489. @end cartouche
  490. If some part of the task insertion depends on the value of some computation,
  491. the @code{STARPU_DATA_ACQUIRE_CB} macro can be very convenient. For
  492. instance, assuming that the index variable @code{i} was registered as handle
  493. @code{i_handle}:
  494. @cartouche
  495. @smallexample
  496. /* Compute which portion we will work on, e.g. pivot */
  497. starpu_insert_task(&which_index, STARPU_W, i_handle, 0);
  498. /* And submit the corresponding task */
  499. STARPU_DATA_ACQUIRE_CB(i_handle, STARPU_R,
  500. starpu_insert_task(&work, STARPU_RW, A_handle[i], 0));
  501. @end smallexample
  502. @end cartouche
  503. The @code{STARPU_DATA_ACQUIRE_CB} macro submits an asynchronous request for
  504. acquiring data @code{i} for the main application, and will execute the code
  505. given as third parameter when it is acquired. In other words, as soon as the
  506. value of @code{i} computed by the @code{which_index} codelet can be read, the
  507. portion of code passed as third parameter of @code{STARPU_DATA_ACQUIRE_CB} will
  508. be executed, and is allowed to read from @code{i} to use it e.g. as an
  509. index. Note that this macro is only avaible when compiling StarPU with
  510. the compiler @code{gcc}.
  511. @node Data reduction
  512. @section Data reduction
  513. In various cases, some piece of data is used to accumulate intermediate
  514. results. For instances, the dot product of a vector, maximum/minimum finding,
  515. the histogram of a photograph, etc. When these results are produced along the
  516. whole machine, it would not be efficient to accumulate them in only one place,
  517. incurring data transmission each and access concurrency.
  518. StarPU provides a @code{STARPU_REDUX} mode, which permits to optimize
  519. that case: it will allocate a buffer on each memory node, and accumulate
  520. intermediate results there. When the data is eventually accessed in the normal
  521. @code{STARPU_R} mode, StarPU will collect the intermediate results in just one
  522. buffer.
  523. For this to work, the user has to use the
  524. @code{starpu_data_set_reduction_methods} to declare how to initialize these
  525. buffers, and how to assemble partial results.
  526. For instance, @code{cg} uses that to optimize its dot product: it first defines
  527. the codelets for initialization and reduction:
  528. @cartouche
  529. @smallexample
  530. struct starpu_codelet bzero_variable_cl =
  531. @{
  532. .cpu_funcs = @{ bzero_variable_cpu, NULL @},
  533. .cuda_funcs = @{ bzero_variable_cuda, NULL @},
  534. .nbuffers = 1,
  535. @}
  536. static void accumulate_variable_cpu(void *descr[], void *cl_arg)
  537. @{
  538. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  539. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  540. *v_dst = *v_dst + *v_src;
  541. @}
  542. static void accumulate_variable_cuda(void *descr[], void *cl_arg)
  543. @{
  544. double *v_dst = (double *)STARPU_VARIABLE_GET_PTR(descr[0]);
  545. double *v_src = (double *)STARPU_VARIABLE_GET_PTR(descr[1]);
  546. cublasaxpy(1, (double)1.0, v_src, 1, v_dst, 1);
  547. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  548. @}
  549. struct starpu_codelet accumulate_variable_cl =
  550. @{
  551. .cpu_funcs = @{ accumulate_variable_cpu, NULL @},
  552. .cuda_funcs = @{ accumulate_variable_cuda, NULL @},
  553. .nbuffers = 1,
  554. @}
  555. @end smallexample
  556. @end cartouche
  557. and attaches them as reduction methods for its dtq handle:
  558. @cartouche
  559. @smallexample
  560. starpu_variable_data_register(&dtq_handle, -1, NULL, sizeof(type));
  561. starpu_data_set_reduction_methods(dtq_handle,
  562. &accumulate_variable_cl, &bzero_variable_cl);
  563. @end smallexample
  564. @end cartouche
  565. and @code{dtq_handle} can now be used in @code{STARPU_REDUX} mode for the dot products
  566. with partitioned vectors:
  567. @cartouche
  568. @smallexample
  569. for (b = 0; b < nblocks; b++)
  570. starpu_insert_task(&dot_kernel_cl,
  571. STARPU_REDUX, dtq_handle,
  572. STARPU_R, starpu_data_get_sub_data(v1, 1, b),
  573. STARPU_R, starpu_data_get_sub_data(v2, 1, b),
  574. 0);
  575. @end smallexample
  576. @end cartouche
  577. During registration, we have here provided NULL, i.e. there is no initial value
  578. to be taken into account during reduction. StarPU will thus only take into
  579. account the contributions from the @code{dot_kernel_cl} tasks. Also, it will not
  580. allocate any memory for @code{dtq_handle} before @code{dot_kernel_cl} tasks are
  581. ready to run.
  582. If another dot product has to be performed, one could unregister
  583. @code{dtq_handle}, and re-register it. But one can also use
  584. @code{starpu_data_invalidate_submit(dtq_handle)}, which will clear all data from the handle,
  585. thus resetting it back to the initial @code{register(NULL)} state.
  586. The @code{cg} example also uses reduction for the blocked gemv kernel, leading
  587. to yet more relaxed dependencies and more parallelism.
  588. STARPU_REDUX can also be passed to @code{starpu_mpi_insert_task} in the MPI
  589. case. That will however not produce any MPI communication, but just pass
  590. STARPU_REDUX to the underlying @code{starpu_insert_task}. It is up to the
  591. application to call @code{starpu_mpi_redux_data}, which posts tasks that will
  592. reduce the partial results among MPI nodes into the MPI node which owns the
  593. data. For instance, some hypothetical application which collects partial results
  594. into data @code{res}, then uses it for other computation, before looping again
  595. with a new reduction:
  596. @cartouche
  597. @smallexample
  598. for (i = 0; i < 100; i++) @{
  599. starpu_mpi_insert_task(MPI_COMM_WORLD, &init_res, STARPU_W, res, 0);
  600. starpu_mpi_insert_task(MPI_COMM_WORLD, &work, STARPU_RW, A,
  601. STARPU_R, B, STARPU_REDUX, res, 0);
  602. starpu_mpi_redux_data(MPI_COMM_WORLD, res);
  603. starpu_mpi_insert_task(MPI_COMM_WORLD, &work2, STARPU_RW, B, STARPU_R, res, 0);
  604. @}
  605. @end smallexample
  606. @end cartouche
  607. @node Temporary buffers
  608. @section Temporary buffers
  609. There are two kinds of temporary buffers: temporary data which just pass results
  610. from a task to another, and scratch data which are needed only internally by
  611. tasks.
  612. @subsection Temporary data
  613. Data can sometimes be entirely produced by a task, and entirely consumed by
  614. another task, without the need for other parts of the application to access
  615. it. In such case, registration can be done without prior allocation, by using
  616. the special -1 memory node number, and passing a zero pointer. StarPU will
  617. actually allocate memory only when the task creating the content gets scheduled,
  618. and destroy it on unregistration.
  619. In addition to that, it can be tedious for the application to have to unregister
  620. the data, since it will not use its content anyway. The unregistration can be
  621. done lazily by using the @code{starpu_data_unregister_submit(handle)} function,
  622. which will record that no more tasks accessing the handle will be submitted, so
  623. that it can be freed as soon as the last task accessing it is over.
  624. The following code examplifies both points: it registers the temporary
  625. data, submits three tasks accessing it, and records the data for automatic
  626. unregistration.
  627. @cartouche
  628. @smallexample
  629. starpu_vector_data_register(&handle, -1, 0, n, sizeof(float));
  630. starpu_insert_task(&produce_data, STARPU_W, handle, 0);
  631. starpu_insert_task(&compute_data, STARPU_RW, handle, 0);
  632. starpu_insert_task(&summarize_data, STARPU_R, handle, STARPU_W, result_handle, 0);
  633. starpu_data_unregister_submit(handle);
  634. @end smallexample
  635. @end cartouche
  636. @subsection Scratch data
  637. Some kernels sometimes need temporary data to achieve the computations, i.e. a
  638. workspace. The application could allocate it at the start of the codelet
  639. function, and free it at the end, but that would be costly. It could also
  640. allocate one buffer per worker (similarly to @ref{Per-worker library
  641. initialization}), but that would make them systematic and permanent. A more
  642. optimized way is to use the SCRATCH data access mode, as examplified below,
  643. which provides per-worker buffers without content consistency.
  644. @cartouche
  645. @smallexample
  646. starpu_vector_data_register(&workspace, -1, 0, sizeof(float));
  647. for (i = 0; i < N; i++)
  648. starpu_insert_task(&compute, STARPU_R, input[i],
  649. STARPU_SCRATCH, workspace, STARPU_W, output[i], 0);
  650. @end smallexample
  651. @end cartouche
  652. StarPU will make sure that the buffer is allocated before executing the task,
  653. and make this allocation per-worker: for CPU workers, notably, each worker has
  654. its own buffer. This means that each task submitted above will actually have its
  655. own workspace, which will actually be the same for all tasks running one after
  656. the other on the same worker. Also, if for instance GPU memory becomes scarce,
  657. StarPU will notice that it can free such buffers easily, since the content does
  658. not matter.
  659. The @code{examples/pi} example uses scratches for some temporary buffer.
  660. @node Parallel Tasks
  661. @section Parallel Tasks
  662. StarPU can leverage existing parallel computation libraries by the means of
  663. parallel tasks. A parallel task is a task which gets worked on by a set of CPUs
  664. (called a parallel or combined worker) at the same time, by using an existing
  665. parallel CPU implementation of the computation to be achieved. This can also be
  666. useful to improve the load balance between slow CPUs and fast GPUs: since CPUs
  667. work collectively on a single task, the completion time of tasks on CPUs become
  668. comparable to the completion time on GPUs, thus relieving from granularity
  669. discrepancy concerns. Hwloc support needs to be enabled to get good performance,
  670. otherwise StarPU will not know how to better group cores.
  671. Two modes of execution exist to accomodate with existing usages.
  672. @subsection Fork-mode parallel tasks
  673. In the Fork mode, StarPU will call the codelet function on one
  674. of the CPUs of the combined worker. The codelet function can use
  675. @code{starpu_combined_worker_get_size()} to get the number of threads it is
  676. allowed to start to achieve the computation. The CPU binding mask for the whole
  677. set of CPUs is already enforced, so that threads created by the function will
  678. inherit the mask, and thus execute where StarPU expected, the OS being in charge
  679. of choosing how to schedule threads on the corresponding CPUs. The application
  680. can also choose to bind threads by hand, using e.g. sched_getaffinity to know
  681. the CPU binding mask that StarPU chose.
  682. For instance, using OpenMP (full source is available in
  683. @code{examples/openmp/vector_scal.c}):
  684. @cartouche
  685. @smallexample
  686. void scal_cpu_func(void *buffers[], void *_args)
  687. @{
  688. unsigned i;
  689. float *factor = _args;
  690. struct starpu_vector_interface *vector = buffers[0];
  691. unsigned n = STARPU_VECTOR_GET_NX(vector);
  692. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  693. #pragma omp parallel for num_threads(starpu_combined_worker_get_size())
  694. for (i = 0; i < n; i++)
  695. val[i] *= *factor;
  696. @}
  697. static struct starpu_codelet cl =
  698. @{
  699. .modes = @{ STARPU_RW @},
  700. .where = STARPU_CPU,
  701. .type = STARPU_FORKJOIN,
  702. .max_parallelism = INT_MAX,
  703. .cpu_funcs = @{scal_cpu_func, NULL@},
  704. .nbuffers = 1,
  705. @};
  706. @end smallexample
  707. @end cartouche
  708. Other examples include for instance calling a BLAS parallel CPU implementation
  709. (see @code{examples/mult/xgemm.c}).
  710. @subsection SPMD-mode parallel tasks
  711. In the SPMD mode, StarPU will call the codelet function on
  712. each CPU of the combined worker. The codelet function can use
  713. @code{starpu_combined_worker_get_size()} to get the total number of CPUs
  714. involved in the combined worker, and thus the number of calls that are made in
  715. parallel to the function, and @code{starpu_combined_worker_get_rank()} to get
  716. the rank of the current CPU within the combined worker. For instance:
  717. @cartouche
  718. @smallexample
  719. static void func(void *buffers[], void *args)
  720. @{
  721. unsigned i;
  722. float *factor = _args;
  723. struct starpu_vector_interface *vector = buffers[0];
  724. unsigned n = STARPU_VECTOR_GET_NX(vector);
  725. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  726. /* Compute slice to compute */
  727. unsigned m = starpu_combined_worker_get_size();
  728. unsigned j = starpu_combined_worker_get_rank();
  729. unsigned slice = (n+m-1)/m;
  730. for (i = j * slice; i < (j+1) * slice && i < n; i++)
  731. val[i] *= *factor;
  732. @}
  733. static struct starpu_codelet cl =
  734. @{
  735. .modes = @{ STARPU_RW @},
  736. .where = STARP_CPU,
  737. .type = STARPU_SPMD,
  738. .max_parallelism = INT_MAX,
  739. .cpu_funcs = @{ func, NULL @},
  740. .nbuffers = 1,
  741. @}
  742. @end smallexample
  743. @end cartouche
  744. Of course, this trivial example will not really benefit from parallel task
  745. execution, and was only meant to be simple to understand. The benefit comes
  746. when the computation to be done is so that threads have to e.g. exchange
  747. intermediate results, or write to the data in a complex but safe way in the same
  748. buffer.
  749. @subsection Parallel tasks performance
  750. To benefit from parallel tasks, a parallel-task-aware StarPU scheduler has to
  751. be used. When exposed to codelets with a Fork or SPMD flag, the @code{pheft}
  752. (parallel-heft) and @code{peager} (parallel eager) schedulers will indeed also
  753. try to execute tasks with several CPUs. It will automatically try the various
  754. available combined worker sizes and thus be able to avoid choosing a large
  755. combined worker if the codelet does not actually scale so much.
  756. @subsection Combined workers
  757. By default, StarPU creates combined workers according to the architecture
  758. structure as detected by hwloc. It means that for each object of the hwloc
  759. topology (NUMA node, socket, cache, ...) a combined worker will be created. If
  760. some nodes of the hierarchy have a big arity (e.g. many cores in a socket
  761. without a hierarchy of shared caches), StarPU will create combined workers of
  762. intermediate sizes. The @code{STARPU_SYNTHESIZE_ARITY_COMBINED_WORKER} variable
  763. permits to tune the maximum arity between levels of combined workers.
  764. The combined workers actually produced can be seen in the output of the
  765. @code{starpu_machine_display} tool (the @code{STARPU_SCHED} environment variable
  766. has to be set to a combined worker-aware scheduler such as @code{pheft} or
  767. @code{peager}).
  768. @subsection Concurrent parallel tasks
  769. Unfortunately, many environments and librairies do not support concurrent
  770. calls.
  771. For instance, most OpenMP implementations (including the main ones) do not
  772. support concurrent @code{pragma omp parallel} statements without nesting them in
  773. another @code{pragma omp parallel} statement, but StarPU does not yet support
  774. creating its CPU workers by using such pragma.
  775. Other parallel libraries are also not safe when being invoked concurrently
  776. from different threads, due to the use of global variables in their sequential
  777. sections for instance.
  778. The solution is then to use only one combined worker at a time. This can be
  779. done by setting @code{single_combined_worker} to 1 in the @code{starpu_conf}
  780. structure, or setting the @code{STARPU_SINGLE_COMBINED_WORKER} environment
  781. variable to 1. StarPU will then run only one parallel task at a time (but other
  782. CPU and GPU tasks are not affected and can be run concurrently). The parallel
  783. task scheduler will however still however still try varying combined worker
  784. sizes to look for the most efficient ones.
  785. @node Debugging
  786. @section Debugging
  787. StarPU provides several tools to help debugging aplications. Execution traces
  788. can be generated and displayed graphically, see @ref{Generating traces}. Some
  789. gdb helpers are also provided to show the whole StarPU state:
  790. @smallexample
  791. (gdb) source tools/gdbinit
  792. (gdb) help starpu
  793. @end smallexample
  794. The Temanejo task debugger can also be used, see @ref{Task debugger}.
  795. @node The multiformat interface
  796. @section The multiformat interface
  797. It may be interesting to represent the same piece of data using two different
  798. data structures: one that would only be used on CPUs, and one that would only
  799. be used on GPUs. This can be done by using the multiformat interface. StarPU
  800. will be able to convert data from one data structure to the other when needed.
  801. Note that the dmda scheduler is the only one optimized for this interface. The
  802. user must provide StarPU with conversion codelets:
  803. @cartouche
  804. @smallexample
  805. #define NX 1024
  806. struct point array_of_structs[NX];
  807. starpu_data_handle_t handle;
  808. /*
  809. * The conversion of a piece of data is itself a task, though it is created,
  810. * submitted and destroyed by StarPU internals and not by the user. Therefore,
  811. * we have to define two codelets.
  812. * Note that for now the conversion from the CPU format to the GPU format has to
  813. * be executed on the GPU, and the conversion from the GPU to the CPU has to be
  814. * executed on the CPU.
  815. */
  816. #ifdef STARPU_USE_OPENCL
  817. void cpu_to_opencl_opencl_func(void *buffers[], void *args);
  818. struct starpu_codelet cpu_to_opencl_cl = @{
  819. .where = STARPU_OPENCL,
  820. .opencl_funcs = @{ cpu_to_opencl_opencl_func, NULL @},
  821. .nbuffers = 1,
  822. .modes = @{ STARPU_RW @}
  823. @};
  824. void opencl_to_cpu_func(void *buffers[], void *args);
  825. struct starpu_codelet opencl_to_cpu_cl = @{
  826. .where = STARPU_CPU,
  827. .cpu_funcs = @{ opencl_to_cpu_func, NULL @},
  828. .nbuffers = 1,
  829. .modes = @{ STARPU_RW @}
  830. @};
  831. #endif
  832. struct starpu_multiformat_data_interface_ops format_ops = @{
  833. #ifdef STARPU_USE_OPENCL
  834. .opencl_elemsize = 2 * sizeof(float),
  835. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  836. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  837. #endif
  838. .cpu_elemsize = 2 * sizeof(float),
  839. ...
  840. @};
  841. starpu_multiformat_data_register(handle, 0, &array_of_structs, NX, &format_ops);
  842. @end smallexample
  843. @end cartouche
  844. Kernels can be written almost as for any other interface. Note that
  845. STARPU_MULTIFORMAT_GET_CPU_PTR shall only be used for CPU kernels. CUDA kernels
  846. must use STARPU_MULTIFORMAT_GET_CUDA_PTR, and OpenCL kernels must use
  847. STARPU_MULTIFORMAT_GET_OPENCL_PTR. STARPU_MULTIFORMAT_GET_NX may be used in any
  848. kind of kernel.
  849. @cartouche
  850. @smallexample
  851. static void
  852. multiformat_scal_cpu_func(void *buffers[], void *args)
  853. @{
  854. struct point *aos;
  855. unsigned int n;
  856. aos = STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  857. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  858. ...
  859. @}
  860. extern "C" void multiformat_scal_cuda_func(void *buffers[], void *_args)
  861. @{
  862. unsigned int n;
  863. struct struct_of_arrays *soa;
  864. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  865. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  866. ...
  867. @}
  868. @end smallexample
  869. @end cartouche
  870. A full example may be found in @code{examples/basic_examples/multiformat.c}.
  871. @node Using the Driver API
  872. @section Using the Driver API
  873. @pxref{Running drivers}
  874. @cartouche
  875. @smallexample
  876. int ret;
  877. struct starpu_driver = @{
  878. .type = STARPU_CUDA_WORKER,
  879. .id.cuda_id = 0
  880. @};
  881. ret = starpu_driver_init(&d);
  882. if (ret != 0)
  883. error();
  884. while (some_condition) @{
  885. ret = starpu_driver_run_once(&d);
  886. if (ret != 0)
  887. error();
  888. @}
  889. ret = starpu_driver_deinit(&d);
  890. if (ret != 0)
  891. error();
  892. @end smallexample
  893. @end cartouche
  894. @node Defining a New Scheduling Policy
  895. @section Defining a New Scheduling Policy
  896. A full example showing how to define a new scheduling policy is available in
  897. the StarPU sources in the directory @code{examples/scheduler/}.
  898. @pxref{Scheduling Policy}
  899. @cartouche
  900. @smallexample
  901. static struct starpu_sched_policy dummy_sched_policy = @{
  902. .init_sched = init_dummy_sched,
  903. .deinit_sched = deinit_dummy_sched,
  904. .add_workers = dummy_sched_add_workers,
  905. .remove_workers = dummy_sched_remove_workers,
  906. .push_task = push_task_dummy,
  907. .push_prio_task = NULL,
  908. .pop_task = pop_task_dummy,
  909. .post_exec_hook = NULL,
  910. .pop_every_task = NULL,
  911. .policy_name = "dummy",
  912. .policy_description = "dummy scheduling strategy"
  913. @};
  914. @end smallexample
  915. @end cartouche
  916. @node On-GPU rendering
  917. @section On-GPU rendering
  918. Graphical-oriented applications need to draw the result of their computations,
  919. typically on the very GPU where these happened. Technologies such as OpenGL/CUDA
  920. interoperability permit to let CUDA directly work on the OpenGL buffers, making
  921. them thus immediately ready for drawing, by mapping OpenGL buffer, textures or
  922. renderbuffer objects into CUDA. CUDA however imposes some technical
  923. constraints: peer memcpy has to be disabled, and the thread that runs OpenGL has
  924. to be the one that runs CUDA computations for that GPU.
  925. To achieve this with StarPU, pass the @code{--disable-cuda-memcpy-peer} option
  926. to @code{./configure} (TODO: make it dynamic), OpenGL/GLUT has to be initialized
  927. first, and the interoperability mode has to
  928. be enabled by using the @code{cuda_opengl_interoperability} field of the
  929. @code{starpu_conf} structure, and the driver loop has to be run by
  930. the application, by using the @code{not_launched_drivers} field of
  931. @code{starpu_conf} to prevent StarPU from running it in a separate thread, and
  932. by using @code{starpu_driver_run} to run the loop. The @code{gl_interop} and
  933. @code{gl_interop_idle} examples shows how it articulates in a simple case, where
  934. rendering is done in task callbacks. The former uses @code{glutMainLoopEvent}
  935. to make GLUT progress from the StarPU driver loop, while the latter uses
  936. @code{glutIdleFunc} to make StarPU progress from the GLUT main loop.
  937. Then, to use an OpenGL buffer as a CUDA data, StarPU simply needs to be given
  938. the CUDA pointer at registration, for instance:
  939. @cartouche
  940. @smallexample
  941. /* Get the CUDA worker id */
  942. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  943. if (starpu_worker_get_type(workerid) == STARPU_CUDA_WORKER)
  944. break;
  945. /* Build a CUDA pointer pointing at the OpenGL buffer */
  946. cudaGraphicsResourceGetMappedPointer((void**)&output, &num_bytes, resource);
  947. /* And register it to StarPU */
  948. starpu_vector_data_register(&handle, starpu_worker_get_memory_node(workerid),
  949. output, num_bytes / sizeof(float4), sizeof(float4));
  950. /* The handle can now be used as usual */
  951. starpu_insert_task(&cl, STARPU_RW, handle, 0);
  952. /* ... */
  953. /* This gets back data into the OpenGL buffer */
  954. starpu_data_unregister(handle);
  955. @end smallexample
  956. @end cartouche
  957. and display it e.g. in the callback function.
  958. @node Defining a New Data Interface
  959. @section Defining a New Data Interface
  960. Let's define a new data interface to manage complex numbers.
  961. @cartouche
  962. @smallexample
  963. /* interface for complex numbers */
  964. struct starpu_complex_interface
  965. @{
  966. double *real;
  967. double *imaginary;
  968. int nx;
  969. @};
  970. @end smallexample
  971. @end cartouche
  972. Registering such a data to StarPU is easily done using the function
  973. @code{starpu_data_register} (@pxref{Basic Data Management API}). The last
  974. parameter of the function, @code{interface_complex_ops}, will be
  975. described below.
  976. @cartouche
  977. @smallexample
  978. void starpu_complex_data_register(starpu_data_handle_t *handle,
  979. unsigned home_node, double *real, double *imaginary, int nx)
  980. @{
  981. struct starpu_complex_interface complex =
  982. @{
  983. .real = real,
  984. .imaginary = imaginary,
  985. .nx = nx
  986. @};
  987. if (interface_complex_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  988. @{
  989. interface_complex_ops.interfaceid = starpu_data_interface_get_next_id();
  990. @}
  991. starpu_data_register(handleptr, home_node, &complex, &interface_complex_ops);
  992. @}
  993. @end smallexample
  994. @end cartouche
  995. Different operations need to be defined for a data interface through
  996. the type @code{struct starpu_data_interface_ops} (@pxref{Defining
  997. Interface}). We only define here the basic operations needed to
  998. run simple applications. The source code for the different functions
  999. can be found in the file
  1000. @code{examples/interface/complex_interface.c}.
  1001. @cartouche
  1002. @smallexample
  1003. static struct starpu_data_interface_ops interface_complex_ops =
  1004. @{
  1005. .register_data_handle = complex_register_data_handle,
  1006. .allocate_data_on_node = complex_allocate_data_on_node,
  1007. .copy_methods = &complex_copy_methods,
  1008. .get_size = complex_get_size,
  1009. .footprint = complex_footprint,
  1010. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  1011. .interface_size = sizeof(struct starpu_complex_interface),
  1012. @};
  1013. @end smallexample
  1014. @end cartouche
  1015. Functions need to be defined to access the different fields of the
  1016. complex interface from a StarPU data handle.
  1017. @cartouche
  1018. @smallexample
  1019. double *starpu_complex_get_real(starpu_data_handle_t handle)
  1020. @{
  1021. struct starpu_complex_interface *complex_interface =
  1022. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, 0);
  1023. return complex_interface->real;
  1024. @}
  1025. double *starpu_complex_get_imaginary(starpu_data_handle_t handle);
  1026. int starpu_complex_get_nx(starpu_data_handle_t handle);
  1027. @end smallexample
  1028. @end cartouche
  1029. Similar functions need to be defined to access the different fields of the
  1030. complex interface from a @code{void *} pointer to be used within codelet
  1031. implemetations.
  1032. @cartouche
  1033. @smallexample
  1034. #define STARPU_COMPLEX_GET_REAL(interface) \
  1035. (((struct starpu_complex_interface *)(interface))->real)
  1036. #define STARPU_COMPLEX_GET_IMAGINARY(interface) \
  1037. (((struct starpu_complex_interface *)(interface))->imaginary)
  1038. #define STARPU_COMPLEX_GET_NX(interface) \
  1039. (((struct starpu_complex_interface *)(interface))->nx)
  1040. @end smallexample
  1041. @end cartouche
  1042. Complex data interfaces can then be registered to StarPU.
  1043. @cartouche
  1044. @smallexample
  1045. double real = 45.0;
  1046. double imaginary = 12.0;
  1047. starpu_complex_data_register(&handle1, 0, &real, &imaginary, 1);
  1048. starpu_insert_task(&cl_display, STARPU_R, handle1, 0);
  1049. @end smallexample
  1050. @end cartouche
  1051. and used by codelets.
  1052. @cartouche
  1053. @smallexample
  1054. void display_complex_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  1055. @{
  1056. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  1057. double *real = STARPU_COMPLEX_GET_REAL(descr[0]);
  1058. double *imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  1059. int i;
  1060. for(i=0 ; i<nx ; i++)
  1061. @{
  1062. fprintf(stderr, "Complex[%d] = %3.2f + %3.2f i\n", i, real[i], imaginary[i]);
  1063. @}
  1064. @}
  1065. @end smallexample
  1066. @end cartouche
  1067. The whole code for this complex data interface is available in the
  1068. directory @code{examples/interface/}.
  1069. @node Setting the Data Handles for a Task
  1070. @section Setting the Data Handles for a Task
  1071. The number of data a task can manage is fixed by the
  1072. @code{STARPU_NMAXBUFS} which has a default value which can be changed
  1073. through the configure option @code{--enable-maxbuffers} (see
  1074. @ref{--enable-maxbuffers}).
  1075. However, it is possible to define tasks managing more data by using
  1076. the field @code{dyn_handles} when defining a task and the field
  1077. @code{dyn_modes} when defining the corresponding codelet.
  1078. @cartouche
  1079. @smallexample
  1080. enum starpu_data_access_mode modes[STARPU_NMAXBUFS+1] = @{
  1081. STARPU_R, STARPU_R, ...
  1082. @};
  1083. struct starpu_codelet dummy_big_cl =
  1084. @{
  1085. .cuda_funcs = @{dummy_big_kernel, NULL@},
  1086. .opencl_funcs = @{dummy_big_kernel, NULL@},
  1087. .cpu_funcs = @{dummy_big_kernel, NULL@},
  1088. .nbuffers = STARPU_NMAXBUFS+1,
  1089. .dyn_modes = modes
  1090. @};
  1091. task = starpu_task_create();
  1092. task->cl = &dummy_big_cl;
  1093. task->dyn_handles = malloc(task->cl->nbuffers * sizeof(starpu_data_handle_t));
  1094. for(i=0 ; i<task->cl->nbuffers ; i++)
  1095. @{
  1096. task->dyn_handles[i] = handle;
  1097. @}
  1098. starpu_task_submit(task);
  1099. @end smallexample
  1100. @end cartouche
  1101. @cartouche
  1102. @smallexample
  1103. starpu_data_handle_t *handles = malloc(dummy_big_cl.nbuffers * sizeof(starpu_data_handle_t));
  1104. for(i=0 ; i<dummy_big_cl.nbuffers ; i++)
  1105. @{
  1106. handles[i] = handle;
  1107. @}
  1108. starpu_insert_task(&dummy_big_cl,
  1109. STARPU_VALUE, &dummy_big_cl.nbuffers, sizeof(dummy_big_cl.nbuffers),
  1110. STARPU_DATA_ARRAY, handles, dummy_big_cl.nbuffers,
  1111. 0);
  1112. @end smallexample
  1113. @end cartouche
  1114. The whole code for this complex data interface is available in the
  1115. directory @code{examples/basic_examples/dynamic_handles.c}.
  1116. @node More examples
  1117. @section More examples
  1118. More examples are available in the StarPU sources in the @code{examples/}
  1119. directory. Simple examples include:
  1120. @table @asis
  1121. @item @code{incrementer/}:
  1122. Trivial incrementation test.
  1123. @item @code{basic_examples/}:
  1124. Simple documented Hello world and vector/scalar product (as
  1125. shown in @ref{Basic Examples}), matrix
  1126. product examples (as shown in @ref{Performance model example}), an example using the blocked matrix data
  1127. interface, an example using the variable data interface, and an example
  1128. using different formats on CPUs and GPUs.
  1129. @item @code{matvecmult/}:
  1130. OpenCL example from NVidia, adapted to StarPU.
  1131. @item @code{axpy/}:
  1132. AXPY CUBLAS operation adapted to StarPU.
  1133. @item @code{fortran/}:
  1134. Example of Fortran bindings.
  1135. @end table
  1136. More advanced examples include:
  1137. @table @asis
  1138. @item @code{filters/}:
  1139. Examples using filters, as shown in @ref{Partitioning Data}.
  1140. @item @code{lu/}:
  1141. LU matrix factorization, see for instance @code{xlu_implicit.c}
  1142. @item @code{cholesky/}:
  1143. Cholesky matrix factorization, see for instance @code{cholesky_implicit.c}.
  1144. @end table