openmp_runtime_support.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014, 2017 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. int max_task_priority_var;
  86. };
  87. struct starpu_omp_device_icvs
  88. {
  89. /* parallel region icvs */
  90. int max_active_levels_var;
  91. /* loop region icvs */
  92. int def_sched_var;
  93. unsigned long long def_sched_chunk_var;
  94. /* program execution icvs */
  95. int stacksize_var;
  96. int wait_policy_var;
  97. };
  98. struct starpu_omp_implicit_task_icvs
  99. {
  100. /* parallel region icvs */
  101. int place_partition_var;
  102. };
  103. struct starpu_omp_global_icvs
  104. {
  105. /* program execution icvs */
  106. int cancel_var;
  107. };
  108. struct starpu_omp_initial_icv_values
  109. {
  110. int dyn_var;
  111. int nest_var;
  112. int *nthreads_var;
  113. int run_sched_var;
  114. unsigned long long run_sched_chunk_var;
  115. int def_sched_var;
  116. unsigned long long def_sched_chunk_var;
  117. int *bind_var;
  118. int stacksize_var;
  119. int wait_policy_var;
  120. int thread_limit_var;
  121. int max_active_levels_var;
  122. int active_levels_var;
  123. int levels_var;
  124. int place_partition_var;
  125. int cancel_var;
  126. int default_device_var;
  127. int max_task_priority_var;
  128. /* not a real ICV, but needed to store the contents of OMP_PLACES */
  129. struct starpu_omp_place places;
  130. };
  131. struct starpu_omp_task_group
  132. {
  133. int descendent_task_count;
  134. struct starpu_omp_task *leader_task;
  135. struct starpu_omp_task_group *p_previous_task_group;
  136. };
  137. struct starpu_omp_task_link
  138. {
  139. struct starpu_omp_task *task;
  140. struct starpu_omp_task_link *next;
  141. };
  142. struct starpu_omp_condition
  143. {
  144. struct starpu_omp_task_link *contention_list_head;
  145. };
  146. struct starpu_omp_critical
  147. {
  148. UT_hash_handle hh;
  149. struct _starpu_spinlock lock;
  150. unsigned state;
  151. struct starpu_omp_task_link *contention_list_head;
  152. const char *name;
  153. };
  154. enum starpu_omp_task_state
  155. {
  156. starpu_omp_task_state_clear = 0,
  157. starpu_omp_task_state_preempted = 1,
  158. starpu_omp_task_state_terminated = 2,
  159. starpu_omp_task_state_zombie = 3,
  160. /* target tasks are non-preemptible tasks, without dedicated stack and OpenMP Runtime Support context */
  161. starpu_omp_task_state_target = 4,
  162. };
  163. enum starpu_omp_task_wait_on
  164. {
  165. starpu_omp_task_wait_on_task_childs = 1 << 0,
  166. starpu_omp_task_wait_on_region_tasks = 1 << 1,
  167. starpu_omp_task_wait_on_barrier = 1 << 2,
  168. starpu_omp_task_wait_on_group = 1 << 3,
  169. starpu_omp_task_wait_on_critical = 1 << 4,
  170. starpu_omp_task_wait_on_ordered = 1 << 5,
  171. starpu_omp_task_wait_on_lock = 1 << 6,
  172. starpu_omp_task_wait_on_nest_lock = 1 << 7,
  173. };
  174. enum starpu_omp_task_flags
  175. {
  176. STARPU_OMP_TASK_FLAGS_IMPLICIT = 1 << 0,
  177. STARPU_OMP_TASK_FLAGS_UNDEFERRED = 1 << 1,
  178. STARPU_OMP_TASK_FLAGS_FINAL = 1 << 2,
  179. STARPU_OMP_TASK_FLAGS_UNTIED = 1 << 3,
  180. };
  181. LIST_TYPE(starpu_omp_task,
  182. struct starpu_omp_implicit_task_icvs icvs;
  183. struct starpu_omp_task *parent_task;
  184. struct starpu_omp_thread *owner_thread;
  185. struct starpu_omp_region *owner_region;
  186. struct starpu_omp_region *nested_region;
  187. int rank;
  188. int child_task_count;
  189. struct starpu_omp_task_group *task_group;
  190. struct _starpu_spinlock lock;
  191. int transaction_pending;
  192. int wait_on;
  193. int barrier_count;
  194. int single_id;
  195. int single_first;
  196. int loop_id;
  197. unsigned long long ordered_first_i;
  198. unsigned long long ordered_nb_i;
  199. int sections_id;
  200. struct starpu_omp_data_environment_icvs data_env_icvs;
  201. struct starpu_omp_implicit_task_icvs implicit_task_icvs;
  202. struct handle_entry *registered_handles;
  203. struct starpu_task *starpu_task;
  204. struct starpu_codelet cl;
  205. void **starpu_buffers;
  206. void *starpu_cl_arg;
  207. /* actual task function to be run */
  208. void (*cpu_f)(void **starpu_buffers, void *starpu_cl_arg);
  209. #ifdef STARPU_USE_CUDA
  210. void (*cuda_f)(void **starpu_buffers, void *starpu_cl_arg);
  211. #endif
  212. #ifdef STARPU_USE_OPENCL
  213. void (*opencl_f)(void **starpu_buffers, void *starpu_cl_arg);
  214. #endif
  215. enum starpu_omp_task_state state;
  216. enum starpu_omp_task_flags flags;
  217. /*
  218. * context to store the processing state of the task
  219. * in case of blocking/recursive task operation
  220. */
  221. ucontext_t ctx;
  222. /*
  223. * stack to execute the task over, to be able to switch
  224. * in case blocking/recursive task operation
  225. */
  226. void *stack;
  227. /*
  228. * Valgrind stack id
  229. */
  230. int stack_vg_id;
  231. size_t stacksize;
  232. )
  233. LIST_TYPE(starpu_omp_thread,
  234. UT_hash_handle hh;
  235. struct starpu_omp_task *current_task;
  236. struct starpu_omp_region *owner_region;
  237. /*
  238. * stack to execute the initial thread over
  239. * when preempting the initial task
  240. * note: should not be used for other threads
  241. */
  242. void *initial_thread_stack;
  243. /*
  244. * Valgrind stack id
  245. */
  246. int initial_thread_stack_vg_id;
  247. /*
  248. * context to store the 'scheduler' state of the thread,
  249. * to which the execution of thread comes back upon a
  250. * blocking/recursive task operation
  251. */
  252. ucontext_t ctx;
  253. struct starpu_driver starpu_driver;
  254. struct _starpu_worker *worker;
  255. )
  256. struct _starpu_omp_lock_internal
  257. {
  258. struct _starpu_spinlock lock;
  259. struct starpu_omp_condition cond;
  260. unsigned state;
  261. };
  262. struct _starpu_omp_nest_lock_internal
  263. {
  264. struct _starpu_spinlock lock;
  265. struct starpu_omp_condition cond;
  266. unsigned state;
  267. struct starpu_omp_task *owner_task;
  268. unsigned nesting;
  269. };
  270. struct starpu_omp_loop
  271. {
  272. int id;
  273. unsigned long long next_iteration;
  274. int nb_completed_threads;
  275. struct starpu_omp_loop *next_loop;
  276. struct _starpu_spinlock ordered_lock;
  277. struct starpu_omp_condition ordered_cond;
  278. unsigned long long ordered_iteration;
  279. };
  280. struct starpu_omp_sections
  281. {
  282. int id;
  283. unsigned long long next_section_num;
  284. int nb_completed_threads;
  285. struct starpu_omp_sections *next_sections;
  286. };
  287. struct starpu_omp_region
  288. {
  289. struct starpu_omp_data_environment_icvs icvs;
  290. struct starpu_omp_region *parent_region;
  291. struct starpu_omp_device *owner_device;
  292. struct starpu_omp_thread *master_thread;
  293. /* note: the list of threads does not include the master_thread */
  294. struct starpu_omp_thread_list thread_list;
  295. /* list of implicit omp tasks created to run the region */
  296. struct starpu_omp_task **implicit_task_array;
  297. /* include both the master thread and the region own threads */
  298. int nb_threads;
  299. struct _starpu_spinlock lock;
  300. struct starpu_omp_task *waiting_task;
  301. int barrier_count;
  302. int bound_explicit_task_count;
  303. int single_id;
  304. void *copy_private_data;
  305. int level;
  306. struct starpu_omp_loop *loop_list;
  307. struct starpu_omp_sections *sections_list;
  308. struct starpu_task *continuation_starpu_task;
  309. struct handle_entry *registered_handles;
  310. struct _starpu_spinlock registered_handles_lock;
  311. };
  312. struct starpu_omp_device
  313. {
  314. struct starpu_omp_device_icvs icvs;
  315. /* atomic fallback implementation lock */
  316. struct _starpu_spinlock atomic_lock;
  317. };
  318. struct starpu_omp_global
  319. {
  320. struct starpu_omp_global_icvs icvs;
  321. struct starpu_omp_task *initial_task;
  322. struct starpu_omp_thread *initial_thread;
  323. struct starpu_omp_region *initial_region;
  324. struct starpu_omp_device *initial_device;
  325. struct starpu_omp_critical *default_critical;
  326. struct starpu_omp_critical *named_criticals;
  327. struct _starpu_spinlock named_criticals_lock;
  328. struct starpu_omp_thread *hash_workers;
  329. struct _starpu_spinlock hash_workers_lock;
  330. struct starpu_arbiter *default_arbiter;
  331. unsigned nb_starpu_cpu_workers;
  332. int *starpu_cpu_worker_ids;
  333. };
  334. /*
  335. * internal global variables
  336. */
  337. extern struct starpu_omp_initial_icv_values *_starpu_omp_initial_icv_values;
  338. extern struct starpu_omp_global *_starpu_omp_global_state;
  339. extern double _starpu_omp_clock_ref;
  340. /*
  341. * internal API
  342. */
  343. void _starpu_omp_environment_init(void);
  344. void _starpu_omp_environment_exit(void);
  345. struct starpu_omp_thread *_starpu_omp_get_thread(void);
  346. struct starpu_omp_region *_starpu_omp_get_region_at_level(int level);
  347. struct starpu_omp_task *_starpu_omp_get_task(void);
  348. int _starpu_omp_get_region_thread_num(const struct starpu_omp_region *const region);
  349. void _starpu_omp_dummy_init(void);
  350. void _starpu_omp_dummy_shutdown(void);
  351. #endif // STARPU_OPENMP
  352. #endif // __OPENMP_RUNTIME_SUPPORT_H__