libgomp.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file contains data types and function declarations that are not
  21. part of the official OpenACC or OpenMP user interfaces. There are
  22. declarations in here that are part of the GNU Offloading and Multi
  23. Processing ABI, in that the compiler is required to know about them
  24. and use them.
  25. The convention is that the all caps prefix "GOMP" is used group items
  26. that are part of the external ABI, and the lower case prefix "gomp"
  27. is used group items that are completely private to the library. */
  28. #ifndef LIBGOMP_H
  29. #define LIBGOMP_H 1
  30. #include "config.h"
  31. #include "gstdint.h"
  32. #include "libgomp-plugin.h"
  33. #include <pthread.h>
  34. #include <stdbool.h>
  35. #include <stdlib.h>
  36. #include <stdarg.h>
  37. #ifdef HAVE_ATTRIBUTE_VISIBILITY
  38. # pragma GCC visibility push(hidden)
  39. #endif
  40. /* If we were a C++ library, we'd get this from <std/atomic>. */
  41. enum memmodel
  42. {
  43. MEMMODEL_RELAXED = 0,
  44. MEMMODEL_CONSUME = 1,
  45. MEMMODEL_ACQUIRE = 2,
  46. MEMMODEL_RELEASE = 3,
  47. MEMMODEL_ACQ_REL = 4,
  48. MEMMODEL_SEQ_CST = 5
  49. };
  50. #include "sem.h"
  51. #include "mutex.h"
  52. #include "bar.h"
  53. #include "ptrlock.h"
  54. /* This structure contains the data to control one work-sharing construct,
  55. either a LOOP (FOR/DO) or a SECTIONS. */
  56. enum gomp_schedule_type
  57. {
  58. GFS_RUNTIME,
  59. GFS_STATIC,
  60. GFS_DYNAMIC,
  61. GFS_GUIDED,
  62. GFS_AUTO
  63. };
  64. struct gomp_work_share
  65. {
  66. /* This member records the SCHEDULE clause to be used for this construct.
  67. The user specification of "runtime" will already have been resolved.
  68. If this is a SECTIONS construct, this value will always be DYNAMIC. */
  69. enum gomp_schedule_type sched;
  70. int mode;
  71. union {
  72. struct {
  73. /* This is the chunk_size argument to the SCHEDULE clause. */
  74. long chunk_size;
  75. /* This is the iteration end point. If this is a SECTIONS construct,
  76. this is the number of contained sections. */
  77. long end;
  78. /* This is the iteration step. If this is a SECTIONS construct, this
  79. is always 1. */
  80. long incr;
  81. };
  82. struct {
  83. /* The same as above, but for the unsigned long long loop variants. */
  84. unsigned long long chunk_size_ull;
  85. unsigned long long end_ull;
  86. unsigned long long incr_ull;
  87. };
  88. };
  89. /* This is a circular queue that details which threads will be allowed
  90. into the ordered region and in which order. When a thread allocates
  91. iterations on which it is going to work, it also registers itself at
  92. the end of the array. When a thread reaches the ordered region, it
  93. checks to see if it is the one at the head of the queue. If not, it
  94. blocks on its RELEASE semaphore. */
  95. unsigned *ordered_team_ids;
  96. /* This is the number of threads that have registered themselves in
  97. the circular queue ordered_team_ids. */
  98. unsigned ordered_num_used;
  99. /* This is the team_id of the currently acknowledged owner of the ordered
  100. section, or -1u if the ordered section has not been acknowledged by
  101. any thread. This is distinguished from the thread that is *allowed*
  102. to take the section next. */
  103. unsigned ordered_owner;
  104. /* This is the index into the circular queue ordered_team_ids of the
  105. current thread that's allowed into the ordered reason. */
  106. unsigned ordered_cur;
  107. /* This is a chain of allocated gomp_work_share blocks, valid only
  108. in the first gomp_work_share struct in the block. */
  109. struct gomp_work_share *next_alloc;
  110. /* The above fields are written once during workshare initialization,
  111. or related to ordered worksharing. Make sure the following fields
  112. are in a different cache line. */
  113. /* This lock protects the update of the following members. */
  114. gomp_mutex_t lock __attribute__((aligned (64)));
  115. /* This is the count of the number of threads that have exited the work
  116. share construct. If the construct was marked nowait, they have moved on
  117. to other work; otherwise they're blocked on a barrier. The last member
  118. of the team to exit the work share construct must deallocate it. */
  119. unsigned threads_completed;
  120. union {
  121. /* This is the next iteration value to be allocated. In the case of
  122. GFS_STATIC loops, this the iteration start point and never changes. */
  123. long next;
  124. /* The same, but with unsigned long long type. */
  125. unsigned long long next_ull;
  126. /* This is the returned data structure for SINGLE COPYPRIVATE. */
  127. void *copyprivate;
  128. };
  129. union {
  130. /* Link to gomp_work_share struct for next work sharing construct
  131. encountered after this one. */
  132. gomp_ptrlock_t next_ws;
  133. /* gomp_work_share structs are chained in the free work share cache
  134. through this. */
  135. struct gomp_work_share *next_free;
  136. };
  137. /* If only few threads are in the team, ordered_team_ids can point
  138. to this array which fills the padding at the end of this struct. */
  139. unsigned inline_ordered_team_ids[0];
  140. };
  141. /* This structure contains all of the thread-local data associated with
  142. a thread team. This is the data that must be saved when a thread
  143. encounters a nested PARALLEL construct. */
  144. struct gomp_team_state
  145. {
  146. /* This is the team of which the thread is currently a member. */
  147. struct gomp_team *team;
  148. /* This is the work share construct which this thread is currently
  149. processing. Recall that with NOWAIT, not all threads may be
  150. processing the same construct. */
  151. struct gomp_work_share *work_share;
  152. /* This is the previous work share construct or NULL if there wasn't any.
  153. When all threads are done with the current work sharing construct,
  154. the previous one can be freed. The current one can't, as its
  155. next_ws field is used. */
  156. struct gomp_work_share *last_work_share;
  157. /* This is the ID of this thread within the team. This value is
  158. guaranteed to be between 0 and N-1, where N is the number of
  159. threads in the team. */
  160. unsigned team_id;
  161. /* Nesting level. */
  162. unsigned level;
  163. /* Active nesting level. Only active parallel regions are counted. */
  164. unsigned active_level;
  165. /* Place-partition-var, offset and length into gomp_places_list array. */
  166. unsigned place_partition_off;
  167. unsigned place_partition_len;
  168. #ifdef HAVE_SYNC_BUILTINS
  169. /* Number of single stmts encountered. */
  170. unsigned long single_count;
  171. #endif
  172. /* For GFS_RUNTIME loops that resolved to GFS_STATIC, this is the
  173. trip number through the loop. So first time a particular loop
  174. is encountered this number is 0, the second time through the loop
  175. is 1, etc. This is unused when the compiler knows in advance that
  176. the loop is statically scheduled. */
  177. unsigned long static_trip;
  178. };
  179. struct target_mem_desc;
  180. /* These are the OpenMP 4.0 Internal Control Variables described in
  181. section 2.3.1. Those described as having one copy per task are
  182. stored within the structure; those described as having one copy
  183. for the whole program are (naturally) global variables. */
  184. struct gomp_task_icv
  185. {
  186. unsigned long nthreads_var;
  187. enum gomp_schedule_type run_sched_var;
  188. int run_sched_modifier;
  189. int default_device_var;
  190. unsigned int thread_limit_var;
  191. bool dyn_var;
  192. bool nest_var;
  193. char bind_var;
  194. /* Internal ICV. */
  195. struct target_mem_desc *target_data;
  196. };
  197. extern struct gomp_task_icv gomp_global_icv;
  198. #ifndef HAVE_SYNC_BUILTINS
  199. extern gomp_mutex_t gomp_managed_threads_lock;
  200. #endif
  201. extern unsigned long gomp_max_active_levels_var;
  202. extern bool gomp_cancel_var;
  203. extern unsigned long long gomp_spin_count_var, gomp_throttled_spin_count_var;
  204. extern unsigned long gomp_available_cpus, gomp_managed_threads;
  205. extern unsigned long *gomp_nthreads_var_list, gomp_nthreads_var_list_len;
  206. extern char *gomp_bind_var_list;
  207. extern unsigned long gomp_bind_var_list_len;
  208. extern void **gomp_places_list;
  209. extern unsigned long gomp_places_list_len;
  210. extern int gomp_debug_var;
  211. extern int goacc_device_num;
  212. extern char *goacc_device_type;
  213. enum gomp_task_kind
  214. {
  215. GOMP_TASK_IMPLICIT,
  216. GOMP_TASK_IFFALSE,
  217. GOMP_TASK_WAITING,
  218. GOMP_TASK_TIED
  219. };
  220. struct gomp_task;
  221. struct gomp_taskgroup;
  222. struct htab;
  223. struct gomp_task_depend_entry
  224. {
  225. void *addr;
  226. struct gomp_task_depend_entry *next;
  227. struct gomp_task_depend_entry *prev;
  228. struct gomp_task *task;
  229. bool is_in;
  230. bool redundant;
  231. bool redundant_out;
  232. };
  233. struct gomp_dependers_vec
  234. {
  235. size_t n_elem;
  236. size_t allocated;
  237. struct gomp_task *elem[];
  238. };
  239. /* Used when in GOMP_taskwait or in gomp_task_maybe_wait_for_dependencies. */
  240. struct gomp_taskwait
  241. {
  242. bool in_taskwait;
  243. bool in_depend_wait;
  244. size_t n_depend;
  245. struct gomp_task *last_parent_depends_on;
  246. gomp_sem_t taskwait_sem;
  247. };
  248. /* This structure describes a "task" to be run by a thread. */
  249. struct gomp_task
  250. {
  251. struct gomp_task *parent;
  252. struct gomp_task *children;
  253. struct gomp_task *next_child;
  254. struct gomp_task *prev_child;
  255. struct gomp_task *next_queue;
  256. struct gomp_task *prev_queue;
  257. struct gomp_task *next_taskgroup;
  258. struct gomp_task *prev_taskgroup;
  259. struct gomp_taskgroup *taskgroup;
  260. struct gomp_dependers_vec *dependers;
  261. struct htab *depend_hash;
  262. struct gomp_taskwait *taskwait;
  263. size_t depend_count;
  264. size_t num_dependees;
  265. struct gomp_task_icv icv;
  266. void (*fn) (void *);
  267. void *fn_data;
  268. enum gomp_task_kind kind;
  269. bool in_tied_task;
  270. bool final_task;
  271. bool copy_ctors_done;
  272. bool parent_depends_on;
  273. struct gomp_task_depend_entry depend[];
  274. };
  275. struct gomp_taskgroup
  276. {
  277. struct gomp_taskgroup *prev;
  278. struct gomp_task *children;
  279. bool in_taskgroup_wait;
  280. bool cancelled;
  281. gomp_sem_t taskgroup_sem;
  282. size_t num_children;
  283. };
  284. /* This structure describes a "team" of threads. These are the threads
  285. that are spawned by a PARALLEL constructs, as well as the work sharing
  286. constructs that the team encounters. */
  287. struct gomp_team
  288. {
  289. /* This is the number of threads in the current team. */
  290. unsigned nthreads;
  291. /* This is number of gomp_work_share structs that have been allocated
  292. as a block last time. */
  293. unsigned work_share_chunk;
  294. /* This is the saved team state that applied to a master thread before
  295. the current thread was created. */
  296. struct gomp_team_state prev_ts;
  297. /* This semaphore should be used by the master thread instead of its
  298. "native" semaphore in the thread structure. Required for nested
  299. parallels, as the master is a member of two teams. */
  300. gomp_sem_t master_release;
  301. /* This points to an array with pointers to the release semaphore
  302. of the threads in the team. */
  303. gomp_sem_t **ordered_release;
  304. /* List of work shares on which gomp_fini_work_share hasn't been
  305. called yet. If the team hasn't been cancelled, this should be
  306. equal to each thr->ts.work_share, but otherwise it can be a possibly
  307. long list of workshares. */
  308. struct gomp_work_share *work_shares_to_free;
  309. /* List of gomp_work_share structs chained through next_free fields.
  310. This is populated and taken off only by the first thread in the
  311. team encountering a new work sharing construct, in a critical
  312. section. */
  313. struct gomp_work_share *work_share_list_alloc;
  314. /* List of gomp_work_share structs freed by free_work_share. New
  315. entries are atomically added to the start of the list, and
  316. alloc_work_share can safely only move all but the first entry
  317. to work_share_list alloc, as free_work_share can happen concurrently
  318. with alloc_work_share. */
  319. struct gomp_work_share *work_share_list_free;
  320. #ifdef HAVE_SYNC_BUILTINS
  321. /* Number of simple single regions encountered by threads in this
  322. team. */
  323. unsigned long single_count;
  324. #else
  325. /* Mutex protecting addition of workshares to work_share_list_free. */
  326. gomp_mutex_t work_share_list_free_lock;
  327. #endif
  328. /* This barrier is used for most synchronization of the team. */
  329. gomp_barrier_t barrier;
  330. /* Initial work shares, to avoid allocating any gomp_work_share
  331. structs in the common case. */
  332. struct gomp_work_share work_shares[8];
  333. gomp_mutex_t task_lock;
  334. struct gomp_task *task_queue;
  335. /* Number of all GOMP_TASK_{WAITING,TIED} tasks in the team. */
  336. unsigned int task_count;
  337. /* Number of GOMP_TASK_WAITING tasks currently waiting to be scheduled. */
  338. unsigned int task_queued_count;
  339. /* Number of GOMP_TASK_{WAITING,TIED} tasks currently running
  340. directly in gomp_barrier_handle_tasks; tasks spawned
  341. from e.g. GOMP_taskwait or GOMP_taskgroup_end don't count, even when
  342. that is called from a task run from gomp_barrier_handle_tasks.
  343. task_running_count should be always <= team->nthreads,
  344. and if current task isn't in_tied_task, then it will be
  345. even < team->nthreads. */
  346. unsigned int task_running_count;
  347. int work_share_cancelled;
  348. int team_cancelled;
  349. /* This array contains structures for implicit tasks. */
  350. struct gomp_task implicit_task[];
  351. };
  352. /* This structure contains all data that is private to libgomp and is
  353. allocated per thread. */
  354. struct gomp_thread
  355. {
  356. /* This is the function that the thread should run upon launch. */
  357. void (*fn) (void *data);
  358. void *data;
  359. /* This is the current team state for this thread. The ts.team member
  360. is NULL only if the thread is idle. */
  361. struct gomp_team_state ts;
  362. /* This is the task that the thread is currently executing. */
  363. struct gomp_task *task;
  364. /* This semaphore is used for ordered loops. */
  365. gomp_sem_t release;
  366. /* Place this thread is bound to plus one, or zero if not bound
  367. to any place. */
  368. unsigned int place;
  369. /* User pthread thread pool */
  370. struct gomp_thread_pool *thread_pool;
  371. };
  372. struct gomp_thread_pool
  373. {
  374. /* This array manages threads spawned from the top level, which will
  375. return to the idle loop once the current PARALLEL construct ends. */
  376. struct gomp_thread **threads;
  377. unsigned threads_size;
  378. unsigned threads_used;
  379. struct gomp_team *last_team;
  380. /* Number of threads running in this contention group. */
  381. unsigned long threads_busy;
  382. /* This barrier holds and releases threads waiting in threads. */
  383. gomp_barrier_t threads_dock;
  384. };
  385. enum gomp_cancel_kind
  386. {
  387. GOMP_CANCEL_PARALLEL = 1,
  388. GOMP_CANCEL_LOOP = 2,
  389. GOMP_CANCEL_FOR = GOMP_CANCEL_LOOP,
  390. GOMP_CANCEL_DO = GOMP_CANCEL_LOOP,
  391. GOMP_CANCEL_SECTIONS = 4,
  392. GOMP_CANCEL_TASKGROUP = 8
  393. };
  394. /* ... and here is that TLS data. */
  395. #if defined HAVE_TLS || defined USE_EMUTLS
  396. extern __thread struct gomp_thread gomp_tls_data;
  397. static inline struct gomp_thread *gomp_thread (void)
  398. {
  399. return &gomp_tls_data;
  400. }
  401. #else
  402. extern pthread_key_t gomp_tls_key;
  403. static inline struct gomp_thread *gomp_thread (void)
  404. {
  405. return pthread_getspecific (gomp_tls_key);
  406. }
  407. #endif
  408. extern struct gomp_task_icv *gomp_new_icv (void);
  409. /* Here's how to access the current copy of the ICVs. */
  410. static inline struct gomp_task_icv *gomp_icv (bool write)
  411. {
  412. struct gomp_task *task = gomp_thread ()->task;
  413. if (task)
  414. return &task->icv;
  415. else if (write)
  416. return gomp_new_icv ();
  417. else
  418. return &gomp_global_icv;
  419. }
  420. /* The attributes to be used during thread creation. */
  421. extern pthread_attr_t gomp_thread_attr;
  422. /* Function prototypes. */
  423. /* affinity.c */
  424. extern void gomp_init_affinity (void);
  425. extern void gomp_init_thread_affinity (pthread_attr_t *, unsigned int);
  426. extern void **gomp_affinity_alloc (unsigned long, bool);
  427. extern void gomp_affinity_init_place (void *);
  428. extern bool gomp_affinity_add_cpus (void *, unsigned long, unsigned long,
  429. long, bool);
  430. extern bool gomp_affinity_remove_cpu (void *, unsigned long);
  431. extern bool gomp_affinity_copy_place (void *, void *, long);
  432. extern bool gomp_affinity_same_place (void *, void *);
  433. extern bool gomp_affinity_finalize_place_list (bool);
  434. extern bool gomp_affinity_init_level (int, unsigned long, bool);
  435. extern void gomp_affinity_print_place (void *);
  436. /* alloc.c */
  437. extern void *gomp_malloc (size_t) __attribute__((malloc));
  438. extern void *gomp_malloc_cleared (size_t) __attribute__((malloc));
  439. extern void *gomp_realloc (void *, size_t);
  440. /* Avoid conflicting prototypes of alloca() in system headers by using
  441. GCC's builtin alloca(). */
  442. #define gomp_alloca(x) __builtin_alloca(x)
  443. /* error.c */
  444. extern void gomp_vdebug (int, const char *, va_list);
  445. extern void gomp_debug (int, const char *, ...)
  446. __attribute__ ((format (printf, 2, 3)));
  447. #define gomp_vdebug(KIND, FMT, VALIST) \
  448. do { \
  449. if (__builtin_expect (gomp_debug_var, 0)) \
  450. (gomp_vdebug) ((KIND), (FMT), (VALIST)); \
  451. } while (0)
  452. #define gomp_debug(KIND, ...) \
  453. do { \
  454. if (__builtin_expect (gomp_debug_var, 0)) \
  455. (gomp_debug) ((KIND), __VA_ARGS__); \
  456. } while (0)
  457. extern void gomp_verror (const char *, va_list);
  458. extern void gomp_error (const char *, ...)
  459. __attribute__ ((format (printf, 1, 2)));
  460. extern void gomp_vfatal (const char *, va_list)
  461. __attribute__ ((noreturn));
  462. extern void gomp_fatal (const char *, ...)
  463. __attribute__ ((noreturn, format (printf, 1, 2)));
  464. /* iter.c */
  465. extern int gomp_iter_static_next (long *, long *);
  466. extern bool gomp_iter_dynamic_next_locked (long *, long *);
  467. extern bool gomp_iter_guided_next_locked (long *, long *);
  468. #ifdef HAVE_SYNC_BUILTINS
  469. extern bool gomp_iter_dynamic_next (long *, long *);
  470. extern bool gomp_iter_guided_next (long *, long *);
  471. #endif
  472. /* iter_ull.c */
  473. extern int gomp_iter_ull_static_next (unsigned long long *,
  474. unsigned long long *);
  475. extern bool gomp_iter_ull_dynamic_next_locked (unsigned long long *,
  476. unsigned long long *);
  477. extern bool gomp_iter_ull_guided_next_locked (unsigned long long *,
  478. unsigned long long *);
  479. #if defined HAVE_SYNC_BUILTINS && defined __LP64__
  480. extern bool gomp_iter_ull_dynamic_next (unsigned long long *,
  481. unsigned long long *);
  482. extern bool gomp_iter_ull_guided_next (unsigned long long *,
  483. unsigned long long *);
  484. #endif
  485. /* ordered.c */
  486. extern void gomp_ordered_first (void);
  487. extern void gomp_ordered_last (void);
  488. extern void gomp_ordered_next (void);
  489. extern void gomp_ordered_static_init (void);
  490. extern void gomp_ordered_static_next (void);
  491. extern void gomp_ordered_sync (void);
  492. /* parallel.c */
  493. extern unsigned gomp_resolve_num_threads (unsigned, unsigned);
  494. /* proc.c (in config/) */
  495. extern void gomp_init_num_threads (void);
  496. extern unsigned gomp_dynamic_max_threads (void);
  497. /* task.c */
  498. extern void gomp_init_task (struct gomp_task *, struct gomp_task *,
  499. struct gomp_task_icv *);
  500. extern void gomp_end_task (void);
  501. extern void gomp_barrier_handle_tasks (gomp_barrier_state_t);
  502. static void inline
  503. gomp_finish_task (struct gomp_task *task)
  504. {
  505. if (__builtin_expect (task->depend_hash != NULL, 0))
  506. free (task->depend_hash);
  507. }
  508. /* team.c */
  509. extern struct gomp_team *gomp_new_team (unsigned);
  510. extern void gomp_team_start (void (*) (void *), void *, unsigned,
  511. unsigned, struct gomp_team *);
  512. extern void gomp_team_end (void);
  513. extern void gomp_free_thread (void *);
  514. /* target.c */
  515. extern void gomp_init_targets_once (void);
  516. extern int gomp_get_num_devices (void);
  517. typedef struct splay_tree_node_s *splay_tree_node;
  518. typedef struct splay_tree_s *splay_tree;
  519. typedef struct splay_tree_key_s *splay_tree_key;
  520. struct target_mem_desc {
  521. /* Reference count. */
  522. uintptr_t refcount;
  523. /* All the splay nodes allocated together. */
  524. splay_tree_node array;
  525. /* Start of the target region. */
  526. uintptr_t tgt_start;
  527. /* End of the targer region. */
  528. uintptr_t tgt_end;
  529. /* Handle to free. */
  530. void *to_free;
  531. /* Previous target_mem_desc. */
  532. struct target_mem_desc *prev;
  533. /* Number of items in following list. */
  534. size_t list_count;
  535. /* Corresponding target device descriptor. */
  536. struct gomp_device_descr *device_descr;
  537. /* List of splay keys to remove (or decrease refcount)
  538. at the end of region. */
  539. splay_tree_key list[];
  540. };
  541. struct splay_tree_key_s {
  542. /* Address of the host object. */
  543. uintptr_t host_start;
  544. /* Address immediately after the host object. */
  545. uintptr_t host_end;
  546. /* Descriptor of the target memory. */
  547. struct target_mem_desc *tgt;
  548. /* Offset from tgt->tgt_start to the start of the target object. */
  549. uintptr_t tgt_offset;
  550. /* Reference count. */
  551. uintptr_t refcount;
  552. /* Asynchronous reference count. */
  553. uintptr_t async_refcount;
  554. /* True if data should be copied from device to host at the end. */
  555. bool copy_from;
  556. };
  557. #include "splay-tree.h"
  558. typedef struct acc_dispatch_t
  559. {
  560. /* This is a linked list of data mapped using the
  561. acc_map_data/acc_unmap_data or "acc enter data"/"acc exit data" pragmas.
  562. Unlike mapped_data in the goacc_thread struct, unmapping can
  563. happen out-of-order with respect to mapping. */
  564. /* This is guarded by the lock in the "outer" struct gomp_device_descr. */
  565. struct target_mem_desc *data_environ;
  566. /* Execute. */
  567. void (*exec_func) (void (*) (void *), size_t, void **, void **, size_t *,
  568. unsigned short *, int, int, int, int, void *);
  569. /* Async cleanup callback registration. */
  570. void (*register_async_cleanup_func) (void *);
  571. /* Asynchronous routines. */
  572. int (*async_test_func) (int);
  573. int (*async_test_all_func) (void);
  574. void (*async_wait_func) (int);
  575. void (*async_wait_async_func) (int, int);
  576. void (*async_wait_all_func) (void);
  577. void (*async_wait_all_async_func) (int);
  578. void (*async_set_async_func) (int);
  579. /* Create/destroy TLS data. */
  580. void *(*create_thread_data_func) (int);
  581. void (*destroy_thread_data_func) (void *);
  582. /* NVIDIA target specific routines. */
  583. struct {
  584. void *(*get_current_device_func) (void);
  585. void *(*get_current_context_func) (void);
  586. void *(*get_stream_func) (int);
  587. int (*set_stream_func) (int, void *);
  588. } cuda;
  589. } acc_dispatch_t;
  590. /* This structure describes accelerator device.
  591. It contains name of the corresponding libgomp plugin, function handlers for
  592. interaction with the device, ID-number of the device, and information about
  593. mapped memory. */
  594. struct gomp_device_descr
  595. {
  596. /* Immutable data, which is only set during initialization, and which is not
  597. guarded by the lock. */
  598. /* The name of the device. */
  599. const char *name;
  600. /* Capabilities of device (supports OpenACC, OpenMP). */
  601. unsigned int capabilities;
  602. /* This is the ID number of device among devices of the same type. */
  603. int target_id;
  604. /* This is the TYPE of device. */
  605. enum offload_target_type type;
  606. /* Function handlers. */
  607. const char *(*get_name_func) (void);
  608. unsigned int (*get_caps_func) (void);
  609. int (*get_type_func) (void);
  610. int (*get_num_devices_func) (void);
  611. void (*init_device_func) (int);
  612. void (*fini_device_func) (int);
  613. int (*load_image_func) (int, void *, struct addr_pair **);
  614. void (*unload_image_func) (int, void *);
  615. void *(*alloc_func) (int, size_t);
  616. void (*free_func) (int, void *);
  617. void *(*dev2host_func) (int, void *, const void *, size_t);
  618. void *(*host2dev_func) (int, void *, const void *, size_t);
  619. void (*run_func) (int, void *, void *);
  620. /* Splay tree containing information about mapped memory regions. */
  621. struct splay_tree_s mem_map;
  622. /* Mutex for the mutable data. */
  623. gomp_mutex_t lock;
  624. /* Set to true when device is initialized. */
  625. bool is_initialized;
  626. /* OpenACC-specific data and functions. */
  627. /* This is mutable because of its mutable data_environ and target_data
  628. members. */
  629. acc_dispatch_t openacc;
  630. };
  631. extern void gomp_acc_insert_pointer (size_t, void **, size_t *, void *);
  632. extern void gomp_acc_remove_pointer (void *, bool, int, int);
  633. extern struct target_mem_desc *gomp_map_vars (struct gomp_device_descr *,
  634. size_t, void **, void **,
  635. size_t *, void *, bool, bool);
  636. extern void gomp_copy_from_async (struct target_mem_desc *);
  637. extern void gomp_unmap_vars (struct target_mem_desc *, bool);
  638. extern void gomp_init_device (struct gomp_device_descr *);
  639. extern void gomp_free_memmap (struct splay_tree_s *);
  640. extern void gomp_fini_device (struct gomp_device_descr *);
  641. /* work.c */
  642. extern void gomp_init_work_share (struct gomp_work_share *, bool, unsigned);
  643. extern void gomp_fini_work_share (struct gomp_work_share *);
  644. extern bool gomp_work_share_start (bool);
  645. extern void gomp_work_share_end (void);
  646. extern bool gomp_work_share_end_cancel (void);
  647. extern void gomp_work_share_end_nowait (void);
  648. static inline void
  649. gomp_work_share_init_done (void)
  650. {
  651. struct gomp_thread *thr = gomp_thread ();
  652. if (__builtin_expect (thr->ts.last_work_share != NULL, 1))
  653. gomp_ptrlock_set (&thr->ts.last_work_share->next_ws, thr->ts.work_share);
  654. }
  655. #ifdef HAVE_ATTRIBUTE_VISIBILITY
  656. # pragma GCC visibility pop
  657. #endif
  658. /* Now that we're back to default visibility, include the globals. */
  659. #include "libgomp_g.h"
  660. /* Include omp.h by parts. */
  661. #include "omp-lock.h"
  662. #define _LIBGOMP_OMP_LOCK_DEFINED 1
  663. #include "omp.h.in"
  664. #if !defined (HAVE_ATTRIBUTE_VISIBILITY) \
  665. || !defined (HAVE_ATTRIBUTE_ALIAS) \
  666. || !defined (HAVE_AS_SYMVER_DIRECTIVE) \
  667. || !defined (PIC) \
  668. || !defined (HAVE_SYMVER_SYMBOL_RENAMING_RUNTIME_SUPPORT)
  669. # undef LIBGOMP_GNU_SYMBOL_VERSIONING
  670. #endif
  671. #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
  672. extern void gomp_init_lock_30 (omp_lock_t *) __GOMP_NOTHROW;
  673. extern void gomp_destroy_lock_30 (omp_lock_t *) __GOMP_NOTHROW;
  674. extern void gomp_set_lock_30 (omp_lock_t *) __GOMP_NOTHROW;
  675. extern void gomp_unset_lock_30 (omp_lock_t *) __GOMP_NOTHROW;
  676. extern int gomp_test_lock_30 (omp_lock_t *) __GOMP_NOTHROW;
  677. extern void gomp_init_nest_lock_30 (omp_nest_lock_t *) __GOMP_NOTHROW;
  678. extern void gomp_destroy_nest_lock_30 (omp_nest_lock_t *) __GOMP_NOTHROW;
  679. extern void gomp_set_nest_lock_30 (omp_nest_lock_t *) __GOMP_NOTHROW;
  680. extern void gomp_unset_nest_lock_30 (omp_nest_lock_t *) __GOMP_NOTHROW;
  681. extern int gomp_test_nest_lock_30 (omp_nest_lock_t *) __GOMP_NOTHROW;
  682. extern void gomp_init_lock_25 (omp_lock_25_t *) __GOMP_NOTHROW;
  683. extern void gomp_destroy_lock_25 (omp_lock_25_t *) __GOMP_NOTHROW;
  684. extern void gomp_set_lock_25 (omp_lock_25_t *) __GOMP_NOTHROW;
  685. extern void gomp_unset_lock_25 (omp_lock_25_t *) __GOMP_NOTHROW;
  686. extern int gomp_test_lock_25 (omp_lock_25_t *) __GOMP_NOTHROW;
  687. extern void gomp_init_nest_lock_25 (omp_nest_lock_25_t *) __GOMP_NOTHROW;
  688. extern void gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *) __GOMP_NOTHROW;
  689. extern void gomp_set_nest_lock_25 (omp_nest_lock_25_t *) __GOMP_NOTHROW;
  690. extern void gomp_unset_nest_lock_25 (omp_nest_lock_25_t *) __GOMP_NOTHROW;
  691. extern int gomp_test_nest_lock_25 (omp_nest_lock_25_t *) __GOMP_NOTHROW;
  692. # define strong_alias(fn, al) \
  693. extern __typeof (fn) al __attribute__ ((alias (#fn)));
  694. # define omp_lock_symver(fn) \
  695. __asm (".symver g" #fn "_30, " #fn "@@OMP_3.0"); \
  696. __asm (".symver g" #fn "_25, " #fn "@OMP_1.0");
  697. #else
  698. # define gomp_init_lock_30 omp_init_lock
  699. # define gomp_destroy_lock_30 omp_destroy_lock
  700. # define gomp_set_lock_30 omp_set_lock
  701. # define gomp_unset_lock_30 omp_unset_lock
  702. # define gomp_test_lock_30 omp_test_lock
  703. # define gomp_init_nest_lock_30 omp_init_nest_lock
  704. # define gomp_destroy_nest_lock_30 omp_destroy_nest_lock
  705. # define gomp_set_nest_lock_30 omp_set_nest_lock
  706. # define gomp_unset_nest_lock_30 omp_unset_nest_lock
  707. # define gomp_test_nest_lock_30 omp_test_nest_lock
  708. #endif
  709. #ifdef HAVE_ATTRIBUTE_VISIBILITY
  710. # define attribute_hidden __attribute__ ((visibility ("hidden")))
  711. #else
  712. # define attribute_hidden
  713. #endif
  714. #ifdef HAVE_ATTRIBUTE_ALIAS
  715. # define ialias_ulp ialias_str1(__USER_LABEL_PREFIX__)
  716. # define ialias_str1(x) ialias_str2(x)
  717. # define ialias_str2(x) #x
  718. # define ialias(fn) \
  719. extern __typeof (fn) gomp_ialias_##fn \
  720. __attribute__ ((alias (#fn))) attribute_hidden;
  721. # define ialias_redirect(fn) \
  722. extern __typeof (fn) fn __asm__ (ialias_ulp "gomp_ialias_" #fn) attribute_hidden;
  723. # define ialias_call(fn) gomp_ialias_ ## fn
  724. #else
  725. # define ialias(fn)
  726. # define ialias_redirect(fn)
  727. # define ialias_call(fn) fn
  728. #endif
  729. #endif /* LIBGOMP_H */