oacc-init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* OpenACC Runtime initialization routines
  2. Copyright (C) 2013-2015 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 "plugin/plugin-host.h"
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <strings.h>
  28. #include <stdbool.h>
  29. #include <string.h>
  30. static gomp_mutex_t acc_device_lock;
  31. /* A cached version of the dispatcher for the global "current" accelerator type,
  32. e.g. used as the default when creating new host threads. This is the
  33. device-type equivalent of goacc_device_num (which specifies which device to
  34. use out of potentially several of the same type). If there are several
  35. devices of a given type, this points at the first one. */
  36. static struct gomp_device_descr *cached_base_dev = NULL;
  37. #if defined HAVE_TLS || defined USE_EMUTLS
  38. __thread struct goacc_thread *goacc_tls_data;
  39. #else
  40. pthread_key_t goacc_tls_key;
  41. #endif
  42. static pthread_key_t goacc_cleanup_key;
  43. static struct goacc_thread *goacc_threads;
  44. static gomp_mutex_t goacc_thread_lock;
  45. /* An array of dispatchers for device types, indexed by the type. This array
  46. only references "base" devices, and other instances of the same type are
  47. found by simply indexing from each such device (which are stored linearly,
  48. grouped by device in target.c:devices). */
  49. static struct gomp_device_descr *dispatchers[_ACC_device_hwm] = { 0 };
  50. attribute_hidden void
  51. goacc_register (struct gomp_device_descr *disp)
  52. {
  53. /* Only register the 0th device here. */
  54. if (disp->target_id != 0)
  55. return;
  56. gomp_mutex_lock (&acc_device_lock);
  57. assert (acc_device_type (disp->type) != acc_device_none
  58. && acc_device_type (disp->type) != acc_device_default
  59. && acc_device_type (disp->type) != acc_device_not_host);
  60. assert (!dispatchers[disp->type]);
  61. dispatchers[disp->type] = disp;
  62. gomp_mutex_unlock (&acc_device_lock);
  63. }
  64. /* OpenACC names some things a little differently. */
  65. static const char *
  66. get_openacc_name (const char *name)
  67. {
  68. if (strcmp (name, "nvptx") == 0)
  69. return "nvidia";
  70. else
  71. return name;
  72. }
  73. static const char *
  74. name_of_acc_device_t (enum acc_device_t type)
  75. {
  76. switch (type)
  77. {
  78. case acc_device_none: return "none";
  79. case acc_device_default: return "default";
  80. case acc_device_host: return "host";
  81. case acc_device_host_nonshm: return "host_nonshm";
  82. case acc_device_not_host: return "not_host";
  83. case acc_device_nvidia: return "nvidia";
  84. default: gomp_fatal ("unknown device type %u", (unsigned) type);
  85. }
  86. }
  87. static struct gomp_device_descr *
  88. resolve_device (acc_device_t d)
  89. {
  90. acc_device_t d_arg = d;
  91. switch (d)
  92. {
  93. case acc_device_default:
  94. {
  95. if (goacc_device_type)
  96. {
  97. /* Lookup the named device. */
  98. while (++d != _ACC_device_hwm)
  99. if (dispatchers[d]
  100. && !strcasecmp (goacc_device_type,
  101. get_openacc_name (dispatchers[d]->name))
  102. && dispatchers[d]->get_num_devices_func () > 0)
  103. goto found;
  104. gomp_fatal ("device type %s not supported", goacc_device_type);
  105. }
  106. /* No default device specified, so start scanning for any non-host
  107. device that is available. */
  108. d = acc_device_not_host;
  109. }
  110. /* FALLTHROUGH */
  111. case acc_device_not_host:
  112. /* Find the first available device after acc_device_not_host. */
  113. while (++d != _ACC_device_hwm)
  114. if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0)
  115. goto found;
  116. if (d_arg == acc_device_default)
  117. {
  118. d = acc_device_host;
  119. goto found;
  120. }
  121. gomp_fatal ("no device found");
  122. break;
  123. case acc_device_host:
  124. break;
  125. default:
  126. if (d > _ACC_device_hwm)
  127. gomp_fatal ("device %u out of range", (unsigned)d);
  128. break;
  129. }
  130. found:
  131. assert (d != acc_device_none
  132. && d != acc_device_default
  133. && d != acc_device_not_host);
  134. return dispatchers[d];
  135. }
  136. /* This is called when plugins have been initialized, and serves to call
  137. (indirectly) the target's device_init hook. Calling multiple times without
  138. an intervening acc_shutdown_1 call is an error. */
  139. static struct gomp_device_descr *
  140. acc_init_1 (acc_device_t d)
  141. {
  142. struct gomp_device_descr *base_dev, *acc_dev;
  143. int ndevs;
  144. base_dev = resolve_device (d);
  145. ndevs = base_dev->get_num_devices_func ();
  146. if (!base_dev || ndevs <= 0 || goacc_device_num >= ndevs)
  147. gomp_fatal ("device %s not supported", name_of_acc_device_t (d));
  148. acc_dev = &base_dev[goacc_device_num];
  149. if (acc_dev->is_initialized)
  150. gomp_fatal ("device already active");
  151. gomp_init_device (acc_dev);
  152. return base_dev;
  153. }
  154. static void
  155. acc_shutdown_1 (acc_device_t d)
  156. {
  157. struct gomp_device_descr *base_dev;
  158. struct goacc_thread *walk;
  159. int ndevs, i;
  160. bool devices_active = false;
  161. /* Get the base device for this device type. */
  162. base_dev = resolve_device (d);
  163. if (!base_dev)
  164. gomp_fatal ("device %s not supported", name_of_acc_device_t (d));
  165. gomp_mutex_lock (&goacc_thread_lock);
  166. /* Free target-specific TLS data and close all devices. */
  167. for (walk = goacc_threads; walk != NULL; walk = walk->next)
  168. {
  169. if (walk->target_tls)
  170. base_dev->openacc.destroy_thread_data_func (walk->target_tls);
  171. walk->target_tls = NULL;
  172. /* This would mean the user is shutting down OpenACC in the middle of an
  173. "acc data" pragma. Likely not intentional. */
  174. if (walk->mapped_data)
  175. gomp_fatal ("shutdown in 'acc data' region");
  176. /* Similarly, if this happens then user code has done something weird. */
  177. if (walk->saved_bound_dev)
  178. gomp_fatal ("shutdown during host fallback");
  179. if (walk->dev)
  180. {
  181. gomp_mutex_lock (&walk->dev->lock);
  182. gomp_free_memmap (&walk->dev->mem_map);
  183. gomp_mutex_unlock (&walk->dev->lock);
  184. walk->dev = NULL;
  185. walk->base_dev = NULL;
  186. }
  187. }
  188. gomp_mutex_unlock (&goacc_thread_lock);
  189. ndevs = base_dev->get_num_devices_func ();
  190. /* Close all the devices of this type that have been opened. */
  191. for (i = 0; i < ndevs; i++)
  192. {
  193. struct gomp_device_descr *acc_dev = &base_dev[i];
  194. if (acc_dev->is_initialized)
  195. {
  196. devices_active = true;
  197. gomp_fini_device (acc_dev);
  198. }
  199. }
  200. if (!devices_active)
  201. gomp_fatal ("no device initialized");
  202. }
  203. static struct goacc_thread *
  204. goacc_new_thread (void)
  205. {
  206. struct goacc_thread *thr = gomp_malloc (sizeof (struct gomp_thread));
  207. #if defined HAVE_TLS || defined USE_EMUTLS
  208. goacc_tls_data = thr;
  209. #else
  210. pthread_setspecific (goacc_tls_key, thr);
  211. #endif
  212. pthread_setspecific (goacc_cleanup_key, thr);
  213. gomp_mutex_lock (&goacc_thread_lock);
  214. thr->next = goacc_threads;
  215. goacc_threads = thr;
  216. gomp_mutex_unlock (&goacc_thread_lock);
  217. return thr;
  218. }
  219. static void
  220. goacc_destroy_thread (void *data)
  221. {
  222. struct goacc_thread *thr = data, *walk, *prev;
  223. gomp_mutex_lock (&goacc_thread_lock);
  224. if (thr)
  225. {
  226. struct gomp_device_descr *acc_dev = thr->dev;
  227. if (acc_dev && thr->target_tls)
  228. {
  229. acc_dev->openacc.destroy_thread_data_func (thr->target_tls);
  230. thr->target_tls = NULL;
  231. }
  232. assert (!thr->mapped_data);
  233. /* Remove from thread list. */
  234. for (prev = NULL, walk = goacc_threads; walk;
  235. prev = walk, walk = walk->next)
  236. if (walk == thr)
  237. {
  238. if (prev == NULL)
  239. goacc_threads = walk->next;
  240. else
  241. prev->next = walk->next;
  242. free (thr);
  243. break;
  244. }
  245. assert (walk);
  246. }
  247. gomp_mutex_unlock (&goacc_thread_lock);
  248. }
  249. /* Use the ORD'th device instance for the current host thread (or -1 for the
  250. current global default). The device (and the runtime) must be initialised
  251. before calling this function. */
  252. void
  253. goacc_attach_host_thread_to_device (int ord)
  254. {
  255. struct goacc_thread *thr = goacc_thread ();
  256. struct gomp_device_descr *acc_dev = NULL, *base_dev = NULL;
  257. int num_devices;
  258. if (thr && thr->dev && (thr->dev->target_id == ord || ord < 0))
  259. return;
  260. if (ord < 0)
  261. ord = goacc_device_num;
  262. /* Decide which type of device to use. If the current thread has a device
  263. type already (e.g. set by acc_set_device_type), use that, else use the
  264. global default. */
  265. if (thr && thr->base_dev)
  266. base_dev = thr->base_dev;
  267. else
  268. {
  269. assert (cached_base_dev);
  270. base_dev = cached_base_dev;
  271. }
  272. num_devices = base_dev->get_num_devices_func ();
  273. if (num_devices <= 0 || ord >= num_devices)
  274. gomp_fatal ("device %u out of range", ord);
  275. if (!thr)
  276. thr = goacc_new_thread ();
  277. thr->base_dev = base_dev;
  278. thr->dev = acc_dev = &base_dev[ord];
  279. thr->saved_bound_dev = NULL;
  280. thr->mapped_data = NULL;
  281. thr->target_tls
  282. = acc_dev->openacc.create_thread_data_func (ord);
  283. acc_dev->openacc.async_set_async_func (acc_async_sync);
  284. }
  285. /* OpenACC 2.0a (3.2.12, 3.2.13) doesn't specify whether the serialization of
  286. init/shutdown is per-process or per-thread. We choose per-process. */
  287. void
  288. acc_init (acc_device_t d)
  289. {
  290. if (!cached_base_dev)
  291. gomp_init_targets_once ();
  292. gomp_mutex_lock (&acc_device_lock);
  293. cached_base_dev = acc_init_1 (d);
  294. gomp_mutex_unlock (&acc_device_lock);
  295. goacc_attach_host_thread_to_device (-1);
  296. }
  297. ialias (acc_init)
  298. void
  299. acc_shutdown (acc_device_t d)
  300. {
  301. gomp_mutex_lock (&acc_device_lock);
  302. acc_shutdown_1 (d);
  303. gomp_mutex_unlock (&acc_device_lock);
  304. }
  305. ialias (acc_shutdown)
  306. int
  307. acc_get_num_devices (acc_device_t d)
  308. {
  309. int n = 0;
  310. struct gomp_device_descr *acc_dev;
  311. if (d == acc_device_none)
  312. return 0;
  313. gomp_init_targets_once ();
  314. acc_dev = resolve_device (d);
  315. if (!acc_dev)
  316. return 0;
  317. n = acc_dev->get_num_devices_func ();
  318. if (n < 0)
  319. n = 0;
  320. return n;
  321. }
  322. ialias (acc_get_num_devices)
  323. /* Set the device type for the current thread only (using the current global
  324. default device number), initialising that device if necessary. Also set the
  325. default device type for new threads to D. */
  326. void
  327. acc_set_device_type (acc_device_t d)
  328. {
  329. struct gomp_device_descr *base_dev, *acc_dev;
  330. struct goacc_thread *thr = goacc_thread ();
  331. gomp_mutex_lock (&acc_device_lock);
  332. if (!cached_base_dev)
  333. gomp_init_targets_once ();
  334. cached_base_dev = base_dev = resolve_device (d);
  335. acc_dev = &base_dev[goacc_device_num];
  336. if (!acc_dev->is_initialized)
  337. gomp_init_device (acc_dev);
  338. gomp_mutex_unlock (&acc_device_lock);
  339. /* We're changing device type: invalidate the current thread's dev and
  340. base_dev pointers. */
  341. if (thr && thr->base_dev != base_dev)
  342. {
  343. thr->base_dev = thr->dev = NULL;
  344. if (thr->mapped_data)
  345. gomp_fatal ("acc_set_device_type in 'acc data' region");
  346. }
  347. goacc_attach_host_thread_to_device (-1);
  348. }
  349. ialias (acc_set_device_type)
  350. acc_device_t
  351. acc_get_device_type (void)
  352. {
  353. acc_device_t res = acc_device_none;
  354. struct gomp_device_descr *dev;
  355. struct goacc_thread *thr = goacc_thread ();
  356. if (thr && thr->base_dev)
  357. res = acc_device_type (thr->base_dev->type);
  358. else
  359. {
  360. gomp_init_targets_once ();
  361. dev = resolve_device (acc_device_default);
  362. res = acc_device_type (dev->type);
  363. }
  364. assert (res != acc_device_default
  365. && res != acc_device_not_host);
  366. return res;
  367. }
  368. ialias (acc_get_device_type)
  369. int
  370. acc_get_device_num (acc_device_t d)
  371. {
  372. const struct gomp_device_descr *dev;
  373. struct goacc_thread *thr = goacc_thread ();
  374. if (d >= _ACC_device_hwm)
  375. gomp_fatal ("device %u out of range", (unsigned)d);
  376. if (!cached_base_dev)
  377. gomp_init_targets_once ();
  378. dev = resolve_device (d);
  379. if (!dev)
  380. gomp_fatal ("device %s not supported", name_of_acc_device_t (d));
  381. if (thr && thr->base_dev == dev && thr->dev)
  382. return thr->dev->target_id;
  383. return goacc_device_num;
  384. }
  385. ialias (acc_get_device_num)
  386. void
  387. acc_set_device_num (int ord, acc_device_t d)
  388. {
  389. struct gomp_device_descr *base_dev, *acc_dev;
  390. int num_devices;
  391. if (!cached_base_dev)
  392. gomp_init_targets_once ();
  393. if (ord < 0)
  394. ord = goacc_device_num;
  395. if ((int) d == 0)
  396. /* Set whatever device is being used by the current host thread to use
  397. device instance ORD. It's unclear if this is supposed to affect other
  398. host threads too (OpenACC 2.0 (3.2.4) acc_set_device_num). */
  399. goacc_attach_host_thread_to_device (ord);
  400. else
  401. {
  402. gomp_mutex_lock (&acc_device_lock);
  403. cached_base_dev = base_dev = resolve_device (d);
  404. num_devices = base_dev->get_num_devices_func ();
  405. if (ord >= num_devices)
  406. gomp_fatal ("device %u out of range", ord);
  407. acc_dev = &base_dev[ord];
  408. if (!acc_dev->is_initialized)
  409. gomp_init_device (acc_dev);
  410. gomp_mutex_unlock (&acc_device_lock);
  411. goacc_attach_host_thread_to_device (ord);
  412. }
  413. goacc_device_num = ord;
  414. }
  415. ialias (acc_set_device_num)
  416. int
  417. acc_on_device (acc_device_t dev)
  418. {
  419. struct goacc_thread *thr = goacc_thread ();
  420. /* We only want to appear to be the "host_nonshm" plugin from "offloaded"
  421. code -- i.e. within a parallel region. Test a flag set by the
  422. openacc_parallel hook of the host_nonshm plugin to determine that. */
  423. if (acc_get_device_type () == acc_device_host_nonshm
  424. && thr && thr->target_tls
  425. && ((struct nonshm_thread *)thr->target_tls)->nonshm_exec)
  426. return dev == acc_device_host_nonshm || dev == acc_device_not_host;
  427. /* For OpenACC, libgomp is only built for the host, so this is sufficient. */
  428. return dev == acc_device_host || dev == acc_device_none;
  429. }
  430. ialias (acc_on_device)
  431. attribute_hidden void
  432. goacc_runtime_initialize (void)
  433. {
  434. gomp_mutex_init (&acc_device_lock);
  435. #if !(defined HAVE_TLS || defined USE_EMUTLS)
  436. pthread_key_create (&goacc_tls_key, NULL);
  437. #endif
  438. pthread_key_create (&goacc_cleanup_key, goacc_destroy_thread);
  439. cached_base_dev = NULL;
  440. goacc_threads = NULL;
  441. gomp_mutex_init (&goacc_thread_lock);
  442. }
  443. /* Compiler helper functions */
  444. attribute_hidden void
  445. goacc_save_and_set_bind (acc_device_t d)
  446. {
  447. struct goacc_thread *thr = goacc_thread ();
  448. assert (!thr->saved_bound_dev);
  449. thr->saved_bound_dev = thr->dev;
  450. thr->dev = dispatchers[d];
  451. }
  452. attribute_hidden void
  453. goacc_restore_bind (void)
  454. {
  455. struct goacc_thread *thr = goacc_thread ();
  456. thr->dev = thr->saved_bound_dev;
  457. thr->saved_bound_dev = NULL;
  458. }
  459. /* This is called from any OpenACC support function that may need to implicitly
  460. initialize the libgomp runtime, either globally or from a new host thread.
  461. On exit "goacc_thread" will return a valid & populated thread block. */
  462. attribute_hidden void
  463. goacc_lazy_initialize (void)
  464. {
  465. struct goacc_thread *thr = goacc_thread ();
  466. if (thr && thr->dev)
  467. return;
  468. if (!cached_base_dev)
  469. acc_init (acc_device_default);
  470. else
  471. goacc_attach_host_thread_to_device (-1);
  472. }