openmp_runtime_support.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __OPENMP_RUNTIME_SUPPORT_H__
  17. #define __OPENMP_RUNTIME_SUPPORT_H__
  18. #include <starpu.h>
  19. #ifdef STARPU_OPENMP
  20. #include <common/list.h>
  21. #include <common/starpu_spinlock.h>
  22. #include <common/uthash.h>
  23. /* ucontexts have been deprecated as of POSIX 1-2004
  24. * _XOPEN_SOURCE required at least on OS/X
  25. *
  26. * TODO: add detection in configure.ac
  27. */
  28. #ifndef _XOPEN_SOURCE
  29. #define _XOPEN_SOURCE
  30. #endif
  31. #include <ucontext.h>
  32. /*
  33. * Arbitrary limit on the number of nested parallel sections
  34. */
  35. #define STARPU_OMP_MAX_ACTIVE_LEVELS 1
  36. /*
  37. * Possible abstract names for OpenMP places
  38. */
  39. enum starpu_omp_place_name
  40. {
  41. starpu_omp_place_undefined = 0,
  42. starpu_omp_place_threads = 1,
  43. starpu_omp_place_cores = 2,
  44. starpu_omp_place_sockets = 3,
  45. starpu_omp_place_numerical = 4 /* place specified numerically */
  46. };
  47. struct starpu_omp_numeric_place
  48. {
  49. int excluded_place;
  50. int *included_numeric_items;
  51. int nb_included_numeric_items;
  52. int *excluded_numeric_items;
  53. int nb_excluded_numeric_items;
  54. };
  55. /*
  56. * OpenMP place for thread afinity, defined by the OpenMP spec
  57. */
  58. struct starpu_omp_place
  59. {
  60. int abstract_name;
  61. int abstract_excluded;
  62. int abstract_length;
  63. struct starpu_omp_numeric_place *numeric_places;
  64. int nb_numeric_places;
  65. };
  66. /*
  67. * Internal Control Variables (ICVs) declared following
  68. * OpenMP 4.0.0 spec section 2.3.1
  69. */
  70. struct starpu_omp_data_environment_icvs
  71. {
  72. /* parallel region icvs */
  73. int dyn_var;
  74. int nest_var;
  75. int *nthreads_var; /* nthreads_var ICV is a list */
  76. int thread_limit_var;
  77. int active_levels_var;
  78. int levels_var;
  79. int *bind_var; /* bind_var ICV is a list */
  80. /* loop region icvs */
  81. int run_sched_var;
  82. unsigned long long run_sched_chunk_var;
  83. /* program execution icvs */
  84. int default_device_var;
  85. };
  86. struct starpu_omp_device_icvs
  87. {
  88. /* parallel region icvs */
  89. int max_active_levels_var;
  90. /* loop region icvs */
  91. int def_sched_var;
  92. unsigned long long def_sched_chunk_var;
  93. /* program execution icvs */
  94. int stacksize_var;
  95. int wait_policy_var;
  96. };
  97. struct starpu_omp_implicit_task_icvs
  98. {
  99. /* parallel region icvs */
  100. int place_partition_var;
  101. };
  102. struct starpu_omp_global_icvs
  103. {
  104. /* program execution icvs */
  105. int cancel_var;
  106. };
  107. struct starpu_omp_initial_icv_values
  108. {
  109. int dyn_var;
  110. int nest_var;
  111. int *nthreads_var;
  112. int run_sched_var;
  113. unsigned long long run_sched_chunk_var;
  114. int def_sched_var;
  115. unsigned long long def_sched_chunk_var;
  116. int *bind_var;
  117. int stacksize_var;
  118. int wait_policy_var;
  119. int thread_limit_var;
  120. int max_active_levels_var;
  121. int active_levels_var;
  122. int levels_var;
  123. int place_partition_var;
  124. int cancel_var;
  125. int default_device_var;
  126. /* not a real ICV, but needed to store the contents of OMP_PLACES */
  127. struct starpu_omp_place places;
  128. };
  129. struct starpu_omp_task_group
  130. {
  131. int descendent_task_count;
  132. struct starpu_omp_task *leader_task;
  133. struct starpu_omp_task_group *p_previous_task_group;
  134. };
  135. struct starpu_omp_task_link
  136. {
  137. struct starpu_omp_task *task;
  138. struct starpu_omp_task_link *next;
  139. };
  140. struct starpu_omp_condition
  141. {
  142. struct starpu_omp_task_link *contention_list_head;
  143. };
  144. struct starpu_omp_critical
  145. {
  146. UT_hash_handle hh;
  147. struct _starpu_spinlock lock;
  148. unsigned state;
  149. struct starpu_omp_task_link *contention_list_head;
  150. const char *name;
  151. };
  152. enum starpu_omp_task_state
  153. {
  154. starpu_omp_task_state_clear = 0,
  155. starpu_omp_task_state_preempted = 1,
  156. starpu_omp_task_state_terminated = 2,
  157. starpu_omp_task_state_zombie = 3,
  158. /* target tasks are non-preemptible tasks, without dedicated stack and OpenMP Runtime Support context */
  159. starpu_omp_task_state_target = 4,
  160. };
  161. enum starpu_omp_task_wait_on
  162. {
  163. starpu_omp_task_wait_on_task_childs = 1 << 0,
  164. starpu_omp_task_wait_on_region_tasks = 1 << 1,
  165. starpu_omp_task_wait_on_barrier = 1 << 2,
  166. starpu_omp_task_wait_on_group = 1 << 3,
  167. starpu_omp_task_wait_on_critical = 1 << 4,
  168. starpu_omp_task_wait_on_condition = 1 << 5
  169. };
  170. LIST_TYPE(starpu_omp_task,
  171. struct starpu_omp_implicit_task_icvs icvs;
  172. struct starpu_omp_task *parent_task;
  173. struct starpu_omp_thread *owner_thread;
  174. struct starpu_omp_region *owner_region;
  175. struct starpu_omp_region *nested_region;
  176. int is_implicit;
  177. int is_undeferred;
  178. int is_final;
  179. int is_untied;
  180. int rank;
  181. int child_task_count;
  182. struct starpu_omp_task_group *task_group;
  183. struct _starpu_spinlock lock;
  184. int transaction_pending;
  185. int wait_on;
  186. int barrier_count;
  187. int single_id;
  188. int single_first;
  189. int loop_id;
  190. unsigned long long ordered_first_i;
  191. unsigned long long ordered_nb_i;
  192. int sections_id;
  193. struct starpu_omp_data_environment_icvs data_env_icvs;
  194. struct starpu_omp_implicit_task_icvs implicit_task_icvs;
  195. struct handle_entry *registered_handles;
  196. struct starpu_task *starpu_task;
  197. struct starpu_codelet cl;
  198. void **starpu_buffers;
  199. void *starpu_cl_arg;
  200. /* actual task function to be run */
  201. void (*cpu_f)(void **starpu_buffers, void *starpu_cl_arg);
  202. #ifdef STARPU_USE_CUDA
  203. void (*cuda_f)(void **starpu_buffers, void *starpu_cl_arg);
  204. #endif
  205. #ifdef STARPU_USE_OPENCL
  206. void (*opencl_f)(void **starpu_buffers, void *starpu_cl_arg);
  207. #endif
  208. enum starpu_omp_task_state state;
  209. /*
  210. * context to store the processing state of the task
  211. * in case of blocking/recursive task operation
  212. */
  213. ucontext_t ctx;
  214. /*
  215. * stack to execute the task over, to be able to switch
  216. * in case blocking/recursive task operation
  217. */
  218. void *stack;
  219. /*
  220. * Valgrind stack id
  221. */
  222. int stack_vg_id;
  223. size_t stacksize;
  224. )
  225. LIST_TYPE(starpu_omp_thread,
  226. UT_hash_handle hh;
  227. struct starpu_omp_task *current_task;
  228. struct starpu_omp_region *owner_region;
  229. /*
  230. * stack to execute the initial thread over
  231. * when preempting the initial task
  232. * note: should not be used for other threads
  233. */
  234. void *initial_thread_stack;
  235. /*
  236. * Valgrind stack id
  237. */
  238. int initial_thread_stack_vg_id;
  239. /*
  240. * context to store the 'scheduler' state of the thread,
  241. * to which the execution of thread comes back upon a
  242. * blocking/recursive task operation
  243. */
  244. ucontext_t ctx;
  245. struct starpu_driver starpu_driver;
  246. struct _starpu_worker *worker;
  247. )
  248. struct _starpu_omp_lock_internal
  249. {
  250. struct _starpu_spinlock lock;
  251. struct starpu_omp_condition cond;
  252. unsigned state;
  253. };
  254. struct _starpu_omp_nest_lock_internal
  255. {
  256. struct _starpu_spinlock lock;
  257. struct starpu_omp_condition cond;
  258. unsigned state;
  259. struct starpu_omp_task *owner_task;
  260. unsigned nesting;
  261. };
  262. struct starpu_omp_loop
  263. {
  264. int id;
  265. unsigned long long next_iteration;
  266. int nb_completed_threads;
  267. struct starpu_omp_loop *next_loop;
  268. struct _starpu_spinlock ordered_lock;
  269. struct starpu_omp_condition ordered_cond;
  270. unsigned long long ordered_iteration;
  271. };
  272. struct starpu_omp_sections
  273. {
  274. int id;
  275. unsigned long long next_section_num;
  276. int nb_completed_threads;
  277. struct starpu_omp_sections *next_sections;
  278. };
  279. struct starpu_omp_region
  280. {
  281. struct starpu_omp_data_environment_icvs icvs;
  282. struct starpu_omp_region *parent_region;
  283. struct starpu_omp_device *owner_device;
  284. struct starpu_omp_thread *master_thread;
  285. /* note: the list of threads does not include the master_thread */
  286. struct starpu_omp_thread_list thread_list;
  287. /* list of implicit omp tasks created to run the region */
  288. struct starpu_omp_task_list implicit_task_list;
  289. /* include both the master thread and the region own threads */
  290. int nb_threads;
  291. struct _starpu_spinlock lock;
  292. struct starpu_omp_task *waiting_task;
  293. int barrier_count;
  294. int bound_explicit_task_count;
  295. int single_id;
  296. void *copy_private_data;
  297. int level;
  298. struct starpu_omp_loop *loop_list;
  299. struct starpu_omp_sections *sections_list;
  300. struct starpu_task *continuation_starpu_task;
  301. struct handle_entry *registered_handles;
  302. struct _starpu_spinlock registered_handles_lock;
  303. };
  304. struct starpu_omp_device
  305. {
  306. struct starpu_omp_device_icvs icvs;
  307. /* atomic fallback implementation lock */
  308. struct _starpu_spinlock atomic_lock;
  309. };
  310. struct starpu_omp_global
  311. {
  312. struct starpu_omp_global_icvs icvs;
  313. struct starpu_omp_task *initial_task;
  314. struct starpu_omp_thread *initial_thread;
  315. struct starpu_omp_region *initial_region;
  316. struct starpu_omp_device *initial_device;
  317. struct starpu_omp_critical *default_critical;
  318. struct starpu_omp_critical *named_criticals;
  319. struct _starpu_spinlock named_criticals_lock;
  320. struct starpu_omp_thread *hash_workers;
  321. struct _starpu_spinlock hash_workers_lock;
  322. struct starpu_arbiter *default_arbiter;
  323. int nb_starpu_cpu_workers;
  324. int *starpu_cpu_worker_ids;
  325. };
  326. /*
  327. * internal global variables
  328. */
  329. extern struct starpu_omp_initial_icv_values *_starpu_omp_initial_icv_values;
  330. extern struct starpu_omp_global *_starpu_omp_global_state;
  331. extern double _starpu_omp_clock_ref;
  332. /*
  333. * internal API
  334. */
  335. void _starpu_omp_environment_init(void);
  336. void _starpu_omp_environment_exit(void);
  337. struct starpu_omp_thread *_starpu_omp_get_thread(void);
  338. struct starpu_omp_task *_starpu_omp_get_task(void);
  339. void _starpu_omp_dummy_init(void);
  340. void _starpu_omp_dummy_shutdown(void);
  341. #endif // STARPU_OPENMP
  342. #endif // __OPENMP_RUNTIME_SUPPORT_H__