301_tasks.doxy 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2019 CNRS
  4. * Copyright (C) 2011,2012,2018 Inria
  5. * Copyright (C) 2009-2011,2014-2016,2018-2019 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*! \page TasksInStarPU Tasks In StarPU
  19. \section TaskGranularity Task Granularity
  20. Like any other runtime, StarPU has some overhead to manage tasks. Since
  21. it does smart scheduling and data management, this overhead is not always
  22. neglectable. The order of magnitude of the overhead is typically a couple of
  23. microseconds, which is actually quite smaller than the CUDA overhead itself. The
  24. amount of work that a task should do should thus be somewhat
  25. bigger, to make sure that the overhead becomes neglectible. The offline
  26. performance feedback can provide a measure of task length, which should thus be
  27. checked if bad performance are observed. To get a grasp at the scalability
  28. possibility according to task size, one can run
  29. <c>tests/microbenchs/tasks_size_overhead.sh</c> which draws curves of the
  30. speedup of independent tasks of very small sizes.
  31. To determine what task size your application is actually using, one can use
  32. <c>starpu_fxt_data_trace</c>, see \ref DataTrace .
  33. The choice of scheduler also has impact over the overhead: for instance, the
  34. scheduler <c>dmda</c> takes time to make a decision, while <c>eager</c> does
  35. not. <c>tasks_size_overhead.sh</c> can again be used to get a grasp at how much
  36. impact that has on the target machine.
  37. \section TaskSubmission Task Submission
  38. To let StarPU make online optimizations, tasks should be submitted
  39. asynchronously as much as possible. Ideally, all tasks should be
  40. submitted, and mere calls to starpu_task_wait_for_all() or
  41. starpu_data_unregister() be done to wait for
  42. termination. StarPU will then be able to rework the whole schedule, overlap
  43. computation with communication, manage accelerator local memory usage, etc.
  44. \section TaskPriorities Task Priorities
  45. By default, StarPU will consider the tasks in the order they are submitted by
  46. the application. If the application programmer knows that some tasks should
  47. be performed in priority (for instance because their output is needed by many
  48. other tasks and may thus be a bottleneck if not executed early
  49. enough), the field starpu_task::priority should be set to provide the
  50. priority information to StarPU.
  51. \section TaskDependencies Task Dependencies
  52. \subsection SequentialConsistency Sequential Consistency
  53. By default, task dependencies are inferred from data dependency (sequential
  54. coherency) by StarPU. The application can however disable sequential coherency
  55. for some data, and dependencies can be specifically expressed.
  56. Setting (or unsetting) sequential consistency can be done at the data
  57. level by calling starpu_data_set_sequential_consistency_flag() for a
  58. specific data or starpu_data_set_default_sequential_consistency_flag()
  59. for all datas.
  60. Setting (or unsetting) sequential consistency can also be done at task
  61. level by setting the field starpu_task::sequential_consistency to \c 0.
  62. Sequential consistency can also be set (or unset) for each handle of a
  63. specific task, this is done by using the field
  64. starpu_task::handles_sequential_consistency. When set, its value
  65. should be a array with the number of elements being the number of
  66. handles for the task, each element of the array being the sequential
  67. consistency for the \c i-th handle of the task. The field can easily be
  68. set when calling starpu_task_insert() with the flag
  69. ::STARPU_HANDLES_SEQUENTIAL_CONSISTENCY
  70. \code{.c}
  71. char *seq_consistency = malloc(cl.nbuffers * sizeof(char));
  72. seq_consistency[0] = 1;
  73. seq_consistency[1] = 1;
  74. seq_consistency[2] = 0;
  75. ret = starpu_task_insert(&cl,
  76. STARPU_RW, handleA, STARPU_RW, handleB, STARPU_RW, handleC,
  77. STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, seq_consistency,
  78. 0);
  79. free(seq_consistency);
  80. \endcode
  81. The internal algorithm used by StarPU to set up implicit dependency is
  82. as follows:
  83. \code{.c}
  84. if (sequential_consistency(task) == 1)
  85. for(i=0 ; i<STARPU_TASK_GET_NBUFFERS(task) ; i++)
  86. if (sequential_consistency(i-th data, task) == 1)
  87. if (sequential_consistency(i-th data) == 1)
  88. create_implicit_dependency(...)
  89. \endcode
  90. \subsection TasksAndTagsDependencies Tasks And Tags Dependencies
  91. One can explicitely set dependencies between tasks using
  92. starpu_task_declare_deps() or starpu_task_declare_deps_array(). Dependencies between tasks can be
  93. expressed through tags associated to a tag with the field
  94. starpu_task::tag_id and using the function starpu_tag_declare_deps()
  95. or starpu_tag_declare_deps_array().
  96. The termination of a task can be delayed through the function
  97. starpu_task_end_dep_add() which specifies the number of calls to the function
  98. starpu_task_end_dep_release() needed to trigger the task termination. One can
  99. also use starpu_task_declare_end_deps() or starpu_task_declare_end_deps_array()
  100. to delay the termination of a task until the termination of other tasks.
  101. \section SettingManyDataHandlesForATask Setting Many Data Handles For a Task
  102. The maximum number of data a task can manage is fixed by the environment variable
  103. \ref STARPU_NMAXBUFS which has a default value which can be changed
  104. through the \c configure option \ref enable-maxbuffers "--enable-maxbuffers".
  105. However, it is possible to define tasks managing more data by using
  106. the field starpu_task::dyn_handles when defining a task and the field
  107. starpu_codelet::dyn_modes when defining the corresponding codelet.
  108. \code{.c}
  109. enum starpu_data_access_mode modes[STARPU_NMAXBUFS+1] =
  110. {
  111. STARPU_R, STARPU_R, ...
  112. };
  113. struct starpu_codelet dummy_big_cl =
  114. {
  115. .cuda_funcs = { dummy_big_kernel },
  116. .opencl_funcs = { dummy_big_kernel },
  117. .cpu_funcs = { dummy_big_kernel },
  118. .cpu_funcs_name = { "dummy_big_kernel" },
  119. .nbuffers = STARPU_NMAXBUFS+1,
  120. .dyn_modes = modes
  121. };
  122. task = starpu_task_create();
  123. task->cl = &dummy_big_cl;
  124. task->dyn_handles = malloc(task->cl->nbuffers * sizeof(starpu_data_handle_t));
  125. for(i=0 ; i<task->cl->nbuffers ; i++)
  126. {
  127. task->dyn_handles[i] = handle;
  128. }
  129. starpu_task_submit(task);
  130. \endcode
  131. \code{.c}
  132. starpu_data_handle_t *handles = malloc(dummy_big_cl.nbuffers * sizeof(starpu_data_handle_t));
  133. for(i=0 ; i<dummy_big_cl.nbuffers ; i++)
  134. {
  135. handles[i] = handle;
  136. }
  137. starpu_task_insert(&dummy_big_cl,
  138. STARPU_VALUE, &dummy_big_cl.nbuffers, sizeof(dummy_big_cl.nbuffers),
  139. STARPU_DATA_ARRAY, handles, dummy_big_cl.nbuffers,
  140. 0);
  141. \endcode
  142. The whole code for this complex data interface is available in the
  143. file <c>examples/basic_examples/dynamic_handles.c</c>.
  144. \section SettingVariableDataHandlesForATask Setting a Variable Number Of Data Handles For a Task
  145. Normally, the number of data handles given to a task is set with
  146. starpu_codelet::nbuffers. This field can however be set to
  147. \ref STARPU_VARIABLE_NBUFFERS, in which case starpu_task::nbuffers
  148. must be set, and starpu_task::modes (or starpu_task::dyn_modes,
  149. see \ref SettingManyDataHandlesForATask) should be used to specify the modes for
  150. the handles.
  151. \section UsingMultipleImplementationsOfACodelet Using Multiple Implementations Of A Codelet
  152. One may want to write multiple implementations of a codelet for a single type of
  153. device and let StarPU choose which one to run. As an example, we will show how
  154. to use SSE to scale a vector. The codelet can be written as follows:
  155. \code{.c}
  156. #include <xmmintrin.h>
  157. void scal_sse_func(void *buffers[], void *cl_arg)
  158. {
  159. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  160. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]);
  161. unsigned int n_iterations = n/4;
  162. if (n % 4 != 0)
  163. n_iterations++;
  164. __m128 *VECTOR = (__m128*) vector;
  165. __m128 factor __attribute__((aligned(16)));
  166. factor = _mm_set1_ps(*(float *) cl_arg);
  167. unsigned int i;
  168. for (i = 0; i < n_iterations; i++)
  169. VECTOR[i] = _mm_mul_ps(factor, VECTOR[i]);
  170. }
  171. \endcode
  172. \code{.c}
  173. struct starpu_codelet cl =
  174. {
  175. .cpu_funcs = { scal_cpu_func, scal_sse_func },
  176. .cpu_funcs_name = { "scal_cpu_func", "scal_sse_func" },
  177. .nbuffers = 1,
  178. .modes = { STARPU_RW }
  179. };
  180. \endcode
  181. Schedulers which are multi-implementation aware (only <c>dmda</c> and
  182. <c>pheft</c> for now) will use the performance models of all the
  183. provided implementations, and pick the one which seems to be the fastest.
  184. \section EnablingImplementationAccordingToCapabilities Enabling Implementation According To Capabilities
  185. Some implementations may not run on some devices. For instance, some CUDA
  186. devices do not support double floating point precision, and thus the kernel
  187. execution would just fail; or the device may not have enough shared memory for
  188. the implementation being used. The field starpu_codelet::can_execute
  189. permits to express this. For instance:
  190. \code{.c}
  191. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  192. {
  193. const struct cudaDeviceProp *props;
  194. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  195. return 1;
  196. /* Cuda device */
  197. props = starpu_cuda_get_device_properties(workerid);
  198. if (props->major >= 2 || props->minor >= 3)
  199. /* At least compute capability 1.3, supports doubles */
  200. return 1;
  201. /* Old card, does not support doubles */
  202. return 0;
  203. }
  204. struct starpu_codelet cl =
  205. {
  206. .can_execute = can_execute,
  207. .cpu_funcs = { cpu_func },
  208. .cpu_funcs_name = { "cpu_func" },
  209. .cuda_funcs = { gpu_func }
  210. .nbuffers = 1,
  211. .modes = { STARPU_RW }
  212. };
  213. \endcode
  214. This can be essential e.g. when running on a machine which mixes various models
  215. of CUDA devices, to take benefit from the new models without crashing on old models.
  216. Note: the function starpu_codelet::can_execute is called by the
  217. scheduler each time it tries to match a task with a worker, and should
  218. thus be very fast. The function starpu_cuda_get_device_properties()
  219. provides a quick access to CUDA properties of CUDA devices to achieve
  220. such efficiency.
  221. Another example is to compile CUDA code for various compute capabilities,
  222. resulting with two CUDA functions, e.g. <c>scal_gpu_13</c> for compute capability
  223. 1.3, and <c>scal_gpu_20</c> for compute capability 2.0. Both functions can be
  224. provided to StarPU by using starpu_codelet::cuda_funcs, and
  225. starpu_codelet::can_execute can then be used to rule out the
  226. <c>scal_gpu_20</c> variant on a CUDA device which will not be able to execute it:
  227. \code{.c}
  228. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  229. {
  230. const struct cudaDeviceProp *props;
  231. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  232. return 1;
  233. /* Cuda device */
  234. if (nimpl == 0)
  235. /* Trying to execute the 1.3 capability variant, we assume it is ok in all cases. */
  236. return 1;
  237. /* Trying to execute the 2.0 capability variant, check that the card can do it. */
  238. props = starpu_cuda_get_device_properties(workerid);
  239. if (props->major >= 2 || props->minor >= 0)
  240. /* At least compute capability 2.0, can run it */
  241. return 1;
  242. /* Old card, does not support 2.0, will not be able to execute the 2.0 variant. */
  243. return 0;
  244. }
  245. struct starpu_codelet cl =
  246. {
  247. .can_execute = can_execute,
  248. .cpu_funcs = { cpu_func },
  249. .cpu_funcs_name = { "cpu_func" },
  250. .cuda_funcs = { scal_gpu_13, scal_gpu_20 },
  251. .nbuffers = 1,
  252. .modes = { STARPU_RW }
  253. };
  254. \endcode
  255. Another example is having specialized implementations for some given common
  256. sizes, for instance here we have a specialized implementation for 1024x1024
  257. matrices:
  258. \code{.c}
  259. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  260. {
  261. const struct cudaDeviceProp *props;
  262. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  263. return 1;
  264. /* Cuda device */
  265. switch (nimpl)
  266. {
  267. case 0:
  268. /* Trying to execute the generic capability variant. */
  269. return 1;
  270. case 1:
  271. {
  272. /* Trying to execute the size == 1024 specific variant. */
  273. struct starpu_matrix_interface *interface = starpu_data_get_interface_on_node(task->handles[0]);
  274. return STARPU_MATRIX_GET_NX(interface) == 1024 && STARPU_MATRIX_GET_NY(interface == 1024);
  275. }
  276. }
  277. }
  278. struct starpu_codelet cl =
  279. {
  280. .can_execute = can_execute,
  281. .cpu_funcs = { cpu_func },
  282. .cpu_funcs_name = { "cpu_func" },
  283. .cuda_funcs = { potrf_gpu_generic, potrf_gpu_1024 },
  284. .nbuffers = 1,
  285. .modes = { STARPU_RW }
  286. };
  287. \endcode
  288. Note that the most generic variant should be provided first, as some schedulers are
  289. not able to try the different variants.
  290. \section InsertTaskUtility Insert Task Utility
  291. StarPU provides the wrapper function starpu_task_insert() to ease
  292. the creation and submission of tasks.
  293. Here the implementation of a codelet:
  294. \code{.c}
  295. void func_cpu(void *descr[], void *_args)
  296. {
  297. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  298. float *x1 = (float *)STARPU_VARIABLE_GET_PTR(descr[1]);
  299. int ifactor;
  300. float ffactor;
  301. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  302. *x0 = *x0 * ifactor;
  303. *x1 = *x1 * ffactor;
  304. }
  305. struct starpu_codelet mycodelet =
  306. {
  307. .cpu_funcs = { func_cpu },
  308. .cpu_funcs_name = { "func_cpu" },
  309. .nbuffers = 2,
  310. .modes = { STARPU_RW, STARPU_RW }
  311. };
  312. \endcode
  313. And the call to the function starpu_task_insert():
  314. \code{.c}
  315. starpu_task_insert(&mycodelet,
  316. STARPU_VALUE, &ifactor, sizeof(ifactor),
  317. STARPU_VALUE, &ffactor, sizeof(ffactor),
  318. STARPU_RW, data_handles[0],
  319. STARPU_RW, data_handles[1],
  320. 0);
  321. \endcode
  322. The call to starpu_task_insert() is equivalent to the following
  323. code:
  324. \code{.c}
  325. struct starpu_task *task = starpu_task_create();
  326. task->cl = &mycodelet;
  327. task->handles[0] = data_handles[0];
  328. task->handles[1] = data_handles[1];
  329. char *arg_buffer;
  330. size_t arg_buffer_size;
  331. starpu_codelet_pack_args(&arg_buffer, &arg_buffer_size,
  332. STARPU_VALUE, &ifactor, sizeof(ifactor),
  333. STARPU_VALUE, &ffactor, sizeof(ffactor),
  334. 0);
  335. task->cl_arg = arg_buffer;
  336. task->cl_arg_size = arg_buffer_size;
  337. int ret = starpu_task_submit(task);
  338. \endcode
  339. Here a similar call using ::STARPU_DATA_ARRAY.
  340. \code{.c}
  341. starpu_task_insert(&mycodelet,
  342. STARPU_DATA_ARRAY, data_handles, 2,
  343. STARPU_VALUE, &ifactor, sizeof(ifactor),
  344. STARPU_VALUE, &ffactor, sizeof(ffactor),
  345. 0);
  346. \endcode
  347. If some part of the task insertion depends on the value of some computation,
  348. the macro ::STARPU_DATA_ACQUIRE_CB can be very convenient. For
  349. instance, assuming that the index variable <c>i</c> was registered as handle
  350. <c>A_handle[i]</c>:
  351. \code{.c}
  352. /* Compute which portion we will work on, e.g. pivot */
  353. starpu_task_insert(&which_index, STARPU_W, i_handle, 0);
  354. /* And submit the corresponding task */
  355. STARPU_DATA_ACQUIRE_CB(i_handle, STARPU_R,
  356. starpu_task_insert(&work, STARPU_RW, A_handle[i], 0));
  357. \endcode
  358. The macro ::STARPU_DATA_ACQUIRE_CB submits an asynchronous request for
  359. acquiring data <c>i</c> for the main application, and will execute the code
  360. given as third parameter when it is acquired. In other words, as soon as the
  361. value of <c>i</c> computed by the codelet <c>which_index</c> can be read, the
  362. portion of code passed as third parameter of ::STARPU_DATA_ACQUIRE_CB will
  363. be executed, and is allowed to read from <c>i</c> to use it e.g. as an
  364. index. Note that this macro is only avaible when compiling StarPU with
  365. the compiler <c>gcc</c>.
  366. StarPU also provides a utility function starpu_codelet_unpack_args() to retrieve the ::STARPU_VALUE arguments passed to the task. There is several ways of calling this function starpu_codelet_unpack_args().
  367. \code{.c}
  368. void func_cpu(void *descr[], void *_args)
  369. {
  370. int ifactor;
  371. float ffactor;
  372. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  373. }
  374. \endcode
  375. \code{.c}
  376. void func_cpu(void *descr[], void *_args)
  377. {
  378. int ifactor;
  379. float ffactor;
  380. starpu_codelet_unpack_args(_args, &ifactor, 0);
  381. starpu_codelet_unpack_args(_args, &ifactor, &ffactor);
  382. }
  383. \endcode
  384. \code{.c}
  385. void func_cpu(void *descr[], void *_args)
  386. {
  387. int ifactor;
  388. float ffactor;
  389. char buffer[100];
  390. starpu_codelet_unpack_args_and_copyleft(_args, buffer, 100, &ifactor, 0);
  391. starpu_codelet_unpack_args(buffer, &ffactor);
  392. }
  393. \endcode
  394. \section GettingTaskChildren Getting Task Children
  395. It may be interesting to get the list of tasks which depend on a given task,
  396. notably when using implicit dependencies, since this list is computed by StarPU.
  397. starpu_task_get_task_succs() provides it. For instance:
  398. \code{.c}
  399. struct starpu_task *tasks[4];
  400. ret = starpu_task_get_task_succs(task, sizeof(tasks)/sizeof(*tasks), tasks);
  401. \endcode
  402. \section ParallelTasks Parallel Tasks
  403. StarPU can leverage existing parallel computation libraries by the means of
  404. parallel tasks. A parallel task is a task which is run by a set of CPUs
  405. (called a parallel or combined worker) at the same time, by using an existing
  406. parallel CPU implementation of the computation to be achieved. This can also be
  407. useful to improve the load balance between slow CPUs and fast GPUs: since CPUs
  408. work collectively on a single task, the completion time of tasks on CPUs become
  409. comparable to the completion time on GPUs, thus relieving from granularity
  410. discrepancy concerns. <c>hwloc</c> support needs to be enabled to get
  411. good performance, otherwise StarPU will not know how to better group
  412. cores.
  413. Two modes of execution exist to accomodate with existing usages.
  414. \subsection Fork-modeParallelTasks Fork-mode Parallel Tasks
  415. In the Fork mode, StarPU will call the codelet function on one
  416. of the CPUs of the combined worker. The codelet function can use
  417. starpu_combined_worker_get_size() to get the number of threads it is
  418. allowed to start to achieve the computation. The CPU binding mask for the whole
  419. set of CPUs is already enforced, so that threads created by the function will
  420. inherit the mask, and thus execute where StarPU expected, the OS being in charge
  421. of choosing how to schedule threads on the corresponding CPUs. The application
  422. can also choose to bind threads by hand, using e.g. <c>sched_getaffinity</c> to know
  423. the CPU binding mask that StarPU chose.
  424. For instance, using OpenMP (full source is available in
  425. <c>examples/openmp/vector_scal.c</c>):
  426. \snippet forkmode.c To be included. You should update doxygen if you see this text.
  427. Other examples include for instance calling a BLAS parallel CPU implementation
  428. (see <c>examples/mult/xgemm.c</c>).
  429. \subsection SPMD-modeParallelTasks SPMD-mode Parallel Tasks
  430. In the SPMD mode, StarPU will call the codelet function on
  431. each CPU of the combined worker. The codelet function can use
  432. starpu_combined_worker_get_size() to get the total number of CPUs
  433. involved in the combined worker, and thus the number of calls that are made in
  434. parallel to the function, and starpu_combined_worker_get_rank() to get
  435. the rank of the current CPU within the combined worker. For instance:
  436. \code{.c}
  437. static void func(void *buffers[], void *args)
  438. {
  439. unsigned i;
  440. float *factor = _args;
  441. struct starpu_vector_interface *vector = buffers[0];
  442. unsigned n = STARPU_VECTOR_GET_NX(vector);
  443. float *val = (float *)STARPU_VECTOR_GET_PTR(vector);
  444. /* Compute slice to compute */
  445. unsigned m = starpu_combined_worker_get_size();
  446. unsigned j = starpu_combined_worker_get_rank();
  447. unsigned slice = (n+m-1)/m;
  448. for (i = j * slice; i < (j+1) * slice && i < n; i++)
  449. val[i] *= *factor;
  450. }
  451. static struct starpu_codelet cl =
  452. {
  453. .modes = { STARPU_RW },
  454. .type = STARPU_SPMD,
  455. .max_parallelism = INT_MAX,
  456. .cpu_funcs = { func },
  457. .cpu_funcs_name = { "func" },
  458. .nbuffers = 1,
  459. }
  460. \endcode
  461. Of course, this trivial example will not really benefit from parallel task
  462. execution, and was only meant to be simple to understand. The benefit comes
  463. when the computation to be done is so that threads have to e.g. exchange
  464. intermediate results, or write to the data in a complex but safe way in the same
  465. buffer.
  466. \subsection ParallelTasksPerformance Parallel Tasks Performance
  467. To benefit from parallel tasks, a parallel-task-aware StarPU scheduler has to
  468. be used. When exposed to codelets with a flag ::STARPU_FORKJOIN or
  469. ::STARPU_SPMD, the schedulers <c>pheft</c> (parallel-heft) and <c>peager</c>
  470. (parallel eager) will indeed also try to execute tasks with
  471. several CPUs. It will automatically try the various available combined
  472. worker sizes (making several measurements for each worker size) and
  473. thus be able to avoid choosing a large combined worker if the codelet
  474. does not actually scale so much.
  475. This is however for now only proof of concept, and has not really been optimized yet.
  476. \subsection CombinedWorkers Combined Workers
  477. By default, StarPU creates combined workers according to the architecture
  478. structure as detected by <c>hwloc</c>. It means that for each object of the <c>hwloc</c>
  479. topology (NUMA node, socket, cache, ...) a combined worker will be created. If
  480. some nodes of the hierarchy have a big arity (e.g. many cores in a socket
  481. without a hierarchy of shared caches), StarPU will create combined workers of
  482. intermediate sizes. The variable \ref STARPU_SYNTHESIZE_ARITY_COMBINED_WORKER
  483. permits to tune the maximum arity between levels of combined workers.
  484. The combined workers actually produced can be seen in the output of the
  485. tool <c>starpu_machine_display</c> (the environment variable
  486. \ref STARPU_SCHED has to be set to a combined worker-aware scheduler such
  487. as <c>pheft</c> or <c>peager</c>).
  488. \subsection ConcurrentParallelTasks Concurrent Parallel Tasks
  489. Unfortunately, many environments and librairies do not support concurrent
  490. calls.
  491. For instance, most OpenMP implementations (including the main ones) do not
  492. support concurrent <c>pragma omp parallel</c> statements without nesting them in
  493. another <c>pragma omp parallel</c> statement, but StarPU does not yet support
  494. creating its CPU workers by using such pragma.
  495. Other parallel libraries are also not safe when being invoked concurrently
  496. from different threads, due to the use of global variables in their sequential
  497. sections for instance.
  498. The solution is then to use only one combined worker at a time. This can be
  499. done by setting the field starpu_conf::single_combined_worker to <c>1</c>, or
  500. setting the environment variable \ref STARPU_SINGLE_COMBINED_WORKER
  501. to <c>1</c>. StarPU will then run only one parallel task at a time (but other
  502. CPU and GPU tasks are not affected and can be run concurrently). The parallel
  503. task scheduler will however still try varying combined worker
  504. sizes to look for the most efficient ones.
  505. \subsection SynchronizationTasks Synchronization Tasks
  506. For the application conveniency, it may be useful to define tasks which do not
  507. actually make any computation, but wear for instance dependencies between other
  508. tasks or tags, or to be submitted in callbacks, etc.
  509. The obvious way is of course to make kernel functions empty, but such task will
  510. thus have to wait for a worker to become ready, transfer data, etc.
  511. A much lighter way to define a synchronization task is to set its starpu_task::cl
  512. field to <c>NULL</c>. The task will thus be a mere synchronization point,
  513. without any data access or execution content: as soon as its dependencies become
  514. available, it will terminate, call the callbacks, and release dependencies.
  515. An intermediate solution is to define a codelet with its
  516. starpu_codelet::where field set to \ref STARPU_NOWHERE, for instance:
  517. \code{.c}
  518. struct starpu_codelet cl =
  519. {
  520. .where = STARPU_NOWHERE,
  521. .nbuffers = 1,
  522. .modes = { STARPU_R },
  523. }
  524. task = starpu_task_create();
  525. task->cl = &cl;
  526. task->handles[0] = handle;
  527. starpu_task_submit(task);
  528. \endcode
  529. will create a task which simply waits for the value of <c>handle</c> to be
  530. available for read. This task can then be depended on, etc.
  531. */