advanced-examples.texi 42 KB

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