1 /* OpenACC Runtime initialization routines
3 Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 Contributed by Mentor Embedded.
7 This file is part of the GNU Offloading and Multi Processing Library
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)
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
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.
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/>. */
31 #include "gomp-constants.h"
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. */
40 lookup_host (struct gomp_device_descr
*dev
, void *h
, size_t s
)
42 struct splay_tree_key_s node
;
45 node
.host_start
= (uintptr_t) h
;
46 node
.host_end
= (uintptr_t) h
+ s
;
48 key
= splay_tree_lookup (&dev
->mem_map
, &node
);
53 /* Helper for lookup_dev. Iterate over splay tree. */
56 lookup_dev_1 (splay_tree_node node
, uintptr_t d
, size_t s
)
58 splay_tree_key key
= &node
->key
;
59 if (d
>= key
->tgt
->tgt_start
&& d
+ s
<= key
->tgt
->tgt_end
)
64 key
= lookup_dev_1 (node
->left
, d
, s
);
65 if (!key
&& node
->right
)
66 key
= lookup_dev_1 (node
->right
, d
, s
);
71 /* Return block containing [D->S), or NULL if not contained.
73 This iterates over the splay tree. This is not expected to be a common
76 The device lock associated with MEM_MAP must be locked on entry, and remains
80 lookup_dev (splay_tree mem_map
, void *d
, size_t s
)
82 if (!mem_map
|| !mem_map
->root
)
85 return lookup_dev_1 (mem_map
->root
, (uintptr_t) d
, s
);
89 /* OpenACC is silent on how memory exhaustion is indicated. We return
98 goacc_lazy_initialize ();
100 struct goacc_thread
*thr
= goacc_thread ();
104 if (thr
->dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
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
);
111 void *res
= thr
->dev
->alloc_func (thr
->dev
->target_id
, s
);
115 thr
->prof_info
= NULL
;
116 thr
->api_info
= NULL
;
130 struct goacc_thread
*thr
= goacc_thread ();
132 assert (thr
&& thr
->dev
);
134 struct gomp_device_descr
*acc_dev
= thr
->dev
;
136 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
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
);
143 gomp_mutex_lock (&acc_dev
->lock
);
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)))
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]",
161 gomp_mutex_unlock (&acc_dev
->lock
);
163 if (!acc_dev
->free_func (acc_dev
->target_id
, d
))
164 gomp_fatal ("error in freeing device memory in %s", __FUNCTION__
);
168 thr
->prof_info
= NULL
;
169 thr
->api_info
= NULL
;
174 memcpy_tofrom_device (bool from
, void *d
, void *h
, size_t s
, int async
,
175 const char *libfnname
)
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 ();
181 assert (thr
&& thr
->dev
);
183 if (thr
->dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
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
);
197 prof_info
.async
= async
;
198 prof_info
.async_queue
= prof_info
.async
;
201 goacc_aq aq
= get_goacc_asyncqueue (async
);
203 gomp_copy_dev2host (thr
->dev
, aq
, h
, d
, s
);
205 gomp_copy_host2dev (thr
->dev
, aq
, d
, h
, s
, /* TODO: cbuf? */ NULL
);
209 thr
->prof_info
= NULL
;
210 thr
->api_info
= NULL
;
215 acc_memcpy_to_device (void *d
, void *h
, size_t s
)
217 memcpy_tofrom_device (false, d
, h
, s
, acc_async_sync
, __FUNCTION__
);
221 acc_memcpy_to_device_async (void *d
, void *h
, size_t s
, int async
)
223 memcpy_tofrom_device (false, d
, h
, s
, async
, __FUNCTION__
);
227 acc_memcpy_from_device (void *h
, void *d
, size_t s
)
229 memcpy_tofrom_device (true, d
, h
, s
, acc_async_sync
, __FUNCTION__
);
233 acc_memcpy_from_device_async (void *h
, void *d
, size_t s
, int async
)
235 memcpy_tofrom_device (true, d
, h
, s
, async
, __FUNCTION__
);
238 /* Return the device pointer that corresponds to host data H. Or NULL
242 acc_deviceptr (void *h
)
248 goacc_lazy_initialize ();
250 struct goacc_thread
*thr
= goacc_thread ();
251 struct gomp_device_descr
*dev
= thr
->dev
;
253 if (thr
->dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
256 /* In the following, no OpenACC Profiling Interface events can possibly be
259 gomp_mutex_lock (&dev
->lock
);
261 n
= lookup_host (dev
, h
, 1);
265 gomp_mutex_unlock (&dev
->lock
);
269 offset
= h
- n
->host_start
;
271 d
= n
->tgt
->tgt_start
+ n
->tgt_offset
+ offset
;
273 gomp_mutex_unlock (&dev
->lock
);
278 /* Return the host pointer that corresponds to device data D. Or NULL
282 acc_hostptr (void *d
)
288 goacc_lazy_initialize ();
290 struct goacc_thread
*thr
= goacc_thread ();
291 struct gomp_device_descr
*acc_dev
= thr
->dev
;
293 if (thr
->dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
296 /* In the following, no OpenACC Profiling Interface events can possibly be
299 gomp_mutex_lock (&acc_dev
->lock
);
301 n
= lookup_dev (&acc_dev
->mem_map
, d
, 1);
305 gomp_mutex_unlock (&acc_dev
->lock
);
309 offset
= d
- n
->tgt
->tgt_start
+ n
->tgt_offset
;
311 h
= n
->host_start
+ offset
;
313 gomp_mutex_unlock (&acc_dev
->lock
);
318 /* Return 1 if host data [H,+S] is present on the device. */
321 acc_is_present (void *h
, size_t s
)
328 goacc_lazy_initialize ();
330 struct goacc_thread
*thr
= goacc_thread ();
331 struct gomp_device_descr
*acc_dev
= thr
->dev
;
333 if (thr
->dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
336 /* In the following, no OpenACC Profiling Interface events can possibly be
339 gomp_mutex_lock (&acc_dev
->lock
);
341 n
= lookup_host (acc_dev
, h
, s
);
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
))
348 gomp_mutex_unlock (&acc_dev
->lock
);
353 /* Create a mapping for host [H,+S] -> device [D,+S] */
356 acc_map_data (void *h
, void *d
, size_t s
)
358 struct target_mem_desc
*tgt
= NULL
;
363 unsigned short kinds
= GOMP_MAP_ALLOC
;
365 goacc_lazy_initialize ();
367 struct goacc_thread
*thr
= goacc_thread ();
368 struct gomp_device_descr
*acc_dev
= thr
->dev
;
370 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
373 gomp_fatal ("cannot map data on shared-memory system");
377 struct goacc_thread
*thr
= goacc_thread ();
380 gomp_fatal ("[%p,+%d]->[%p,+%d] is a bad map",
381 (void *)h
, (int)s
, (void *)d
, (int)s
);
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
);
387 gomp_mutex_lock (&acc_dev
->lock
);
389 if (lookup_host (acc_dev
, h
, s
))
391 gomp_mutex_unlock (&acc_dev
->lock
);
392 gomp_fatal ("host address [%p, +%d] is already mapped", (void *)h
,
396 if (lookup_dev (&thr
->dev
->mem_map
, d
, s
))
398 gomp_mutex_unlock (&acc_dev
->lock
);
399 gomp_fatal ("device address [%p, +%d] is already mapped", (void *)d
,
403 gomp_mutex_unlock (&acc_dev
->lock
);
405 tgt
= gomp_map_vars (acc_dev
, mapnum
, &hostaddrs
, &devaddrs
, &sizes
,
406 &kinds
, true, GOMP_MAP_VARS_ENTER_DATA
);
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
;
416 thr
->prof_info
= NULL
;
417 thr
->api_info
= NULL
;
423 acc_unmap_data (void *h
)
425 struct goacc_thread
*thr
= goacc_thread ();
426 struct gomp_device_descr
*acc_dev
= thr
->dev
;
428 /* No need to call lazy open, as the address must have been mapped. */
430 /* This is a no-op on shared-memory targets. */
431 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
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
);
440 gomp_mutex_lock (&acc_dev
->lock
);
442 splay_tree_key n
= lookup_host (acc_dev
, h
, 1);
443 struct target_mem_desc
*t
;
447 gomp_mutex_unlock (&acc_dev
->lock
);
448 gomp_fatal ("%p is not a mapped block", (void *)h
);
451 host_size
= n
->host_end
- n
->host_start
;
453 if (n
->host_start
!= (uintptr_t) h
)
455 gomp_mutex_unlock (&acc_dev
->lock
);
456 gomp_fatal ("[%p,%d] surrounds %p",
457 (void *) n
->host_start
, (int) host_size
, (void *) h
);
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'
464 else if (n
->refcount
!= REFCOUNT_INFINITY
)
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
);
474 if (t
->refcount
== 1)
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. */
483 bool is_tgt_unmapped
= gomp_remove_var (acc_dev
, n
);
484 assert (is_tgt_unmapped
);
486 gomp_mutex_unlock (&acc_dev
->lock
);
490 thr
->prof_info
= NULL
;
491 thr
->api_info
= NULL
;
496 /* Enter dynamic mappings.
498 The handling for MAPNUM bigger than one is special handling for
499 'GOMP_MAP_POINTER', 'GOMP_MAP_TO_PSET'. For these, only the first mapping
500 is considered in reference counting; the following ones implicitly follow
503 If there's just one mapping, return the device pointer. */
506 goacc_enter_data (size_t mapnum
, void **hostaddrs
, size_t *sizes
, void *kinds
,
514 && (!hostaddrs
[0] || !sizes
[0]))
515 gomp_fatal ("[%p,+%d] is a bad range", hostaddrs
[0], (int) sizes
[0]);
518 return /* n/a */ (void *) -1;
520 goacc_lazy_initialize ();
522 struct goacc_thread
*thr
= goacc_thread ();
523 struct gomp_device_descr
*acc_dev
= thr
->dev
;
525 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
530 return /* n/a */ (void *) -1;
533 acc_prof_info prof_info
;
534 acc_api_info api_info
;
535 bool profiling_p
= GOACC_PROFILING_SETUP_P (thr
, &prof_info
, &api_info
);
538 prof_info
.async
= async
;
539 prof_info
.async_queue
= prof_info
.async
;
542 gomp_mutex_lock (&acc_dev
->lock
);
544 n
= lookup_host (acc_dev
, hostaddrs
[0], sizes
[0]);
545 if (n
&& mapnum
== 1)
547 void *h
= hostaddrs
[0];
551 d
= (void *) (n
->tgt
->tgt_start
+ n
->tgt_offset
+ h
- n
->host_start
);
553 if ((h
+ s
) > (void *)n
->host_end
)
555 gomp_mutex_unlock (&acc_dev
->lock
);
556 gomp_fatal ("[%p,+%d] not mapped", (void *)h
, (int)s
);
559 assert (n
->refcount
!= REFCOUNT_LINK
);
560 if (n
->refcount
!= REFCOUNT_INFINITY
)
562 n
->dynamic_refcount
++;
564 gomp_mutex_unlock (&acc_dev
->lock
);
566 else if (n
&& mapnum
> 1)
568 d
= /* n/a */ (void *) -1;
570 assert (n
->refcount
!= REFCOUNT_INFINITY
571 && n
->refcount
!= REFCOUNT_LINK
);
573 bool processed
= false;
575 struct target_mem_desc
*tgt
= n
->tgt
;
576 for (size_t i
= 0; i
< tgt
->list_count
; i
++)
577 if (tgt
->list
[i
].key
== n
)
579 for (size_t j
= 0; j
< mapnum
; j
++)
580 if (i
+ j
< tgt
->list_count
&& tgt
->list
[i
+ j
].key
)
582 tgt
->list
[i
+ j
].key
->refcount
++;
583 tgt
->list
[i
+ j
].key
->dynamic_refcount
++;
588 gomp_mutex_unlock (&acc_dev
->lock
);
590 gomp_fatal ("dynamic refcount incrementing failed for pointer/pset");
594 gomp_mutex_unlock (&acc_dev
->lock
);
596 goacc_aq aq
= get_goacc_asyncqueue (async
);
598 struct target_mem_desc
*tgt
599 = gomp_map_vars_async (acc_dev
, aq
, mapnum
, hostaddrs
, NULL
, sizes
,
600 kinds
, true, GOMP_MAP_VARS_ENTER_DATA
);
602 n
= tgt
->list
[0].key
;
603 assert (n
->refcount
== 1);
604 assert (n
->dynamic_refcount
== 0);
605 n
->dynamic_refcount
++;
612 thr
->prof_info
= NULL
;
613 thr
->api_info
= NULL
;
620 acc_create (void *h
, size_t s
)
622 unsigned short kinds
[1] = { GOMP_MAP_ALLOC
};
623 return goacc_enter_data (1, &h
, &s
, &kinds
, acc_async_sync
);
627 acc_create_async (void *h
, size_t s
, int async
)
629 unsigned short kinds
[1] = { GOMP_MAP_ALLOC
};
630 goacc_enter_data (1, &h
, &s
, &kinds
, async
);
633 /* acc_present_or_create used to be what acc_create is now. */
634 /* acc_pcreate is acc_present_or_create by a different name. */
635 #ifdef HAVE_ATTRIBUTE_ALIAS
636 strong_alias (acc_create
, acc_present_or_create
)
637 strong_alias (acc_create
, acc_pcreate
)
640 acc_present_or_create (void *h
, size_t s
)
642 return acc_create (h
, s
);
646 acc_pcreate (void *h
, size_t s
)
648 return acc_create (h
, s
);
653 acc_copyin (void *h
, size_t s
)
655 unsigned short kinds
[1] = { GOMP_MAP_TO
};
656 return goacc_enter_data (1, &h
, &s
, &kinds
, acc_async_sync
);
660 acc_copyin_async (void *h
, size_t s
, int async
)
662 unsigned short kinds
[1] = { GOMP_MAP_TO
};
663 goacc_enter_data (1, &h
, &s
, &kinds
, async
);
666 /* acc_present_or_copyin used to be what acc_copyin is now. */
667 /* acc_pcopyin is acc_present_or_copyin by a different name. */
668 #ifdef HAVE_ATTRIBUTE_ALIAS
669 strong_alias (acc_copyin
, acc_present_or_copyin
)
670 strong_alias (acc_copyin
, acc_pcopyin
)
673 acc_present_or_copyin (void *h
, size_t s
)
675 return acc_copyin (h
, s
);
679 acc_pcopyin (void *h
, size_t s
)
681 return acc_copyin (h
, s
);
686 /* Exit a dynamic mapping. */
689 goacc_exit_data (void *h
, size_t s
, unsigned short kind
, int async
)
691 /* No need to call lazy open, as the data must already have been
696 struct goacc_thread
*thr
= goacc_thread ();
697 struct gomp_device_descr
*acc_dev
= thr
->dev
;
699 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
702 acc_prof_info prof_info
;
703 acc_api_info api_info
;
704 bool profiling_p
= GOACC_PROFILING_SETUP_P (thr
, &prof_info
, &api_info
);
707 prof_info
.async
= async
;
708 prof_info
.async_queue
= prof_info
.async
;
711 gomp_mutex_lock (&acc_dev
->lock
);
713 splay_tree_key n
= lookup_host (acc_dev
, h
, s
);
715 /* PR92726, RP92970, PR92984: no-op. */
718 if ((uintptr_t) h
< n
->host_start
|| (uintptr_t) h
+ s
> n
->host_end
)
720 size_t host_size
= n
->host_end
- n
->host_start
;
721 gomp_mutex_unlock (&acc_dev
->lock
);
722 gomp_fatal ("[%p,+%d] outside mapped block [%p,+%d]",
723 (void *) h
, (int) s
, (void *) n
->host_start
, (int) host_size
);
726 assert (n
->refcount
!= REFCOUNT_LINK
);
727 if (n
->refcount
!= REFCOUNT_INFINITY
728 && n
->refcount
< n
->dynamic_refcount
)
730 gomp_mutex_unlock (&acc_dev
->lock
);
731 gomp_fatal ("Dynamic reference counting assert fail\n");
734 bool finalize
= (kind
== GOMP_MAP_DELETE
735 || kind
== GOMP_MAP_FORCE_FROM
);
738 if (n
->refcount
!= REFCOUNT_INFINITY
)
739 n
->refcount
-= n
->dynamic_refcount
;
740 n
->dynamic_refcount
= 0;
742 else if (n
->dynamic_refcount
)
744 if (n
->refcount
!= REFCOUNT_INFINITY
)
746 n
->dynamic_refcount
--;
749 if (n
->refcount
== 0)
751 goacc_aq aq
= get_goacc_asyncqueue (async
);
753 bool copyout
= (kind
== GOMP_MAP_FROM
754 || kind
== GOMP_MAP_FORCE_FROM
);
757 void *d
= (void *) (n
->tgt
->tgt_start
+ n
->tgt_offset
758 + (uintptr_t) h
- n
->host_start
);
759 gomp_copy_dev2host (acc_dev
, aq
, h
, d
, s
);
763 /* TODO We can't do the 'is_tgt_unmapped' checking -- see the
764 'gomp_unref_tgt' comment in
765 <http://mid.mail-archive.com/878snl36eu.fsf@euler.schwinge.homeip.net>;
767 gomp_remove_var_async (acc_dev
, n
, aq
);
770 bool is_tgt_unmapped
= gomp_remove_var (acc_dev
, n
);
771 assert (is_tgt_unmapped
);
776 gomp_mutex_unlock (&acc_dev
->lock
);
780 thr
->prof_info
= NULL
;
781 thr
->api_info
= NULL
;
786 acc_delete (void *h
, size_t s
)
788 goacc_exit_data (h
, s
, GOMP_MAP_RELEASE
, acc_async_sync
);
792 acc_delete_async (void *h
, size_t s
, int async
)
794 goacc_exit_data (h
, s
, GOMP_MAP_RELEASE
, async
);
798 acc_delete_finalize (void *h
, size_t s
)
800 goacc_exit_data (h
, s
, GOMP_MAP_DELETE
, acc_async_sync
);
804 acc_delete_finalize_async (void *h
, size_t s
, int async
)
806 goacc_exit_data (h
, s
, GOMP_MAP_DELETE
, async
);
810 acc_copyout (void *h
, size_t s
)
812 goacc_exit_data (h
, s
, GOMP_MAP_FROM
, acc_async_sync
);
816 acc_copyout_async (void *h
, size_t s
, int async
)
818 goacc_exit_data (h
, s
, GOMP_MAP_FROM
, async
);
822 acc_copyout_finalize (void *h
, size_t s
)
824 goacc_exit_data (h
, s
, GOMP_MAP_FORCE_FROM
, acc_async_sync
);
828 acc_copyout_finalize_async (void *h
, size_t s
, int async
)
830 goacc_exit_data (h
, s
, GOMP_MAP_FORCE_FROM
, async
);
834 update_dev_host (int is_dev
, void *h
, size_t s
, int async
)
839 goacc_lazy_initialize ();
841 struct goacc_thread
*thr
= goacc_thread ();
842 struct gomp_device_descr
*acc_dev
= thr
->dev
;
844 if (acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
847 /* Fortran optional arguments that are non-present result in a
848 NULL host address here. This can safely be ignored as it is
849 not possible to 'update' a non-present optional argument. */
853 acc_prof_info prof_info
;
854 acc_api_info api_info
;
855 bool profiling_p
= GOACC_PROFILING_SETUP_P (thr
, &prof_info
, &api_info
);
858 prof_info
.async
= async
;
859 prof_info
.async_queue
= prof_info
.async
;
862 gomp_mutex_lock (&acc_dev
->lock
);
864 n
= lookup_host (acc_dev
, h
, s
);
868 gomp_mutex_unlock (&acc_dev
->lock
);
869 gomp_fatal ("[%p,%d] is not mapped", h
, (int)s
);
872 d
= (void *) (n
->tgt
->tgt_start
+ n
->tgt_offset
873 + (uintptr_t) h
- n
->host_start
);
875 goacc_aq aq
= get_goacc_asyncqueue (async
);
878 gomp_copy_host2dev (acc_dev
, aq
, d
, h
, s
, /* TODO: cbuf? */ NULL
);
880 gomp_copy_dev2host (acc_dev
, aq
, h
, d
, s
);
882 gomp_mutex_unlock (&acc_dev
->lock
);
886 thr
->prof_info
= NULL
;
887 thr
->api_info
= NULL
;
892 acc_update_device (void *h
, size_t s
)
894 update_dev_host (1, h
, s
, acc_async_sync
);
898 acc_update_device_async (void *h
, size_t s
, int async
)
900 update_dev_host (1, h
, s
, async
);
904 acc_update_self (void *h
, size_t s
)
906 update_dev_host (0, h
, s
, acc_async_sync
);
910 acc_update_self_async (void *h
, size_t s
, int async
)
912 update_dev_host (0, h
, s
, async
);
916 /* OpenACC 'enter data', 'exit data': 'GOACC_enter_exit_data' and its helper
919 /* Special handling for 'GOMP_MAP_POINTER', 'GOMP_MAP_TO_PSET'.
921 Only the first mapping is considered in reference counting; the following
922 ones implicitly follow suit. Similarly, 'copyout' is done only for the
926 goacc_remove_pointer (void *h
, size_t s
, unsigned short kind
, int async
)
930 struct goacc_thread
*thr
= goacc_thread ();
931 struct gomp_device_descr
*acc_dev
= thr
->dev
;
933 struct target_mem_desc
*t
;
935 if (!acc_is_present (h
, s
))
938 gomp_mutex_lock (&acc_dev
->lock
);
940 n
= lookup_host (acc_dev
, h
, 1);
944 gomp_mutex_unlock (&acc_dev
->lock
);
945 gomp_fatal ("%p is not a mapped block", (void *)h
);
948 gomp_debug (0, " %s: restore mappings\n", __FUNCTION__
);
952 assert (n
->refcount
!= REFCOUNT_INFINITY
953 && n
->refcount
!= REFCOUNT_LINK
);
954 if (n
->refcount
< n
->dynamic_refcount
)
956 gomp_mutex_unlock (&acc_dev
->lock
);
957 gomp_fatal ("Dynamic reference counting assert fail\n");
960 bool finalize
= (kind
== GOMP_MAP_DELETE
961 || kind
== GOMP_MAP_FORCE_FROM
);
964 n
->refcount
-= n
->dynamic_refcount
;
965 n
->dynamic_refcount
= 0;
967 else if (n
->dynamic_refcount
)
970 n
->dynamic_refcount
--;
973 if (n
->refcount
== 0)
975 goacc_aq aq
= get_goacc_asyncqueue (async
);
977 bool copyout
= (kind
== GOMP_MAP_FROM
978 || kind
== GOMP_MAP_FORCE_FROM
);
981 void *d
= (void *) (t
->tgt_start
+ n
->tgt_offset
982 + (uintptr_t) h
- n
->host_start
);
983 gomp_copy_dev2host (acc_dev
, aq
, h
, d
, s
);
988 /* TODO The way the following code is currently implemented, we need
989 the 'is_tgt_unmapped' return value from 'gomp_remove_var', so
990 can't use 'gomp_remove_var_async' here -- see the 'gomp_unref_tgt'
992 <http://mid.mail-archive.com/878snl36eu.fsf@euler.schwinge.homeip.net>;
993 PR92881 -- so have to synchronize here. */
994 if (!acc_dev
->openacc
.async
.synchronize_func (aq
))
996 gomp_mutex_unlock (&acc_dev
->lock
);
997 gomp_fatal ("synchronize failed");
1000 bool is_tgt_unmapped
= false;
1001 for (size_t i
= 0; i
< t
->list_count
; i
++)
1003 is_tgt_unmapped
= gomp_remove_var (acc_dev
, t
->list
[i
].key
);
1004 if (is_tgt_unmapped
)
1007 assert (is_tgt_unmapped
);
1010 gomp_mutex_unlock (&acc_dev
->lock
);
1012 gomp_debug (0, " %s: mappings restored\n", __FUNCTION__
);
1015 /* Return the number of mappings associated with 'GOMP_MAP_TO_PSET' or
1016 'GOMP_MAP_POINTER'. */
1019 find_pointer (int pos
, size_t mapnum
, unsigned short *kinds
)
1021 if (pos
+ 1 >= mapnum
)
1024 unsigned char kind
= kinds
[pos
+1] & 0xff;
1026 if (kind
== GOMP_MAP_TO_PSET
)
1028 else if (kind
== GOMP_MAP_POINTER
)
1035 GOACC_enter_exit_data (int flags_m
, size_t mapnum
, void **hostaddrs
,
1036 size_t *sizes
, unsigned short *kinds
, int async
,
1039 int flags
= GOACC_FLAGS_UNMARSHAL (flags_m
);
1041 struct goacc_thread
*thr
;
1042 struct gomp_device_descr
*acc_dev
;
1043 bool data_enter
= false;
1046 goacc_lazy_initialize ();
1048 thr
= goacc_thread ();
1051 /* Determine if this is an "acc enter data". */
1052 for (i
= 0; i
< mapnum
; ++i
)
1054 unsigned char kind
= kinds
[i
] & 0xff;
1056 if (kind
== GOMP_MAP_POINTER
|| kind
== GOMP_MAP_TO_PSET
)
1059 if (kind
== GOMP_MAP_FORCE_ALLOC
1060 || kind
== GOMP_MAP_FORCE_PRESENT
1061 || kind
== GOMP_MAP_FORCE_TO
1062 || kind
== GOMP_MAP_TO
1063 || kind
== GOMP_MAP_ALLOC
)
1069 if (kind
== GOMP_MAP_RELEASE
1070 || kind
== GOMP_MAP_DELETE
1071 || kind
== GOMP_MAP_FROM
1072 || kind
== GOMP_MAP_FORCE_FROM
)
1075 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1079 bool profiling_p
= GOACC_PROFILING_DISPATCH_P (true);
1081 acc_prof_info prof_info
;
1084 thr
->prof_info
= &prof_info
;
1086 prof_info
.event_type
1087 = data_enter
? acc_ev_enter_data_start
: acc_ev_exit_data_start
;
1088 prof_info
.valid_bytes
= _ACC_PROF_INFO_VALID_BYTES
;
1089 prof_info
.version
= _ACC_PROF_INFO_VERSION
;
1090 prof_info
.device_type
= acc_device_type (acc_dev
->type
);
1091 prof_info
.device_number
= acc_dev
->target_id
;
1092 prof_info
.thread_id
= -1;
1093 prof_info
.async
= async
;
1094 prof_info
.async_queue
= prof_info
.async
;
1095 prof_info
.src_file
= NULL
;
1096 prof_info
.func_name
= NULL
;
1097 prof_info
.line_no
= -1;
1098 prof_info
.end_line_no
= -1;
1099 prof_info
.func_line_no
= -1;
1100 prof_info
.func_end_line_no
= -1;
1102 acc_event_info enter_exit_data_event_info
;
1105 enter_exit_data_event_info
.other_event
.event_type
1106 = prof_info
.event_type
;
1107 enter_exit_data_event_info
.other_event
.valid_bytes
1108 = _ACC_OTHER_EVENT_INFO_VALID_BYTES
;
1109 enter_exit_data_event_info
.other_event
.parent_construct
1110 = data_enter
? acc_construct_enter_data
: acc_construct_exit_data
;
1111 enter_exit_data_event_info
.other_event
.implicit
= 0;
1112 enter_exit_data_event_info
.other_event
.tool_info
= NULL
;
1114 acc_api_info api_info
;
1117 thr
->api_info
= &api_info
;
1119 api_info
.device_api
= acc_device_api_none
;
1120 api_info
.valid_bytes
= _ACC_API_INFO_VALID_BYTES
;
1121 api_info
.device_type
= prof_info
.device_type
;
1122 api_info
.vendor
= -1;
1123 api_info
.device_handle
= NULL
;
1124 api_info
.context_handle
= NULL
;
1125 api_info
.async_handle
= NULL
;
1129 goacc_profiling_dispatch (&prof_info
, &enter_exit_data_event_info
,
1132 if ((acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
1133 || (flags
& GOACC_FLAG_HOST_FALLBACK
))
1135 prof_info
.device_type
= acc_device_host
;
1136 api_info
.device_type
= prof_info
.device_type
;
1145 va_start (ap
, num_waits
);
1146 goacc_wait (async
, num_waits
, &ap
);
1150 /* In c, non-pointers and arrays are represented by a single data clause.
1151 Dynamically allocated arrays and subarrays are represented by a data
1152 clause followed by an internal GOMP_MAP_POINTER.
1154 In fortran, scalars and not allocated arrays are represented by a
1155 single data clause. Allocated arrays and subarrays have three mappings:
1156 1) the original data clause, 2) a PSET 3) a pointer to the array data.
1161 for (i
= 0; i
< mapnum
; i
++)
1163 /* Scan for pointers and PSETs. */
1164 int pointer
= find_pointer (i
, mapnum
, kinds
);
1168 unsigned char kind
= kinds
[i
] & 0xff;
1171 case GOMP_MAP_ALLOC
:
1172 case GOMP_MAP_FORCE_ALLOC
:
1174 case GOMP_MAP_FORCE_TO
:
1177 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1182 /* We actually have one mapping. */
1186 goacc_enter_data (pointer
, &hostaddrs
[i
], &sizes
[i
], &kinds
[i
],
1188 /* If applicable, increment 'i' further; OpenACC requires fortran
1189 arrays to be contiguous, so each PSET is associated with
1190 one of MAP_FORCE_ALLOC/MAP_FORCE_PRESET/MAP_FORCE_TO, and
1196 for (i
= 0; i
< mapnum
; ++i
)
1198 int pointer
= find_pointer (i
, mapnum
, kinds
);
1202 unsigned char kind
= kinds
[i
] & 0xff;
1205 case GOMP_MAP_RELEASE
:
1206 case GOMP_MAP_DELETE
:
1208 case GOMP_MAP_FORCE_FROM
:
1211 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
1216 goacc_exit_data (hostaddrs
[i
], sizes
[i
], kinds
[i
], async
);
1220 goacc_remove_pointer (hostaddrs
[i
], sizes
[i
], kinds
[i
], async
);
1221 /* See the above comment. */
1229 prof_info
.event_type
1230 = data_enter
? acc_ev_enter_data_end
: acc_ev_exit_data_end
;
1231 enter_exit_data_event_info
.other_event
.event_type
= prof_info
.event_type
;
1232 goacc_profiling_dispatch (&prof_info
, &enter_exit_data_event_info
,
1235 thr
->prof_info
= NULL
;
1236 thr
->api_info
= NULL
;