openmp_runtime_support.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. 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_condition = 1 << 5
  171. };
  172. enum starpu_omp_task_flags
  173. {
  174. STARPU_OMP_TASK_FLAGS_IMPLICIT = 1 << 0,
  175. STARPU_OMP_TASK_FLAGS_UNDEFERRED = 1 << 1,
  176. STARPU_OMP_TASK_FLAGS_FINAL = 1 << 2,
  177. STARPU_OMP_TASK_FLAGS_UNTIED = 1 << 3,
  178. };
  179. LIST_TYPE(starpu_omp_task,
  180. struct starpu_omp_implicit_task_icvs icvs;
  181. struct starpu_omp_task *parent_task;
  182. struct starpu_omp_thread *owner_thread;
  183. struct starpu_omp_region *owner_region;
  184. struct starpu_omp_region *nested_region;
  185. int rank;
  186. int child_task_count;
  187. struct starpu_omp_task_group *task_group;
  188. struct _starpu_spinlock lock;
  189. int transaction_pending;
  190. int wait_on;
  191. int barrier_count;
  192. int single_id;
  193. int single_first;
  194. int loop_id;
  195. unsigned long long ordered_first_i;
  196. unsigned long long ordered_nb_i;
  197. int sections_id;
  198. struct starpu_omp_data_environment_icvs data_env_icvs;
  199. struct starpu_omp_implicit_task_icvs implicit_task_icvs;
  200. struct handle_entry *registered_handles;
  201. struct starpu_task *starpu_task;
  202. struct starpu_codelet cl;
  203. void **starpu_buffers;
  204. void *starpu_cl_arg;
  205. /* actual task function to be run */
  206. void (*cpu_f)(void **starpu_buffers, void *starpu_cl_arg);
  207. #ifdef STARPU_USE_CUDA
  208. void (*cuda_f)(void **starpu_buffers, void *starpu_cl_arg);
  209. #endif
  210. #ifdef STARPU_USE_OPENCL
  211. void (*opencl_f)(void **starpu_buffers, void *starpu_cl_arg);
  212. #endif
  213. enum starpu_omp_task_state state;
  214. enum starpu_omp_task_flags flags;
  215. /*
  216. * context to store the processing state of the task
  217. * in case of blocking/recursive task operation
  218. */
  219. ucontext_t ctx;
  220. /*
  221. * stack to execute the task over, to be able to switch
  222. * in case blocking/recursive task operation
  223. */
  224. void *stack;
  225. /*
  226. * Valgrind stack id
  227. */
  228. int stack_vg_id;
  229. size_t stacksize;
  230. )
  231. LIST_TYPE(starpu_omp_thread,
  232. UT_hash_handle hh;
  233. struct starpu_omp_task *current_task;
  234. struct starpu_omp_region *owner_region;
  235. /*
  236. * stack to execute the initial thread over
  237. * when preempting the initial task
  238. * note: should not be used for other threads
  239. */
  240. void *initial_thread_stack;
  241. /*
  242. * Valgrind stack id
  243. */
  244. int initial_thread_stack_vg_id;
  245. /*
  246. * context to store the 'scheduler' state of the thread,
  247. * to which the execution of thread comes back upon a
  248. * blocking/recursive task operation
  249. */
  250. ucontext_t ctx;
  251. struct starpu_driver starpu_driver;
  252. struct _starpu_worker *worker;
  253. )
  254. struct _starpu_omp_lock_internal
  255. {
  256. struct _starpu_spinlock lock;
  257. struct starpu_omp_condition cond;
  258. unsigned state;
  259. };
  260. struct _starpu_omp_nest_lock_internal
  261. {
  262. struct _starpu_spinlock lock;
  263. struct starpu_omp_condition cond;
  264. unsigned state;
  265. struct starpu_omp_task *owner_task;
  266. unsigned nesting;
  267. };
  268. struct starpu_omp_loop
  269. {
  270. int id;
  271. unsigned long long next_iteration;
  272. int nb_completed_threads;
  273. struct starpu_omp_loop *next_loop;
  274. struct _starpu_spinlock ordered_lock;
  275. struct starpu_omp_condition ordered_cond;
  276. unsigned long long ordered_iteration;
  277. };
  278. struct starpu_omp_sections
  279. {
  280. int id;
  281. unsigned long long next_section_num;
  282. int nb_completed_threads;
  283. struct starpu_omp_sections *next_sections;
  284. };
  285. struct starpu_omp_region
  286. {
  287. struct starpu_omp_data_environment_icvs icvs;
  288. struct starpu_omp_region *parent_region;
  289. struct starpu_omp_device *owner_device;
  290. struct starpu_omp_thread *master_thread;
  291. /* note: the list of threads does not include the master_thread */
  292. struct starpu_omp_thread_list thread_list;
  293. /* list of implicit omp tasks created to run the region */
  294. struct starpu_omp_task_list implicit_task_list;
  295. /* include both the master thread and the region own threads */
  296. int nb_threads;
  297. struct _starpu_spinlock lock;
  298. struct starpu_omp_task *waiting_task;
  299. int barrier_count;
  300. int bound_explicit_task_count;
  301. int single_id;
  302. void *copy_private_data;
  303. int level;
  304. struct starpu_omp_loop *loop_list;
  305. struct starpu_omp_sections *sections_list;
  306. struct starpu_task *continuation_starpu_task;
  307. struct handle_entry *registered_handles;
  308. struct _starpu_spinlock registered_handles_lock;
  309. };
  310. struct starpu_omp_device
  311. {
  312. struct starpu_omp_device_icvs icvs;
  313. /* atomic fallback implementation lock */
  314. struct _starpu_spinlock atomic_lock;
  315. };
  316. struct starpu_omp_global
  317. {
  318. struct starpu_omp_global_icvs icvs;
  319. struct starpu_omp_task *initial_task;
  320. struct starpu_omp_thread *initial_thread;
  321. struct starpu_omp_region *initial_region;
  322. struct starpu_omp_device *initial_device;
  323. struct starpu_omp_critical *default_critical;
  324. struct starpu_omp_critical *named_criticals;
  325. struct _starpu_spinlock named_criticals_lock;
  326. struct starpu_omp_thread *hash_workers;
  327. struct _starpu_spinlock hash_workers_lock;
  328. struct starpu_arbiter *default_arbiter;
  329. int nb_starpu_cpu_workers;
  330. int *starpu_cpu_worker_ids;
  331. };
  332. /*
  333. * internal global variables
  334. */
  335. extern struct starpu_omp_initial_icv_values *_starpu_omp_initial_icv_values;
  336. extern struct starpu_omp_global *_starpu_omp_global_state;
  337. extern double _starpu_omp_clock_ref;
  338. /*
  339. * internal API
  340. */
  341. void _starpu_omp_environment_init(void);
  342. void _starpu_omp_environment_exit(void);
  343. struct starpu_omp_thread *_starpu_omp_get_thread(void);
  344. struct starpu_omp_task *_starpu_omp_get_task(void);
  345. void _starpu_omp_dummy_init(void);
  346. void _starpu_omp_dummy_shutdown(void);
  347. #endif // STARPU_OPENMP
  348. #endif // __OPENMP_RUNTIME_SUPPORT_H__