oacc-async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* OpenACC Runtime Library Definitions.
  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 <assert.h>
  22. #include <string.h>
  23. #include "openacc.h"
  24. #include "libgomp.h"
  25. #include "oacc-int.h"
  26. static struct goacc_thread *
  27. get_goacc_thread (void)
  28. {
  29. struct goacc_thread *thr = goacc_thread ();
  30. if (!thr || !thr->dev)
  31. gomp_fatal ("no device active");
  32. return thr;
  33. }
  34. static int
  35. validate_async_val (int async)
  36. {
  37. if (!async_valid_p (async))
  38. gomp_fatal ("invalid async-argument: %d", async);
  39. if (async == acc_async_sync)
  40. return -1;
  41. if (async == acc_async_noval)
  42. return 0;
  43. if (async >= 0)
  44. /* TODO: we reserve 0 for acc_async_noval before we can clarify the
  45. semantics of "default_async". */
  46. return 1 + async;
  47. else
  48. __builtin_unreachable ();
  49. }
  50. /* Return the asyncqueue to be used for OpenACC async-argument ASYNC. This
  51. might return NULL if no asyncqueue is to be used. Otherwise, if CREATE,
  52. create the asyncqueue if it doesn't exist yet.
  53. Unless CREATE, this will not generate any OpenACC Profiling Interface
  54. events. */
  55. attribute_hidden struct goacc_asyncqueue *
  56. lookup_goacc_asyncqueue (struct goacc_thread *thr, bool create, int async)
  57. {
  58. async = validate_async_val (async);
  59. if (async < 0)
  60. return NULL;
  61. struct goacc_asyncqueue *ret_aq = NULL;
  62. struct gomp_device_descr *dev = thr->dev;
  63. gomp_mutex_lock (&dev->openacc.async.lock);
  64. if (!create
  65. && (async >= dev->openacc.async.nasyncqueue
  66. || !dev->openacc.async.asyncqueue[async]))
  67. goto end;
  68. if (async >= dev->openacc.async.nasyncqueue)
  69. {
  70. int diff = async + 1 - dev->openacc.async.nasyncqueue;
  71. dev->openacc.async.asyncqueue
  72. = gomp_realloc (dev->openacc.async.asyncqueue,
  73. sizeof (goacc_aq) * (async + 1));
  74. memset (dev->openacc.async.asyncqueue + dev->openacc.async.nasyncqueue,
  75. 0, sizeof (goacc_aq) * diff);
  76. dev->openacc.async.nasyncqueue = async + 1;
  77. }
  78. if (!dev->openacc.async.asyncqueue[async])
  79. {
  80. dev->openacc.async.asyncqueue[async]
  81. = dev->openacc.async.construct_func (dev->target_id);
  82. if (!dev->openacc.async.asyncqueue[async])
  83. {
  84. gomp_mutex_unlock (&dev->openacc.async.lock);
  85. gomp_fatal ("async %d creation failed", async);
  86. }
  87. /* Link new async queue into active list. */
  88. goacc_aq_list n = gomp_malloc (sizeof (struct goacc_asyncqueue_list));
  89. n->aq = dev->openacc.async.asyncqueue[async];
  90. n->next = dev->openacc.async.active;
  91. dev->openacc.async.active = n;
  92. }
  93. ret_aq = dev->openacc.async.asyncqueue[async];
  94. end:
  95. gomp_mutex_unlock (&dev->openacc.async.lock);
  96. return ret_aq;
  97. }
  98. /* Return the asyncqueue to be used for OpenACC async-argument ASYNC. This
  99. might return NULL if no asyncqueue is to be used. Otherwise, create the
  100. asyncqueue if it doesn't exist yet. */
  101. attribute_hidden struct goacc_asyncqueue *
  102. get_goacc_asyncqueue (int async)
  103. {
  104. struct goacc_thread *thr = get_goacc_thread ();
  105. return lookup_goacc_asyncqueue (thr, true, async);
  106. }
  107. int
  108. acc_async_test (int async)
  109. {
  110. struct goacc_thread *thr = goacc_thread ();
  111. if (!thr || !thr->dev)
  112. gomp_fatal ("no device active");
  113. goacc_aq aq = lookup_goacc_asyncqueue (thr, false, async);
  114. if (!aq)
  115. return 1;
  116. acc_prof_info prof_info;
  117. acc_api_info api_info;
  118. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  119. if (profiling_p)
  120. {
  121. prof_info.async = async;
  122. prof_info.async_queue = prof_info.async;
  123. }
  124. int res = thr->dev->openacc.async.test_func (aq);
  125. if (profiling_p)
  126. {
  127. thr->prof_info = NULL;
  128. thr->api_info = NULL;
  129. }
  130. return res;
  131. }
  132. int
  133. acc_async_test_all (void)
  134. {
  135. struct goacc_thread *thr = get_goacc_thread ();
  136. acc_prof_info prof_info;
  137. acc_api_info api_info;
  138. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  139. int ret = 1;
  140. gomp_mutex_lock (&thr->dev->openacc.async.lock);
  141. for (goacc_aq_list l = thr->dev->openacc.async.active; l; l = l->next)
  142. if (!thr->dev->openacc.async.test_func (l->aq))
  143. {
  144. ret = 0;
  145. break;
  146. }
  147. gomp_mutex_unlock (&thr->dev->openacc.async.lock);
  148. if (profiling_p)
  149. {
  150. thr->prof_info = NULL;
  151. thr->api_info = NULL;
  152. }
  153. return ret;
  154. }
  155. void
  156. acc_wait (int async)
  157. {
  158. struct goacc_thread *thr = get_goacc_thread ();
  159. goacc_aq aq = lookup_goacc_asyncqueue (thr, false, async);
  160. if (!aq)
  161. return;
  162. acc_prof_info prof_info;
  163. acc_api_info api_info;
  164. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  165. if (profiling_p)
  166. {
  167. prof_info.async = async;
  168. prof_info.async_queue = prof_info.async;
  169. }
  170. if (!thr->dev->openacc.async.synchronize_func (aq))
  171. gomp_fatal ("wait on %d failed", async);
  172. if (profiling_p)
  173. {
  174. thr->prof_info = NULL;
  175. thr->api_info = NULL;
  176. }
  177. }
  178. /* acc_async_wait is an OpenACC 1.0 compatibility name for acc_wait. */
  179. #ifdef HAVE_ATTRIBUTE_ALIAS
  180. strong_alias (acc_wait, acc_async_wait)
  181. #else
  182. void
  183. acc_async_wait (int async)
  184. {
  185. acc_wait (async);
  186. }
  187. #endif
  188. void
  189. acc_wait_async (int async1, int async2)
  190. {
  191. struct goacc_thread *thr = get_goacc_thread ();
  192. goacc_aq aq1 = lookup_goacc_asyncqueue (thr, false, async1);
  193. /* TODO: Is this also correct for acc_async_sync, assuming that in this case,
  194. we'll always be synchronous anyways? */
  195. if (!aq1)
  196. return;
  197. acc_prof_info prof_info;
  198. acc_api_info api_info;
  199. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  200. if (profiling_p)
  201. {
  202. prof_info.async = async2;
  203. prof_info.async_queue = prof_info.async;
  204. }
  205. goacc_aq aq2 = lookup_goacc_asyncqueue (thr, true, async2);
  206. /* An async queue is always synchronized with itself. */
  207. if (aq1 == aq2)
  208. goto out_prof;
  209. if (aq2)
  210. {
  211. if (!thr->dev->openacc.async.serialize_func (aq1, aq2))
  212. gomp_fatal ("ordering of async ids %d and %d failed", async1, async2);
  213. }
  214. else
  215. {
  216. /* TODO: Local thread synchronization.
  217. Necessary for the "async2 == acc_async_sync" case, or can just skip? */
  218. if (!thr->dev->openacc.async.synchronize_func (aq1))
  219. gomp_fatal ("wait on %d failed", async1);
  220. }
  221. out_prof:
  222. if (profiling_p)
  223. {
  224. thr->prof_info = NULL;
  225. thr->api_info = NULL;
  226. }
  227. }
  228. void
  229. acc_wait_all (void)
  230. {
  231. struct goacc_thread *thr = goacc_thread ();
  232. acc_prof_info prof_info;
  233. acc_api_info api_info;
  234. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  235. bool ret = true;
  236. gomp_mutex_lock (&thr->dev->openacc.async.lock);
  237. for (goacc_aq_list l = thr->dev->openacc.async.active; l; l = l->next)
  238. ret &= thr->dev->openacc.async.synchronize_func (l->aq);
  239. gomp_mutex_unlock (&thr->dev->openacc.async.lock);
  240. if (profiling_p)
  241. {
  242. thr->prof_info = NULL;
  243. thr->api_info = NULL;
  244. }
  245. if (!ret)
  246. gomp_fatal ("wait all failed");
  247. }
  248. /* acc_async_wait_all is an OpenACC 1.0 compatibility name for acc_wait_all. */
  249. #ifdef HAVE_ATTRIBUTE_ALIAS
  250. strong_alias (acc_wait_all, acc_async_wait_all)
  251. #else
  252. void
  253. acc_async_wait_all (void)
  254. {
  255. acc_wait_all ();
  256. }
  257. #endif
  258. void
  259. acc_wait_all_async (int async)
  260. {
  261. struct goacc_thread *thr = get_goacc_thread ();
  262. acc_prof_info prof_info;
  263. acc_api_info api_info;
  264. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  265. if (profiling_p)
  266. {
  267. prof_info.async = async;
  268. prof_info.async_queue = prof_info.async;
  269. }
  270. goacc_aq waiting_queue = lookup_goacc_asyncqueue (thr, true, async);
  271. bool ret = true;
  272. gomp_mutex_lock (&thr->dev->openacc.async.lock);
  273. for (goacc_aq_list l = thr->dev->openacc.async.active; l; l = l->next)
  274. {
  275. if (waiting_queue)
  276. ret &= thr->dev->openacc.async.serialize_func (l->aq, waiting_queue);
  277. else
  278. /* TODO: Local thread synchronization.
  279. Necessary for the "async2 == acc_async_sync" case, or can just skip? */
  280. ret &= thr->dev->openacc.async.synchronize_func (l->aq);
  281. }
  282. gomp_mutex_unlock (&thr->dev->openacc.async.lock);
  283. if (profiling_p)
  284. {
  285. thr->prof_info = NULL;
  286. thr->api_info = NULL;
  287. }
  288. if (!ret)
  289. gomp_fatal ("wait all async(%d) failed", async);
  290. }
  291. void
  292. GOACC_wait (int async, int num_waits, ...)
  293. {
  294. goacc_lazy_initialize ();
  295. struct goacc_thread *thr = goacc_thread ();
  296. /* No nesting. */
  297. assert (thr->prof_info == NULL);
  298. assert (thr->api_info == NULL);
  299. acc_prof_info prof_info;
  300. acc_api_info api_info;
  301. bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
  302. if (profiling_p)
  303. {
  304. prof_info.async = async;
  305. prof_info.async_queue = prof_info.async;
  306. }
  307. if (num_waits)
  308. {
  309. va_list ap;
  310. va_start (ap, num_waits);
  311. goacc_wait (async, num_waits, &ap);
  312. va_end (ap);
  313. }
  314. else if (async == acc_async_sync)
  315. acc_wait_all ();
  316. else
  317. acc_wait_all_async (async);
  318. if (profiling_p)
  319. {
  320. thr->prof_info = NULL;
  321. thr->api_info = NULL;
  322. }
  323. }
  324. attribute_hidden void
  325. goacc_wait (int async, int num_waits, va_list *ap)
  326. {
  327. while (num_waits--)
  328. {
  329. int qid = va_arg (*ap, int);
  330. /* Waiting on ACC_ASYNC_NOVAL maps to 'wait all'. */
  331. if (qid == acc_async_noval)
  332. {
  333. if (async == acc_async_sync)
  334. acc_wait_all ();
  335. else
  336. acc_wait_all_async (async);
  337. break;
  338. }
  339. if (acc_async_test (qid))
  340. continue;
  341. if (async == acc_async_sync)
  342. acc_wait (qid);
  343. else if (qid == async)
  344. /* If we're waiting on the same asynchronous queue as we're
  345. launching on, the queue itself will order work as
  346. required, so there's no need to wait explicitly. */
  347. ;
  348. else
  349. acc_wait_async (qid, async);
  350. }
  351. }
  352. attribute_hidden void
  353. goacc_async_free (struct gomp_device_descr *devicep,
  354. struct goacc_asyncqueue *aq, void *ptr)
  355. {
  356. if (!aq)
  357. free (ptr);
  358. else
  359. devicep->openacc.async.queue_callback_func (aq, free, ptr);
  360. }
  361. /* This function initializes the asyncqueues for the device specified by
  362. DEVICEP. TODO DEVICEP must be locked on entry, and remains locked on
  363. return. */
  364. attribute_hidden void
  365. goacc_init_asyncqueues (struct gomp_device_descr *devicep)
  366. {
  367. devicep->openacc.async.nasyncqueue = 0;
  368. devicep->openacc.async.asyncqueue = NULL;
  369. devicep->openacc.async.active = NULL;
  370. gomp_mutex_init (&devicep->openacc.async.lock);
  371. }
  372. /* This function finalizes the asyncqueues for the device specified by DEVICEP.
  373. TODO DEVICEP must be locked on entry, and remains locked on return. */
  374. attribute_hidden bool
  375. goacc_fini_asyncqueues (struct gomp_device_descr *devicep)
  376. {
  377. bool ret = true;
  378. gomp_mutex_lock (&devicep->openacc.async.lock);
  379. if (devicep->openacc.async.nasyncqueue > 0)
  380. {
  381. goacc_aq_list next;
  382. for (goacc_aq_list l = devicep->openacc.async.active; l; l = next)
  383. {
  384. ret &= devicep->openacc.async.destruct_func (l->aq);
  385. next = l->next;
  386. free (l);
  387. }
  388. free (devicep->openacc.async.asyncqueue);
  389. devicep->openacc.async.nasyncqueue = 0;
  390. devicep->openacc.async.asyncqueue = NULL;
  391. devicep->openacc.async.active = NULL;
  392. }
  393. gomp_mutex_unlock (&devicep->openacc.async.lock);
  394. gomp_mutex_destroy (&devicep->openacc.async.lock);
  395. return ret;
  396. }