oacc-init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /* OpenACC Runtime initialization routines
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor Embedded.
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgomp.h"
  22. #include "oacc-int.h"
  23. #include "openacc.h"
  24. #include <assert.h>
  25. #include <stdlib.h>
  26. #include <strings.h>
  27. #include <stdbool.h>
  28. #include <string.h>
  29. /* This lock is used to protect access to cached_base_dev, dispatchers and
  30. the (abstract) initialisation state of attached offloading devices. */
  31. static gomp_mutex_t acc_device_lock;
  32. static gomp_mutex_t acc_init_state_lock;
  33. static enum { uninitialized, initializing, initialized } acc_init_state
  34. = uninitialized;
  35. static pthread_t acc_init_thread;
  36. /* A cached version of the dispatcher for the global "current" accelerator type,
  37. e.g. used as the default when creating new host threads. This is the
  38. device-type equivalent of goacc_device_num (which specifies which device to
  39. use out of potentially several of the same type). If there are several
  40. devices of a given type, this points at the first one. */
  41. static struct gomp_device_descr *cached_base_dev = NULL;
  42. #if defined HAVE_TLS || defined USE_EMUTLS
  43. __thread struct goacc_thread *goacc_tls_data;
  44. #else
  45. pthread_key_t goacc_tls_key;
  46. #endif
  47. static pthread_key_t goacc_cleanup_key;
  48. static struct goacc_thread *goacc_threads;
  49. static gomp_mutex_t goacc_thread_lock;
  50. /* An array of dispatchers for device types, indexed by the type. This array
  51. only references "base" devices, and other instances of the same type are
  52. found by simply indexing from each such device (which are stored linearly,
  53. grouped by device in target.c:devices). */
  54. static struct gomp_device_descr *dispatchers[_ACC_device_hwm] = { 0 };
  55. attribute_hidden void
  56. goacc_register (struct gomp_device_descr *disp)
  57. {
  58. /* Only register the 0th device here. */
  59. if (disp->target_id != 0)
  60. return;
  61. gomp_mutex_lock (&acc_device_lock);
  62. assert (acc_device_type (disp->type) != acc_device_none
  63. && acc_device_type (disp->type) != acc_device_default
  64. && acc_device_type (disp->type) != acc_device_not_host);
  65. assert (!dispatchers[disp->type]);
  66. dispatchers[disp->type] = disp;
  67. gomp_mutex_unlock (&acc_device_lock);
  68. }
  69. static bool
  70. known_device_type_p (acc_device_t d)
  71. {
  72. return d >= 0 && d < _ACC_device_hwm;
  73. }
  74. static void
  75. unknown_device_type_error (acc_device_t invalid_type)
  76. {
  77. gomp_fatal ("unknown device type %u", invalid_type);
  78. }
  79. /* OpenACC names some things a little differently. */
  80. static const char *
  81. get_openacc_name (const char *name)
  82. {
  83. if (strcmp (name, "gcn") == 0)
  84. return "radeon";
  85. else if (strcmp (name, "nvptx") == 0)
  86. return "nvidia";
  87. else
  88. return name;
  89. }
  90. static const char *
  91. name_of_acc_device_t (enum acc_device_t type)
  92. {
  93. switch (type)
  94. {
  95. case acc_device_none: return "none";
  96. case acc_device_default: return "default";
  97. case acc_device_host: return "host";
  98. case acc_device_not_host: return "not_host";
  99. case acc_device_nvidia: return "nvidia";
  100. case acc_device_radeon: return "radeon";
  101. default: unknown_device_type_error (type);
  102. }
  103. __builtin_unreachable ();
  104. }
  105. /* ACC_DEVICE_LOCK must be held before calling this function. If FAIL_IS_ERROR
  106. is true, this function raises an error if there are no devices of type D,
  107. otherwise it returns NULL in that case. */
  108. static struct gomp_device_descr *
  109. resolve_device (acc_device_t d, bool fail_is_error)
  110. {
  111. acc_device_t d_arg = d;
  112. switch (d)
  113. {
  114. case acc_device_default:
  115. {
  116. if (goacc_device_type)
  117. {
  118. /* Lookup the named device. */
  119. while (known_device_type_p (++d))
  120. if (dispatchers[d]
  121. && !strcasecmp (goacc_device_type,
  122. get_openacc_name (dispatchers[d]->name))
  123. && dispatchers[d]->get_num_devices_func () > 0)
  124. goto found;
  125. if (fail_is_error)
  126. {
  127. gomp_mutex_unlock (&acc_device_lock);
  128. gomp_fatal ("device type %s not supported", goacc_device_type);
  129. }
  130. else
  131. return NULL;
  132. }
  133. /* No default device specified, so start scanning for any non-host
  134. device that is available. */
  135. d = acc_device_not_host;
  136. }
  137. /* FALLTHROUGH */
  138. case acc_device_not_host:
  139. /* Find the first available device after acc_device_not_host. */
  140. while (known_device_type_p (++d))
  141. if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0)
  142. goto found;
  143. if (d_arg == acc_device_default)
  144. {
  145. d = acc_device_host;
  146. goto found;
  147. }
  148. if (fail_is_error)
  149. {
  150. gomp_mutex_unlock (&acc_device_lock);
  151. gomp_fatal ("no device found");
  152. }
  153. else
  154. return NULL;
  155. break;
  156. case acc_device_host:
  157. break;
  158. default:
  159. if (!known_device_type_p (d))
  160. {
  161. if (fail_is_error)
  162. goto unsupported_device;
  163. else
  164. return NULL;
  165. }
  166. break;
  167. }
  168. found:
  169. assert (d != acc_device_none
  170. && d != acc_device_default
  171. && d != acc_device_not_host);
  172. if (dispatchers[d] == NULL && fail_is_error)
  173. {
  174. unsupported_device:
  175. gomp_mutex_unlock (&acc_device_lock);
  176. gomp_fatal ("device type %s not supported", name_of_acc_device_t (d));
  177. }
  178. return dispatchers[d];
  179. }
  180. /* Emit a suitable error if no device of a particular type is available, or
  181. the given device number is out-of-range. */
  182. static void
  183. acc_dev_num_out_of_range (acc_device_t d, int ord, int ndevs)
  184. {
  185. if (ndevs == 0)
  186. gomp_fatal ("no devices of type %s available", name_of_acc_device_t (d));
  187. else
  188. gomp_fatal ("device %u out of range", ord);
  189. }
  190. /* This is called when plugins have been initialized, and serves to call
  191. (indirectly) the target's device_init hook. Calling multiple times without
  192. an intervening acc_shutdown_1 call is an error. ACC_DEVICE_LOCK must be
  193. held before calling this function. */
  194. static struct gomp_device_descr *
  195. acc_init_1 (acc_device_t d, acc_construct_t parent_construct, int implicit)
  196. {
  197. gomp_mutex_lock (&acc_init_state_lock);
  198. acc_init_state = initializing;
  199. acc_init_thread = pthread_self ();
  200. gomp_mutex_unlock (&acc_init_state_lock);
  201. bool check_not_nested_p;
  202. if (implicit)
  203. {
  204. /* In the implicit case, there should (TODO: must?) already be something
  205. have been set up for an outer construct. */
  206. check_not_nested_p = false;
  207. }
  208. else
  209. {
  210. check_not_nested_p = true;
  211. /* TODO: should we set 'thr->prof_info' etc. in this case ('acc_init')?
  212. The problem is, that we don't have 'thr' yet? (So,
  213. 'check_not_nested_p = true' also is pointless actually.) */
  214. }
  215. bool profiling_p = GOACC_PROFILING_DISPATCH_P (check_not_nested_p);
  216. acc_prof_info prof_info;
  217. if (profiling_p)
  218. {
  219. prof_info.event_type = acc_ev_device_init_start;
  220. prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
  221. prof_info.version = _ACC_PROF_INFO_VERSION;
  222. prof_info.device_type = d;
  223. prof_info.device_number = goacc_device_num;
  224. prof_info.thread_id = -1;
  225. prof_info.async = acc_async_sync;
  226. prof_info.async_queue = prof_info.async;
  227. prof_info.src_file = NULL;
  228. prof_info.func_name = NULL;
  229. prof_info.line_no = -1;
  230. prof_info.end_line_no = -1;
  231. prof_info.func_line_no = -1;
  232. prof_info.func_end_line_no = -1;
  233. }
  234. acc_event_info device_init_event_info;
  235. if (profiling_p)
  236. {
  237. device_init_event_info.other_event.event_type = prof_info.event_type;
  238. device_init_event_info.other_event.valid_bytes
  239. = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
  240. device_init_event_info.other_event.parent_construct = parent_construct;
  241. device_init_event_info.other_event.implicit = implicit;
  242. device_init_event_info.other_event.tool_info = NULL;
  243. }
  244. acc_api_info api_info;
  245. if (profiling_p)
  246. {
  247. api_info.device_api = acc_device_api_none;
  248. api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
  249. api_info.device_type = prof_info.device_type;
  250. api_info.vendor = -1;
  251. api_info.device_handle = NULL;
  252. api_info.context_handle = NULL;
  253. api_info.async_handle = NULL;
  254. }
  255. if (profiling_p)
  256. goacc_profiling_dispatch (&prof_info, &device_init_event_info, &api_info);
  257. struct gomp_device_descr *base_dev, *acc_dev;
  258. int ndevs;
  259. base_dev = resolve_device (d, true);
  260. ndevs = base_dev->get_num_devices_func ();
  261. if (ndevs <= 0 || goacc_device_num >= ndevs)
  262. acc_dev_num_out_of_range (d, goacc_device_num, ndevs);
  263. acc_dev = &base_dev[goacc_device_num];
  264. gomp_mutex_lock (&acc_dev->lock);
  265. if (acc_dev->state == GOMP_DEVICE_INITIALIZED)
  266. {
  267. gomp_mutex_unlock (&acc_dev->lock);
  268. gomp_fatal ("device already active");
  269. }
  270. gomp_init_device (acc_dev);
  271. gomp_mutex_unlock (&acc_dev->lock);
  272. if (profiling_p)
  273. {
  274. prof_info.event_type = acc_ev_device_init_end;
  275. device_init_event_info.other_event.event_type = prof_info.event_type;
  276. goacc_profiling_dispatch (&prof_info, &device_init_event_info,
  277. &api_info);
  278. }
  279. /* We're setting 'initialized' *after* 'goacc_profiling_dispatch', so that a
  280. nested 'acc_get_device_type' called from a profiling callback still sees
  281. 'initializing', so that we don't deadlock when it then again tries to lock
  282. 'goacc_prof_lock'. See also the discussion in 'acc_get_device_type'. */
  283. gomp_mutex_lock (&acc_init_state_lock);
  284. acc_init_state = initialized;
  285. gomp_mutex_unlock (&acc_init_state_lock);
  286. return base_dev;
  287. }
  288. /* ACC_DEVICE_LOCK must be held before calling this function. */
  289. static void
  290. acc_shutdown_1 (acc_device_t d)
  291. {
  292. struct gomp_device_descr *base_dev;
  293. struct goacc_thread *walk;
  294. int ndevs, i;
  295. bool devices_active = false;
  296. /* Get the base device for this device type. */
  297. base_dev = resolve_device (d, true);
  298. ndevs = base_dev->get_num_devices_func ();
  299. /* Unload all the devices of this type that have been opened. */
  300. for (i = 0; i < ndevs; i++)
  301. {
  302. struct gomp_device_descr *acc_dev = &base_dev[i];
  303. gomp_mutex_lock (&acc_dev->lock);
  304. gomp_unload_device (acc_dev);
  305. gomp_mutex_unlock (&acc_dev->lock);
  306. }
  307. gomp_mutex_lock (&goacc_thread_lock);
  308. /* Free target-specific TLS data and close all devices. */
  309. for (walk = goacc_threads; walk != NULL; walk = walk->next)
  310. {
  311. if (walk->target_tls)
  312. base_dev->openacc.destroy_thread_data_func (walk->target_tls);
  313. walk->target_tls = NULL;
  314. /* This would mean the user is shutting down OpenACC in the middle of an
  315. "acc data" pragma. Likely not intentional. */
  316. if (walk->mapped_data)
  317. {
  318. gomp_mutex_unlock (&goacc_thread_lock);
  319. gomp_fatal ("shutdown in 'acc data' region");
  320. }
  321. /* Similarly, if this happens then user code has done something weird. */
  322. if (walk->saved_bound_dev)
  323. {
  324. gomp_mutex_unlock (&goacc_thread_lock);
  325. gomp_fatal ("shutdown during host fallback");
  326. }
  327. if (walk->dev)
  328. {
  329. gomp_mutex_lock (&walk->dev->lock);
  330. while (walk->dev->mem_map.root)
  331. {
  332. splay_tree_key k = &walk->dev->mem_map.root->key;
  333. if (k->aux)
  334. k->aux->link_key = NULL;
  335. gomp_remove_var (walk->dev, k);
  336. }
  337. gomp_mutex_unlock (&walk->dev->lock);
  338. walk->dev = NULL;
  339. walk->base_dev = NULL;
  340. }
  341. }
  342. gomp_mutex_unlock (&goacc_thread_lock);
  343. /* Close all the devices of this type that have been opened. */
  344. bool ret = true;
  345. for (i = 0; i < ndevs; i++)
  346. {
  347. struct gomp_device_descr *acc_dev = &base_dev[i];
  348. gomp_mutex_lock (&acc_dev->lock);
  349. if (acc_dev->state == GOMP_DEVICE_INITIALIZED)
  350. {
  351. devices_active = true;
  352. ret &= gomp_fini_device (acc_dev);
  353. acc_dev->state = GOMP_DEVICE_UNINITIALIZED;
  354. }
  355. gomp_mutex_unlock (&acc_dev->lock);
  356. }
  357. if (!ret)
  358. gomp_fatal ("device finalization failed");
  359. if (!devices_active)
  360. gomp_fatal ("no device initialized");
  361. }
  362. static struct goacc_thread *
  363. goacc_new_thread (void)
  364. {
  365. struct goacc_thread *thr = gomp_malloc (sizeof (struct goacc_thread));
  366. #if defined HAVE_TLS || defined USE_EMUTLS
  367. goacc_tls_data = thr;
  368. #else
  369. pthread_setspecific (goacc_tls_key, thr);
  370. #endif
  371. pthread_setspecific (goacc_cleanup_key, thr);
  372. gomp_mutex_lock (&goacc_thread_lock);
  373. thr->next = goacc_threads;
  374. goacc_threads = thr;
  375. gomp_mutex_unlock (&goacc_thread_lock);
  376. return thr;
  377. }
  378. static void
  379. goacc_destroy_thread (void *data)
  380. {
  381. struct goacc_thread *thr = data, *walk, *prev;
  382. gomp_mutex_lock (&goacc_thread_lock);
  383. if (thr)
  384. {
  385. struct gomp_device_descr *acc_dev = thr->dev;
  386. if (acc_dev && thr->target_tls)
  387. {
  388. acc_dev->openacc.destroy_thread_data_func (thr->target_tls);
  389. thr->target_tls = NULL;
  390. }
  391. assert (!thr->mapped_data);
  392. /* Remove from thread list. */
  393. for (prev = NULL, walk = goacc_threads; walk;
  394. prev = walk, walk = walk->next)
  395. if (walk == thr)
  396. {
  397. if (prev == NULL)
  398. goacc_threads = walk->next;
  399. else
  400. prev->next = walk->next;
  401. free (thr);
  402. break;
  403. }
  404. assert (walk);
  405. }
  406. gomp_mutex_unlock (&goacc_thread_lock);
  407. }
  408. /* Use the ORD'th device instance for the current host thread (or -1 for the
  409. current global default). The device (and the runtime) must be initialised
  410. before calling this function. */
  411. void
  412. goacc_attach_host_thread_to_device (int ord)
  413. {
  414. struct goacc_thread *thr = goacc_thread ();
  415. struct gomp_device_descr *acc_dev = NULL, *base_dev = NULL;
  416. int num_devices;
  417. if (thr && thr->dev && (thr->dev->target_id == ord || ord < 0))
  418. return;
  419. if (ord < 0)
  420. ord = goacc_device_num;
  421. /* Decide which type of device to use. If the current thread has a device
  422. type already (e.g. set by acc_set_device_type), use that, else use the
  423. global default. */
  424. if (thr && thr->base_dev)
  425. base_dev = thr->base_dev;
  426. else
  427. {
  428. assert (cached_base_dev);
  429. base_dev = cached_base_dev;
  430. }
  431. num_devices = base_dev->get_num_devices_func ();
  432. if (num_devices <= 0 || ord >= num_devices)
  433. acc_dev_num_out_of_range (acc_device_type (base_dev->type), ord,
  434. num_devices);
  435. if (!thr)
  436. thr = goacc_new_thread ();
  437. thr->base_dev = base_dev;
  438. thr->dev = acc_dev = &base_dev[ord];
  439. thr->saved_bound_dev = NULL;
  440. thr->mapped_data = NULL;
  441. thr->prof_info = NULL;
  442. thr->api_info = NULL;
  443. /* Initially, all callbacks for all events are enabled. */
  444. thr->prof_callbacks_enabled = true;
  445. thr->target_tls
  446. = acc_dev->openacc.create_thread_data_func (ord);
  447. }
  448. /* OpenACC 2.0a (3.2.12, 3.2.13) doesn't specify whether the serialization of
  449. init/shutdown is per-process or per-thread. We choose per-process. */
  450. void
  451. acc_init (acc_device_t d)
  452. {
  453. if (!known_device_type_p (d))
  454. unknown_device_type_error (d);
  455. gomp_init_targets_once ();
  456. gomp_mutex_lock (&acc_device_lock);
  457. cached_base_dev = acc_init_1 (d, acc_construct_runtime_api, 0);
  458. gomp_mutex_unlock (&acc_device_lock);
  459. goacc_attach_host_thread_to_device (-1);
  460. }
  461. ialias (acc_init)
  462. void
  463. acc_shutdown (acc_device_t d)
  464. {
  465. if (!known_device_type_p (d))
  466. unknown_device_type_error (d);
  467. gomp_init_targets_once ();
  468. gomp_mutex_lock (&acc_device_lock);
  469. acc_shutdown_1 (d);
  470. gomp_mutex_unlock (&acc_device_lock);
  471. }
  472. ialias (acc_shutdown)
  473. int
  474. acc_get_num_devices (acc_device_t d)
  475. {
  476. if (!known_device_type_p (d))
  477. unknown_device_type_error (d);
  478. int n = 0;
  479. struct gomp_device_descr *acc_dev;
  480. if (d == acc_device_none)
  481. return 0;
  482. gomp_init_targets_once ();
  483. gomp_mutex_lock (&acc_device_lock);
  484. acc_dev = resolve_device (d, false);
  485. gomp_mutex_unlock (&acc_device_lock);
  486. if (!acc_dev)
  487. return 0;
  488. n = acc_dev->get_num_devices_func ();
  489. if (n < 0)
  490. n = 0;
  491. return n;
  492. }
  493. ialias (acc_get_num_devices)
  494. /* Set the device type for the current thread only (using the current global
  495. default device number), initialising that device if necessary. Also set the
  496. default device type for new threads to D. */
  497. void
  498. acc_set_device_type (acc_device_t d)
  499. {
  500. if (!known_device_type_p (d))
  501. unknown_device_type_error (d);
  502. struct gomp_device_descr *base_dev, *acc_dev;
  503. struct goacc_thread *thr = goacc_thread ();
  504. acc_prof_info prof_info;
  505. acc_api_info api_info;
  506. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  507. if (profiling_p)
  508. prof_info.device_type = d;
  509. gomp_init_targets_once ();
  510. gomp_mutex_lock (&acc_device_lock);
  511. cached_base_dev = base_dev = resolve_device (d, true);
  512. acc_dev = &base_dev[goacc_device_num];
  513. gomp_mutex_lock (&acc_dev->lock);
  514. if (acc_dev->state == GOMP_DEVICE_UNINITIALIZED)
  515. gomp_init_device (acc_dev);
  516. gomp_mutex_unlock (&acc_dev->lock);
  517. gomp_mutex_unlock (&acc_device_lock);
  518. /* We're changing device type: invalidate the current thread's dev and
  519. base_dev pointers. */
  520. if (thr && thr->base_dev != base_dev)
  521. {
  522. thr->base_dev = thr->dev = NULL;
  523. if (thr->mapped_data)
  524. gomp_fatal ("acc_set_device_type in 'acc data' region");
  525. }
  526. goacc_attach_host_thread_to_device (-1);
  527. if (profiling_p)
  528. {
  529. thr->prof_info = NULL;
  530. thr->api_info = NULL;
  531. }
  532. }
  533. ialias (acc_set_device_type)
  534. static bool
  535. self_initializing_p (void)
  536. {
  537. bool res;
  538. gomp_mutex_lock (&acc_init_state_lock);
  539. res = (acc_init_state == initializing
  540. && pthread_equal (acc_init_thread, pthread_self ()));
  541. gomp_mutex_unlock (&acc_init_state_lock);
  542. return res;
  543. }
  544. acc_device_t
  545. acc_get_device_type (void)
  546. {
  547. acc_device_t res = acc_device_none;
  548. struct gomp_device_descr *dev;
  549. struct goacc_thread *thr = goacc_thread ();
  550. if (thr && thr->base_dev)
  551. res = acc_device_type (thr->base_dev->type);
  552. else if (self_initializing_p ())
  553. /* The Cuda libaccinj64.so version 9.0+ calls acc_get_device_type during the
  554. acc_ev_device_init_start event callback, which is dispatched during
  555. acc_init_1. Trying to lock acc_device_lock during such a call (as we do
  556. in the else clause below), will result in deadlock, since the lock has
  557. already been taken by the acc_init_1 caller. We work around this problem
  558. by using the acc_get_device_type property "If the device type has not yet
  559. been selected, the value acc_device_none may be returned". */
  560. ;
  561. else
  562. {
  563. acc_prof_info prof_info;
  564. acc_api_info api_info;
  565. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  566. gomp_init_targets_once ();
  567. gomp_mutex_lock (&acc_device_lock);
  568. dev = resolve_device (acc_device_default, true);
  569. gomp_mutex_unlock (&acc_device_lock);
  570. res = acc_device_type (dev->type);
  571. if (profiling_p)
  572. {
  573. thr->prof_info = NULL;
  574. thr->api_info = NULL;
  575. }
  576. }
  577. assert (res != acc_device_default
  578. && res != acc_device_not_host
  579. && res != acc_device_current);
  580. return res;
  581. }
  582. ialias (acc_get_device_type)
  583. int
  584. acc_get_device_num (acc_device_t d)
  585. {
  586. if (!known_device_type_p (d))
  587. unknown_device_type_error (d);
  588. const struct gomp_device_descr *dev;
  589. struct goacc_thread *thr = goacc_thread ();
  590. acc_prof_info prof_info;
  591. acc_api_info api_info;
  592. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  593. if (profiling_p)
  594. prof_info.device_type = d;
  595. gomp_init_targets_once ();
  596. gomp_mutex_lock (&acc_device_lock);
  597. dev = resolve_device (d, true);
  598. gomp_mutex_unlock (&acc_device_lock);
  599. if (profiling_p)
  600. {
  601. thr->prof_info = NULL;
  602. thr->api_info = NULL;
  603. }
  604. if (thr && thr->base_dev == dev && thr->dev)
  605. return thr->dev->target_id;
  606. return goacc_device_num;
  607. }
  608. ialias (acc_get_device_num)
  609. void
  610. acc_set_device_num (int ord, acc_device_t d)
  611. {
  612. if (!known_device_type_p (d))
  613. unknown_device_type_error (d);
  614. struct gomp_device_descr *base_dev, *acc_dev;
  615. int num_devices;
  616. gomp_init_targets_once ();
  617. if (ord < 0)
  618. ord = goacc_device_num;
  619. if ((int) d == 0)
  620. /* Set whatever device is being used by the current host thread to use
  621. device instance ORD. It's unclear if this is supposed to affect other
  622. host threads too (OpenACC 2.0 (3.2.4) acc_set_device_num). */
  623. goacc_attach_host_thread_to_device (ord);
  624. else
  625. {
  626. gomp_mutex_lock (&acc_device_lock);
  627. cached_base_dev = base_dev = resolve_device (d, true);
  628. num_devices = base_dev->get_num_devices_func ();
  629. if (num_devices <= 0 || ord >= num_devices)
  630. acc_dev_num_out_of_range (d, ord, num_devices);
  631. acc_dev = &base_dev[ord];
  632. gomp_mutex_lock (&acc_dev->lock);
  633. if (acc_dev->state == GOMP_DEVICE_UNINITIALIZED)
  634. gomp_init_device (acc_dev);
  635. gomp_mutex_unlock (&acc_dev->lock);
  636. gomp_mutex_unlock (&acc_device_lock);
  637. goacc_attach_host_thread_to_device (ord);
  638. }
  639. goacc_device_num = ord;
  640. }
  641. ialias (acc_set_device_num)
  642. static union goacc_property_value
  643. get_property_any (int ord, acc_device_t d, acc_device_property_t prop)
  644. {
  645. goacc_lazy_initialize ();
  646. struct goacc_thread *thr = goacc_thread ();
  647. if (d == acc_device_current && thr && thr->dev)
  648. return thr->dev->openacc.get_property_func (thr->dev->target_id, prop);
  649. gomp_mutex_lock (&acc_device_lock);
  650. struct gomp_device_descr *dev = resolve_device (d, true);
  651. int num_devices = dev->get_num_devices_func ();
  652. if (num_devices <= 0 || ord >= num_devices)
  653. acc_dev_num_out_of_range (d, ord, num_devices);
  654. dev += ord;
  655. gomp_mutex_lock (&dev->lock);
  656. if (dev->state == GOMP_DEVICE_UNINITIALIZED)
  657. gomp_init_device (dev);
  658. gomp_mutex_unlock (&dev->lock);
  659. gomp_mutex_unlock (&acc_device_lock);
  660. assert (dev);
  661. return dev->openacc.get_property_func (dev->target_id, prop);
  662. }
  663. size_t
  664. acc_get_property (int ord, acc_device_t d, acc_device_property_t prop)
  665. {
  666. if (!known_device_type_p (d))
  667. unknown_device_type_error(d);
  668. if (prop & GOACC_PROPERTY_STRING_MASK)
  669. return 0;
  670. else
  671. return get_property_any (ord, d, prop).val;
  672. }
  673. ialias (acc_get_property)
  674. const char *
  675. acc_get_property_string (int ord, acc_device_t d, acc_device_property_t prop)
  676. {
  677. if (!known_device_type_p (d))
  678. unknown_device_type_error(d);
  679. if (prop & GOACC_PROPERTY_STRING_MASK)
  680. return get_property_any (ord, d, prop).ptr;
  681. else
  682. return NULL;
  683. }
  684. ialias (acc_get_property_string)
  685. /* For -O and higher, the compiler always attempts to expand acc_on_device, but
  686. if the user disables the builtin, or calls it via a pointer, we'll need this
  687. version.
  688. Compile this with optimization, so that the compiler expands
  689. this, rather than generating infinitely recursive code.
  690. The function just forwards its argument to __builtin_acc_on_device. It does
  691. not verify that the argument is a valid acc_device_t enumeration value. */
  692. int __attribute__ ((__optimize__ ("O2")))
  693. acc_on_device (acc_device_t dev)
  694. {
  695. return __builtin_acc_on_device (dev);
  696. }
  697. ialias (acc_on_device)
  698. attribute_hidden void
  699. goacc_runtime_initialize (void)
  700. {
  701. gomp_mutex_init (&acc_device_lock);
  702. #if !(defined HAVE_TLS || defined USE_EMUTLS)
  703. pthread_key_create (&goacc_tls_key, NULL);
  704. #endif
  705. pthread_key_create (&goacc_cleanup_key, goacc_destroy_thread);
  706. cached_base_dev = NULL;
  707. goacc_threads = NULL;
  708. gomp_mutex_init (&goacc_thread_lock);
  709. /* Initialize and register the 'host' device type. */
  710. goacc_host_init ();
  711. }
  712. static void __attribute__((destructor))
  713. goacc_runtime_deinitialize (void)
  714. {
  715. #if !(defined HAVE_TLS || defined USE_EMUTLS)
  716. pthread_key_delete (goacc_tls_key);
  717. #endif
  718. pthread_key_delete (goacc_cleanup_key);
  719. }
  720. /* Compiler helper functions */
  721. attribute_hidden void
  722. goacc_save_and_set_bind (acc_device_t d)
  723. {
  724. struct goacc_thread *thr = goacc_thread ();
  725. assert (!thr->saved_bound_dev);
  726. thr->saved_bound_dev = thr->dev;
  727. thr->dev = dispatchers[d];
  728. }
  729. attribute_hidden void
  730. goacc_restore_bind (void)
  731. {
  732. struct goacc_thread *thr = goacc_thread ();
  733. thr->dev = thr->saved_bound_dev;
  734. thr->saved_bound_dev = NULL;
  735. }
  736. /* This is called from any OpenACC support function that may need to implicitly
  737. initialize the libgomp runtime, either globally or from a new host thread.
  738. On exit "goacc_thread" will return a valid & populated thread block. */
  739. attribute_hidden void
  740. goacc_lazy_initialize (void)
  741. {
  742. struct goacc_thread *thr = goacc_thread ();
  743. if (thr && thr->dev)
  744. return;
  745. gomp_init_targets_once ();
  746. gomp_mutex_lock (&acc_device_lock);
  747. if (!cached_base_dev)
  748. cached_base_dev = acc_init_1 (acc_device_default,
  749. acc_construct_parallel, 1);
  750. gomp_mutex_unlock (&acc_device_lock);
  751. goacc_attach_host_thread_to_device (-1);
  752. }