22openmp_runtime_support.doxy 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <a
  8. href="http://www.openmp.org/">OpenMP</a> 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>
  31. by providing the <c>--enable-openmp</c> flag to StarPU's
  32. <c>configure</c>. 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. \section Parallel Parallel Regions and Worksharing
  57. The SORS provides functions to create OpenMP parallel regions as well as
  58. mapping work on participating workers. The current implementation does
  59. not provide nested active parallel regions: Parallel regions may be
  60. created recursively, however only the first level parallel region may
  61. have more than one worker. From an internal point-of-view, the SORS'
  62. parallel regions are implemented as a set of implicit, extended semantics
  63. StarPU tasks, following the execution model of the OpenMP specification.
  64. Thus the SORS' parallel region tasks may block and be preempted, by
  65. SORS calls, enabling constructs such as barriers.
  66. \subsection OMPParallel Parallel Regions
  67. Parallel regions can be created with the function
  68. starpu_omp_parallel_region() which accepts a set of attributes as
  69. parameter. The execution of the calling task is suspended until the
  70. parallel region completes. The <c>attr.cl</c> field is a regular StarPU
  71. codelet. However only CPU codelets are supported for parallel regions.
  72. Here is an example of use:
  73. \code{.c}
  74. void parallel_region_f(void *buffers[], void *args)
  75. {
  76. (void) buffers;
  77. (void) args;
  78. pthread_t tid = pthread_self();
  79. int worker_id = starpu_worker_get_id();
  80. printf("[tid %p] task thread = %d\n", (void *)tid, worker_id);
  81. }
  82. void f(void)
  83. {
  84. struct starpu_omp_parallel_region_attr attr;
  85. memset(&attr, 0, sizeof(attr));
  86. attr.cl.cpu_funcs[0] = parallel_region_f;
  87. attr.cl.where = STARPU_CPU;
  88. attr.if_clause = 1;
  89. starpu_omp_parallel_region(&attr);
  90. return 0;
  91. }
  92. \endcode
  93. \subsection OMPFor Parallel For
  94. OpenMP <c>for</c> loops are provided by the starpu_omp_for() group of
  95. functions. Variants are available for inline or outlined
  96. implementations. The SORS supports <c>static</c>, <c>dynamic</c>, and
  97. <c>guided</c> loop scheduling clauses. The <c>auto</c> scheduling clause
  98. is implemented as <c>static</c>. The <c>runtime</c> scheduling clause
  99. honors the scheduling mode selected through the environment variable
  100. OMP_SCHEDULE or the starpu_omp_set_schedule() function. For loops with
  101. the <c>ordered</c> clause are also supported. An implicit barrier can be
  102. enforced or skipped at the end of the worksharing construct, according
  103. to the value of the <c>nowait</c> parameter.
  104. \code{.c}
  105. void for_g(unsigned long long i, unsigned long long nb_i, void *arg)
  106. {
  107. (void) arg;
  108. for (; nb_i > 0; i++, nb_i--)
  109. {
  110. array[i] = 1;
  111. }
  112. }
  113. void parallel_region_f(void *buffers[], void *args)
  114. {
  115. (void) buffers;
  116. (void) args;
  117. starpu_omp_for(for_g, NULL, NB_ITERS, CHUNK, starpu_omp_sched_static, 0, 0);
  118. }
  119. \endcode
  120. \subsection OMPSections Sections
  121. OpenMP <c>sections</c> worksharing constructs are supported using the
  122. set of starpu_omp_sections() variants. The general principle is either
  123. to provide an array of per-section functions or a single function that
  124. will redirect to execution to the suitable per-section functions. An
  125. implicit barrier can be enforced or skipped at the end of the
  126. worksharing construct, according to the value of the <c>nowait</c>
  127. parameter.
  128. \code{.c}
  129. void parallel_region_f(void *buffers[], void *args)
  130. {
  131. (void) buffers;
  132. (void) args;
  133. section_funcs[0] = f;
  134. section_funcs[1] = g;
  135. section_funcs[2] = h;
  136. section_funcs[3] = i;
  137. section_args[0] = arg_f;
  138. section_args[1] = arg_g;
  139. section_args[2] = arg_h;
  140. section_args[3] = arg_i;
  141. starpu_omp_sections(4, section_f, section_args, 0);
  142. }
  143. \endcode
  144. \subsection OMPSingle Single
  145. OpenMP <c>single</c> workharing constructs are supported using the set
  146. of starpu_omp_single() variants. An
  147. implicit barrier can be enforced or skipped at the end of the
  148. worksharing construct, according to the value of the <c>nowait</c>
  149. parameter.
  150. \code{.c}
  151. void single_f(void *arg)
  152. {
  153. (void) arg;
  154. pthread_t tid = pthread_self();
  155. int worker_id = starpu_worker_get_id();
  156. printf("[tid %p] task thread = %d -- single\n", (void *)tid, worker_id);
  157. }
  158. void parallel_region_f(void *buffers[], void *args)
  159. {
  160. (void) buffers;
  161. (void) args;
  162. starpu_omp_single(single_f, NULL, 0);
  163. }
  164. \endcode
  165. The SORS also provides dedicated support for <c>single</c> sections
  166. with <c>copyprivate</c> clauses through the
  167. starpu_omp_single_copyprivate() function variants. The OpenMP
  168. <c>master</c> directive is supported as well using the
  169. starpu_omp_master() function variants.
  170. \section Task Tasks
  171. The SORS implements the necessary support of OpenMP 3.1 and OpenMP 4.0's
  172. so-called explicit tasks, together with OpenMP 4.0's data dependency
  173. management.
  174. \subsection OMPTask Explicit Tasks
  175. Explicit OpenMP tasks are created with the SORS using the
  176. starpu_omp_task_region() function. The implementation supports
  177. <c>if</c>, <c>final</c>, <c>untied</c> and <c>mergeable</c> clauses
  178. as defined in the OpenMP specification. Unless specified otherwise by
  179. the appropriate clause(s), the created task may be executed by any
  180. participating worker of the current parallel region.
  181. The current SORS implementation requires explicit tasks to be created
  182. within the context of an active parallel region. In particular, an
  183. explicit task cannot be created by the main thread outside of a parallel
  184. region. Explicit OpenMP tasks created using starpu_omp_task_region() are
  185. implemented as StarPU tasks with extended semantics, and may as such be
  186. blocked and preempted by SORS routines.
  187. The current SORS implementation supports recursive explicit tasks
  188. creation, to ensure compliance with the OpenMP specification. However,
  189. it should be noted that StarPU is not designed nor optimized for
  190. efficiently scheduling of recursive task applications.
  191. The code below shows how to create 4 explicit tasks within a parallel
  192. region.
  193. \code{.c}
  194. void task_region_g(void *buffers[], void *args)
  195. {
  196. (void) buffers;
  197. (void) args;
  198. pthread tid = pthread_self();
  199. int worker_id = starpu_worker_get_id();
  200. printf("[tid %p] task thread = %d: explicit task \"g\"\n", (void *)tid, worker_id);
  201. }
  202. void parallel_region_f(void *buffers[], void *args)
  203. {
  204. (void) buffers;
  205. (void) args;
  206. struct starpu_omp_task_region_attr attr;
  207. memset(&attr, 0, sizeof(attr));
  208. attr.cl.cpu_funcs[0] = task_region_g;
  209. attr.cl.where = STARPU_CPU;
  210. attr.if_clause = 1;
  211. attr.final_clause = 0;
  212. attr.untied_clause = 1;
  213. attr.mergeable_clause = 0;
  214. starpu_omp_task_region(&attr);
  215. starpu_omp_task_region(&attr);
  216. starpu_omp_task_region(&attr);
  217. starpu_omp_task_region(&attr);
  218. }
  219. \endcode
  220. \subsection DataDependencies Data Dependencies
  221. The SORS implements inter-tasks data dependencies as specified in OpenMP
  222. 4.0. Data dependencies are expressed using regular StarPU data handles
  223. (<c>starpu_data_handle_t</c>) plugged into the task's <c>attr.cl</c>
  224. codelet. The family of starpu_vector_data_register() -like functions and the
  225. starpu_data_lookup() function may be used to register a memory area and
  226. to retrieve the current data handle associated with a pointer
  227. respectively. The testcase <c>./tests/openmp/task_02.c</c> gives a
  228. detailed example of using OpenMP 4.0 tasks dependencies with the SORS
  229. implementation.
  230. Note: the OpenMP 4.0 specification only supports data dependencies
  231. between sibling tasks, that is tasks created by the same implicit or
  232. explicit parent task. The current SORS implementation also only supports data
  233. dependencies between sibling tasks. Consequently the behaviour is
  234. unspecified if dependencies are expressed beween tasks that have not
  235. been created by the same parent task.
  236. \subsection TaskSyncs TaskWait and TaskGroup
  237. The SORS implements both the <c>taskwait</c> and <c>taskgroup</c> OpenMP
  238. task synchronization constructs specified in OpenMP 4.0, with the
  239. starpu_omp_taskwait() and starpu_omp_taskgroup() functions respectively.
  240. An example of starpu_omp_taskwait() use, creating two explicit tasks and
  241. waiting for their completion:
  242. \code{.c}
  243. void task_region_g(void *buffers[], void *args)
  244. {
  245. (void) buffers;
  246. (void) args;
  247. printf("Hello, World!\n");
  248. }
  249. void parallel_region_f(void *buffers[], void *args)
  250. {
  251. (void) buffers;
  252. (void) args;
  253. struct starpu_omp_task_region_attr attr;
  254. memset(&attr, 0, sizeof(attr));
  255. attr.cl.cpu_funcs[0] = task_region_g;
  256. attr.cl.where = STARPU_CPU;
  257. attr.if_clause = 1;
  258. attr.final_clause = 0;
  259. attr.untied_clause = 1;
  260. attr.mergeable_clause = 0;
  261. starpu_omp_task_region(&attr);
  262. starpu_omp_task_region(&attr);
  263. starpu_omp_taskwait();
  264. \endcode
  265. An example of starpu_omp_taskgroup() use, creating a task group of two explicit tasks:
  266. \code{.c}
  267. void task_region_g(void *buffers[], void *args)
  268. {
  269. (void) buffers;
  270. (void) args;
  271. printf("Hello, World!\n");
  272. }
  273. void taskgroup_f(void *arg)
  274. {
  275. (void)arg;
  276. struct starpu_omp_task_region_attr attr;
  277. memset(&attr, 0, sizeof(attr));
  278. attr.cl.cpu_funcs[0] = task_region_g;
  279. attr.cl.where = STARPU_CPU;
  280. attr.if_clause = 1;
  281. attr.final_clause = 0;
  282. attr.untied_clause = 1;
  283. attr.mergeable_clause = 0;
  284. starpu_omp_task_region(&attr);
  285. starpu_omp_task_region(&attr);
  286. }
  287. void parallel_region_f(void *buffers[], void *args)
  288. {
  289. (void) buffers;
  290. (void) args;
  291. starpu_omp_taskgroup(taskgroup_f, (void *)NULL);
  292. }
  293. \endcode
  294. \section Synchronization Synchronization Support
  295. Synchronization objects and methods.
  296. */