[PATCH, GCC/ARM, 9/10] Call nscall function with blxns
[official-gcc.git] / libgomp / oacc-init.c
blob89a30b3e716db201a1fe2186a34e4dee9abf1714
1 /* OpenACC Runtime initialization routines
3 Copyright (C) 2013-2020 Free Software Foundation, Inc.
5 Contributed by Mentor Embedded.
7 This file is part of the GNU Offloading and Multi Processing Library
8 (libgomp).
10 Libgomp is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 more details.
20 Under Section 7 of GPL version 3, you are granted additional
21 permissions described in the GCC Runtime Library Exception, version
22 3.1, as published by the Free Software Foundation.
24 You should have received a copy of the GNU General Public License and
25 a copy of the GCC Runtime Library Exception along with this program;
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
27 <http://www.gnu.org/licenses/>. */
29 #include "libgomp.h"
30 #include "oacc-int.h"
31 #include "openacc.h"
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <stdbool.h>
36 #include <string.h>
38 /* This lock is used to protect access to cached_base_dev, dispatchers and
39 the (abstract) initialisation state of attached offloading devices. */
41 static gomp_mutex_t acc_device_lock;
43 /* A cached version of the dispatcher for the global "current" accelerator type,
44 e.g. used as the default when creating new host threads. This is the
45 device-type equivalent of goacc_device_num (which specifies which device to
46 use out of potentially several of the same type). If there are several
47 devices of a given type, this points at the first one. */
49 static struct gomp_device_descr *cached_base_dev = NULL;
51 #if defined HAVE_TLS || defined USE_EMUTLS
52 __thread struct goacc_thread *goacc_tls_data;
53 #else
54 pthread_key_t goacc_tls_key;
55 #endif
56 static pthread_key_t goacc_cleanup_key;
58 static struct goacc_thread *goacc_threads;
59 static gomp_mutex_t goacc_thread_lock;
61 /* An array of dispatchers for device types, indexed by the type. This array
62 only references "base" devices, and other instances of the same type are
63 found by simply indexing from each such device (which are stored linearly,
64 grouped by device in target.c:devices). */
65 static struct gomp_device_descr *dispatchers[_ACC_device_hwm] = { 0 };
67 attribute_hidden void
68 goacc_register (struct gomp_device_descr *disp)
70 /* Only register the 0th device here. */
71 if (disp->target_id != 0)
72 return;
74 gomp_mutex_lock (&acc_device_lock);
76 assert (acc_device_type (disp->type) != acc_device_none
77 && acc_device_type (disp->type) != acc_device_default
78 && acc_device_type (disp->type) != acc_device_not_host);
79 assert (!dispatchers[disp->type]);
80 dispatchers[disp->type] = disp;
82 gomp_mutex_unlock (&acc_device_lock);
85 static bool
86 known_device_type_p (acc_device_t d)
88 return d >= 0 && d < _ACC_device_hwm;
91 static void
92 unknown_device_type_error (acc_device_t invalid_type)
94 gomp_fatal ("unknown device type %u", invalid_type);
97 /* OpenACC names some things a little differently. */
99 static const char *
100 get_openacc_name (const char *name)
102 if (strcmp (name, "nvptx") == 0)
103 return "nvidia";
104 else
105 return name;
108 static const char *
109 name_of_acc_device_t (enum acc_device_t type)
111 switch (type)
113 case acc_device_none: return "none";
114 case acc_device_default: return "default";
115 case acc_device_host: return "host";
116 case acc_device_not_host: return "not_host";
117 case acc_device_nvidia: return "nvidia";
118 default: unknown_device_type_error (type);
120 __builtin_unreachable ();
123 /* ACC_DEVICE_LOCK must be held before calling this function. If FAIL_IS_ERROR
124 is true, this function raises an error if there are no devices of type D,
125 otherwise it returns NULL in that case. */
127 static struct gomp_device_descr *
128 resolve_device (acc_device_t d, bool fail_is_error)
130 acc_device_t d_arg = d;
132 switch (d)
134 case acc_device_default:
136 if (goacc_device_type)
138 /* Lookup the named device. */
139 while (known_device_type_p (++d))
140 if (dispatchers[d]
141 && !strcasecmp (goacc_device_type,
142 get_openacc_name (dispatchers[d]->name))
143 && dispatchers[d]->get_num_devices_func () > 0)
144 goto found;
146 if (fail_is_error)
148 gomp_mutex_unlock (&acc_device_lock);
149 gomp_fatal ("device type %s not supported", goacc_device_type);
151 else
152 return NULL;
155 /* No default device specified, so start scanning for any non-host
156 device that is available. */
157 d = acc_device_not_host;
159 /* FALLTHROUGH */
161 case acc_device_not_host:
162 /* Find the first available device after acc_device_not_host. */
163 while (known_device_type_p (++d))
164 if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0)
165 goto found;
166 if (d_arg == acc_device_default)
168 d = acc_device_host;
169 goto found;
171 if (fail_is_error)
173 gomp_mutex_unlock (&acc_device_lock);
174 gomp_fatal ("no device found");
176 else
177 return NULL;
178 break;
180 case acc_device_host:
181 break;
183 default:
184 if (!known_device_type_p (d))
186 if (fail_is_error)
187 goto unsupported_device;
188 else
189 return NULL;
191 break;
193 found:
195 assert (d != acc_device_none
196 && d != acc_device_default
197 && d != acc_device_not_host);
199 if (dispatchers[d] == NULL && fail_is_error)
201 unsupported_device:
202 gomp_mutex_unlock (&acc_device_lock);
203 gomp_fatal ("device type %s not supported", name_of_acc_device_t (d));
206 return dispatchers[d];
209 /* Emit a suitable error if no device of a particular type is available, or
210 the given device number is out-of-range. */
211 static void
212 acc_dev_num_out_of_range (acc_device_t d, int ord, int ndevs)
214 if (ndevs == 0)
215 gomp_fatal ("no devices of type %s available", name_of_acc_device_t (d));
216 else
217 gomp_fatal ("device %u out of range", ord);
220 /* This is called when plugins have been initialized, and serves to call
221 (indirectly) the target's device_init hook. Calling multiple times without
222 an intervening acc_shutdown_1 call is an error. ACC_DEVICE_LOCK must be
223 held before calling this function. */
225 static struct gomp_device_descr *
226 acc_init_1 (acc_device_t d, acc_construct_t parent_construct, int implicit)
228 bool check_not_nested_p;
229 if (implicit)
231 /* In the implicit case, there should (TODO: must?) already be something
232 have been set up for an outer construct. */
233 check_not_nested_p = false;
235 else
237 check_not_nested_p = true;
238 /* TODO: should we set 'thr->prof_info' etc. in this case ('acc_init')?
239 The problem is, that we don't have 'thr' yet? (So,
240 'check_not_nested_p = true' also is pointless actually.) */
242 bool profiling_p = GOACC_PROFILING_DISPATCH_P (check_not_nested_p);
244 acc_prof_info prof_info;
245 if (profiling_p)
247 prof_info.event_type = acc_ev_device_init_start;
248 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
249 prof_info.version = _ACC_PROF_INFO_VERSION;
250 prof_info.device_type = d;
251 prof_info.device_number = goacc_device_num;
252 prof_info.thread_id = -1;
253 prof_info.async = acc_async_sync;
254 prof_info.async_queue = prof_info.async;
255 prof_info.src_file = NULL;
256 prof_info.func_name = NULL;
257 prof_info.line_no = -1;
258 prof_info.end_line_no = -1;
259 prof_info.func_line_no = -1;
260 prof_info.func_end_line_no = -1;
262 acc_event_info device_init_event_info;
263 if (profiling_p)
265 device_init_event_info.other_event.event_type = prof_info.event_type;
266 device_init_event_info.other_event.valid_bytes
267 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
268 device_init_event_info.other_event.parent_construct = parent_construct;
269 device_init_event_info.other_event.implicit = implicit;
270 device_init_event_info.other_event.tool_info = NULL;
272 acc_api_info api_info;
273 if (profiling_p)
275 api_info.device_api = acc_device_api_none;
276 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
277 api_info.device_type = prof_info.device_type;
278 api_info.vendor = -1;
279 api_info.device_handle = NULL;
280 api_info.context_handle = NULL;
281 api_info.async_handle = NULL;
284 if (profiling_p)
285 goacc_profiling_dispatch (&prof_info, &device_init_event_info, &api_info);
287 struct gomp_device_descr *base_dev, *acc_dev;
288 int ndevs;
290 base_dev = resolve_device (d, true);
292 ndevs = base_dev->get_num_devices_func ();
294 if (ndevs <= 0 || goacc_device_num >= ndevs)
295 acc_dev_num_out_of_range (d, goacc_device_num, ndevs);
297 acc_dev = &base_dev[goacc_device_num];
299 gomp_mutex_lock (&acc_dev->lock);
300 if (acc_dev->state == GOMP_DEVICE_INITIALIZED)
302 gomp_mutex_unlock (&acc_dev->lock);
303 gomp_fatal ("device already active");
306 gomp_init_device (acc_dev);
307 gomp_mutex_unlock (&acc_dev->lock);
309 if (profiling_p)
311 prof_info.event_type = acc_ev_device_init_end;
312 device_init_event_info.other_event.event_type = prof_info.event_type;
313 goacc_profiling_dispatch (&prof_info, &device_init_event_info,
314 &api_info);
317 return base_dev;
320 /* ACC_DEVICE_LOCK must be held before calling this function. */
322 static void
323 acc_shutdown_1 (acc_device_t d)
325 struct gomp_device_descr *base_dev;
326 struct goacc_thread *walk;
327 int ndevs, i;
328 bool devices_active = false;
330 /* Get the base device for this device type. */
331 base_dev = resolve_device (d, true);
333 ndevs = base_dev->get_num_devices_func ();
335 /* Unload all the devices of this type that have been opened. */
336 for (i = 0; i < ndevs; i++)
338 struct gomp_device_descr *acc_dev = &base_dev[i];
340 gomp_mutex_lock (&acc_dev->lock);
341 gomp_unload_device (acc_dev);
342 gomp_mutex_unlock (&acc_dev->lock);
345 gomp_mutex_lock (&goacc_thread_lock);
347 /* Free target-specific TLS data and close all devices. */
348 for (walk = goacc_threads; walk != NULL; walk = walk->next)
350 if (walk->target_tls)
351 base_dev->openacc.destroy_thread_data_func (walk->target_tls);
353 walk->target_tls = NULL;
355 /* This would mean the user is shutting down OpenACC in the middle of an
356 "acc data" pragma. Likely not intentional. */
357 if (walk->mapped_data)
359 gomp_mutex_unlock (&goacc_thread_lock);
360 gomp_fatal ("shutdown in 'acc data' region");
363 /* Similarly, if this happens then user code has done something weird. */
364 if (walk->saved_bound_dev)
366 gomp_mutex_unlock (&goacc_thread_lock);
367 gomp_fatal ("shutdown during host fallback");
370 if (walk->dev)
372 gomp_mutex_lock (&walk->dev->lock);
374 while (walk->dev->mem_map.root)
376 splay_tree_key k = &walk->dev->mem_map.root->key;
377 if (k->aux)
378 k->aux->link_key = NULL;
379 gomp_remove_var (walk->dev, k);
382 gomp_mutex_unlock (&walk->dev->lock);
384 walk->dev = NULL;
385 walk->base_dev = NULL;
389 gomp_mutex_unlock (&goacc_thread_lock);
391 /* Close all the devices of this type that have been opened. */
392 bool ret = true;
393 for (i = 0; i < ndevs; i++)
395 struct gomp_device_descr *acc_dev = &base_dev[i];
396 gomp_mutex_lock (&acc_dev->lock);
397 if (acc_dev->state == GOMP_DEVICE_INITIALIZED)
399 devices_active = true;
400 ret &= gomp_fini_device (acc_dev);
401 acc_dev->state = GOMP_DEVICE_UNINITIALIZED;
403 gomp_mutex_unlock (&acc_dev->lock);
406 if (!ret)
407 gomp_fatal ("device finalization failed");
409 if (!devices_active)
410 gomp_fatal ("no device initialized");
413 static struct goacc_thread *
414 goacc_new_thread (void)
416 struct goacc_thread *thr = gomp_malloc (sizeof (struct goacc_thread));
418 #if defined HAVE_TLS || defined USE_EMUTLS
419 goacc_tls_data = thr;
420 #else
421 pthread_setspecific (goacc_tls_key, thr);
422 #endif
424 pthread_setspecific (goacc_cleanup_key, thr);
426 gomp_mutex_lock (&goacc_thread_lock);
427 thr->next = goacc_threads;
428 goacc_threads = thr;
429 gomp_mutex_unlock (&goacc_thread_lock);
431 return thr;
434 static void
435 goacc_destroy_thread (void *data)
437 struct goacc_thread *thr = data, *walk, *prev;
439 gomp_mutex_lock (&goacc_thread_lock);
441 if (thr)
443 struct gomp_device_descr *acc_dev = thr->dev;
445 if (acc_dev && thr->target_tls)
447 acc_dev->openacc.destroy_thread_data_func (thr->target_tls);
448 thr->target_tls = NULL;
451 assert (!thr->mapped_data);
453 /* Remove from thread list. */
454 for (prev = NULL, walk = goacc_threads; walk;
455 prev = walk, walk = walk->next)
456 if (walk == thr)
458 if (prev == NULL)
459 goacc_threads = walk->next;
460 else
461 prev->next = walk->next;
463 free (thr);
465 break;
468 assert (walk);
471 gomp_mutex_unlock (&goacc_thread_lock);
474 /* Use the ORD'th device instance for the current host thread (or -1 for the
475 current global default). The device (and the runtime) must be initialised
476 before calling this function. */
478 void
479 goacc_attach_host_thread_to_device (int ord)
481 struct goacc_thread *thr = goacc_thread ();
482 struct gomp_device_descr *acc_dev = NULL, *base_dev = NULL;
483 int num_devices;
485 if (thr && thr->dev && (thr->dev->target_id == ord || ord < 0))
486 return;
488 if (ord < 0)
489 ord = goacc_device_num;
491 /* Decide which type of device to use. If the current thread has a device
492 type already (e.g. set by acc_set_device_type), use that, else use the
493 global default. */
494 if (thr && thr->base_dev)
495 base_dev = thr->base_dev;
496 else
498 assert (cached_base_dev);
499 base_dev = cached_base_dev;
502 num_devices = base_dev->get_num_devices_func ();
503 if (num_devices <= 0 || ord >= num_devices)
504 acc_dev_num_out_of_range (acc_device_type (base_dev->type), ord,
505 num_devices);
507 if (!thr)
508 thr = goacc_new_thread ();
510 thr->base_dev = base_dev;
511 thr->dev = acc_dev = &base_dev[ord];
512 thr->saved_bound_dev = NULL;
513 thr->mapped_data = NULL;
514 thr->prof_info = NULL;
515 thr->api_info = NULL;
516 /* Initially, all callbacks for all events are enabled. */
517 thr->prof_callbacks_enabled = true;
519 thr->target_tls
520 = acc_dev->openacc.create_thread_data_func (ord);
523 /* OpenACC 2.0a (3.2.12, 3.2.13) doesn't specify whether the serialization of
524 init/shutdown is per-process or per-thread. We choose per-process. */
526 void
527 acc_init (acc_device_t d)
529 if (!known_device_type_p (d))
530 unknown_device_type_error (d);
532 gomp_init_targets_once ();
534 gomp_mutex_lock (&acc_device_lock);
535 cached_base_dev = acc_init_1 (d, acc_construct_runtime_api, 0);
536 gomp_mutex_unlock (&acc_device_lock);
538 goacc_attach_host_thread_to_device (-1);
541 ialias (acc_init)
543 void
544 acc_shutdown (acc_device_t d)
546 if (!known_device_type_p (d))
547 unknown_device_type_error (d);
549 gomp_init_targets_once ();
551 gomp_mutex_lock (&acc_device_lock);
553 acc_shutdown_1 (d);
555 gomp_mutex_unlock (&acc_device_lock);
558 ialias (acc_shutdown)
561 acc_get_num_devices (acc_device_t d)
563 if (!known_device_type_p (d))
564 unknown_device_type_error (d);
566 int n = 0;
567 struct gomp_device_descr *acc_dev;
569 if (d == acc_device_none)
570 return 0;
572 gomp_init_targets_once ();
574 gomp_mutex_lock (&acc_device_lock);
575 acc_dev = resolve_device (d, false);
576 gomp_mutex_unlock (&acc_device_lock);
578 if (!acc_dev)
579 return 0;
581 n = acc_dev->get_num_devices_func ();
582 if (n < 0)
583 n = 0;
585 return n;
588 ialias (acc_get_num_devices)
590 /* Set the device type for the current thread only (using the current global
591 default device number), initialising that device if necessary. Also set the
592 default device type for new threads to D. */
594 void
595 acc_set_device_type (acc_device_t d)
597 if (!known_device_type_p (d))
598 unknown_device_type_error (d);
600 struct gomp_device_descr *base_dev, *acc_dev;
601 struct goacc_thread *thr = goacc_thread ();
603 acc_prof_info prof_info;
604 acc_api_info api_info;
605 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
606 if (profiling_p)
607 prof_info.device_type = d;
609 gomp_init_targets_once ();
611 gomp_mutex_lock (&acc_device_lock);
613 cached_base_dev = base_dev = resolve_device (d, true);
614 acc_dev = &base_dev[goacc_device_num];
616 gomp_mutex_lock (&acc_dev->lock);
617 if (acc_dev->state == GOMP_DEVICE_UNINITIALIZED)
618 gomp_init_device (acc_dev);
619 gomp_mutex_unlock (&acc_dev->lock);
621 gomp_mutex_unlock (&acc_device_lock);
623 /* We're changing device type: invalidate the current thread's dev and
624 base_dev pointers. */
625 if (thr && thr->base_dev != base_dev)
627 thr->base_dev = thr->dev = NULL;
628 if (thr->mapped_data)
629 gomp_fatal ("acc_set_device_type in 'acc data' region");
632 goacc_attach_host_thread_to_device (-1);
634 if (profiling_p)
636 thr->prof_info = NULL;
637 thr->api_info = NULL;
641 ialias (acc_set_device_type)
643 acc_device_t
644 acc_get_device_type (void)
646 acc_device_t res = acc_device_none;
647 struct gomp_device_descr *dev;
648 struct goacc_thread *thr = goacc_thread ();
650 if (thr && thr->base_dev)
651 res = acc_device_type (thr->base_dev->type);
652 else
654 acc_prof_info prof_info;
655 acc_api_info api_info;
656 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
658 gomp_init_targets_once ();
660 gomp_mutex_lock (&acc_device_lock);
661 dev = resolve_device (acc_device_default, true);
662 gomp_mutex_unlock (&acc_device_lock);
663 res = acc_device_type (dev->type);
665 if (profiling_p)
667 thr->prof_info = NULL;
668 thr->api_info = NULL;
672 assert (res != acc_device_default
673 && res != acc_device_not_host
674 && res != acc_device_current);
676 return res;
679 ialias (acc_get_device_type)
682 acc_get_device_num (acc_device_t d)
684 if (!known_device_type_p (d))
685 unknown_device_type_error (d);
687 const struct gomp_device_descr *dev;
688 struct goacc_thread *thr = goacc_thread ();
690 acc_prof_info prof_info;
691 acc_api_info api_info;
692 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
693 if (profiling_p)
694 prof_info.device_type = d;
696 gomp_init_targets_once ();
698 gomp_mutex_lock (&acc_device_lock);
699 dev = resolve_device (d, true);
700 gomp_mutex_unlock (&acc_device_lock);
702 if (profiling_p)
704 thr->prof_info = NULL;
705 thr->api_info = NULL;
708 if (thr && thr->base_dev == dev && thr->dev)
709 return thr->dev->target_id;
711 return goacc_device_num;
714 ialias (acc_get_device_num)
716 void
717 acc_set_device_num (int ord, acc_device_t d)
719 if (!known_device_type_p (d))
720 unknown_device_type_error (d);
722 struct gomp_device_descr *base_dev, *acc_dev;
723 int num_devices;
725 gomp_init_targets_once ();
727 if (ord < 0)
728 ord = goacc_device_num;
730 if ((int) d == 0)
731 /* Set whatever device is being used by the current host thread to use
732 device instance ORD. It's unclear if this is supposed to affect other
733 host threads too (OpenACC 2.0 (3.2.4) acc_set_device_num). */
734 goacc_attach_host_thread_to_device (ord);
735 else
737 gomp_mutex_lock (&acc_device_lock);
739 cached_base_dev = base_dev = resolve_device (d, true);
741 num_devices = base_dev->get_num_devices_func ();
743 if (num_devices <= 0 || ord >= num_devices)
744 acc_dev_num_out_of_range (d, ord, num_devices);
746 acc_dev = &base_dev[ord];
748 gomp_mutex_lock (&acc_dev->lock);
749 if (acc_dev->state == GOMP_DEVICE_UNINITIALIZED)
750 gomp_init_device (acc_dev);
751 gomp_mutex_unlock (&acc_dev->lock);
753 gomp_mutex_unlock (&acc_device_lock);
755 goacc_attach_host_thread_to_device (ord);
758 goacc_device_num = ord;
761 ialias (acc_set_device_num)
763 static union goacc_property_value
764 get_property_any (int ord, acc_device_t d, acc_device_property_t prop)
766 goacc_lazy_initialize ();
767 struct goacc_thread *thr = goacc_thread ();
769 if (d == acc_device_current && thr && thr->dev)
770 return thr->dev->openacc.get_property_func (thr->dev->target_id, prop);
772 gomp_mutex_lock (&acc_device_lock);
774 struct gomp_device_descr *dev = resolve_device (d, true);
776 int num_devices = dev->get_num_devices_func ();
778 if (num_devices <= 0 || ord >= num_devices)
779 acc_dev_num_out_of_range (d, ord, num_devices);
781 dev += ord;
783 gomp_mutex_lock (&dev->lock);
784 if (dev->state == GOMP_DEVICE_UNINITIALIZED)
785 gomp_init_device (dev);
786 gomp_mutex_unlock (&dev->lock);
788 gomp_mutex_unlock (&acc_device_lock);
790 assert (dev);
792 return dev->openacc.get_property_func (dev->target_id, prop);
795 size_t
796 acc_get_property (int ord, acc_device_t d, acc_device_property_t prop)
798 if (!known_device_type_p (d))
799 unknown_device_type_error(d);
801 if (prop & GOACC_PROPERTY_STRING_MASK)
802 return 0;
803 else
804 return get_property_any (ord, d, prop).val;
807 ialias (acc_get_property)
809 const char *
810 acc_get_property_string (int ord, acc_device_t d, acc_device_property_t prop)
812 if (!known_device_type_p (d))
813 unknown_device_type_error(d);
815 if (prop & GOACC_PROPERTY_STRING_MASK)
816 return get_property_any (ord, d, prop).ptr;
817 else
818 return NULL;
821 ialias (acc_get_property_string)
823 /* For -O and higher, the compiler always attempts to expand acc_on_device, but
824 if the user disables the builtin, or calls it via a pointer, we'll need this
825 version.
827 Compile this with optimization, so that the compiler expands
828 this, rather than generating infinitely recursive code.
830 The function just forwards its argument to __builtin_acc_on_device. It does
831 not verify that the argument is a valid acc_device_t enumeration value. */
833 int __attribute__ ((__optimize__ ("O2")))
834 acc_on_device (acc_device_t dev)
836 return __builtin_acc_on_device (dev);
839 ialias (acc_on_device)
841 attribute_hidden void
842 goacc_runtime_initialize (void)
844 gomp_mutex_init (&acc_device_lock);
846 #if !(defined HAVE_TLS || defined USE_EMUTLS)
847 pthread_key_create (&goacc_tls_key, NULL);
848 #endif
850 pthread_key_create (&goacc_cleanup_key, goacc_destroy_thread);
852 cached_base_dev = NULL;
854 goacc_threads = NULL;
855 gomp_mutex_init (&goacc_thread_lock);
857 /* Initialize and register the 'host' device type. */
858 goacc_host_init ();
861 static void __attribute__((destructor))
862 goacc_runtime_deinitialize (void)
864 #if !(defined HAVE_TLS || defined USE_EMUTLS)
865 pthread_key_delete (goacc_tls_key);
866 #endif
867 pthread_key_delete (goacc_cleanup_key);
870 /* Compiler helper functions */
872 attribute_hidden void
873 goacc_save_and_set_bind (acc_device_t d)
875 struct goacc_thread *thr = goacc_thread ();
877 assert (!thr->saved_bound_dev);
879 thr->saved_bound_dev = thr->dev;
880 thr->dev = dispatchers[d];
883 attribute_hidden void
884 goacc_restore_bind (void)
886 struct goacc_thread *thr = goacc_thread ();
888 thr->dev = thr->saved_bound_dev;
889 thr->saved_bound_dev = NULL;
892 /* This is called from any OpenACC support function that may need to implicitly
893 initialize the libgomp runtime, either globally or from a new host thread.
894 On exit "goacc_thread" will return a valid & populated thread block. */
896 attribute_hidden void
897 goacc_lazy_initialize (void)
899 struct goacc_thread *thr = goacc_thread ();
901 if (thr && thr->dev)
902 return;
904 gomp_init_targets_once ();
906 gomp_mutex_lock (&acc_device_lock);
907 if (!cached_base_dev)
908 cached_base_dev = acc_init_1 (acc_device_default,
909 acc_construct_parallel, 1);
910 gomp_mutex_unlock (&acc_device_lock);
912 goacc_attach_host_thread_to_device (-1);