advanced-examples.texi 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 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. @node Advanced Examples
  8. @chapter Advanced Examples
  9. @menu
  10. * Using multiple implementations of a codelet::
  11. * Enabling implementation according to capabilities::
  12. * Task and Worker Profiling::
  13. * Partitioning Data:: Partitioning Data
  14. * Performance model example::
  15. * Theoretical lower bound on execution time::
  16. * Insert Task Utility::
  17. * The multiformat interface::
  18. * More examples:: More examples shipped with StarPU
  19. * Debugging:: When things go wrong.
  20. @end menu
  21. @node Using multiple implementations of a codelet
  22. @section Using multiple implementations of a codelet
  23. One may want to write multiple implementations of a codelet for a single type of
  24. device and let StarPU choose which one to run. As an example, we will show how
  25. to use SSE to scale a vector. The codelet can be written as follows :
  26. @cartouche
  27. @smallexample
  28. #include <xmmintrin.h>
  29. void scal_sse_func(void *buffers[], void *cl_arg)
  30. @{
  31. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  32. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]);
  33. unsigned int n_iterations = n/4;
  34. if (n % 4 != 0)
  35. n_iterations++;
  36. __m128 *VECTOR = (__m128*) vector;
  37. __m128 factor __attribute__((aligned(16)));
  38. factor = _mm_set1_ps(*(float *) cl_arg);
  39. unsigned int i;
  40. for (i = 0; i < n_iterations; i++)
  41. VECTOR[i] = _mm_mul_ps(factor, VECTOR[i]);
  42. @}
  43. @end smallexample
  44. @end cartouche
  45. @cartouche
  46. @smallexample
  47. struct starpu_codelet cl = @{
  48. .where = STARPU_CPU,
  49. .cpu_funcs = @{ scal_cpu_func, scal_sse_func, NULL @},
  50. .nbuffers = 1
  51. @};
  52. @end smallexample
  53. @end cartouche
  54. Schedulers which are multi-implementation aware (only @code{dmda}, @code{heft}
  55. and @code{pheft} for now) will use the performance models of all the
  56. implementations it was given, and pick the one that seems to be the fastest.
  57. @node Enabling implementation according to capabilities
  58. @section Enabling implementation according to capabilities
  59. Some implementations may not run on some devices. For instance, some CUDA
  60. devices do not support double floating point precision, and thus the kernel
  61. execution would just fail; or the device may not have enough shared memory for
  62. the implementation being used. The @code{can_execute} field of the @code{struct
  63. starpu_codelet} structure permits to express this. For instance:
  64. @cartouche
  65. @smallexample
  66. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  67. @{
  68. const struct cudaDeviceProp *props;
  69. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  70. return 1;
  71. /* Cuda device */
  72. props = starpu_cuda_get_device_properties(workerid);
  73. if (props->major >= 2 || props->minor >= 3)
  74. /* At least compute capability 1.3, supports doubles */
  75. return 1;
  76. /* Old card, does not support doubles */
  77. return 0;
  78. @}
  79. struct starpu_codelet cl = @{
  80. .where = STARPU_CPU|STARPU_CUDA,
  81. .can_execute = can_execute,
  82. .cpu_funcs = @{ cpu_func, NULL @},
  83. .cuda_funcs = @{ gpu_func, NULL @}
  84. .nbuffers = 1
  85. @};
  86. @end smallexample
  87. @end cartouche
  88. This can be essential e.g. when running on a machine which mixes various models
  89. of CUDA devices, to take benefit from the new models without crashing on old models.
  90. Note: the @code{can_execute} function is called by the scheduler each time it
  91. tries to match a task with a worker, and should thus be very fast. The
  92. @code{starpu_cuda_get_device_properties} provides a quick access to CUDA
  93. properties of CUDA devices to achieve such efficiency.
  94. Another example is compiling CUDA code for various compute capabilities,
  95. resulting with two CUDA functions, e.g. @code{scal_gpu_13} for compute capability
  96. 1.3, and @code{scal_gpu_20} for compute capability 2.0. Both functions can be
  97. provided to StarPU by using @code{cuda_funcs}, and @code{can_execute} can then be
  98. used to rule out the @code{scal_gpu_20} variant on a CUDA device which
  99. will not be able to execute it:
  100. @cartouche
  101. @smallexample
  102. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  103. @{
  104. const struct cudaDeviceProp *props;
  105. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  106. return 1;
  107. /* Cuda device */
  108. if (nimpl == 0)
  109. /* Trying to execute the 1.3 capability variant, we assume it is ok in all cases. */
  110. return 1;
  111. /* Trying to execute the 2.0 capability variant, check that the card can do it. */
  112. props = starpu_cuda_get_device_properties(workerid);
  113. if (props->major >= 2 || props->minor >= 0)
  114. /* At least compute capability 2.0, can run it */
  115. return 1;
  116. /* Old card, does not support 2.0, will not be able to execute the 2.0 variant. */
  117. return 0;
  118. @}
  119. struct starpu_codelet cl = @{
  120. .where = STARPU_CPU|STARPU_CUDA,
  121. .can_execute = can_execute,
  122. .cpu_funcs = @{ cpu_func, NULL @},
  123. .cuda_funcs = @{ scal_gpu_13, scal_gpu_20, NULL @},
  124. .nbuffers = 1
  125. @};
  126. @end smallexample
  127. @end cartouche
  128. Note: the most generic variant should be provided first, as some schedulers are
  129. not able to try the different variants.
  130. @node Task and Worker Profiling
  131. @section Task and Worker Profiling
  132. A full example showing how to use the profiling API is available in
  133. the StarPU sources in the directory @code{examples/profiling/}.
  134. @cartouche
  135. @smallexample
  136. struct starpu_task *task = starpu_task_create();
  137. task->cl = &cl;
  138. task->synchronous = 1;
  139. /* We will destroy the task structure by hand so that we can
  140. * query the profiling info before the task is destroyed. */
  141. task->destroy = 0;
  142. /* Submit and wait for completion (since synchronous was set to 1) */
  143. starpu_task_submit(task);
  144. /* The task is finished, get profiling information */
  145. struct starpu_task_profiling_info *info = task->profiling_info;
  146. /* How much time did it take before the task started ? */
  147. double delay += starpu_timing_timespec_delay_us(&info->submit_time, &info->start_time);
  148. /* How long was the task execution ? */
  149. double length += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  150. /* We don't need the task structure anymore */
  151. starpu_task_destroy(task);
  152. @end smallexample
  153. @end cartouche
  154. @cartouche
  155. @smallexample
  156. /* Display the occupancy of all workers during the test */
  157. int worker;
  158. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  159. @{
  160. struct starpu_worker_profiling_info worker_info;
  161. int ret = starpu_worker_get_profiling_info(worker, &worker_info);
  162. STARPU_ASSERT(!ret);
  163. double total_time = starpu_timing_timespec_to_us(&worker_info.total_time);
  164. double executing_time = starpu_timing_timespec_to_us(&worker_info.executing_time);
  165. double sleeping_time = starpu_timing_timespec_to_us(&worker_info.sleeping_time);
  166. float executing_ratio = 100.0*executing_time/total_time;
  167. float sleeping_ratio = 100.0*sleeping_time/total_time;
  168. char workername[128];
  169. starpu_worker_get_name(worker, workername, 128);
  170. fprintf(stderr, "Worker %s:\n", workername);
  171. fprintf(stderr, "\ttotal time : %.2lf ms\n", total_time*1e-3);
  172. fprintf(stderr, "\texec time : %.2lf ms (%.2f %%)\n", executing_time*1e-3,
  173. executing_ratio);
  174. fprintf(stderr, "\tblocked time : %.2lf ms (%.2f %%)\n", sleeping_time*1e-3,
  175. sleeping_ratio);
  176. @}
  177. @end smallexample
  178. @end cartouche
  179. @node Partitioning Data
  180. @section Partitioning Data
  181. An existing piece of data can be partitioned in sub parts to be used by different tasks, for instance:
  182. @cartouche
  183. @smallexample
  184. int vector[NX];
  185. starpu_data_handle_t handle;
  186. /* Declare data to StarPU */
  187. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  188. /* Partition the vector in PARTS sub-vectors */
  189. starpu_filter f =
  190. @{
  191. .filter_func = starpu_block_filter_func_vector,
  192. .nchildren = PARTS
  193. @};
  194. starpu_data_partition(handle, &f);
  195. @end smallexample
  196. @end cartouche
  197. The task submission then uses @code{starpu_data_get_sub_data} to retrive the
  198. sub-handles to be passed as tasks parameters.
  199. @cartouche
  200. @smallexample
  201. /* Submit a task on each sub-vector */
  202. for (i=0; i<starpu_data_get_nb_children(handle); i++) @{
  203. /* Get subdata number i (there is only 1 dimension) */
  204. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  205. struct starpu_task *task = starpu_task_create();
  206. task->buffers[0].handle = sub_handle;
  207. task->buffers[0].mode = STARPU_RW;
  208. task->cl = &cl;
  209. task->synchronous = 1;
  210. task->cl_arg = &factor;
  211. task->cl_arg_size = sizeof(factor);
  212. starpu_task_submit(task);
  213. @}
  214. @end smallexample
  215. @end cartouche
  216. Partitioning can be applied several times, see
  217. @code{examples/basic_examples/mult.c} and @code{examples/filters/}.
  218. Wherever the whole piece of data is already available, the partitioning will
  219. be done in-place, i.e. without allocating new buffers but just using pointers
  220. inside the existing copy. This is particularly important to be aware of when
  221. using OpenCL, where the kernel parameters are not pointers, but handles. The
  222. kernel thus needs to be also passed the offset within the OpenCL buffer:
  223. @cartouche
  224. @smallexample
  225. void opencl_func(void *buffers[], void *cl_arg)
  226. @{
  227. cl_mem vector = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  228. unsigned offset = STARPU_BLOCK_GET_OFFSET(buffers[0]);
  229. ...
  230. clSetKernelArg(kernel, 0, sizeof(vector), &vector);
  231. clSetKernelArg(kernel, 1, sizeof(offset), &offset);
  232. ...
  233. @}
  234. @end smallexample
  235. @end cartouche
  236. And the kernel has to shift from the pointer passed by the OpenCL driver:
  237. @cartouche
  238. @smallexample
  239. __kernel void opencl_kernel(__global int *vector, unsigned offset)
  240. @{
  241. block = (__global void *)block + offset;
  242. ...
  243. @}
  244. @end smallexample
  245. @end cartouche
  246. @node Performance model example
  247. @section Performance model example
  248. To achieve good scheduling, StarPU scheduling policies need to be able to
  249. estimate in advance the duration of a task. This is done by giving to codelets
  250. a performance model, by defining a @code{starpu_perfmodel} structure and
  251. providing its address in the @code{model} field of the @code{struct starpu_codelet}
  252. structure. The @code{symbol} and @code{type} fields of @code{starpu_perfmodel}
  253. are mandatory, to give a name to the model, and the type of the model, since
  254. there are several kinds of performance models.
  255. @itemize
  256. @item
  257. Measured at runtime (@code{STARPU_HISTORY_BASED} model type). This assumes that for a
  258. given set of data input/output sizes, the performance will always be about the
  259. same. This is very true for regular kernels on GPUs for instance (<0.1% error),
  260. and just a bit less true on CPUs (~=1% error). This also assumes that there are
  261. few different sets of data input/output sizes. StarPU will then keep record of
  262. the average time of previous executions on the various processing units, and use
  263. it as an estimation. History is done per task size, by using a hash of the input
  264. and ouput sizes as an index.
  265. It will also save it in @code{~/.starpu/sampling/codelets}
  266. for further executions, and can be observed by using the
  267. @code{starpu_perfmodel_display} command, or drawn by using
  268. the @code{starpu_perfmodel_plot}. The models are indexed by machine name. To
  269. share the models between machines (e.g. for a homogeneous cluster), use
  270. @code{export STARPU_HOSTNAME=some_global_name}. Measurements are only done when using a task scheduler which makes use of it, such as @code{heft} or @code{dmda}.
  271. The following is a small code example.
  272. If e.g. the code is recompiled with other compilation options, or several
  273. variants of the code are used, the symbol string should be changed to reflect
  274. that, in order to recalibrate a new model from zero. The symbol string can even
  275. be constructed dynamically at execution time, as long as this is done before
  276. submitting any task using it.
  277. @cartouche
  278. @smallexample
  279. static struct starpu_perfmodel mult_perf_model = @{
  280. .type = STARPU_HISTORY_BASED,
  281. .symbol = "mult_perf_model"
  282. @};
  283. struct starpu_codelet cl = @{
  284. .where = STARPU_CPU,
  285. .cpu_funcs = @{ cpu_mult, NULL @},
  286. .nbuffers = 3,
  287. /* for the scheduling policy to be able to use performance models */
  288. .model = &mult_perf_model
  289. @};
  290. @end smallexample
  291. @end cartouche
  292. @item
  293. Measured at runtime and refined by regression (@code{STARPU_REGRESSION_*_BASED}
  294. model type). This still assumes performance regularity, but can work
  295. with various data input sizes, by applying regression over observed
  296. execution times. STARPU_REGRESSION_BASED uses an a*n^b regression
  297. form, STARPU_NL_REGRESSION_BASED uses an a*n^b+c (more precise than
  298. STARPU_REGRESSION_BASED, but costs a lot more to compute). For instance,
  299. @code{tests/perfmodels/regression_based.c} uses a regression-based performance
  300. model for the @code{memset} operation. Of course, the application has to issue
  301. tasks with varying size so that the regression can be computed. StarPU will not
  302. trust the regression unless there is at least 10% difference between the minimum
  303. and maximum observed input size. For non-linear regression, since computing it
  304. is quite expensive, it is only done at termination of the application. This
  305. means that the first execution uses history-based performance model to perform
  306. scheduling.
  307. @item
  308. Provided as an estimation from the application itself (@code{STARPU_COMMON} model type and @code{cost_model} field),
  309. see for instance
  310. @code{examples/common/blas_model.h} and @code{examples/common/blas_model.c}.
  311. @item
  312. Provided explicitly by the application (@code{STARPU_PER_ARCH} model type): the
  313. @code{.per_arch[i].cost_model} fields have to be filled with pointers to
  314. functions which return the expected duration of the task in micro-seconds, one
  315. per architecture.
  316. @end itemize
  317. How to use schedulers which can benefit from such performance model is explained
  318. in @ref{Task scheduling policy}.
  319. The same can be done for task power consumption estimation, by setting the
  320. @code{power_model} field the same way as the @code{model} field. Note: for
  321. now, the application has to give to the power consumption performance model
  322. a name which is different from the execution time performance model.
  323. The application can request time estimations from the StarPU performance
  324. models by filling a task structure as usual without actually submitting
  325. it. The data handles can be created by calling @code{starpu_data_register}
  326. functions with a @code{NULL} pointer (and need to be unregistered as usual)
  327. and the desired data sizes. The @code{starpu_task_expected_length} and
  328. @code{starpu_task_expected_power} functions can then be called to get an
  329. estimation of the task duration on a given arch. @code{starpu_task_destroy}
  330. needs to be called to destroy the dummy task afterwards. See
  331. @code{tests/perfmodels/regression_based.c} for an example.
  332. @node Theoretical lower bound on execution time
  333. @section Theoretical lower bound on execution time
  334. For kernels with history-based performance models, StarPU can very easily provide a theoretical lower
  335. bound for the execution time of a whole set of tasks. See for
  336. instance @code{examples/lu/lu_example.c}: before submitting tasks,
  337. call @code{starpu_bound_start}, and after complete execution, call
  338. @code{starpu_bound_stop}. @code{starpu_bound_print_lp} or
  339. @code{starpu_bound_print_mps} can then be used to output a Linear Programming
  340. problem corresponding to the schedule of your tasks. Run it through
  341. @code{lp_solve} or any other linear programming solver, and that will give you a
  342. lower bound for the total execution time of your tasks. If StarPU was compiled
  343. with the glpk library installed, @code{starpu_bound_compute} can be used to
  344. solve it immediately and get the optimized minimum, in ms. Its @code{integer}
  345. parameter allows to decide whether integer resolution should be computed
  346. and returned too.
  347. The @code{deps} parameter tells StarPU whether to take tasks and implicit data
  348. dependencies into account. It must be understood that the linear programming
  349. problem size is quadratic with the number of tasks and thus the time to solve it
  350. will be very long, it could be minutes for just a few dozen tasks. You should
  351. probably use @code{lp_solve -timeout 1 test.pl -wmps test.mps} to convert the
  352. problem to MPS format and then use a better solver, @code{glpsol} might be
  353. better than @code{lp_solve} for instance (the @code{--pcost} option may be
  354. useful), but sometimes doesn't manage to converge. @code{cbc} might look
  355. slower, but it is parallel. Be sure to try at least all the @code{-B} options
  356. of @code{lp_solve}. For instance, we often just use
  357. @code{lp_solve -cc -B1 -Bb -Bg -Bp -Bf -Br -BG -Bd -Bs -BB -Bo -Bc -Bi} , and
  358. the @code{-gr} option can also be quite useful.
  359. Setting @code{deps} to 0 will only take into account the actual computations
  360. on processing units. It however still properly takes into account the varying
  361. performances of kernels and processing units, which is quite more accurate than
  362. just comparing StarPU performances with the fastest of the kernels being used.
  363. The @code{prio} parameter tells StarPU whether to simulate taking into account
  364. the priorities as the StarPU scheduler would, i.e. schedule prioritized
  365. tasks before less prioritized tasks, to check to which extend this results
  366. to a less optimal solution. This increases even more computation time.
  367. Note that for simplicity, all this however doesn't take into account data
  368. transfers, which are assumed to be completely overlapped.
  369. @node Insert Task Utility
  370. @section Insert Task Utility
  371. StarPU provides the wrapper function @code{starpu_insert_task} to ease
  372. the creation and submission of tasks.
  373. @deftypefun int starpu_insert_task (struct starpu_codelet *@var{cl}, ...)
  374. Create and submit a task corresponding to @var{cl} with the following
  375. arguments. The argument list must be zero-terminated.
  376. The arguments following the codelets can be of the following types:
  377. @itemize
  378. @item
  379. @code{STARPU_R}, @code{STARPU_W}, @code{STARPU_RW}, @code{STARPU_SCRATCH}, @code{STARPU_REDUX} an access mode followed by a data handle;
  380. @item
  381. @code{STARPU_VALUE} followed by a pointer to a constant value and
  382. the size of the constant;
  383. @item
  384. @code{STARPU_CALLBACK} followed by a pointer to a callback function;
  385. @item
  386. @code{STARPU_CALLBACK_ARG} followed by a pointer to be given as an
  387. argument to the callback function;
  388. @item
  389. @code{STARPU_CALLBACK_WITH_ARG} followed by two pointers: one to a callback
  390. function, and the other to be given as an argument to the callback
  391. function; this is equivalent to using both @code{STARPU_CALLBACK} and
  392. @code{STARPU_CALLBACK_WITH_ARG}
  393. @item
  394. @code{STARPU_PRIORITY} followed by a integer defining a priority level.
  395. @end itemize
  396. Parameters to be passed to the codelet implementation are defined
  397. through the type @code{STARPU_VALUE}. The function
  398. @code{starpu_unpack_cl_args} must be called within the codelet
  399. implementation to retrieve them.
  400. @end deftypefun
  401. Here the implementation of the codelet:
  402. @smallexample
  403. void func_cpu(void *descr[], void *_args)
  404. @{
  405. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  406. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  407. int ifactor;
  408. float ffactor;
  409. starpu_unpack_cl_args(_args, &ifactor, &ffactor);
  410. *x0 = *x0 * ifactor;
  411. *x1 = *x1 * ffactor;
  412. @}
  413. struct starpu_codelet mycodelet = @{
  414. .where = STARPU_CPU,
  415. .cpu_funcs = @{ func_cpu, NULL @},
  416. .nbuffers = 2
  417. @};
  418. @end smallexample
  419. And the call to the @code{starpu_insert_task} wrapper:
  420. @smallexample
  421. starpu_insert_task(&mycodelet,
  422. STARPU_VALUE, &ifactor, sizeof(ifactor),
  423. STARPU_VALUE, &ffactor, sizeof(ffactor),
  424. STARPU_RW, data_handles[0], STARPU_RW, data_handles[1],
  425. 0);
  426. @end smallexample
  427. The call to @code{starpu_insert_task} is equivalent to the following
  428. code:
  429. @smallexample
  430. struct starpu_task *task = starpu_task_create();
  431. task->cl = &mycodelet;
  432. task->buffers[0].handle = data_handles[0];
  433. task->buffers[0].mode = STARPU_RW;
  434. task->buffers[1].handle = data_handles[1];
  435. task->buffers[1].mode = STARPU_RW;
  436. char *arg_buffer;
  437. size_t arg_buffer_size;
  438. starpu_pack_cl_args(&arg_buffer, &arg_buffer_size,
  439. STARPU_VALUE, &ifactor, sizeof(ifactor),
  440. STARPU_VALUE, &ffactor, sizeof(ffactor),
  441. 0);
  442. task->cl_arg = arg_buffer;
  443. task->cl_arg_size = arg_buffer_size;
  444. int ret = starpu_task_submit(task);
  445. @end smallexample
  446. If some part of the task insertion depends on the value of some computation,
  447. the @code{STARPU_DATA_ACQUIRE_CB} macro can be very convenient. For
  448. instance, assuming that the index variable @code{i} was registered as handle
  449. @code{i_handle}:
  450. @smallexample
  451. /* Compute which portion we will work on, e.g. pivot */
  452. starpu_insert_task(&which_index, STARPU_W, i_handle, 0);
  453. /* And submit the corresponding task */
  454. STARPU_DATA_ACQUIRE_CB(i_handle, STARPU_R, starpu_insert_task(&work, STARPU_RW, A_handle[i], 0));
  455. @end smallexample
  456. The @code{STARPU_DATA_ACQUIRE_CB} macro submits an asynchronous request for
  457. acquiring data @code{i} for the main application, and will execute the code
  458. given as third parameter when it is acquired. In other words, as soon as the
  459. value of @code{i} computed by the @code{which_index} codelet can be read, the
  460. portion of code passed as third parameter of @code{STARPU_DATA_ACQUIRE_CB} will
  461. be executed, and is allowed to read from @code{i} to use it e.g. as an
  462. index. Note that this macro is only avaible when compiling StarPU with
  463. the compiler @code{gcc}.
  464. @node Debugging
  465. @section Debugging
  466. StarPU provides several tools to help debugging aplications. Execution traces
  467. can be generated and displayed graphically, see @ref{Generating traces}. Some
  468. gdb helpers are also provided to show the whole StarPU state:
  469. @smallexample
  470. (gdb) source tools/gdbinit
  471. (gdb) help starpu
  472. @end smallexample
  473. @node The multiformat interface
  474. @section The multiformat interface
  475. It may be interesting to represent the same piece of data using two different
  476. data structures : one that would only be used on CPUs, and one that would only
  477. be used on GPUs. This can be done by using the multiformat interface. StarPU
  478. will be able to convert data from one data structure to the other when needed.
  479. Note that the heft scheduler is the only one optimized for this interface. The
  480. user must provide StarPU with conversion codelets :
  481. @example
  482. #define NX 1024
  483. struct point array_of_structs[NX];
  484. starpu_data_handle_t handle;
  485. /*
  486. * The conversion of a piece of data is itself a task, though it is created,
  487. * submitted and destroyed by StarPU internals and not by the user. Therefore,
  488. * we have to define two codelets.
  489. * Note that for now the conversion from the CPU format to the GPU format has to
  490. * be executed on the GPU, and the conversion from the GPU to the CPU has to be
  491. * executed on the CPU.
  492. */
  493. #ifdef STARPU_USE_OPENCL
  494. void cpu_to_opencl_opencl_func(void *buffers[], void *args);
  495. struct starpu_codelet cpu_to_opencl_cl = @{
  496. .where = STARPU_OPENCL,
  497. .opencl_funcs = @{ cpu_to_opencl_opencl_func, NULL @},
  498. .nbuffers = 1
  499. @};
  500. void opencl_to_cpu_func(void *buffers[], void *args);
  501. struct starpu_codelet opencl_to_cpu_cl = @{
  502. .where = STARPU_CPU,
  503. .cpu_funcs = @{ opencl_to_cpu_func, NULL @},
  504. .nbuffers = 1
  505. @};
  506. #endif
  507. struct starpu_multiformat_data_interface_ops format_ops = @{
  508. #ifdef STARPU_USE_OPENCL
  509. .opencl_elemsize = 2 * sizeof(float),
  510. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  511. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  512. #endif
  513. .cpu_elemsize = 2 * sizeof(float),
  514. ...
  515. @};
  516. starpu_multiformat_data_register(handle, 0, &array_of_structs, NX, &format_ops);
  517. @end example
  518. Kernels can be written almost as for any other interface. Note that
  519. STARPU_MULTIFORMAT_GET_PTR shall only be used for CPU kernels. CUDA kernels
  520. must use STARPU_MULTIFORMAT_GET_CUDA_PTR, and OpenCL kernels must use
  521. STARPU_MULTIFORMAT_GET_OPENCL_PTR. STARPU_MULTIFORMAT_GET_NX may be used in any
  522. kind of kernel.
  523. @example
  524. static void
  525. multiformat_scal_cpu_func(void *buffers[], void *args)
  526. @{
  527. struct point *aos;
  528. unsigned int n;
  529. aos = STARPU_MULTIFORMAT_GET_PTR(buffers[0]);
  530. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  531. ...
  532. @}
  533. extern "C" void multiformat_scal_cuda_func(void *buffers[], void *_args)
  534. @{
  535. unsigned int n;
  536. struct struct_of_arrays *soa;
  537. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  538. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  539. ...
  540. @}
  541. @end example
  542. A full example may be found in @code{examples/basic_examples/multiformat.c}.
  543. @node More examples
  544. @section More examples
  545. More examples are available in the StarPU sources in the @code{examples/}
  546. directory. Simple examples include:
  547. @table @asis
  548. @item @code{incrementer/}:
  549. Trivial incrementation test.
  550. @item @code{basic_examples/}:
  551. Simple documented Hello world (as shown in @ref{Hello World}), vector/scalar product (as shown
  552. in @ref{Vector Scaling on an Hybrid CPU/GPU Machine}), matrix
  553. product examples (as shown in @ref{Performance model example}), an example using the blocked matrix data
  554. interface, an example using the variable data interface, and an example
  555. using different formats on CPUs and GPUs.
  556. @item @code{matvecmult/}:
  557. OpenCL example from NVidia, adapted to StarPU.
  558. @item @code{axpy/}:
  559. AXPY CUBLAS operation adapted to StarPU.
  560. @item @code{fortran/}:
  561. Example of Fortran bindings.
  562. @end table
  563. More advanced examples include:
  564. @table @asis
  565. @item @code{filters/}:
  566. Examples using filters, as shown in @ref{Partitioning Data}.
  567. @item @code{lu/}:
  568. LU matrix factorization, see for instance @code{xlu_implicit.c}
  569. @item @code{cholesky/}:
  570. Cholesky matrix factorization, see for instance @code{cholesky_implicit.c}.
  571. @end table