[OpenACC] Refactor 'goacc_remove_pointer' interface
[gcc.git] / libgomp / oacc-mem.c
1 /* OpenACC Runtime initialization routines
2
3 Copyright (C) 2013-2019 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, /* 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 struct target_mem_desc *tgt = NULL;
359 size_t mapnum = 1;
360 void *hostaddrs = h;
361 void *devaddrs = d;
362 size_t sizes = s;
363 unsigned short kinds = GOMP_MAP_ALLOC;
364
365 goacc_lazy_initialize ();
366
367 struct goacc_thread *thr = goacc_thread ();
368 struct gomp_device_descr *acc_dev = thr->dev;
369
370 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
371 {
372 if (d != h)
373 gomp_fatal ("cannot map data on shared-memory system");
374 }
375 else
376 {
377 struct goacc_thread *thr = goacc_thread ();
378
379 if (!d || !h || !s)
380 gomp_fatal ("[%p,+%d]->[%p,+%d] is a bad map",
381 (void *)h, (int)s, (void *)d, (int)s);
382
383 acc_prof_info prof_info;
384 acc_api_info api_info;
385 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
386
387 gomp_mutex_lock (&acc_dev->lock);
388
389 if (lookup_host (acc_dev, h, s))
390 {
391 gomp_mutex_unlock (&acc_dev->lock);
392 gomp_fatal ("host address [%p, +%d] is already mapped", (void *)h,
393 (int)s);
394 }
395
396 if (lookup_dev (&thr->dev->mem_map, d, s))
397 {
398 gomp_mutex_unlock (&acc_dev->lock);
399 gomp_fatal ("device address [%p, +%d] is already mapped", (void *)d,
400 (int)s);
401 }
402
403 gomp_mutex_unlock (&acc_dev->lock);
404
405 tgt = gomp_map_vars (acc_dev, mapnum, &hostaddrs, &devaddrs, &sizes,
406 &kinds, true, GOMP_MAP_VARS_ENTER_DATA);
407 assert (tgt);
408 splay_tree_key n = tgt->list[0].key;
409 assert (n->refcount == 1);
410 assert (n->dynamic_refcount == 0);
411 /* Special reference counting behavior. */
412 n->refcount = REFCOUNT_INFINITY;
413
414 if (profiling_p)
415 {
416 thr->prof_info = NULL;
417 thr->api_info = NULL;
418 }
419 }
420 }
421
422 void
423 acc_unmap_data (void *h)
424 {
425 struct goacc_thread *thr = goacc_thread ();
426 struct gomp_device_descr *acc_dev = thr->dev;
427
428 /* No need to call lazy open, as the address must have been mapped. */
429
430 /* This is a no-op on shared-memory targets. */
431 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
432 return;
433
434 acc_prof_info prof_info;
435 acc_api_info api_info;
436 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
437
438 size_t host_size;
439
440 gomp_mutex_lock (&acc_dev->lock);
441
442 splay_tree_key n = lookup_host (acc_dev, h, 1);
443 struct target_mem_desc *t;
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 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 /* TODO This currently doesn't catch 'REFCOUNT_INFINITY' usage different from
460 'acc_map_data'. Maybe 'dynamic_refcount' can be used for disambiguating
461 the different 'REFCOUNT_INFINITY' cases, or simply separate
462 'REFCOUNT_INFINITY' values per different usage ('REFCOUNT_ACC_MAP_DATA'
463 etc.)? */
464 else if (n->refcount != REFCOUNT_INFINITY)
465 {
466 gomp_mutex_unlock (&acc_dev->lock);
467 gomp_fatal ("refusing to unmap block [%p,+%d] that has not been mapped"
468 " by 'acc_map_data'",
469 (void *) h, (int) host_size);
470 }
471
472 t = n->tgt;
473
474 if (t->refcount == 1)
475 {
476 /* This is the last reference, so pull the descriptor off the
477 chain. This prevents 'gomp_unmap_tgt' via 'gomp_remove_var' from
478 freeing the device memory. */
479 t->tgt_end = 0;
480 t->to_free = 0;
481 }
482
483 bool is_tgt_unmapped = 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 /* Enter a dynamic mapping.
497
498 Return the device pointer. */
499
500 static void *
501 goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
502 {
503 void *d;
504 splay_tree_key n;
505
506 if (!h || !s)
507 gomp_fatal ("[%p,+%d] is a bad range", (void *)h, (int)s);
508
509 goacc_lazy_initialize ();
510
511 struct goacc_thread *thr = goacc_thread ();
512 struct gomp_device_descr *acc_dev = thr->dev;
513
514 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
515 return h;
516
517 acc_prof_info prof_info;
518 acc_api_info api_info;
519 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
520 if (profiling_p)
521 {
522 prof_info.async = async;
523 prof_info.async_queue = prof_info.async;
524 }
525
526 gomp_mutex_lock (&acc_dev->lock);
527
528 n = lookup_host (acc_dev, h, s);
529 if (n)
530 {
531 /* Present. */
532 d = (void *) (n->tgt->tgt_start + n->tgt_offset + h - n->host_start);
533
534 if ((h + s) > (void *)n->host_end)
535 {
536 gomp_mutex_unlock (&acc_dev->lock);
537 gomp_fatal ("[%p,+%d] not mapped", (void *)h, (int)s);
538 }
539
540 assert (n->refcount != REFCOUNT_LINK);
541 if (n->refcount != REFCOUNT_INFINITY)
542 n->refcount++;
543 n->dynamic_refcount++;
544
545 gomp_mutex_unlock (&acc_dev->lock);
546 }
547 else
548 {
549 struct target_mem_desc *tgt;
550 size_t mapnum = 1;
551 void *hostaddrs = h;
552
553 gomp_mutex_unlock (&acc_dev->lock);
554
555 goacc_aq aq = get_goacc_asyncqueue (async);
556
557 tgt = gomp_map_vars_async (acc_dev, aq, mapnum, &hostaddrs, NULL, &s,
558 &kind, true, GOMP_MAP_VARS_ENTER_DATA);
559 assert (tgt);
560 n = tgt->list[0].key;
561 assert (n->refcount == 1);
562 assert (n->dynamic_refcount == 0);
563 n->dynamic_refcount++;
564
565 d = tgt->to_free;
566 }
567
568 if (profiling_p)
569 {
570 thr->prof_info = NULL;
571 thr->api_info = NULL;
572 }
573
574 return d;
575 }
576
577 void *
578 acc_create (void *h, size_t s)
579 {
580 return goacc_enter_data (h, s, GOMP_MAP_ALLOC, acc_async_sync);
581 }
582
583 void
584 acc_create_async (void *h, size_t s, int async)
585 {
586 goacc_enter_data (h, s, GOMP_MAP_ALLOC, async);
587 }
588
589 /* acc_present_or_create used to be what acc_create is now. */
590 /* acc_pcreate is acc_present_or_create by a different name. */
591 #ifdef HAVE_ATTRIBUTE_ALIAS
592 strong_alias (acc_create, acc_present_or_create)
593 strong_alias (acc_create, acc_pcreate)
594 #else
595 void *
596 acc_present_or_create (void *h, size_t s)
597 {
598 return acc_create (h, s);
599 }
600
601 void *
602 acc_pcreate (void *h, size_t s)
603 {
604 return acc_create (h, s);
605 }
606 #endif
607
608 void *
609 acc_copyin (void *h, size_t s)
610 {
611 return goacc_enter_data (h, s, GOMP_MAP_TO, acc_async_sync);
612 }
613
614 void
615 acc_copyin_async (void *h, size_t s, int async)
616 {
617 goacc_enter_data (h, s, GOMP_MAP_TO, async);
618 }
619
620 /* acc_present_or_copyin used to be what acc_copyin is now. */
621 /* acc_pcopyin is acc_present_or_copyin by a different name. */
622 #ifdef HAVE_ATTRIBUTE_ALIAS
623 strong_alias (acc_copyin, acc_present_or_copyin)
624 strong_alias (acc_copyin, acc_pcopyin)
625 #else
626 void *
627 acc_present_or_copyin (void *h, size_t s)
628 {
629 return acc_copyin (h, s);
630 }
631
632 void *
633 acc_pcopyin (void *h, size_t s)
634 {
635 return acc_copyin (h, s);
636 }
637 #endif
638
639
640 /* Exit a dynamic mapping. */
641
642 static void
643 goacc_exit_data (void *h, size_t s, unsigned short kind, int async)
644 {
645 /* No need to call lazy open, as the data must already have been
646 mapped. */
647
648 kind &= 0xff;
649
650 struct goacc_thread *thr = goacc_thread ();
651 struct gomp_device_descr *acc_dev = thr->dev;
652
653 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
654 return;
655
656 acc_prof_info prof_info;
657 acc_api_info api_info;
658 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
659 if (profiling_p)
660 {
661 prof_info.async = async;
662 prof_info.async_queue = prof_info.async;
663 }
664
665 gomp_mutex_lock (&acc_dev->lock);
666
667 splay_tree_key n = lookup_host (acc_dev, h, s);
668 if (!n)
669 /* PR92726, RP92970, PR92984: no-op. */
670 goto out;
671
672 if ((uintptr_t) h < n->host_start || (uintptr_t) h + s > n->host_end)
673 {
674 size_t host_size = n->host_end - n->host_start;
675 gomp_mutex_unlock (&acc_dev->lock);
676 gomp_fatal ("[%p,+%d] outside mapped block [%p,+%d]",
677 (void *) h, (int) s, (void *) n->host_start, (int) host_size);
678 }
679
680 assert (n->refcount != REFCOUNT_LINK);
681 if (n->refcount != REFCOUNT_INFINITY
682 && n->refcount < n->dynamic_refcount)
683 {
684 gomp_mutex_unlock (&acc_dev->lock);
685 gomp_fatal ("Dynamic reference counting assert fail\n");
686 }
687
688 bool finalize = (kind == GOMP_MAP_DELETE
689 || kind == GOMP_MAP_FORCE_FROM);
690 if (finalize)
691 {
692 if (n->refcount != REFCOUNT_INFINITY)
693 n->refcount -= n->dynamic_refcount;
694 n->dynamic_refcount = 0;
695 }
696 else if (n->dynamic_refcount)
697 {
698 if (n->refcount != REFCOUNT_INFINITY)
699 n->refcount--;
700 n->dynamic_refcount--;
701 }
702
703 if (n->refcount == 0)
704 {
705 goacc_aq aq = get_goacc_asyncqueue (async);
706
707 bool copyout = (kind == GOMP_MAP_FROM
708 || kind == GOMP_MAP_FORCE_FROM);
709 if (copyout)
710 {
711 void *d = (void *) (n->tgt->tgt_start + n->tgt_offset
712 + (uintptr_t) h - n->host_start);
713 gomp_copy_dev2host (acc_dev, aq, h, d, s);
714 }
715
716 if (aq)
717 /* TODO We can't do the 'is_tgt_unmapped' checking -- see the
718 'gomp_unref_tgt' comment in
719 <http://mid.mail-archive.com/878snl36eu.fsf@euler.schwinge.homeip.net>;
720 PR92881. */
721 gomp_remove_var_async (acc_dev, n, aq);
722 else
723 {
724 bool is_tgt_unmapped = gomp_remove_var (acc_dev, n);
725 assert (is_tgt_unmapped);
726 }
727 }
728
729 out:
730 gomp_mutex_unlock (&acc_dev->lock);
731
732 if (profiling_p)
733 {
734 thr->prof_info = NULL;
735 thr->api_info = NULL;
736 }
737 }
738
739 void
740 acc_delete (void *h , size_t s)
741 {
742 goacc_exit_data (h, s, GOMP_MAP_RELEASE, acc_async_sync);
743 }
744
745 void
746 acc_delete_async (void *h , size_t s, int async)
747 {
748 goacc_exit_data (h, s, GOMP_MAP_RELEASE, async);
749 }
750
751 void
752 acc_delete_finalize (void *h , size_t s)
753 {
754 goacc_exit_data (h, s, GOMP_MAP_DELETE, acc_async_sync);
755 }
756
757 void
758 acc_delete_finalize_async (void *h , size_t s, int async)
759 {
760 goacc_exit_data (h, s, GOMP_MAP_DELETE, async);
761 }
762
763 void
764 acc_copyout (void *h, size_t s)
765 {
766 goacc_exit_data (h, s, GOMP_MAP_FROM, acc_async_sync);
767 }
768
769 void
770 acc_copyout_async (void *h, size_t s, int async)
771 {
772 goacc_exit_data (h, s, GOMP_MAP_FROM, async);
773 }
774
775 void
776 acc_copyout_finalize (void *h, size_t s)
777 {
778 goacc_exit_data (h, s, GOMP_MAP_FORCE_FROM, acc_async_sync);
779 }
780
781 void
782 acc_copyout_finalize_async (void *h, size_t s, int async)
783 {
784 goacc_exit_data (h, s, GOMP_MAP_FORCE_FROM, async);
785 }
786
787 static void
788 update_dev_host (int is_dev, void *h, size_t s, int async)
789 {
790 splay_tree_key n;
791 void *d;
792
793 goacc_lazy_initialize ();
794
795 struct goacc_thread *thr = goacc_thread ();
796 struct gomp_device_descr *acc_dev = thr->dev;
797
798 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
799 return;
800
801 /* Fortran optional arguments that are non-present result in a
802 NULL host address here. This can safely be ignored as it is
803 not possible to 'update' a non-present optional argument. */
804 if (h == NULL)
805 return;
806
807 acc_prof_info prof_info;
808 acc_api_info api_info;
809 bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
810 if (profiling_p)
811 {
812 prof_info.async = async;
813 prof_info.async_queue = prof_info.async;
814 }
815
816 gomp_mutex_lock (&acc_dev->lock);
817
818 n = lookup_host (acc_dev, h, s);
819
820 if (!n)
821 {
822 gomp_mutex_unlock (&acc_dev->lock);
823 gomp_fatal ("[%p,%d] is not mapped", h, (int)s);
824 }
825
826 d = (void *) (n->tgt->tgt_start + n->tgt_offset
827 + (uintptr_t) h - n->host_start);
828
829 goacc_aq aq = get_goacc_asyncqueue (async);
830
831 if (is_dev)
832 gomp_copy_host2dev (acc_dev, aq, d, h, s, /* TODO: cbuf? */ NULL);
833 else
834 gomp_copy_dev2host (acc_dev, aq, h, d, s);
835
836 gomp_mutex_unlock (&acc_dev->lock);
837
838 if (profiling_p)
839 {
840 thr->prof_info = NULL;
841 thr->api_info = NULL;
842 }
843 }
844
845 void
846 acc_update_device (void *h, size_t s)
847 {
848 update_dev_host (1, h, s, acc_async_sync);
849 }
850
851 void
852 acc_update_device_async (void *h, size_t s, int async)
853 {
854 update_dev_host (1, h, s, async);
855 }
856
857 void
858 acc_update_self (void *h, size_t s)
859 {
860 update_dev_host (0, h, s, acc_async_sync);
861 }
862
863 void
864 acc_update_self_async (void *h, size_t s, int async)
865 {
866 update_dev_host (0, h, s, async);
867 }
868
869
870 /* OpenACC 'enter data', 'exit data': 'GOACC_enter_exit_data' and its helper
871 functions. */
872
873 /* Special handling for 'GOMP_MAP_POINTER', 'GOMP_MAP_TO_PSET'.
874
875 Only the first mapping is considered in reference counting; the following
876 ones implicitly follow suit. Similarly, 'copyout' is done only for the
877 first mapping. */
878
879 static void
880 goacc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
881 void *kinds, int async)
882 {
883 struct target_mem_desc *tgt;
884 struct goacc_thread *thr = goacc_thread ();
885 struct gomp_device_descr *acc_dev = thr->dev;
886
887 if (*hostaddrs == NULL)
888 return;
889
890 if (acc_is_present (*hostaddrs, *sizes))
891 {
892 splay_tree_key n;
893 gomp_mutex_lock (&acc_dev->lock);
894 n = lookup_host (acc_dev, *hostaddrs, *sizes);
895 assert (n->refcount != REFCOUNT_INFINITY
896 && n->refcount != REFCOUNT_LINK);
897 gomp_mutex_unlock (&acc_dev->lock);
898
899 tgt = n->tgt;
900 for (size_t i = 0; i < tgt->list_count; i++)
901 if (tgt->list[i].key == n)
902 {
903 for (size_t j = 0; j < mapnum; j++)
904 if (i + j < tgt->list_count && tgt->list[i + j].key)
905 {
906 tgt->list[i + j].key->refcount++;
907 tgt->list[i + j].key->dynamic_refcount++;
908 }
909 return;
910 }
911 /* Should not reach here. */
912 gomp_fatal ("Dynamic refcount incrementing failed for pointer/pset");
913 }
914
915 gomp_debug (0, " %s: prepare mappings\n", __FUNCTION__);
916 goacc_aq aq = get_goacc_asyncqueue (async);
917 tgt = gomp_map_vars_async (acc_dev, aq, mapnum, hostaddrs,
918 NULL, sizes, kinds, true, GOMP_MAP_VARS_ENTER_DATA);
919 assert (tgt);
920 splay_tree_key n = tgt->list[0].key;
921 assert (n->refcount == 1);
922 assert (n->dynamic_refcount == 0);
923 n->dynamic_refcount++;
924 gomp_debug (0, " %s: mappings prepared\n", __FUNCTION__);
925 }
926
927 static void
928 goacc_remove_pointer (void *h, size_t s, unsigned short kind, int async)
929 {
930 kind &= 0xff;
931
932 struct goacc_thread *thr = goacc_thread ();
933 struct gomp_device_descr *acc_dev = thr->dev;
934 splay_tree_key n;
935 struct target_mem_desc *t;
936
937 if (!acc_is_present (h, s))
938 return;
939
940 gomp_mutex_lock (&acc_dev->lock);
941
942 n = lookup_host (acc_dev, h, 1);
943
944 if (!n)
945 {
946 gomp_mutex_unlock (&acc_dev->lock);
947 gomp_fatal ("%p is not a mapped block", (void *)h);
948 }
949
950 gomp_debug (0, " %s: restore mappings\n", __FUNCTION__);
951
952 t = n->tgt;
953
954 assert (n->refcount != REFCOUNT_INFINITY
955 && n->refcount != REFCOUNT_LINK);
956 if (n->refcount < n->dynamic_refcount)
957 {
958 gomp_mutex_unlock (&acc_dev->lock);
959 gomp_fatal ("Dynamic reference counting assert fail\n");
960 }
961
962 bool finalize = (kind == GOMP_MAP_DELETE
963 || kind == GOMP_MAP_FORCE_FROM);
964 if (finalize)
965 {
966 n->refcount -= n->dynamic_refcount;
967 n->dynamic_refcount = 0;
968 }
969 else if (n->dynamic_refcount)
970 {
971 n->refcount--;
972 n->dynamic_refcount--;
973 }
974
975 if (n->refcount == 0)
976 {
977 goacc_aq aq = get_goacc_asyncqueue (async);
978
979 bool copyout = (kind == GOMP_MAP_FROM
980 || kind == GOMP_MAP_FORCE_FROM);
981 if (copyout)
982 {
983 void *d = (void *) (t->tgt_start + n->tgt_offset
984 + (uintptr_t) h - n->host_start);
985 gomp_copy_dev2host (acc_dev, aq, h, d, s);
986 }
987
988 if (aq)
989 {
990 /* TODO The way the following code is currently implemented, we need
991 the 'is_tgt_unmapped' return value from 'gomp_remove_var', so
992 can't use 'gomp_remove_var_async' here -- see the 'gomp_unref_tgt'
993 comment in
994 <http://mid.mail-archive.com/878snl36eu.fsf@euler.schwinge.homeip.net>;
995 PR92881 -- so have to synchronize here. */
996 if (!acc_dev->openacc.async.synchronize_func (aq))
997 {
998 gomp_mutex_unlock (&acc_dev->lock);
999 gomp_fatal ("synchronize failed");
1000 }
1001 }
1002 bool is_tgt_unmapped = false;
1003 for (size_t i = 0; i < t->list_count; i++)
1004 {
1005 is_tgt_unmapped = gomp_remove_var (acc_dev, t->list[i].key);
1006 if (is_tgt_unmapped)
1007 break;
1008 }
1009 assert (is_tgt_unmapped);
1010 }
1011
1012 gomp_mutex_unlock (&acc_dev->lock);
1013
1014 gomp_debug (0, " %s: mappings restored\n", __FUNCTION__);
1015 }
1016
1017 /* Return the number of mappings associated with 'GOMP_MAP_TO_PSET' or
1018 'GOMP_MAP_POINTER'. */
1019
1020 static int
1021 find_pointer (int pos, size_t mapnum, unsigned short *kinds)
1022 {
1023 if (pos + 1 >= mapnum)
1024 return 0;
1025
1026 unsigned char kind = kinds[pos+1] & 0xff;
1027
1028 if (kind == GOMP_MAP_TO_PSET)
1029 return 3;
1030 else if (kind == GOMP_MAP_POINTER)
1031 return 2;
1032
1033 return 0;
1034 }
1035
1036 void
1037 GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
1038 size_t *sizes, unsigned short *kinds, int async,
1039 int num_waits, ...)
1040 {
1041 int flags = GOACC_FLAGS_UNMARSHAL (flags_m);
1042
1043 struct goacc_thread *thr;
1044 struct gomp_device_descr *acc_dev;
1045 bool data_enter = false;
1046 size_t i;
1047
1048 goacc_lazy_initialize ();
1049
1050 thr = goacc_thread ();
1051 acc_dev = thr->dev;
1052
1053 /* Determine if this is an "acc enter data". */
1054 for (i = 0; i < mapnum; ++i)
1055 {
1056 unsigned char kind = kinds[i] & 0xff;
1057
1058 if (kind == GOMP_MAP_POINTER || kind == GOMP_MAP_TO_PSET)
1059 continue;
1060
1061 if (kind == GOMP_MAP_FORCE_ALLOC
1062 || kind == GOMP_MAP_FORCE_PRESENT
1063 || kind == GOMP_MAP_FORCE_TO
1064 || kind == GOMP_MAP_TO
1065 || kind == GOMP_MAP_ALLOC)
1066 {
1067 data_enter = true;
1068 break;
1069 }
1070
1071 if (kind == GOMP_MAP_RELEASE
1072 || kind == GOMP_MAP_DELETE
1073 || kind == GOMP_MAP_FROM
1074 || kind == GOMP_MAP_FORCE_FROM)
1075 break;
1076
1077 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1078 kind);
1079 }
1080
1081 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
1082
1083 acc_prof_info prof_info;
1084 if (profiling_p)
1085 {
1086 thr->prof_info = &prof_info;
1087
1088 prof_info.event_type
1089 = data_enter ? acc_ev_enter_data_start : acc_ev_exit_data_start;
1090 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
1091 prof_info.version = _ACC_PROF_INFO_VERSION;
1092 prof_info.device_type = acc_device_type (acc_dev->type);
1093 prof_info.device_number = acc_dev->target_id;
1094 prof_info.thread_id = -1;
1095 prof_info.async = async;
1096 prof_info.async_queue = prof_info.async;
1097 prof_info.src_file = NULL;
1098 prof_info.func_name = NULL;
1099 prof_info.line_no = -1;
1100 prof_info.end_line_no = -1;
1101 prof_info.func_line_no = -1;
1102 prof_info.func_end_line_no = -1;
1103 }
1104 acc_event_info enter_exit_data_event_info;
1105 if (profiling_p)
1106 {
1107 enter_exit_data_event_info.other_event.event_type
1108 = prof_info.event_type;
1109 enter_exit_data_event_info.other_event.valid_bytes
1110 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
1111 enter_exit_data_event_info.other_event.parent_construct
1112 = data_enter ? acc_construct_enter_data : acc_construct_exit_data;
1113 enter_exit_data_event_info.other_event.implicit = 0;
1114 enter_exit_data_event_info.other_event.tool_info = NULL;
1115 }
1116 acc_api_info api_info;
1117 if (profiling_p)
1118 {
1119 thr->api_info = &api_info;
1120
1121 api_info.device_api = acc_device_api_none;
1122 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
1123 api_info.device_type = prof_info.device_type;
1124 api_info.vendor = -1;
1125 api_info.device_handle = NULL;
1126 api_info.context_handle = NULL;
1127 api_info.async_handle = NULL;
1128 }
1129
1130 if (profiling_p)
1131 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
1132 &api_info);
1133
1134 if ((acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
1135 || (flags & GOACC_FLAG_HOST_FALLBACK))
1136 {
1137 prof_info.device_type = acc_device_host;
1138 api_info.device_type = prof_info.device_type;
1139
1140 goto out_prof;
1141 }
1142
1143 if (num_waits)
1144 {
1145 va_list ap;
1146
1147 va_start (ap, num_waits);
1148 goacc_wait (async, num_waits, &ap);
1149 va_end (ap);
1150 }
1151
1152 /* In c, non-pointers and arrays are represented by a single data clause.
1153 Dynamically allocated arrays and subarrays are represented by a data
1154 clause followed by an internal GOMP_MAP_POINTER.
1155
1156 In fortran, scalars and not allocated arrays are represented by a
1157 single data clause. Allocated arrays and subarrays have three mappings:
1158 1) the original data clause, 2) a PSET 3) a pointer to the array data.
1159 */
1160
1161 if (data_enter)
1162 {
1163 for (i = 0; i < mapnum; i++)
1164 {
1165 /* Scan for pointers and PSETs. */
1166 int pointer = find_pointer (i, mapnum, kinds);
1167
1168 if (!pointer)
1169 {
1170 unsigned char kind = kinds[i] & 0xff;
1171 switch (kind)
1172 {
1173 case GOMP_MAP_ALLOC:
1174 case GOMP_MAP_FORCE_ALLOC:
1175 case GOMP_MAP_TO:
1176 case GOMP_MAP_FORCE_TO:
1177 break;
1178 default:
1179 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1180 kind);
1181 break;
1182 }
1183
1184 goacc_enter_data (hostaddrs[i], sizes[i], kinds[i], async);
1185 }
1186 else
1187 {
1188 goacc_insert_pointer (pointer, &hostaddrs[i], &sizes[i], &kinds[i],
1189 async);
1190 /* Increment 'i' by two because OpenACC requires fortran
1191 arrays to be contiguous, so each PSET is associated with
1192 one of MAP_FORCE_ALLOC/MAP_FORCE_PRESET/MAP_FORCE_TO, and
1193 one MAP_POINTER. */
1194 i += pointer - 1;
1195 }
1196 }
1197 }
1198 else
1199 for (i = 0; i < mapnum; ++i)
1200 {
1201 int pointer = find_pointer (i, mapnum, kinds);
1202
1203 if (!pointer)
1204 {
1205 unsigned char kind = kinds[i] & 0xff;
1206 switch (kind)
1207 {
1208 case GOMP_MAP_RELEASE:
1209 case GOMP_MAP_DELETE:
1210 case GOMP_MAP_FROM:
1211 case GOMP_MAP_FORCE_FROM:
1212 break;
1213 default:
1214 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1215 kind);
1216 break;
1217 }
1218
1219 goacc_exit_data (hostaddrs[i], sizes[i], kinds[i], async);
1220 }
1221 else
1222 {
1223 goacc_remove_pointer (hostaddrs[i], sizes[i], kinds[i], async);
1224 /* See the above comment. */
1225 i += pointer - 1;
1226 }
1227 }
1228
1229 out_prof:
1230 if (profiling_p)
1231 {
1232 prof_info.event_type
1233 = data_enter ? acc_ev_enter_data_end : acc_ev_exit_data_end;
1234 enter_exit_data_event_info.other_event.event_type = prof_info.event_type;
1235 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
1236 &api_info);
1237
1238 thr->prof_info = NULL;
1239 thr->api_info = NULL;
1240 }
1241 }