480_openmp_runtime_support.doxy 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2014 INRIA
  4. * See the file version.doxy for copying conditions.
  5. */
  6. /*! \page OpenMPRuntimeSupport The StarPU OpenMP Runtime Support (SORS)
  7. StarPU provides the necessary routines and support to implement an OpenMP
  8. (http://www.openmp.org/) runtime compliant with the
  9. revision 3.1 of the language specification, and compliant with the
  10. task-related data dependency functionalities introduced in the revision
  11. 4.0 of the language. This StarPU OpenMP Runtime Support (SORS) has been
  12. designed to be targetted by OpenMP compilers such as the Klang-OMP
  13. compiler. Most supported OpenMP directives can both be implemented
  14. inline or as outlined functions.
  15. All functions are defined in \ref API_OpenMP_Runtime_Support.
  16. \section Implementation Implementation Details and Specificities
  17. \subsection MainThread Main Thread
  18. When using the SORS, the main thread gets involved in executing OpenMP tasks
  19. just like every other threads, in order to be compliant with the
  20. specification execution model. This contrasts with StarPU's usual
  21. execution model where the main thread submit tasks but does not take
  22. part in executing them.
  23. \subsection TaskSemantics Extended Task Semantics
  24. The semantics of tasks generated by the SORS are extended with respect
  25. to regular StarPU tasks in that SORS' tasks may block and be preempted
  26. by SORS call, whereas regular StarPU tasks cannot. SORS tasks may
  27. coexist with regular StarPU tasks. However, only the tasks created using
  28. SORS API functions inherit from extended semantics.
  29. \section Configuration Configuration
  30. The SORS can be compiled into <c>libstarpu</c> through
  31. the configure option \ref enable-openmp "--enable-openmp".
  32. Conditional compiled source codes may check for the
  33. availability of the OpenMP Runtime Support by testing whether the C
  34. preprocessor macro <c>STARPU_OPENMP</c> is defined or not.
  35. \section InitExit Initialization and Shutdown
  36. The SORS needs to be executed/terminated by the
  37. starpu_omp_init() / starpu_omp_shutdown() instead of
  38. starpu_init() / starpu_shutdown(). This requirement is necessary to make
  39. sure that the main thread gets the proper execution environment to run
  40. OpenMP tasks. These calls will usually be performed by a compiler
  41. runtime. Thus, they can be executed from a constructor/destructor such
  42. as this:
  43. \code{.c}
  44. __attribute__((constructor))
  45. static void omp_constructor(void)
  46. {
  47. int ret = starpu_omp_init();
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  49. }
  50. __attribute__((destructor))
  51. static void omp_destructor(void)
  52. {
  53. starpu_omp_shutdown();
  54. }
  55. \endcode
  56. \sa starpu_omp_init()
  57. \sa starpu_omp_shutdown()
  58. \section Parallel Parallel Regions and Worksharing
  59. The SORS provides functions to create OpenMP parallel regions as well as
  60. mapping work on participating workers. The current implementation does
  61. not provide nested active parallel regions: Parallel regions may be
  62. created recursively, however only the first level parallel region may
  63. have more than one worker. From an internal point-of-view, the SORS'
  64. parallel regions are implemented as a set of implicit, extended semantics
  65. StarPU tasks, following the execution model of the OpenMP specification.
  66. Thus the SORS' parallel region tasks may block and be preempted, by
  67. SORS calls, enabling constructs such as barriers.
  68. \subsection OMPParallel Parallel Regions
  69. Parallel regions can be created with the function
  70. starpu_omp_parallel_region() which accepts a set of attributes as
  71. parameter. The execution of the calling task is suspended until the
  72. parallel region completes. The field starpu_omp_parallel_region_attr::cl
  73. is a regular StarPU codelet. However only CPU codelets are
  74. supported for parallel regions.
  75. Here is an example of use:
  76. \code{.c}
  77. void parallel_region_f(void *buffers[], void *args)
  78. {
  79. (void) buffers;
  80. (void) args;
  81. pthread_t tid = pthread_self();
  82. int worker_id = starpu_worker_get_id();
  83. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  84. }
  85. void f(void)
  86. {
  87. struct starpu_omp_parallel_region_attr attr;
  88. memset(&attr, 0, sizeof(attr));
  89. attr.cl.cpu_funcs[0] = parallel_region_f;
  90. attr.cl.where = STARPU_CPU;
  91. attr.if_clause = 1;
  92. starpu_omp_parallel_region(&attr);
  93. return 0;
  94. }
  95. \endcode
  96. \sa struct starpu_omp_parallel_region_attr
  97. \sa starpu_omp_parallel_region()
  98. \subsection OMPFor Parallel For
  99. OpenMP <c>for</c> loops are provided by the starpu_omp_for() group of
  100. functions. Variants are available for inline or outlined
  101. implementations. The SORS supports <c>static</c>, <c>dynamic</c>, and
  102. <c>guided</c> loop scheduling clauses. The <c>auto</c> scheduling clause
  103. is implemented as <c>static</c>. The <c>runtime</c> scheduling clause
  104. honors the scheduling mode selected through the environment variable
  105. OMP_SCHEDULE or the starpu_omp_set_schedule() function. For loops with
  106. the <c>ordered</c> clause are also supported. An implicit barrier can be
  107. enforced or skipped at the end of the worksharing construct, according
  108. to the value of the <c>nowait</c> parameter.
  109. The canonical family of starpu_omp_for() functions provide each instance
  110. with the first iteration number and the number of iterations (possibly
  111. zero) to perform. The alternate family of starpu_omp_for_alt() functions
  112. provide each instance with the (possibly empty) range of iterations to
  113. perform, including the first and excluding the last.
  114. The family of starpu_omp_ordered() functions enable to implement
  115. OpenMP's ordered construct, a region with a parallel for loop that is
  116. guaranteed to be executed in the sequential order of the loop
  117. iterations.
  118. \code{.c}
  119. void for_g(unsigned long long i, unsigned long long nb_i, void *arg)
  120. {
  121. (void) arg;
  122. for (; nb_i > 0; i++, nb_i--)
  123. {
  124. array[i] = 1;
  125. }
  126. }
  127. void parallel_region_f(void *buffers[], void *args)
  128. {
  129. (void) buffers;
  130. (void) args;
  131. starpu_omp_for(for_g, NULL, NB_ITERS, CHUNK, starpu_omp_sched_static, 0, 0);
  132. }
  133. \endcode
  134. \sa starpu_omp_for()
  135. \sa starpu_omp_for_inline_first()
  136. \sa starpu_omp_for_inline_next()
  137. \sa starpu_omp_for_alt()
  138. \sa starpu_omp_for_inline_first_alt()
  139. \sa starpu_omp_for_inline_next_alt()
  140. \sa starpu_omp_ordered()
  141. \sa starpu_omp_ordered_inline_begin()
  142. \sa starpu_omp_ordered_inline_end()
  143. \subsection OMPSections Sections
  144. OpenMP <c>sections</c> worksharing constructs are supported using the
  145. set of starpu_omp_sections() variants. The general principle is either
  146. to provide an array of per-section functions or a single function that
  147. will redirect to execution to the suitable per-section functions. An
  148. implicit barrier can be enforced or skipped at the end of the
  149. worksharing construct, according to the value of the <c>nowait</c>
  150. parameter.
  151. \code{.c}
  152. void parallel_region_f(void *buffers[], void *args)
  153. {
  154. (void) buffers;
  155. (void) args;
  156. section_funcs[0] = f;
  157. section_funcs[1] = g;
  158. section_funcs[2] = h;
  159. section_funcs[3] = i;
  160. section_args[0] = arg_f;
  161. section_args[1] = arg_g;
  162. section_args[2] = arg_h;
  163. section_args[3] = arg_i;
  164. starpu_omp_sections(4, section_f, section_args, 0);
  165. }
  166. \endcode
  167. \sa starpu_omp_sections()
  168. \sa starpu_omp_sections_combined()
  169. \subsection OMPSingle Single
  170. OpenMP <c>single</c> workharing constructs are supported using the set
  171. of starpu_omp_single() variants. An
  172. implicit barrier can be enforced or skipped at the end of the
  173. worksharing construct, according to the value of the <c>nowait</c>
  174. parameter.
  175. \code{.c}
  176. void single_f(void *arg)
  177. {
  178. (void) arg;
  179. pthread_t tid = pthread_self();
  180. int worker_id = starpu_worker_get_id();
  181. printf("[tid %p] task thread = %d -- single\n", (void *)tid, worker_id);
  182. }
  183. void parallel_region_f(void *buffers[], void *args)
  184. {
  185. (void) buffers;
  186. (void) args;
  187. starpu_omp_single(single_f, NULL, 0);
  188. }
  189. \endcode
  190. The SORS also provides dedicated support for <c>single</c> sections
  191. with <c>copyprivate</c> clauses through the
  192. starpu_omp_single_copyprivate() function variants. The OpenMP
  193. <c>master</c> directive is supported as well using the
  194. starpu_omp_master() function variants.
  195. \sa starpu_omp_master()
  196. \sa starpu_omp_master_inline()
  197. \sa starpu_omp_single()
  198. \sa starpu_omp_single_inline()
  199. \sa starpu_omp_single_copyprivate()
  200. \sa starpu_omp_single_copyprivate_inline_begin()
  201. \sa starpu_omp_single_copyprivate_inline_end()
  202. \section Task Tasks
  203. The SORS implements the necessary support of OpenMP 3.1 and OpenMP 4.0's
  204. so-called explicit tasks, together with OpenMP 4.0's data dependency
  205. management.
  206. \subsection OMPTask Explicit Tasks
  207. Explicit OpenMP tasks are created with the SORS using the
  208. starpu_omp_task_region() function. The implementation supports
  209. <c>if</c>, <c>final</c>, <c>untied</c> and <c>mergeable</c> clauses
  210. as defined in the OpenMP specification. Unless specified otherwise by
  211. the appropriate clause(s), the created task may be executed by any
  212. participating worker of the current parallel region.
  213. The current SORS implementation requires explicit tasks to be created
  214. within the context of an active parallel region. In particular, an
  215. explicit task cannot be created by the main thread outside of a parallel
  216. region. Explicit OpenMP tasks created using starpu_omp_task_region() are
  217. implemented as StarPU tasks with extended semantics, and may as such be
  218. blocked and preempted by SORS routines.
  219. The current SORS implementation supports recursive explicit tasks
  220. creation, to ensure compliance with the OpenMP specification. However,
  221. it should be noted that StarPU is not designed nor optimized for
  222. efficiently scheduling of recursive task applications.
  223. The code below shows how to create 4 explicit tasks within a parallel
  224. region.
  225. \code{.c}
  226. void task_region_g(void *buffers[], void *args)
  227. {
  228. (void) buffers;
  229. (void) args;
  230. pthread tid = pthread_self();
  231. int worker_id = starpu_worker_get_id();
  232. printf("[tid %p] task thread = %d: explicit task \"g\"\n", (void *)tid, worker_id);
  233. }
  234. void parallel_region_f(void *buffers[], void *args)
  235. {
  236. (void) buffers;
  237. (void) args;
  238. struct starpu_omp_task_region_attr attr;
  239. memset(&attr, 0, sizeof(attr));
  240. attr.cl.cpu_funcs[0] = task_region_g;
  241. attr.cl.where = STARPU_CPU;
  242. attr.if_clause = 1;
  243. attr.final_clause = 0;
  244. attr.untied_clause = 1;
  245. attr.mergeable_clause = 0;
  246. starpu_omp_task_region(&attr);
  247. starpu_omp_task_region(&attr);
  248. starpu_omp_task_region(&attr);
  249. starpu_omp_task_region(&attr);
  250. }
  251. \endcode
  252. \sa struct starpu_omp_task_region_attr
  253. \sa starpu_omp_task_region()
  254. \subsection DataDependencies Data Dependencies
  255. The SORS implements inter-tasks data dependencies as specified in OpenMP
  256. 4.0. Data dependencies are expressed using regular StarPU data handles
  257. (starpu_data_handle_t) plugged into the task's <c>attr.cl</c>
  258. codelet. The family of starpu_vector_data_register() -like functions and the
  259. starpu_data_lookup() function may be used to register a memory area and
  260. to retrieve the current data handle associated with a pointer
  261. respectively. The testcase <c>./tests/openmp/task_02.c</c> gives a
  262. detailed example of using OpenMP 4.0 tasks dependencies with the SORS
  263. implementation.
  264. Note: the OpenMP 4.0 specification only supports data dependencies
  265. between sibling tasks, that is tasks created by the same implicit or
  266. explicit parent task. The current SORS implementation also only supports data
  267. dependencies between sibling tasks. Consequently the behaviour is
  268. unspecified if dependencies are expressed beween tasks that have not
  269. been created by the same parent task.
  270. \subsection TaskSyncs TaskWait and TaskGroup
  271. The SORS implements both the <c>taskwait</c> and <c>taskgroup</c> OpenMP
  272. task synchronization constructs specified in OpenMP 4.0, with the
  273. starpu_omp_taskwait() and starpu_omp_taskgroup() functions respectively.
  274. An example of starpu_omp_taskwait() use, creating two explicit tasks and
  275. waiting for their completion:
  276. \code{.c}
  277. void task_region_g(void *buffers[], void *args)
  278. {
  279. (void) buffers;
  280. (void) args;
  281. printf("Hello, World!\n");
  282. }
  283. void parallel_region_f(void *buffers[], void *args)
  284. {
  285. (void) buffers;
  286. (void) args;
  287. struct starpu_omp_task_region_attr attr;
  288. memset(&attr, 0, sizeof(attr));
  289. attr.cl.cpu_funcs[0] = task_region_g;
  290. attr.cl.where = STARPU_CPU;
  291. attr.if_clause = 1;
  292. attr.final_clause = 0;
  293. attr.untied_clause = 1;
  294. attr.mergeable_clause = 0;
  295. starpu_omp_task_region(&attr);
  296. starpu_omp_task_region(&attr);
  297. starpu_omp_taskwait();
  298. \endcode
  299. An example of starpu_omp_taskgroup() use, creating a task group of two explicit tasks:
  300. \code{.c}
  301. void task_region_g(void *buffers[], void *args)
  302. {
  303. (void) buffers;
  304. (void) args;
  305. printf("Hello, World!\n");
  306. }
  307. void taskgroup_f(void *arg)
  308. {
  309. (void)arg;
  310. struct starpu_omp_task_region_attr attr;
  311. memset(&attr, 0, sizeof(attr));
  312. attr.cl.cpu_funcs[0] = task_region_g;
  313. attr.cl.where = STARPU_CPU;
  314. attr.if_clause = 1;
  315. attr.final_clause = 0;
  316. attr.untied_clause = 1;
  317. attr.mergeable_clause = 0;
  318. starpu_omp_task_region(&attr);
  319. starpu_omp_task_region(&attr);
  320. }
  321. void parallel_region_f(void *buffers[], void *args)
  322. {
  323. (void) buffers;
  324. (void) args;
  325. starpu_omp_taskgroup(taskgroup_f, (void *)NULL);
  326. }
  327. \endcode
  328. \sa starpu_omp_task_region()
  329. \sa starpu_omp_taskwait()
  330. \sa starpu_omp_taskgroup()
  331. \sa starpu_omp_taskgroup_inline_begin()
  332. \sa starpu_omp_taskgroup_inline_end()
  333. \section Synchronization Synchronization Support
  334. The SORS implements objects and method to build common OpenMP
  335. synchronization constructs.
  336. \subsection SimpleLock Simple Locks
  337. The SORS Simple Locks are opaque starpu_omp_lock_t objects enabling multiple
  338. tasks to synchronize with each others, following the Simple Lock
  339. constructs defined by the OpenMP specification. In accordance with such
  340. specification, simple locks may not by acquired multiple times by the
  341. same task, without being released in-between; otherwise, deadlocks may
  342. result. Codes requiring the possibility to lock multiple times
  343. recursively should use Nestable Locks (\ref NestableLock). Codes NOT
  344. requiring the possibility to lock multiple times recursively should use
  345. Simple Locks as they incur less processing overhead than Nestable Locks.
  346. \sa starpu_omp_lock_t
  347. \sa starpu_omp_init_lock()
  348. \sa starpu_omp_destroy_lock()
  349. \sa starpu_omp_set_lock()
  350. \sa starpu_omp_unset_lock()
  351. \sa starpu_omp_test_lock()
  352. \subsection NestableLock Nestable Locks
  353. The SORS Nestable Locks are opaque starpu_omp_nest_lock_t objects enabling
  354. multiple tasks to synchronize with each others, following the Nestable
  355. Lock constructs defined by the OpenMP specification. In accordance with
  356. such specification, nestable locks may by acquired multiple times
  357. recursively by the same task without deadlocking. Nested locking and
  358. unlocking operations must be well parenthesized at any time, otherwise
  359. deadlock and/or undefined behaviour may occur. Codes requiring the
  360. possibility to lock multiple times recursively should use Nestable
  361. Locks. Codes NOT requiring the possibility to lock multiple times
  362. recursively should use Simple Locks (\ref SimpleLock) instead, as they
  363. incur less processing overhead than Nestable Locks.
  364. \sa starpu_omp_nest_lock_t
  365. \sa starpu_omp_init_nest_lock()
  366. \sa starpu_omp_destroy_nest_lock()
  367. \sa starpu_omp_set_nest_lock()
  368. \sa starpu_omp_unset_nest_lock()
  369. \sa starpu_omp_test_nest_lock()
  370. \subsection Critical Critical Sections
  371. The SORS implements support for OpenMP critical sections through the
  372. family of starpu_omp_critical functions. Critical sections may optionally
  373. be named. There is a single, common anonymous critical section. Mutual
  374. exclusion only occur within the scope of single critical section, either
  375. a named one or the anonymous one.
  376. \sa starpu_omp_critical()
  377. \sa starpu_omp_critical_inline_begin()
  378. \sa starpu_omp_critical_inline_end()
  379. \subsection Barrier Barriers
  380. The SORS provides the starpu_omp_barrier() function to implement
  381. barriers over parallel region teams. In accordance with the OpenMP
  382. specification, the starpu_omp_barrier() function waits for every
  383. implicit task of the parallel region to reach the barrier and every
  384. explicit task launched by the parallel region to complete, before
  385. returning.
  386. \sa starpu_omp_barrier()
  387. */