Debian Codegraph @

libgomp/oacc-mem.c

raw
1 /* OpenACC Runtime initialization routines
2
3 Copyright (C) 2013-2025 Free Software Foundation, Inc.
4
5 Contributed by Mentor Embedded.
6
7 This file is part of the GNU Offloading and Multi Processing Library
8 (libgomp).
9
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.
14
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.
19
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.
23
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/>. */
28
29 #include "openacc.h"
30 #include "libgomp.h"
31 #include "gomp-constants.h"
32 #include "oacc-int.h"
33 #include <string.h>
34 #include <assert.h>
35
36 /* Return block containing [H->S), or NULL if not contained. The device lock
37 for DEV must be locked on entry, and remains locked on exit. */
38
39 static splay_tree_key
40 lookup_host (struct gomp_device_descr *dev, void *h, size_t s)
41 {
42 struct splay_tree_key_s node;
43 splay_tree_key key;
44
45 node.host_start = (uintptr_t) h;
46 node.host_end = (uintptr_t) h + s;
47
48 key = splay_tree_lookup (&dev->mem_map, &node);
49
50 return key;
51 }
52
53 /* Helper for lookup_dev. Iterate over splay tree. */
54
55 static splay_tree_key
56 lookup_dev_1 (splay_tree_node node, uintptr_t d, size_t s)
57 {
58 splay_tree_key key = &node->key;
59 if (d >= key->tgt->tgt_start && d + s <= key->tgt->tgt_end)
60 return key;
61
62 key = NULL;
63 if (node->left)
64 key = lookup_dev_1 (node->left, d, s);
65 if (!key && node->right)
66 key = lookup_dev_1 (node->right, d, s);
67
68 return key;
69 }
70
71 /* Return block containing [D->S), or NULL if not contained.
72
73 This iterates over the splay tree. This is not expected to be a common
74 operation.
75
76 The device lock associated with MEM_MAP must be locked on entry, and remains
77 locked on exit. */
78
79 static splay_tree_key
80 lookup_dev (splay_tree mem_map, void *d, size_t s)
81 {
82 if (!mem_map || !mem_map->root)
83 return NULL;
84
85 return lookup_dev_1 (mem_map->root, (uintptr_t) d, s);
86 }
87
88
89 /* OpenACC is silent on how memory exhaustion is indicated. We return
90 NULL. */
91
92 void *
93 acc_malloc (size_t s)
94 {
95 if (!s)
96 return NULL;
97
98 goacc_lazy_initialize ();
99
100 struct goacc_thread *thr = goacc_thread ();
101
102 assert (thr->dev);
103
104 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
105 return malloc (s);
106
107 acc_prof_info prof_info;
108 acc_api_info api_info;
109 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
110
111 void *res = thr->dev->alloc_func (thr->dev->target_id, s);
112
113 if (profiling_p)
114 {
115 thr->prof_info = NULL;
116 thr->api_info = NULL;
117 }
118
119 return res;
120 }
121
122 void
123 acc_free (void *d)
124 {
125 splay_tree_key k;
126
127 if (!d)
128 return;
129
130 struct goacc_thread *thr = goacc_thread ();
131
132 assert (thr && thr->dev);
133
134 struct gomp_device_descr *acc_dev = thr->dev;
135
136 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
137 return free (d);
138
139 acc_prof_info prof_info;
140 acc_api_info api_info;
141 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
142
143 gomp_mutex_lock (&acc_dev->lock);
144
145 /* We don't have to call lazy open here, as the ptr value must have
146 been returned by acc_malloc. It's not permitted to pass NULL in
147 (unless you got that null from acc_malloc). */
148 if ((k = lookup_dev (&acc_dev->mem_map, d, 1)))
149 {
150 void *offset = d - k->tgt->tgt_start + k->tgt_offset;
151 void *h = k->host_start + offset;
152 size_t h_size = k->host_end - k->host_start;
153 gomp_mutex_unlock (&acc_dev->lock);
154 /* PR92503 "[OpenACC] Behavior of 'acc_free' if the memory space is still
155 used in a mapping". */
156 gomp_fatal ("refusing to free device memory space at %p that is still"
157 " mapped at [%p,+%d]",
158 d, h, (int) h_size);
159 }
160 else
161 gomp_mutex_unlock (&acc_dev->lock);
162
163 if (!acc_dev->free_func (acc_dev->target_id, d))
164 gomp_fatal ("error in freeing device memory in %s", __FUNCTION__);
165
166 if (profiling_p)
167 {
168 thr->prof_info = NULL;
169 thr->api_info = NULL;
170 }
171 }
172
173 static void
174 memcpy_tofrom_device (bool from, void *d, void *h, size_t s, int async,
175 const char *libfnname)
176 {
177 /* No need to call lazy open here, as the device pointer must have
178 been obtained from a routine that did that. */
179 struct goacc_thread *thr = goacc_thread ();
180
181 assert (thr && thr->dev);
182
183 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
184 {
185 if (from)
186 memmove (h, d, s);
187 else
188 memmove (d, h, s);
189 return;
190 }
191
192 acc_prof_info prof_info;
193 acc_api_info api_info;
194 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
195 if (profiling_p)
196 {
197 prof_info.async = async;
198 prof_info.async_queue = prof_info.async;
199 }
200
201 goacc_aq aq = get_goacc_asyncqueue (async);
202 if (from)
203 gomp_copy_dev2host (thr->dev, aq, h, d, s);
204 else
205 gomp_copy_host2dev (thr->dev, aq, d, h, s, false, /* TODO: cbuf? */ NULL);
206
207 if (profiling_p)
208 {
209 thr->prof_info = NULL;
210 thr->api_info = NULL;
211 }
212 }
213
214 void
215 acc_memcpy_to_device (void *d, void *h, size_t s)
216 {
217 memcpy_tofrom_device (false, d, h, s, acc_async_sync, __FUNCTION__);
218 }
219
220 void
221 acc_memcpy_to_device_async (void *d, void *h, size_t s, int async)
222 {
223 memcpy_tofrom_device (false, d, h, s, async, __FUNCTION__);
224 }
225
226 void
227 acc_memcpy_from_device (void *h, void *d, size_t s)
228 {
229 memcpy_tofrom_device (true, d, h, s, acc_async_sync, __FUNCTION__);
230 }
231
232 void
233 acc_memcpy_from_device_async (void *h, void *d, size_t s, int async)
234 {
235 memcpy_tofrom_device (true, d, h, s, async, __FUNCTION__);
236 }
237
238 /* Return the device pointer that corresponds to host data H. Or NULL
239 if no mapping. */
240
241 void *
242 acc_deviceptr (void *h)
243 {
244 splay_tree_key n;
245 void *d;
246 void *offset;
247
248 goacc_lazy_initialize ();
249
250 struct goacc_thread *thr = goacc_thread ();
251 struct gomp_device_descr *dev = thr->dev;
252
253 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
254 return h;
255
256 /* In the following, no OpenACC Profiling Interface events can possibly be
257 generated. */
258
259 gomp_mutex_lock (&dev->lock);
260
261 n = lookup_host (dev, h, 1);
262
263 if (!n)
264 {
265 gomp_mutex_unlock (&dev->lock);
266 return NULL;
267 }
268
269 offset = h - n->host_start;
270
271 d = n->tgt->tgt_start + n->tgt_offset + offset;
272
273 gomp_mutex_unlock (&dev->lock);
274
275 return d;
276 }
277
278 /* Return the host pointer that corresponds to device data D. Or NULL
279 if no mapping. */
280
281 void *
282 acc_hostptr (void *d)
283 {
284 splay_tree_key n;
285 void *h;
286 void *offset;
287
288 goacc_lazy_initialize ();
289
290 struct goacc_thread *thr = goacc_thread ();
291 struct gomp_device_descr *acc_dev = thr->dev;
292
293 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
294 return d;
295
296 /* In the following, no OpenACC Profiling Interface events can possibly be
297 generated. */
298
299 gomp_mutex_lock (&acc_dev->lock);
300
301 n = lookup_dev (&acc_dev->mem_map, d, 1);
302
303 if (!n)
304 {
305 gomp_mutex_unlock (&acc_dev->lock);
306 return NULL;
307 }
308
309 offset = d - n->tgt->tgt_start + n->tgt_offset;
310
311 h = n->host_start + offset;
312
313 gomp_mutex_unlock (&acc_dev->lock);
314
315 return h;
316 }
317
318 /* Return 1 if host data [H,+S] is present on the device. */
319
320 int
321 acc_is_present (void *h, size_t s)
322 {
323 splay_tree_key n;
324
325 if (!s || !h)
326 return 0;
327
328 goacc_lazy_initialize ();
329
330 struct goacc_thread *thr = goacc_thread ();
331 struct gomp_device_descr *acc_dev = thr->dev;
332
333 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
334 return h != NULL;
335
336 /* In the following, no OpenACC Profiling Interface events can possibly be
337 generated. */
338
339 gomp_mutex_lock (&acc_dev->lock);
340
341 n = lookup_host (acc_dev, h, s);
342
343 if (n && ((uintptr_t)h < n->host_start
344 || (uintptr_t)h + s > n->host_end
345 || s > n->host_end - n->host_start))
346 n = NULL;
347
348 gomp_mutex_unlock (&acc_dev->lock);
349
350 return n != NULL;
351 }
352
353 /* Create a mapping for host [H,+S] -> device [D,+S] */
354
355 void
356 acc_map_data (void *h, void *d, size_t s)
357 {
358 size_t mapnum = 1;
359 void *hostaddrs = h;
360 void *devaddrs = d;
361 size_t sizes = s;
362 unsigned short kinds = GOMP_MAP_ALLOC;
363
364 goacc_lazy_initialize ();
365
366 struct goacc_thread *thr = goacc_thread ();
367 struct gomp_device_descr *acc_dev = thr->dev;
368
369 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
370 {
371 if (d != h)
372 gomp_fatal ("cannot map data on shared-memory system");
373 }
374 else
375 {
376 struct goacc_thread *thr = goacc_thread ();
377
378 if (!d || !h || !s)
379 gomp_fatal ("[%p,+%d]->[%p,+%d] is a bad map",
380 (void *)h, (int)s, (void *)d, (int)s);
381
382 acc_prof_info prof_info;
383 acc_api_info api_info;
384 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
385
386 gomp_mutex_lock (&acc_dev->lock);
387
388 if (lookup_host (acc_dev, h, s))
389 {
390 gomp_mutex_unlock (&acc_dev->lock);
391 gomp_fatal ("host address [%p, +%d] is already mapped", (void *)h,
392 (int)s);
393 }
394
395 if (lookup_dev (&thr->dev->mem_map, d, s))
396 {
397 gomp_mutex_unlock (&acc_dev->lock);
398 gomp_fatal ("device address [%p, +%d] is already mapped", (void *)d,
399 (int)s);
400 }
401
402 gomp_mutex_unlock (&acc_dev->lock);
403
404 struct target_mem_desc *tgt
405 = goacc_map_vars (acc_dev, NULL, mapnum, &hostaddrs, &devaddrs, &sizes,
406 &kinds, true, GOMP_MAP_VARS_ENTER_DATA);
407 assert (tgt);
408 assert (tgt->list_count == 1);
409 splay_tree_key n = tgt->list[0].key;
410 assert (n);
411 assert (n->refcount == 1);
412 assert (n->dynamic_refcount == 0);
413 /* Special reference counting behavior. */
414 n->refcount = REFCOUNT_ACC_MAP_DATA;
415 n->dynamic_refcount = 1;
416
417 if (profiling_p)
418 {
419 thr->prof_info = NULL;
420 thr->api_info = NULL;
421 }
422 }
423 }
424
425 void
426 acc_unmap_data (void *h)
427 {
428 struct goacc_thread *thr = goacc_thread ();
429 struct gomp_device_descr *acc_dev = thr->dev;
430
431 /* No need to call lazy open, as the address must have been mapped. */
432
433 /* This is a no-op on shared-memory targets. */
434 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
435 return;
436
437 acc_prof_info prof_info;
438 acc_api_info api_info;
439 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
440
441 gomp_mutex_lock (&acc_dev->lock);
442
443 splay_tree_key n = lookup_host (acc_dev, h, 1);
444
445 if (!n)
446 {
447 gomp_mutex_unlock (&acc_dev->lock);
448 gomp_fatal ("%p is not a mapped block", (void *)h);
449 }
450
451 size_t host_size = n->host_end - n->host_start;
452
453 if (n->host_start != (uintptr_t) h)
454 {
455 gomp_mutex_unlock (&acc_dev->lock);
456 gomp_fatal ("[%p,%d] surrounds %p",
457 (void *) n->host_start, (int) host_size, (void *) h);
458 }
459 else if (n->refcount != REFCOUNT_ACC_MAP_DATA)
460 {
461 gomp_mutex_unlock (&acc_dev->lock);
462 gomp_fatal ("refusing to unmap block [%p,+%d] that has not been mapped"
463 " by 'acc_map_data'",
464 (void *) h, (int) host_size);
465 }
466
467 /* This should hold for all mappings created by acc_map_data. We are however
468 removing this mapping in a "finalize" manner, so dynamic_refcount > 1 does
469 not matter. */
470 assert (n->dynamic_refcount >= 1);
471
472 struct target_mem_desc *tgt = n->tgt;
473
474 /* Above, we've verified that the mapping must have been set up by
475 'acc_map_data'. */
476 assert (tgt->refcount == 1);
477
478 /* Nullifying these fields prevents 'gomp_unmap_tgt' via 'gomp_remove_var'
479 from freeing the target memory. */
480 tgt->tgt_end = 0;
481 tgt->to_free = NULL;
482
483 bool is_tgt_unmapped __attribute__((unused)) = gomp_remove_var (acc_dev, n);
484 assert (is_tgt_unmapped);
485
486 gomp_mutex_unlock (&acc_dev->lock);
487
488 if (profiling_p)
489 {
490 thr->prof_info = NULL;
491 thr->api_info = NULL;
492 }
493 }
494
495
496 /* Helper function to map a single dynamic data item, represented by a single
497 mapping. The acc_dev->lock should be held on entry, and remains locked on
498 exit. */
499
500 static void *
501 goacc_map_var_existing (struct gomp_device_descr *acc_dev, void *hostaddr,
502 size_t size, splay_tree_key n)
503 {
504 assert (n);
505
506 /* Present. */
507 void *d = (void *) (n->tgt->tgt_start + n->tgt_offset + hostaddr
508 - n->host_start);
509
510 if (hostaddr + size > (void *) n->host_end)
511 {
512 gomp_mutex_unlock (&acc_dev->lock);
513 gomp_fatal ("[%p,+%d] not mapped", hostaddr, (int) size);
514 }
515
516 assert (n->refcount != REFCOUNT_LINK);
517 if (n->refcount != REFCOUNT_INFINITY
518 && n->refcount != REFCOUNT_ACC_MAP_DATA)
519 n->refcount++;
520 n->dynamic_refcount++;
521
522 return d;
523 }
524
525 /* Enter dynamic mapping for a single datum. Return the device pointer. */
526
527 static void *
528 goacc_enter_datum (void **hostaddrs, size_t *sizes, void *kinds, int async)
529 {
530 void *d;
531 splay_tree_key n;
532
533 if (!hostaddrs[0] || !sizes[0])
534 gomp_fatal ("[%p,+%d] is a bad range", hostaddrs[0], (int) sizes[0]);
535
536 goacc_lazy_initialize ();
537
538 struct goacc_thread *thr = goacc_thread ();
539 struct gomp_device_descr *acc_dev = thr->dev;
540
541 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
542 return hostaddrs[0];
543
544 acc_prof_info prof_info;
545 acc_api_info api_info;
546 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
547 if (profiling_p)
548 {
549 prof_info.async = async;
550 prof_info.async_queue = prof_info.async;
551 }
552
553 gomp_mutex_lock (&acc_dev->lock);
554
555 n = lookup_host (acc_dev, hostaddrs[0], sizes[0]);
556 if (n)
557 {
558 d = goacc_map_var_existing (acc_dev, hostaddrs[0], sizes[0], n);
559 gomp_mutex_unlock (&acc_dev->lock);
560 }
561 else
562 {
563 const size_t mapnum = 1;
564
565 gomp_mutex_unlock (&acc_dev->lock);
566
567 goacc_aq aq = get_goacc_asyncqueue (async);
568
569 struct target_mem_desc *tgt
570 = goacc_map_vars (acc_dev, aq, mapnum, hostaddrs, NULL, sizes,
571 kinds, true, GOMP_MAP_VARS_ENTER_DATA);
572 assert (tgt);
573 assert (tgt->list_count == 1);
574 n = tgt->list[0].key;
575 assert (n);
576 assert (n->refcount == 1);
577 assert (n->dynamic_refcount == 0);
578 n->dynamic_refcount++;
579
580 d = (void *) tgt->tgt_start;
581 }
582
583 if (profiling_p)
584 {
585 thr->prof_info = NULL;
586 thr->api_info = NULL;
587 }
588
589 return d;
590 }
591
592 void *
593 acc_create (void *h, size_t s)
594 {
595 unsigned short kinds[1] = { GOMP_MAP_ALLOC };
596 return goacc_enter_datum (&h, &s, &kinds, acc_async_sync);
597 }
598
599 void
600 acc_create_async (void *h, size_t s, int async)
601 {
602 unsigned short kinds[1] = { GOMP_MAP_ALLOC };
603 goacc_enter_datum (&h, &s, &kinds, async);
604 }
605
606 /* acc_present_or_create used to be what acc_create is now. */
607 /* acc_pcreate is acc_present_or_create by a different name. */
608 #ifdef HAVE_ATTRIBUTE_ALIAS
609 strong_alias (acc_create, acc_present_or_create)
610 strong_alias (acc_create, acc_pcreate)
611 #else
612 void *
613 acc_present_or_create (void *h, size_t s)
614 {
615 return acc_create (h, s);
616 }
617
618 void *
619 acc_pcreate (void *h, size_t s)
620 {
621 return acc_create (h, s);
622 }
623 #endif
624
625 void *
626 acc_copyin (void *h, size_t s)
627 {
628 unsigned short kinds[1] = { GOMP_MAP_TO };
629 return goacc_enter_datum (&h, &s, &kinds, acc_async_sync);
630 }
631
632 void
633 acc_copyin_async (void *h, size_t s, int async)
634 {
635 unsigned short kinds[1] = { GOMP_MAP_TO };
636 goacc_enter_datum (&h, &s, &kinds, async);
637 }
638
639 /* acc_present_or_copyin used to be what acc_copyin is now. */
640 /* acc_pcopyin is acc_present_or_copyin by a different name. */
641 #ifdef HAVE_ATTRIBUTE_ALIAS
642 strong_alias (acc_copyin, acc_present_or_copyin)
643 strong_alias (acc_copyin, acc_pcopyin)
644 #else
645 void *
646 acc_present_or_copyin (void *h, size_t s)
647 {
648 return acc_copyin (h, s);
649 }
650
651 void *
652 acc_pcopyin (void *h, size_t s)
653 {
654 return acc_copyin (h, s);
655 }
656 #endif
657
658
659 /* Helper function to unmap a single data item. Device lock should be held on
660 entry, and remains locked on exit. */
661
662 static void
663 goacc_exit_datum_1 (struct gomp_device_descr *acc_dev, void *h, size_t s,
664 unsigned short kind, splay_tree_key n, goacc_aq aq)
665 {
666 assert (kind != GOMP_MAP_DETACH
667 && kind != GOMP_MAP_FORCE_DETACH);
668
669 if ((uintptr_t) h < n->host_start || (uintptr_t) h + s > n->host_end)
670 {
671 size_t host_size = n->host_end - n->host_start;
672 gomp_mutex_unlock (&acc_dev->lock);
673 gomp_fatal ("[%p,+%d] outside mapped block [%p,+%d]",
674 (void *) h, (int) s, (void *) n->host_start, (int) host_size);
675 }
676
677 bool finalize = (kind == GOMP_MAP_FORCE_FROM
678 || kind == GOMP_MAP_DELETE);
679
680 assert (n->refcount != REFCOUNT_LINK);
681 if (n->refcount != REFCOUNT_INFINITY
682 && n->refcount != REFCOUNT_ACC_MAP_DATA
683 && n->refcount < n->dynamic_refcount)
684 {
685 gomp_mutex_unlock (&acc_dev->lock);
686 gomp_fatal ("Dynamic reference counting assert fail\n");
687 }
688
689 if (n->refcount == REFCOUNT_ACC_MAP_DATA)
690 {
691 if (finalize)
692 {
693 /* Mappings created by acc_map_data are returned to initial
694 dynamic_refcount of 1. Can only be deleted by acc_unmap_data. */
695 n->dynamic_refcount = 1;
696 }
697 else if (n->dynamic_refcount)
698 {
699 /* When mapping is created by acc_map_data, dynamic_refcount must be
700 maintained at >= 1. */
701 if (n->dynamic_refcount > 1)
702 n->dynamic_refcount--;
703 }
704 }
705 else if (finalize)
706 {
707 if (n->refcount != REFCOUNT_INFINITY)
708 n->refcount -= n->dynamic_refcount;
709 n->dynamic_refcount = 0;
710 }
711 else if (n->dynamic_refcount)
712 {
713 if (n->refcount != REFCOUNT_INFINITY)
714 n->refcount--;
715 n->dynamic_refcount--;
716 }
717
718 if (n->refcount == 0)
719 {
720 bool copyout = (kind == GOMP_MAP_FROM
721 || kind == GOMP_MAP_FORCE_FROM);
722 if (copyout)
723 {
724 void *d = (void *) (n->tgt->tgt_start + n->tgt_offset
725 + (uintptr_t) h - n->host_start);
726 gomp_copy_dev2host (acc_dev, aq, h, d, s);
727 }
728
729 if (aq)
730 /* TODO We can't do the 'is_tgt_unmapped' checking -- see the
731 'gomp_unref_tgt' comment in
732 <http://mid.mail-archive.com/[email protected]>;
733 PR92881. */
734 gomp_remove_var_async (acc_dev, n, aq);
735 else
736 {
737 size_t num_mappings = 0;
738 /* If the target_mem_desc represents a single data mapping, we can
739 check that it is freed when this splay tree key's refcount reaches
740 zero. Otherwise (e.g. for a 'GOMP_MAP_STRUCT' mapping with
741 multiple members), fall back to skipping the test. */
742 for (size_t l_i = 0; l_i < n->tgt->list_count; ++l_i)
743 if (n->tgt->list[l_i].key
744 && !n->tgt->list[l_i].is_attach)
745 ++num_mappings;
746 bool is_tgt_unmapped __attribute__((unused))
747 = gomp_remove_var (acc_dev, n);
748 assert (is_tgt_unmapped || num_mappings > 1);
749 }
750 }
751 }
752
753
754 /* Exit a dynamic mapping for a single variable. */
755
756 static void
757 goacc_exit_datum (void *h, size_t s, unsigned short kind, int async)
758 {
759 /* No need to call lazy open, as the data must already have been
760 mapped. */
761
762 kind &= 0xff;
763
764 struct goacc_thread *thr = goacc_thread ();
765 struct gomp_device_descr *acc_dev = thr->dev;
766
767 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
768 return;
769
770 acc_prof_info prof_info;
771 acc_api_info api_info;
772 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
773 if (profiling_p)
774 {
775 prof_info.async = async;
776 prof_info.async_queue = prof_info.async;
777 }
778
779 gomp_mutex_lock (&acc_dev->lock);
780
781 splay_tree_key n = lookup_host (acc_dev, h, s);
782 /* Non-present data is a no-op: PR92726, RP92970, PR92984. */
783 if (n)
784 {
785 goacc_aq aq = get_goacc_asyncqueue (async);
786 goacc_exit_datum_1 (acc_dev, h, s, kind, n, aq);
787 }
788
789 gomp_mutex_unlock (&acc_dev->lock);
790
791 if (profiling_p)
792 {
793 thr->prof_info = NULL;
794 thr->api_info = NULL;
795 }
796 }
797
798 void
799 acc_delete (void *h , size_t s)
800 {
801 goacc_exit_datum (h, s, GOMP_MAP_RELEASE, acc_async_sync);
802 }
803
804 void
805 acc_delete_async (void *h , size_t s, int async)
806 {
807 goacc_exit_datum (h, s, GOMP_MAP_RELEASE, async);
808 }
809
810 void
811 acc_delete_finalize (void *h , size_t s)
812 {
813 goacc_exit_datum (h, s, GOMP_MAP_DELETE, acc_async_sync);
814 }
815
816 void
817 acc_delete_finalize_async (void *h , size_t s, int async)
818 {
819 goacc_exit_datum (h, s, GOMP_MAP_DELETE, async);
820 }
821
822 void
823 acc_copyout (void *h, size_t s)
824 {
825 goacc_exit_datum (h, s, GOMP_MAP_FROM, acc_async_sync);
826 }
827
828 void
829 acc_copyout_async (void *h, size_t s, int async)
830 {
831 goacc_exit_datum (h, s, GOMP_MAP_FROM, async);
832 }
833
834 void
835 acc_copyout_finalize (void *h, size_t s)
836 {
837 goacc_exit_datum (h, s, GOMP_MAP_FORCE_FROM, acc_async_sync);
838 }
839
840 void
841 acc_copyout_finalize_async (void *h, size_t s, int async)
842 {
843 goacc_exit_datum (h, s, GOMP_MAP_FORCE_FROM, async);
844 }
845
846 static void
847 update_dev_host (int is_dev, void *h, size_t s, int async)
848 {
849 splay_tree_key n;
850 void *d;
851
852 goacc_lazy_initialize ();
853
854 struct goacc_thread *thr = goacc_thread ();
855 struct gomp_device_descr *acc_dev = thr->dev;
856
857 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
858 return;
859
860 /* Fortran optional arguments that are non-present result in a
861 NULL host address here. This can safely be ignored as it is
862 not possible to 'update' a non-present optional argument. */
863 if (h == NULL)
864 return;
865
866 acc_prof_info prof_info;
867 acc_api_info api_info;
868 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
869 if (profiling_p)
870 {
871 prof_info.async = async;
872 prof_info.async_queue = prof_info.async;
873 }
874
875 gomp_mutex_lock (&acc_dev->lock);
876
877 n = lookup_host (acc_dev, h, s);
878
879 if (!n)
880 {
881 gomp_mutex_unlock (&acc_dev->lock);
882 gomp_fatal ("[%p,%d] is not mapped", h, (int)s);
883 }
884
885 d = (void *) (n->tgt->tgt_start + n->tgt_offset
886 + (uintptr_t) h - n->host_start);
887
888 goacc_aq aq = get_goacc_asyncqueue (async);
889
890 if (is_dev)
891 gomp_copy_host2dev (acc_dev, aq, d, h, s, false, /* TODO: cbuf? */ NULL);
892 else
893 gomp_copy_dev2host (acc_dev, aq, h, d, s);
894
895 gomp_mutex_unlock (&acc_dev->lock);
896
897 if (profiling_p)
898 {
899 thr->prof_info = NULL;
900 thr->api_info = NULL;
901 }
902 }
903
904 void
905 acc_update_device (void *h, size_t s)
906 {
907 update_dev_host (1, h, s, acc_async_sync);
908 }
909
910 void
911 acc_update_device_async (void *h, size_t s, int async)
912 {
913 update_dev_host (1, h, s, async);
914 }
915
916 void
917 acc_update_self (void *h, size_t s)
918 {
919 update_dev_host (0, h, s, acc_async_sync);
920 }
921
922 void
923 acc_update_self_async (void *h, size_t s, int async)
924 {
925 update_dev_host (0, h, s, async);
926 }
927
928 void
929 acc_attach_async (void **hostaddr, int async)
930 {
931 struct goacc_thread *thr = goacc_thread ();
932 struct gomp_device_descr *acc_dev = thr->dev;
933 goacc_aq aq = get_goacc_asyncqueue (async);
934
935 struct splay_tree_key_s cur_node;
936 splay_tree_key n;
937
938 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
939 return;
940
941 gomp_mutex_lock (&acc_dev->lock);
942
943 cur_node.host_start = (uintptr_t) hostaddr;
944 cur_node.host_end = cur_node.host_start + sizeof (void *);
945 n = splay_tree_lookup (&acc_dev->mem_map, &cur_node);
946
947 if (n == NULL)
948 {
949 gomp_mutex_unlock (&acc_dev->lock);
950 gomp_fatal ("struct not mapped for acc_attach");
951 }
952
953 gomp_attach_pointer (acc_dev, aq, &acc_dev->mem_map, n, (uintptr_t) hostaddr,
954 0, NULL, false, true);
955
956 gomp_mutex_unlock (&acc_dev->lock);
957 }
958
959 void
960 acc_attach (void **hostaddr)
961 {
962 acc_attach_async (hostaddr, acc_async_sync);
963 }
964
965 static void
966 goacc_detach_internal (void **hostaddr, int async, bool finalize)
967 {
968 struct goacc_thread *thr = goacc_thread ();
969 struct gomp_device_descr *acc_dev = thr->dev;
970 struct splay_tree_key_s cur_node;
971 splay_tree_key n;
972 struct goacc_asyncqueue *aq = get_goacc_asyncqueue (async);
973
974 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
975 return;
976
977 gomp_mutex_lock (&acc_dev->lock);
978
979 cur_node.host_start = (uintptr_t) hostaddr;
980 cur_node.host_end = cur_node.host_start + sizeof (void *);
981 n = splay_tree_lookup (&acc_dev->mem_map, &cur_node);
982
983 if (n == NULL)
984 {
985 gomp_mutex_unlock (&acc_dev->lock);
986 gomp_fatal ("struct not mapped for acc_detach");
987 }
988
989 gomp_detach_pointer (acc_dev, aq, n, (uintptr_t) hostaddr, finalize, NULL);
990
991 gomp_mutex_unlock (&acc_dev->lock);
992 }
993
994 void
995 acc_detach (void **hostaddr)
996 {
997 goacc_detach_internal (hostaddr, acc_async_sync, false);
998 }
999
1000 void
1001 acc_detach_async (void **hostaddr, int async)
1002 {
1003 goacc_detach_internal (hostaddr, async, false);
1004 }
1005
1006 void
1007 acc_detach_finalize (void **hostaddr)
1008 {
1009 goacc_detach_internal (hostaddr, acc_async_sync, true);
1010 }
1011
1012 void
1013 acc_detach_finalize_async (void **hostaddr, int async)
1014 {
1015 goacc_detach_internal (hostaddr, async, true);
1016 }
1017
1018 /* Some types of (pointer) variables use several consecutive mappings, which
1019 must be treated as a group for enter/exit data directives. This function
1020 returns the last mapping in such a group (inclusive), or POS for singleton
1021 mappings. */
1022
1023 static int
1024 find_group_last (int pos, size_t mapnum, size_t *sizes, unsigned short *kinds)
1025 {
1026 unsigned char kind0 = kinds[pos] & 0xff;
1027 int first_pos __attribute__((unused)) = pos;
1028
1029 switch (kind0)
1030 {
1031 case GOMP_MAP_TO_PSET:
1032 if (pos + 1 < mapnum
1033 && (kinds[pos + 1] & 0xff) == GOMP_MAP_ATTACH)
1034 return pos + 1;
1035
1036 while (pos + 1 < mapnum
1037 && (kinds[pos + 1] & 0xff) == GOMP_MAP_POINTER)
1038 pos++;
1039 /* We expect at least one GOMP_MAP_POINTER (if not a single
1040 GOMP_MAP_ATTACH) after a GOMP_MAP_TO_PSET. */
1041 assert (pos > first_pos);
1042 break;
1043
1044 case GOMP_MAP_STRUCT:
1045 case GOMP_MAP_STRUCT_UNORD:
1046 pos += sizes[pos];
1047 break;
1048
1049 case GOMP_MAP_POINTER:
1050 case GOMP_MAP_ALWAYS_POINTER:
1051 /* These mappings are only expected after some other mapping. If we
1052 see one by itself, something has gone wrong. */
1053 gomp_fatal ("unexpected mapping");
1054 break;
1055
1056 case GOMP_MAP_ATTACH:
1057 break;
1058
1059 default:
1060 /* GOMP_MAP_ALWAYS_POINTER can only appear directly after some other
1061 mapping. */
1062 if (pos + 1 < mapnum)
1063 {
1064 unsigned char kind1 = kinds[pos + 1] & 0xff;
1065 if (kind1 == GOMP_MAP_ALWAYS_POINTER)
1066 return pos + 1;
1067 }
1068
1069 /* We can have a single GOMP_MAP_ATTACH mapping after a to/from
1070 mapping. */
1071 if (pos + 1 < mapnum
1072 && (kinds[pos + 1] & 0xff) == GOMP_MAP_ATTACH)
1073 return pos + 1;
1074
1075 /* We can have zero or more GOMP_MAP_POINTER mappings after a to/from
1076 (etc.) mapping. */
1077 while (pos + 1 < mapnum
1078 && (kinds[pos + 1] & 0xff) == GOMP_MAP_POINTER)
1079 pos++;
1080 }
1081
1082 return pos;
1083 }
1084
1085 /* Map variables for OpenACC "enter data". We can't just call
1086 goacc_map_vars once, because individual mapped variables might have
1087 "exit data" called for them at different times. */
1088
1089 static void
1090 goacc_enter_data_internal (struct gomp_device_descr *acc_dev, size_t mapnum,
1091 void **hostaddrs, size_t *sizes,
1092 unsigned short *kinds, goacc_aq aq)
1093 {
1094 gomp_mutex_lock (&acc_dev->lock);
1095
1096 for (size_t i = 0; i < mapnum; i++)
1097 {
1098 splay_tree_key n;
1099 size_t group_last = find_group_last (i, mapnum, sizes, kinds);
1100 bool struct_p = false;
1101 size_t size, groupnum = (group_last - i) + 1;
1102
1103 switch (kinds[i] & 0xff)
1104 {
1105 case GOMP_MAP_STRUCT:
1106 case GOMP_MAP_STRUCT_UNORD:
1107 {
1108 size = (uintptr_t) hostaddrs[group_last] + sizes[group_last]
1109 - (uintptr_t) hostaddrs[i];
1110 struct_p = true;
1111 }
1112 break;
1113
1114 case GOMP_MAP_ATTACH:
1115 size = sizeof (void *);
1116 break;
1117
1118 default:
1119 size = sizes[i];
1120 }
1121
1122 n = lookup_host (acc_dev, hostaddrs[i], size);
1123
1124 if (n && struct_p)
1125 {
1126 for (size_t j = i + 1; j <= group_last; j++)
1127 {
1128 struct splay_tree_key_s cur_node;
1129 cur_node.host_start = (uintptr_t) hostaddrs[j];
1130 cur_node.host_end = cur_node.host_start + sizes[j];
1131 splay_tree_key n2
1132 = splay_tree_lookup (&acc_dev->mem_map, &cur_node);
1133 if (!n2
1134 || n2->tgt != n->tgt
1135 || n2->host_start - n->host_start
1136 != n2->tgt_offset - n->tgt_offset)
1137 {
1138 gomp_mutex_unlock (&acc_dev->lock);
1139 gomp_fatal ("Trying to map into device [%p..%p) structure "
1140 "element when other mapped elements from the "
1141 "same structure weren't mapped together with "
1142 "it", (void *) cur_node.host_start,
1143 (void *) cur_node.host_end);
1144 }
1145 }
1146 /* This is a special case because we must increment the refcount by
1147 the number of mapped struct elements, rather than by one. */
1148 if (n->refcount != REFCOUNT_INFINITY
1149 && n->refcount != REFCOUNT_ACC_MAP_DATA)
1150 n->refcount += groupnum - 1;
1151 n->dynamic_refcount += groupnum - 1;
1152 }
1153 else if (n && groupnum == 1)
1154 {
1155 void *h = hostaddrs[i];
1156 size_t s = sizes[i];
1157
1158 if ((kinds[i] & 0xff) == GOMP_MAP_ATTACH)
1159 {
1160 gomp_attach_pointer (acc_dev, aq, &acc_dev->mem_map, n,
1161 (uintptr_t) h, s, NULL, false, true);
1162 /* OpenACC 'attach'/'detach' doesn't affect structured/dynamic
1163 reference counts ('n->refcount', 'n->dynamic_refcount'). */
1164 }
1165 else
1166 goacc_map_var_existing (acc_dev, h, s, n);
1167 }
1168 else if (n && groupnum > 1)
1169 {
1170 assert (n->refcount != REFCOUNT_LINK);
1171
1172 for (size_t j = i + 1; j <= group_last; j++)
1173 if ((kinds[j] & 0xff) == GOMP_MAP_ATTACH)
1174 {
1175 splay_tree_key m
1176 = lookup_host (acc_dev, hostaddrs[j], sizeof (void *));
1177 gomp_attach_pointer (acc_dev, aq, &acc_dev->mem_map, m,
1178 (uintptr_t) hostaddrs[j], sizes[j], NULL,
1179 false, true);
1180 }
1181
1182 bool processed = false;
1183
1184 struct target_mem_desc *tgt = n->tgt;
1185
1186 /* Minimal OpenACC variant corresponding to PR96668
1187 "[OpenMP] Re-mapping allocated but previously unallocated
1188 allocatable does not work" 'libgomp/target.c' changes, so that
1189 OpenACC 'declare' code à la PR106643
1190 "[gfortran + OpenACC] Allocate in module causes refcount error"
1191 has a chance to work. */
1192 if ((kinds[i] & 0xff) == GOMP_MAP_TO_PSET
1193 && tgt->list_count == 0)
1194 {
1195 /* 'declare target'. */
1196 assert (n->refcount == REFCOUNT_INFINITY);
1197
1198 for (size_t k = 1; k < groupnum; k++)
1199 {
1200 /* The only thing we expect to see here. */
1201 assert ((kinds[i + k] & 0xff) == GOMP_MAP_POINTER);
1202 }
1203
1204 /* Let 'goacc_map_vars' -> 'gomp_map_vars_internal' handle
1205 this. */
1206 gomp_mutex_unlock (&acc_dev->lock);
1207 struct target_mem_desc *tgt_ __attribute__((unused))
1208 = goacc_map_vars (acc_dev, aq, groupnum, &hostaddrs[i], NULL,
1209 &sizes[i], &kinds[i], true,
1210 GOMP_MAP_VARS_ENTER_DATA);
1211 assert (tgt_ == NULL);
1212 gomp_mutex_lock (&acc_dev->lock);
1213
1214 /* Given that 'goacc_exit_data_internal'/'goacc_exit_datum_1'
1215 will always see 'n->refcount == REFCOUNT_INFINITY',
1216 there's no need to adjust 'n->dynamic_refcount' here. */
1217
1218 processed = true;
1219 }
1220 else
1221 assert (n->refcount != REFCOUNT_INFINITY
1222 && n->refcount != REFCOUNT_ACC_MAP_DATA);
1223
1224 for (size_t j = 0; j < tgt->list_count; j++)
1225 if (tgt->list[j].key == n)
1226 {
1227 /* We are processing a group of mappings (e.g.
1228 [GOMP_MAP_TO, GOMP_MAP_TO_PSET, GOMP_MAP_POINTER]).
1229 Find the right group in the target_mem_desc's variable
1230 list, and increment the refcounts for each item in that
1231 group. */
1232 for (size_t k = 0; k < groupnum; k++)
1233 if (j + k < tgt->list_count
1234 && tgt->list[j + k].key
1235 && !tgt->list[j + k].is_attach)
1236 {
1237 tgt->list[j + k].key->refcount++;
1238 tgt->list[j + k].key->dynamic_refcount++;
1239 }
1240 processed = true;
1241 break;
1242 }
1243
1244 if (!processed)
1245 {
1246 gomp_mutex_unlock (&acc_dev->lock);
1247 gomp_fatal ("dynamic refcount incrementing failed for "
1248 "pointer/pset");
1249 }
1250 }
1251 else if (hostaddrs[i])
1252 {
1253 /* The data is not mapped already. Map it now, unless the first
1254 member in the group has a NULL pointer (e.g. a non-present
1255 optional parameter). */
1256 gomp_mutex_unlock (&acc_dev->lock);
1257
1258 struct target_mem_desc *tgt
1259 = goacc_map_vars (acc_dev, aq, groupnum, &hostaddrs[i], NULL,
1260 &sizes[i], &kinds[i], true,
1261 GOMP_MAP_VARS_ENTER_DATA);
1262 assert (tgt);
1263
1264 gomp_mutex_lock (&acc_dev->lock);
1265
1266 for (size_t j = 0; j < tgt->list_count; j++)
1267 {
1268 n = tgt->list[j].key;
1269 if (n && !tgt->list[j].is_attach)
1270 n->dynamic_refcount++;
1271 }
1272 }
1273
1274 i = group_last;
1275 }
1276
1277 gomp_mutex_unlock (&acc_dev->lock);
1278 }
1279
1280 /* Unmap variables for OpenACC "exit data". */
1281
1282 static void
1283 goacc_exit_data_internal (struct gomp_device_descr *acc_dev, size_t mapnum,
1284 void **hostaddrs, size_t *sizes,
1285 unsigned short *kinds, goacc_aq aq)
1286 {
1287 gomp_mutex_lock (&acc_dev->lock);
1288
1289 /* Handle "detach" before copyback/deletion of mapped data. */
1290 for (size_t i = 0; i < mapnum; ++i)
1291 {
1292 unsigned char kind = kinds[i] & 0xff;
1293 bool finalize = false;
1294 switch (kind)
1295 {
1296 case GOMP_MAP_FORCE_DETACH:
1297 finalize = true;
1298 /* Fallthrough. */
1299
1300 case GOMP_MAP_DETACH:
1301 {
1302 struct splay_tree_key_s cur_node;
1303 uintptr_t hostaddr = (uintptr_t) hostaddrs[i];
1304 cur_node.host_start = hostaddr;
1305 cur_node.host_end = cur_node.host_start + sizeof (void *);
1306 splay_tree_key n
1307 = splay_tree_lookup (&acc_dev->mem_map, &cur_node);
1308
1309 if (n == NULL)
1310 {
1311 gomp_mutex_unlock (&acc_dev->lock);
1312 gomp_fatal ("struct not mapped for detach operation");
1313 }
1314
1315 gomp_detach_pointer (acc_dev, aq, n, hostaddr, finalize, NULL);
1316 }
1317 break;
1318 default:
1319 ;
1320 }
1321 }
1322
1323 for (size_t i = 0; i < mapnum; ++i)
1324 {
1325 unsigned char kind = kinds[i] & 0xff;
1326
1327 switch (kind)
1328 {
1329 case GOMP_MAP_FROM:
1330 case GOMP_MAP_FORCE_FROM:
1331 case GOMP_MAP_TO_PSET:
1332 case GOMP_MAP_POINTER:
1333 case GOMP_MAP_DELETE:
1334 case GOMP_MAP_RELEASE:
1335 {
1336 struct splay_tree_key_s cur_node;
1337 size_t size;
1338 if (kind == GOMP_MAP_POINTER)
1339 size = sizeof (void *);
1340 else
1341 size = sizes[i];
1342 cur_node.host_start = (uintptr_t) hostaddrs[i];
1343 cur_node.host_end = cur_node.host_start + size;
1344 splay_tree_key n
1345 = splay_tree_lookup (&acc_dev->mem_map, &cur_node);
1346
1347 if (n == NULL)
1348 continue;
1349
1350 goacc_exit_datum_1 (acc_dev, hostaddrs[i], size, kind, n, aq);
1351 }
1352 break;
1353
1354 case GOMP_MAP_STRUCT:
1355 case GOMP_MAP_STRUCT_UNORD:
1356 /* Skip the 'GOMP_MAP_STRUCT' itself, and use the regular processing
1357 for all its entries. This special handling exists for GCC 10.1
1358 compatibility; afterwards, we're not generating these no-op
1359 'GOMP_MAP_STRUCT's anymore. */
1360 break;
1361
1362 case GOMP_MAP_DETACH:
1363 case GOMP_MAP_FORCE_DETACH:
1364 /* OpenACC 'attach'/'detach' doesn't affect structured/dynamic
1365 reference counts ('n->refcount', 'n->dynamic_refcount'). */
1366 break;
1367
1368 default:
1369 gomp_fatal (">>>> goacc_exit_data_internal UNHANDLED kind 0x%.2x",
1370 kind);
1371 }
1372 }
1373
1374 gomp_mutex_unlock (&acc_dev->lock);
1375 }
1376
1377 static void
1378 goacc_enter_exit_data_internal (int flags_m, size_t mapnum, void **hostaddrs,
1379 size_t *sizes, unsigned short *kinds,
1380 bool data_enter, int async, int num_waits,
1381 va_list *ap)
1382 {
1383 int flags = GOACC_FLAGS_UNMARSHAL (flags_m);
1384
1385 struct goacc_thread *thr;
1386 struct gomp_device_descr *acc_dev;
1387
1388 goacc_lazy_initialize ();
1389
1390 thr = goacc_thread ();
1391 acc_dev = thr->dev;
1392
1393 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
1394
1395 acc_prof_info prof_info;
1396 if (profiling_p)
1397 {
1398 thr->prof_info = &prof_info;
1399
1400 prof_info.event_type
1401 = data_enter ? acc_ev_enter_data_start : acc_ev_exit_data_start;
1402 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
1403 prof_info.version = _ACC_PROF_INFO_VERSION;
1404 prof_info.device_type = acc_device_type (acc_dev->type);
1405 prof_info.device_number = acc_dev->target_id;
1406 prof_info.thread_id = -1;
1407 prof_info.async = async;
1408 prof_info.async_queue = prof_info.async;
1409 prof_info.src_file = NULL;
1410 prof_info.func_name = NULL;
1411 prof_info.line_no = -1;
1412 prof_info.end_line_no = -1;
1413 prof_info.func_line_no = -1;
1414 prof_info.func_end_line_no = -1;
1415 }
1416 acc_event_info enter_exit_data_event_info;
1417 if (profiling_p)
1418 {
1419 enter_exit_data_event_info.other_event.event_type
1420 = prof_info.event_type;
1421 enter_exit_data_event_info.other_event.valid_bytes
1422 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
1423 enter_exit_data_event_info.other_event.parent_construct
1424 = data_enter ? acc_construct_enter_data : acc_construct_exit_data;
1425 enter_exit_data_event_info.other_event.implicit = 0;
1426 enter_exit_data_event_info.other_event.tool_info = NULL;
1427 }
1428 acc_api_info api_info;
1429 if (profiling_p)
1430 {
1431 thr->api_info = &api_info;
1432
1433 api_info.device_api = acc_device_api_none;
1434 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
1435 api_info.device_type = prof_info.device_type;
1436 api_info.vendor = -1;
1437 api_info.device_handle = NULL;
1438 api_info.context_handle = NULL;
1439 api_info.async_handle = NULL;
1440 }
1441
1442 if (profiling_p)
1443 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
1444 &api_info);
1445
1446 if ((acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
1447 || (flags & GOACC_FLAG_HOST_FALLBACK))
1448 {
1449 prof_info.device_type = acc_device_host;
1450 api_info.device_type = prof_info.device_type;
1451
1452 goto out_prof;
1453 }
1454
1455 if (num_waits)
1456 goacc_wait (async, num_waits, ap);
1457
1458 goacc_aq aq = get_goacc_asyncqueue (async);
1459
1460 if (data_enter)
1461 goacc_enter_data_internal (acc_dev, mapnum, hostaddrs, sizes, kinds, aq);
1462 else
1463 goacc_exit_data_internal (acc_dev, mapnum, hostaddrs, sizes, kinds, aq);
1464
1465 out_prof:
1466 if (profiling_p)
1467 {
1468 prof_info.event_type
1469 = data_enter ? acc_ev_enter_data_end : acc_ev_exit_data_end;
1470 enter_exit_data_event_info.other_event.event_type = prof_info.event_type;
1471 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
1472 &api_info);
1473
1474 thr->prof_info = NULL;
1475 thr->api_info = NULL;
1476 }
1477 }
1478
1479 /* Legacy entry point (GCC 11 and earlier). */
1480
1481 void
1482 GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
1483 size_t *sizes, unsigned short *kinds, int async,
1484 int num_waits, ...)
1485 {
1486 /* Determine if this is an OpenACC "enter data". */
1487 bool data_enter = false;
1488 for (size_t i = 0; i < mapnum; ++i)
1489 {
1490 unsigned char kind = kinds[i] & 0xff;
1491
1492 if (kind == GOMP_MAP_POINTER
1493 || kind == GOMP_MAP_TO_PSET
1494 || kind == GOMP_MAP_STRUCT
1495 || kind == GOMP_MAP_STRUCT_UNORD)
1496 continue;
1497
1498 if (kind == GOMP_MAP_FORCE_ALLOC
1499 || kind == GOMP_MAP_FORCE_PRESENT
1500 || kind == GOMP_MAP_ATTACH
1501 || kind == GOMP_MAP_FORCE_TO
1502 || kind == GOMP_MAP_TO
1503 || kind == GOMP_MAP_ALLOC)
1504 {
1505 data_enter = true;
1506 break;
1507 }
1508
1509 if (kind == GOMP_MAP_RELEASE
1510 || kind == GOMP_MAP_DELETE
1511 || kind == GOMP_MAP_DETACH
1512 || kind == GOMP_MAP_FORCE_DETACH
1513 || kind == GOMP_MAP_FROM
1514 || kind == GOMP_MAP_FORCE_FROM)
1515 break;
1516
1517 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1518 kind);
1519 }
1520
1521 va_list ap;
1522 va_start (ap, num_waits);
1523 goacc_enter_exit_data_internal (flags_m, mapnum, hostaddrs, sizes, kinds,
1524 data_enter, async, num_waits, &ap);
1525 va_end (ap);
1526 }
1527
1528 void
1529 GOACC_enter_data (int flags_m, size_t mapnum, void **hostaddrs,
1530 size_t *sizes, unsigned short *kinds, int async,
1531 int num_waits, ...)
1532 {
1533 va_list ap;
1534 va_start (ap, num_waits);
1535 goacc_enter_exit_data_internal (flags_m, mapnum, hostaddrs, sizes, kinds,
1536 true, async, num_waits, &ap);
1537 va_end (ap);
1538 }
1539
1540 void
1541 GOACC_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
1542 size_t *sizes, unsigned short *kinds, int async,
1543 int num_waits, ...)
1544 {
1545 va_list ap;
1546 va_start (ap, num_waits);
1547 goacc_enter_exit_data_internal (flags_m, mapnum, hostaddrs, sizes, kinds,
1548 false, async, num_waits, &ap);
1549 va_end (ap);
1550 }
1551
1552 void
1553 GOACC_declare (int flags_m, size_t mapnum,
1554 void **hostaddrs, size_t *sizes, unsigned short *kinds)
1555 {
1556 for (size_t i = 0; i < mapnum; i++)
1557 {
1558 unsigned char kind = kinds[i] & 0xff;
1559
1560 if (kind == GOMP_MAP_POINTER || kind == GOMP_MAP_TO_PSET)
1561 continue;
1562
1563 switch (kind)
1564 {
1565 case GOMP_MAP_ALLOC:
1566 if (acc_is_present (hostaddrs[i], sizes[i]))
1567 continue;
1568 /* FALLTHRU */
1569 case GOMP_MAP_FORCE_ALLOC:
1570 case GOMP_MAP_TO:
1571 case GOMP_MAP_FORCE_TO:
1572 goacc_enter_exit_data_internal (flags_m, 1, &hostaddrs[i], &sizes[i],
1573 &kinds[i], true, GOMP_ASYNC_SYNC, 0, NULL);
1574 break;
1575
1576 case GOMP_MAP_FROM:
1577 case GOMP_MAP_FORCE_FROM:
1578 case GOMP_MAP_RELEASE:
1579 case GOMP_MAP_DELETE:
1580 goacc_enter_exit_data_internal (flags_m, 1, &hostaddrs[i], &sizes[i],
1581 &kinds[i], false, GOMP_ASYNC_SYNC, 0, NULL);
1582 break;
1583
1584 case GOMP_MAP_FORCE_DEVICEPTR:
1585 break;
1586
1587 case GOMP_MAP_FORCE_PRESENT:
1588 if (!acc_is_present (hostaddrs[i], sizes[i]))
1589 gomp_fatal ("[%p,%ld] is not mapped", hostaddrs[i],
1590 (unsigned long) sizes[i]);
1591 break;
1592
1593 default:
1594 assert (0);
1595 break;
1596 }
1597 }
1598 }