openmp_runtime_support.h 10 KB

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