testsuite: add two missing label_values annotations
[official-gcc.git] / libgomp / oacc-mem.c
blobbd4b62b006e8ad573e220e8e8589aaa4e5809fcb
1 /* OpenACC Runtime initialization routines
3 Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 Contributed by Mentor Embedded.
7 This file is part of the GNU Offloading and Multi Processing Library
8 (libgomp).
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.
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.
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/>. */
29 #include "openacc.h"
30 #include "config.h"
31 #include "libgomp.h"
32 #include "gomp-constants.h"
33 #include "oacc-int.h"
34 #include <stdint.h>
35 #include <string.h>
36 #include <assert.h>
38 /* Return block containing [H->S), or NULL if not contained. The device lock
39 for DEV must be locked on entry, and remains locked on exit. */
41 static splay_tree_key
42 lookup_host (struct gomp_device_descr *dev, void *h, size_t s)
44 struct splay_tree_key_s node;
45 splay_tree_key key;
47 node.host_start = (uintptr_t) h;
48 node.host_end = (uintptr_t) h + s;
50 key = splay_tree_lookup (&dev->mem_map, &node);
52 return key;
55 /* Return block containing [D->S), or NULL if not contained.
56 The list isn't ordered by device address, so we have to iterate
57 over the whole array. This is not expected to be a common
58 operation. The device lock associated with TGT must be locked on entry, and
59 remains locked on exit. */
61 static splay_tree_key
62 lookup_dev (struct target_mem_desc *tgt, void *d, size_t s)
64 int i;
65 struct target_mem_desc *t;
67 if (!tgt)
68 return NULL;
70 for (t = tgt; t != NULL; t = t->prev)
72 if (t->tgt_start <= (uintptr_t) d && t->tgt_end >= (uintptr_t) d + s)
73 break;
76 if (!t)
77 return NULL;
79 for (i = 0; i < t->list_count; i++)
81 void * offset;
83 splay_tree_key k = &t->array[i].key;
84 offset = d - t->tgt_start + k->tgt_offset;
86 if (k->host_start + offset <= (void *) k->host_end)
87 return k;
90 return NULL;
93 /* OpenACC is silent on how memory exhaustion is indicated. We return
94 NULL. */
96 void *
97 acc_malloc (size_t s)
99 if (!s)
100 return NULL;
102 goacc_lazy_initialize ();
104 struct goacc_thread *thr = goacc_thread ();
106 assert (thr->dev);
108 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
109 return malloc (s);
111 return thr->dev->alloc_func (thr->dev->target_id, s);
114 /* OpenACC 2.0a (3.2.16) doesn't specify what to do in the event
115 the device address is mapped. We choose to check if it mapped,
116 and if it is, to unmap it. */
117 void
118 acc_free (void *d)
120 splay_tree_key k;
122 if (!d)
123 return;
125 struct goacc_thread *thr = goacc_thread ();
127 assert (thr && thr->dev);
129 struct gomp_device_descr *acc_dev = thr->dev;
131 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
132 return free (d);
134 gomp_mutex_lock (&acc_dev->lock);
136 /* We don't have to call lazy open here, as the ptr value must have
137 been returned by acc_malloc. It's not permitted to pass NULL in
138 (unless you got that null from acc_malloc). */
139 if ((k = lookup_dev (acc_dev->openacc.data_environ, d, 1)))
141 void *offset;
143 offset = d - k->tgt->tgt_start + k->tgt_offset;
145 gomp_mutex_unlock (&acc_dev->lock);
147 acc_unmap_data ((void *)(k->host_start + offset));
149 else
150 gomp_mutex_unlock (&acc_dev->lock);
152 if (!acc_dev->free_func (acc_dev->target_id, d))
153 gomp_fatal ("error in freeing device memory in %s", __FUNCTION__);
156 void
157 acc_memcpy_to_device (void *d, void *h, size_t s)
159 /* No need to call lazy open here, as the device pointer must have
160 been obtained from a routine that did that. */
161 struct goacc_thread *thr = goacc_thread ();
163 assert (thr && thr->dev);
165 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
167 memmove (d, h, s);
168 return;
171 if (!thr->dev->host2dev_func (thr->dev->target_id, d, h, s))
172 gomp_fatal ("error in %s", __FUNCTION__);
175 void
176 acc_memcpy_from_device (void *h, void *d, size_t s)
178 /* No need to call lazy open here, as the device pointer must have
179 been obtained from a routine that did that. */
180 struct goacc_thread *thr = goacc_thread ();
182 assert (thr && thr->dev);
184 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
186 memmove (h, d, s);
187 return;
190 if (!thr->dev->dev2host_func (thr->dev->target_id, h, d, s))
191 gomp_fatal ("error in %s", __FUNCTION__);
194 /* Return the device pointer that corresponds to host data H. Or NULL
195 if no mapping. */
197 void *
198 acc_deviceptr (void *h)
200 splay_tree_key n;
201 void *d;
202 void *offset;
204 goacc_lazy_initialize ();
206 struct goacc_thread *thr = goacc_thread ();
207 struct gomp_device_descr *dev = thr->dev;
209 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
210 return h;
212 gomp_mutex_lock (&dev->lock);
214 n = lookup_host (dev, h, 1);
216 if (!n)
218 gomp_mutex_unlock (&dev->lock);
219 return NULL;
222 offset = h - n->host_start;
224 d = n->tgt->tgt_start + n->tgt_offset + offset;
226 gomp_mutex_unlock (&dev->lock);
228 return d;
231 /* Return the host pointer that corresponds to device data D. Or NULL
232 if no mapping. */
234 void *
235 acc_hostptr (void *d)
237 splay_tree_key n;
238 void *h;
239 void *offset;
241 goacc_lazy_initialize ();
243 struct goacc_thread *thr = goacc_thread ();
244 struct gomp_device_descr *acc_dev = thr->dev;
246 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
247 return d;
249 gomp_mutex_lock (&acc_dev->lock);
251 n = lookup_dev (acc_dev->openacc.data_environ, d, 1);
253 if (!n)
255 gomp_mutex_unlock (&acc_dev->lock);
256 return NULL;
259 offset = d - n->tgt->tgt_start + n->tgt_offset;
261 h = n->host_start + offset;
263 gomp_mutex_unlock (&acc_dev->lock);
265 return h;
268 /* Return 1 if host data [H,+S] is present on the device. */
271 acc_is_present (void *h, size_t s)
273 splay_tree_key n;
275 if (!s || !h)
276 return 0;
278 goacc_lazy_initialize ();
280 struct goacc_thread *thr = goacc_thread ();
281 struct gomp_device_descr *acc_dev = thr->dev;
283 if (thr->dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
284 return h != NULL;
286 gomp_mutex_lock (&acc_dev->lock);
288 n = lookup_host (acc_dev, h, s);
290 if (n && ((uintptr_t)h < n->host_start
291 || (uintptr_t)h + s > n->host_end
292 || s > n->host_end - n->host_start))
293 n = NULL;
295 gomp_mutex_unlock (&acc_dev->lock);
297 return n != NULL;
300 /* Create a mapping for host [H,+S] -> device [D,+S] */
302 void
303 acc_map_data (void *h, void *d, size_t s)
305 struct target_mem_desc *tgt = NULL;
306 size_t mapnum = 1;
307 void *hostaddrs = h;
308 void *devaddrs = d;
309 size_t sizes = s;
310 unsigned short kinds = GOMP_MAP_ALLOC;
312 goacc_lazy_initialize ();
314 struct goacc_thread *thr = goacc_thread ();
315 struct gomp_device_descr *acc_dev = thr->dev;
317 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
319 if (d != h)
320 gomp_fatal ("cannot map data on shared-memory system");
322 else
324 struct goacc_thread *thr = goacc_thread ();
326 if (!d || !h || !s)
327 gomp_fatal ("[%p,+%d]->[%p,+%d] is a bad map",
328 (void *)h, (int)s, (void *)d, (int)s);
330 gomp_mutex_lock (&acc_dev->lock);
332 if (lookup_host (acc_dev, h, s))
334 gomp_mutex_unlock (&acc_dev->lock);
335 gomp_fatal ("host address [%p, +%d] is already mapped", (void *)h,
336 (int)s);
339 if (lookup_dev (thr->dev->openacc.data_environ, d, s))
341 gomp_mutex_unlock (&acc_dev->lock);
342 gomp_fatal ("device address [%p, +%d] is already mapped", (void *)d,
343 (int)s);
346 gomp_mutex_unlock (&acc_dev->lock);
348 tgt = gomp_map_vars (acc_dev, mapnum, &hostaddrs, &devaddrs, &sizes,
349 &kinds, true, GOMP_MAP_VARS_OPENACC);
352 gomp_mutex_lock (&acc_dev->lock);
353 tgt->prev = acc_dev->openacc.data_environ;
354 acc_dev->openacc.data_environ = tgt;
355 gomp_mutex_unlock (&acc_dev->lock);
358 void
359 acc_unmap_data (void *h)
361 struct goacc_thread *thr = goacc_thread ();
362 struct gomp_device_descr *acc_dev = thr->dev;
364 /* No need to call lazy open, as the address must have been mapped. */
366 /* This is a no-op on shared-memory targets. */
367 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
368 return;
370 size_t host_size;
372 gomp_mutex_lock (&acc_dev->lock);
374 splay_tree_key n = lookup_host (acc_dev, h, 1);
375 struct target_mem_desc *t;
377 if (!n)
379 gomp_mutex_unlock (&acc_dev->lock);
380 gomp_fatal ("%p is not a mapped block", (void *)h);
383 host_size = n->host_end - n->host_start;
385 if (n->host_start != (uintptr_t) h)
387 gomp_mutex_unlock (&acc_dev->lock);
388 gomp_fatal ("[%p,%d] surrounds %p",
389 (void *) n->host_start, (int) host_size, (void *) h);
392 t = n->tgt;
394 if (t->refcount == 2)
396 struct target_mem_desc *tp;
398 /* This is the last reference, so pull the descriptor off the
399 chain. This avoids gomp_unmap_vars via gomp_unmap_tgt from
400 freeing the device memory. */
401 t->tgt_end = 0;
402 t->to_free = 0;
404 for (tp = NULL, t = acc_dev->openacc.data_environ; t != NULL;
405 tp = t, t = t->prev)
406 if (n->tgt == t)
408 if (tp)
409 tp->prev = t->prev;
410 else
411 acc_dev->openacc.data_environ = t->prev;
413 break;
417 gomp_mutex_unlock (&acc_dev->lock);
419 gomp_unmap_vars (t, true);
422 #define FLAG_PRESENT (1 << 0)
423 #define FLAG_CREATE (1 << 1)
424 #define FLAG_COPY (1 << 2)
426 static void *
427 present_create_copy (unsigned f, void *h, size_t s)
429 void *d;
430 splay_tree_key n;
432 if (!h || !s)
433 gomp_fatal ("[%p,+%d] is a bad range", (void *)h, (int)s);
435 goacc_lazy_initialize ();
437 struct goacc_thread *thr = goacc_thread ();
438 struct gomp_device_descr *acc_dev = thr->dev;
440 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
441 return h;
443 gomp_mutex_lock (&acc_dev->lock);
445 n = lookup_host (acc_dev, h, s);
446 if (n)
448 /* Present. */
449 d = (void *) (n->tgt->tgt_start + n->tgt_offset);
451 if (!(f & FLAG_PRESENT))
453 gomp_mutex_unlock (&acc_dev->lock);
454 gomp_fatal ("[%p,+%d] already mapped to [%p,+%d]",
455 (void *)h, (int)s, (void *)d, (int)s);
457 if ((h + s) > (void *)n->host_end)
459 gomp_mutex_unlock (&acc_dev->lock);
460 gomp_fatal ("[%p,+%d] not mapped", (void *)h, (int)s);
463 gomp_mutex_unlock (&acc_dev->lock);
465 else if (!(f & FLAG_CREATE))
467 gomp_mutex_unlock (&acc_dev->lock);
468 gomp_fatal ("[%p,+%d] not mapped", (void *)h, (int)s);
470 else
472 struct target_mem_desc *tgt;
473 size_t mapnum = 1;
474 unsigned short kinds;
475 void *hostaddrs = h;
477 if (f & FLAG_COPY)
478 kinds = GOMP_MAP_TO;
479 else
480 kinds = GOMP_MAP_ALLOC;
482 gomp_mutex_unlock (&acc_dev->lock);
484 tgt = gomp_map_vars (acc_dev, mapnum, &hostaddrs, NULL, &s, &kinds, true,
485 GOMP_MAP_VARS_OPENACC);
487 gomp_mutex_lock (&acc_dev->lock);
489 d = tgt->to_free;
490 tgt->prev = acc_dev->openacc.data_environ;
491 acc_dev->openacc.data_environ = tgt;
493 gomp_mutex_unlock (&acc_dev->lock);
496 return d;
499 void *
500 acc_create (void *h, size_t s)
502 return present_create_copy (FLAG_CREATE, h, s);
505 void *
506 acc_copyin (void *h, size_t s)
508 return present_create_copy (FLAG_CREATE | FLAG_COPY, h, s);
511 void *
512 acc_present_or_create (void *h, size_t s)
514 return present_create_copy (FLAG_PRESENT | FLAG_CREATE, h, s);
517 void *
518 acc_present_or_copyin (void *h, size_t s)
520 return present_create_copy (FLAG_PRESENT | FLAG_CREATE | FLAG_COPY, h, s);
523 #define FLAG_COPYOUT (1 << 0)
525 static void
526 delete_copyout (unsigned f, void *h, size_t s, const char *libfnname)
528 size_t host_size;
529 splay_tree_key n;
530 void *d;
531 struct goacc_thread *thr = goacc_thread ();
532 struct gomp_device_descr *acc_dev = thr->dev;
534 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
535 return;
537 gomp_mutex_lock (&acc_dev->lock);
539 n = lookup_host (acc_dev, h, s);
541 /* No need to call lazy open, as the data must already have been
542 mapped. */
544 if (!n)
546 gomp_mutex_unlock (&acc_dev->lock);
547 gomp_fatal ("[%p,%d] is not mapped", (void *)h, (int)s);
550 d = (void *) (n->tgt->tgt_start + n->tgt_offset
551 + (uintptr_t) h - n->host_start);
553 host_size = n->host_end - n->host_start;
555 if (n->host_start != (uintptr_t) h || host_size != s)
557 gomp_mutex_unlock (&acc_dev->lock);
558 gomp_fatal ("[%p,%d] surrounds2 [%p,+%d]",
559 (void *) n->host_start, (int) host_size, (void *) h, (int) s);
562 gomp_mutex_unlock (&acc_dev->lock);
564 if (f & FLAG_COPYOUT)
565 acc_dev->dev2host_func (acc_dev->target_id, h, d, s);
567 acc_unmap_data (h);
569 if (!acc_dev->free_func (acc_dev->target_id, d))
570 gomp_fatal ("error in freeing device memory in %s", libfnname);
573 void
574 acc_delete (void *h , size_t s)
576 delete_copyout (0, h, s, __FUNCTION__);
579 void
580 acc_copyout (void *h, size_t s)
582 delete_copyout (FLAG_COPYOUT, h, s, __FUNCTION__);
585 static void
586 update_dev_host (int is_dev, void *h, size_t s)
588 splay_tree_key n;
589 void *d;
591 goacc_lazy_initialize ();
593 struct goacc_thread *thr = goacc_thread ();
594 struct gomp_device_descr *acc_dev = thr->dev;
596 if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
597 return;
599 gomp_mutex_lock (&acc_dev->lock);
601 n = lookup_host (acc_dev, h, s);
603 if (!n)
605 gomp_mutex_unlock (&acc_dev->lock);
606 gomp_fatal ("[%p,%d] is not mapped", h, (int)s);
609 d = (void *) (n->tgt->tgt_start + n->tgt_offset
610 + (uintptr_t) h - n->host_start);
612 if (is_dev)
613 acc_dev->host2dev_func (acc_dev->target_id, d, h, s);
614 else
615 acc_dev->dev2host_func (acc_dev->target_id, h, d, s);
617 gomp_mutex_unlock (&acc_dev->lock);
620 void
621 acc_update_device (void *h, size_t s)
623 update_dev_host (1, h, s);
626 void
627 acc_update_self (void *h, size_t s)
629 update_dev_host (0, h, s);
632 void
633 gomp_acc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
634 void *kinds)
636 struct target_mem_desc *tgt;
637 struct goacc_thread *thr = goacc_thread ();
638 struct gomp_device_descr *acc_dev = thr->dev;
640 gomp_debug (0, " %s: prepare mappings\n", __FUNCTION__);
641 tgt = gomp_map_vars (acc_dev, mapnum, hostaddrs,
642 NULL, sizes, kinds, true, GOMP_MAP_VARS_OPENACC);
643 gomp_debug (0, " %s: mappings prepared\n", __FUNCTION__);
645 gomp_mutex_lock (&acc_dev->lock);
646 tgt->prev = acc_dev->openacc.data_environ;
647 acc_dev->openacc.data_environ = tgt;
648 gomp_mutex_unlock (&acc_dev->lock);
651 void
652 gomp_acc_remove_pointer (void *h, bool force_copyfrom, int async, int mapnum)
654 struct goacc_thread *thr = goacc_thread ();
655 struct gomp_device_descr *acc_dev = thr->dev;
656 splay_tree_key n;
657 struct target_mem_desc *t;
658 int minrefs = (mapnum == 1) ? 2 : 3;
660 gomp_mutex_lock (&acc_dev->lock);
662 n = lookup_host (acc_dev, h, 1);
664 if (!n)
666 gomp_mutex_unlock (&acc_dev->lock);
667 gomp_fatal ("%p is not a mapped block", (void *)h);
670 gomp_debug (0, " %s: restore mappings\n", __FUNCTION__);
672 t = n->tgt;
674 struct target_mem_desc *tp;
676 if (t->refcount == minrefs)
678 /* This is the last reference, so pull the descriptor off the
679 chain. This avoids gomp_unmap_vars via gomp_unmap_tgt from
680 freeing the device memory. */
681 t->tgt_end = 0;
682 t->to_free = 0;
684 for (tp = NULL, t = acc_dev->openacc.data_environ; t != NULL;
685 tp = t, t = t->prev)
687 if (n->tgt == t)
689 if (tp)
690 tp->prev = t->prev;
691 else
692 acc_dev->openacc.data_environ = t->prev;
693 break;
698 if (force_copyfrom)
699 t->list[0].copy_from = 1;
701 gomp_mutex_unlock (&acc_dev->lock);
703 /* If running synchronously, unmap immediately. */
704 if (async < acc_async_noval)
705 gomp_unmap_vars (t, true);
706 else
707 t->device_descr->openacc.register_async_cleanup_func (t, async);
709 gomp_debug (0, " %s: mappings restored\n", __FUNCTION__);