-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtarget.c
5389 lines (4885 loc) · 163 KB
/
target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2013-2024 Free Software Foundation, Inc.
Contributed by Jakub Jelinek <jakub@redhat.com>.
This file is part of the GNU Offloading and Multi Processing Library
(libgomp).
Libgomp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/* This file contains the support of offloading. */
#include "libgomp.h"
#include "oacc-plugin.h"
#include "oacc-int.h"
#include "gomp-constants.h"
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h> /* For PRIu64. */
#endif
#include <string.h>
#include <stdio.h> /* For snprintf. */
#include <assert.h>
#include <errno.h>
#ifdef PLUGIN_SUPPORT
#include <dlfcn.h>
#include "plugin-suffix.h"
#endif
/* Define another splay tree instantiation - for reverse offload. */
#define splay_tree_prefix reverse
#define splay_tree_static
#define splay_tree_c
#include "splay-tree.h"
typedef uintptr_t *hash_entry_type;
static inline void * htab_alloc (size_t size) { return gomp_malloc (size); }
static inline void htab_free (void *ptr) { free (ptr); }
#include "hashtab.h"
ialias_redirect (GOMP_task)
static inline hashval_t
htab_hash (hash_entry_type element)
{
return hash_pointer ((void *) element);
}
static inline bool
htab_eq (hash_entry_type x, hash_entry_type y)
{
return x == y;
}
#define FIELD_TGT_EMPTY (~(size_t) 0)
static void gomp_target_init (void);
/* The whole initialization code for offloading plugins is only run one. */
static pthread_once_t gomp_is_initialized = PTHREAD_ONCE_INIT;
/* Mutex for offload image registration. */
static gomp_mutex_t register_lock;
/* This structure describes an offload image.
It contains type of the target device, pointer to host table descriptor, and
pointer to target data. */
struct offload_image_descr {
unsigned version;
enum offload_target_type type;
const void *host_table;
const void *target_data;
};
/* Array of descriptors of offload images. */
static struct offload_image_descr *offload_images;
/* Total number of offload images. */
static int num_offload_images;
/* Array of descriptors for all available devices. */
static struct gomp_device_descr *devices;
/* Total number of available devices. */
static int num_devices;
/* Number of GOMP_OFFLOAD_CAP_OPENMP_400 devices. */
static int num_devices_openmp;
/* OpenMP requires mask. */
static int omp_requires_mask;
/* Similar to gomp_realloc, but release register_lock before gomp_fatal. */
static void *
gomp_realloc_unlock (void *old, size_t size)
{
void *ret = realloc (old, size);
if (ret == NULL)
{
gomp_mutex_unlock (®ister_lock);
gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
}
return ret;
}
attribute_hidden void
gomp_init_targets_once (void)
{
(void) pthread_once (&gomp_is_initialized, gomp_target_init);
}
attribute_hidden int
gomp_get_num_devices (void)
{
gomp_init_targets_once ();
return num_devices_openmp;
}
static struct gomp_device_descr *
resolve_device (int device_id, bool remapped)
{
/* Get number of devices and thus ensure that 'gomp_init_targets_once' was
called, which must be done before using default_device_var. */
int num_devices = gomp_get_num_devices ();
if (remapped && device_id == GOMP_DEVICE_ICV)
{
struct gomp_task_icv *icv = gomp_icv (false);
device_id = icv->default_device_var;
remapped = false;
}
if (device_id < 0)
{
if (device_id == (remapped ? GOMP_DEVICE_HOST_FALLBACK
: omp_initial_device))
return NULL;
if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
&& num_devices == 0)
gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
"but only the host device is available");
else if (device_id == omp_invalid_device)
gomp_fatal ("omp_invalid_device encountered");
else if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY)
gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
"but device not found");
return NULL;
}
else if (device_id >= num_devices)
{
if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
&& device_id != num_devices)
gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
"but device not found");
return NULL;
}
gomp_mutex_lock (&devices[device_id].lock);
if (devices[device_id].state == GOMP_DEVICE_UNINITIALIZED)
gomp_init_device (&devices[device_id]);
else if (devices[device_id].state == GOMP_DEVICE_FINALIZED)
{
gomp_mutex_unlock (&devices[device_id].lock);
if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY)
gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
"but device is finalized");
return NULL;
}
gomp_mutex_unlock (&devices[device_id].lock);
return &devices[device_id];
}
static inline splay_tree_key
gomp_map_lookup (splay_tree mem_map, splay_tree_key key)
{
if (key->host_start != key->host_end)
return splay_tree_lookup (mem_map, key);
key->host_end++;
splay_tree_key n = splay_tree_lookup (mem_map, key);
key->host_end--;
if (n)
return n;
key->host_start--;
n = splay_tree_lookup (mem_map, key);
key->host_start++;
if (n)
return n;
return splay_tree_lookup (mem_map, key);
}
static inline reverse_splay_tree_key
gomp_map_lookup_rev (reverse_splay_tree mem_map_rev, reverse_splay_tree_key key)
{
return reverse_splay_tree_lookup (mem_map_rev, key);
}
static inline splay_tree_key
gomp_map_0len_lookup (splay_tree mem_map, splay_tree_key key)
{
if (key->host_start != key->host_end)
return splay_tree_lookup (mem_map, key);
key->host_end++;
splay_tree_key n = splay_tree_lookup (mem_map, key);
key->host_end--;
return n;
}
static inline void
gomp_device_copy (struct gomp_device_descr *devicep,
bool (*copy_func) (int, void *, const void *, size_t),
const char *dst, void *dstaddr,
const char *src, const void *srcaddr,
size_t size)
{
if (!copy_func (devicep->target_id, dstaddr, srcaddr, size))
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("Copying of %s object [%p..%p) to %s object [%p..%p) failed",
src, srcaddr, srcaddr + size, dst, dstaddr, dstaddr + size);
}
}
static inline void
goacc_device_copy_async (struct gomp_device_descr *devicep,
bool (*copy_func) (int, void *, const void *, size_t,
struct goacc_asyncqueue *),
const char *dst, void *dstaddr,
const char *src, const void *srcaddr,
const void *srcaddr_orig,
size_t size, struct goacc_asyncqueue *aq)
{
if (!copy_func (devicep->target_id, dstaddr, srcaddr, size, aq))
{
gomp_mutex_unlock (&devicep->lock);
if (srcaddr_orig && srcaddr_orig != srcaddr)
gomp_fatal ("Copying of %s object [%p..%p)"
" via buffer %s object [%p..%p)"
" to %s object [%p..%p) failed",
src, srcaddr_orig, srcaddr_orig + size,
src, srcaddr, srcaddr + size,
dst, dstaddr, dstaddr + size);
else
gomp_fatal ("Copying of %s object [%p..%p)"
" to %s object [%p..%p) failed",
src, srcaddr, srcaddr + size,
dst, dstaddr, dstaddr + size);
}
}
/* Infrastructure for coalescing adjacent or nearly adjacent (in device addresses)
host to device memory transfers. */
struct gomp_coalesce_chunk
{
/* The starting and ending point of a coalesced chunk of memory. */
size_t start, end;
};
struct gomp_coalesce_buf
{
/* Buffer into which gomp_copy_host2dev will memcpy data and from which
it will be copied to the device. */
void *buf;
struct target_mem_desc *tgt;
/* Array with offsets, chunks[i].start is the starting offset and
chunks[i].end ending offset relative to tgt->tgt_start device address
of chunks which are to be copied to buf and later copied to device. */
struct gomp_coalesce_chunk *chunks;
/* Number of chunks in chunks array, or -1 if coalesce buffering should not
be performed. */
long chunk_cnt;
/* During construction of chunks array, how many memory regions are within
the last chunk. If there is just one memory region for a chunk, we copy
it directly to device rather than going through buf. */
long use_cnt;
};
/* Maximum size of memory region considered for coalescing. Larger copies
are performed directly. */
#define MAX_COALESCE_BUF_SIZE (32 * 1024)
/* Maximum size of a gap in between regions to consider them being copied
within the same chunk. All the device offsets considered are within
newly allocated device memory, so it isn't fatal if we copy some padding
in between from host to device. The gaps come either from alignment
padding or from memory regions which are not supposed to be copied from
host to device (e.g. map(alloc:), map(from:) etc.). */
#define MAX_COALESCE_BUF_GAP (4 * 1024)
/* Add region with device tgt_start relative offset and length to CBUF.
This must not be used for asynchronous copies, because the host data might
not be computed yet (by an earlier asynchronous compute region, for
example). The exception is for EPHEMERAL data, that we know is available
already "by construction". */
static inline void
gomp_coalesce_buf_add (struct gomp_coalesce_buf *cbuf, size_t start, size_t len)
{
if (len > MAX_COALESCE_BUF_SIZE || len == 0)
return;
if (cbuf->chunk_cnt)
{
if (cbuf->chunk_cnt < 0)
return;
if (start < cbuf->chunks[cbuf->chunk_cnt - 1].end)
{
cbuf->chunk_cnt = -1;
return;
}
if (start < cbuf->chunks[cbuf->chunk_cnt - 1].end + MAX_COALESCE_BUF_GAP)
{
cbuf->chunks[cbuf->chunk_cnt - 1].end = start + len;
cbuf->use_cnt++;
return;
}
/* If the last chunk is only used by one mapping, discard it,
as it will be one host to device copy anyway and
memcpying it around will only waste cycles. */
if (cbuf->use_cnt == 1)
cbuf->chunk_cnt--;
}
cbuf->chunks[cbuf->chunk_cnt].start = start;
cbuf->chunks[cbuf->chunk_cnt].end = start + len;
cbuf->chunk_cnt++;
cbuf->use_cnt = 1;
}
/* Return true for mapping kinds which need to copy data from the
host to device for regions that weren't previously mapped. */
static inline bool
gomp_to_device_kind_p (int kind)
{
switch (kind)
{
case GOMP_MAP_ALLOC:
case GOMP_MAP_FROM:
case GOMP_MAP_FORCE_ALLOC:
case GOMP_MAP_FORCE_FROM:
case GOMP_MAP_ALWAYS_FROM:
case GOMP_MAP_ALWAYS_PRESENT_FROM:
case GOMP_MAP_FORCE_PRESENT:
return false;
default:
return true;
}
}
/* Copy host memory to an offload device. In asynchronous mode (if AQ is
non-NULL), when the source data is stack or may otherwise be deallocated
before the asynchronous copy takes place, EPHEMERAL must be passed as
TRUE. */
attribute_hidden void
gomp_copy_host2dev (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq,
void *d, const void *h, size_t sz,
bool ephemeral, struct gomp_coalesce_buf *cbuf)
{
if (cbuf)
{
uintptr_t doff = (uintptr_t) d - cbuf->tgt->tgt_start;
if (doff < cbuf->chunks[cbuf->chunk_cnt - 1].end)
{
long first = 0;
long last = cbuf->chunk_cnt - 1;
while (first <= last)
{
long middle = (first + last) >> 1;
if (cbuf->chunks[middle].end <= doff)
first = middle + 1;
else if (cbuf->chunks[middle].start <= doff)
{
if (doff + sz > cbuf->chunks[middle].end)
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("internal libgomp cbuf error");
}
/* In an asynchronous context, verify that CBUF isn't used
with non-EPHEMERAL data; see 'gomp_coalesce_buf_add'. */
if (__builtin_expect (aq != NULL, 0))
assert (ephemeral);
memcpy ((char *) cbuf->buf + (doff - cbuf->chunks[0].start),
h, sz);
return;
}
else
last = middle - 1;
}
}
}
if (__builtin_expect (aq != NULL, 0))
{
void *h_buf = (void *) h;
if (ephemeral)
{
/* We're queueing up an asynchronous copy from data that may
disappear before the transfer takes place (i.e. because it is a
stack local in a function that is no longer executing). As we've
not been able to use CBUF, make a copy of the data into a
temporary buffer. */
h_buf = gomp_malloc (sz);
memcpy (h_buf, h, sz);
}
goacc_device_copy_async (devicep, devicep->openacc.async.host2dev_func,
"dev", d, "host", h_buf, h, sz, aq);
if (ephemeral)
/* Free once the transfer has completed. */
devicep->openacc.async.queue_callback_func (aq, free, h_buf);
}
else
gomp_device_copy (devicep, devicep->host2dev_func,
"dev", d, "host", h, sz);
}
attribute_hidden void
gomp_copy_dev2host (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq,
void *h, const void *d, size_t sz)
{
if (__builtin_expect (aq != NULL, 0))
goacc_device_copy_async (devicep, devicep->openacc.async.dev2host_func,
"host", h, "dev", d, NULL, sz, aq);
else
gomp_device_copy (devicep, devicep->dev2host_func, "host", h, "dev", d, sz);
}
static void
gomp_free_device_memory (struct gomp_device_descr *devicep, void *devptr)
{
if (!devicep->free_func (devicep->target_id, devptr))
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("error in freeing device memory block at %p", devptr);
}
}
/* Increment reference count of a splay_tree_key region K by 1.
If REFCOUNT_SET != NULL, use it to track already seen refcounts, and only
increment the value if refcount is not yet contained in the set (used for
OpenMP 5.0, which specifies that a region's refcount is adjusted at most
once for each construct). */
static inline void
gomp_increment_refcount (splay_tree_key k, htab_t *refcount_set)
{
if (k == NULL
|| k->refcount == REFCOUNT_INFINITY
|| k->refcount == REFCOUNT_ACC_MAP_DATA)
return;
uintptr_t *refcount_ptr = &k->refcount;
if (REFCOUNT_STRUCTELEM_FIRST_P (k->refcount))
refcount_ptr = &k->structelem_refcount;
else if (REFCOUNT_STRUCTELEM_P (k->refcount))
refcount_ptr = k->structelem_refcount_ptr;
if (refcount_set)
{
if (htab_find (*refcount_set, refcount_ptr))
return;
uintptr_t **slot = htab_find_slot (refcount_set, refcount_ptr, INSERT);
*slot = refcount_ptr;
}
*refcount_ptr += 1;
return;
}
/* Decrement reference count of a splay_tree_key region K by 1, or if DELETE_P
is true, set reference count to zero. If REFCOUNT_SET != NULL, use it to
track already seen refcounts, and only adjust the value if refcount is not
yet contained in the set (like gomp_increment_refcount).
Return out-values: set *DO_COPY to true if we set the refcount to zero, or
it is already zero and we know we decremented it earlier. This signals that
associated maps should be copied back to host.
*DO_REMOVE is set to true when we this is the first handling of this refcount
and we are setting it to zero. This signals a removal of this key from the
splay-tree map.
Copy and removal are separated due to cases like handling of structure
elements, e.g. each map of a structure element representing a possible copy
out of a structure field has to be handled individually, but we only signal
removal for one (the first encountered) sibing map. */
static inline void
gomp_decrement_refcount (splay_tree_key k, htab_t *refcount_set, bool delete_p,
bool *do_copy, bool *do_remove)
{
if (k == NULL
|| k->refcount == REFCOUNT_INFINITY
|| k->refcount == REFCOUNT_ACC_MAP_DATA)
{
*do_copy = *do_remove = false;
return;
}
uintptr_t *refcount_ptr = &k->refcount;
if (REFCOUNT_STRUCTELEM_FIRST_P (k->refcount))
refcount_ptr = &k->structelem_refcount;
else if (REFCOUNT_STRUCTELEM_P (k->refcount))
refcount_ptr = k->structelem_refcount_ptr;
bool new_encountered_refcount;
bool set_to_zero = false;
bool is_zero = false;
uintptr_t orig_refcount = *refcount_ptr;
if (refcount_set)
{
if (htab_find (*refcount_set, refcount_ptr))
{
new_encountered_refcount = false;
goto end;
}
uintptr_t **slot = htab_find_slot (refcount_set, refcount_ptr, INSERT);
*slot = refcount_ptr;
new_encountered_refcount = true;
}
else
/* If no refcount_set being used, assume all keys are being decremented
for the first time. */
new_encountered_refcount = true;
if (delete_p)
*refcount_ptr = 0;
else if (*refcount_ptr > 0)
*refcount_ptr -= 1;
end:
if (*refcount_ptr == 0)
{
if (orig_refcount > 0)
set_to_zero = true;
is_zero = true;
}
*do_copy = (set_to_zero || (!new_encountered_refcount && is_zero));
*do_remove = (new_encountered_refcount && set_to_zero);
}
/* Handle the case where gomp_map_lookup, splay_tree_lookup or
gomp_map_0len_lookup found oldn for newn.
Helper function of gomp_map_vars. */
static inline void
gomp_map_vars_existing (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq, splay_tree_key oldn,
splay_tree_key newn, struct target_var_desc *tgt_var,
unsigned char kind, bool always_to_flag, bool implicit,
struct gomp_coalesce_buf *cbuf,
htab_t *refcount_set)
{
assert (kind != GOMP_MAP_ATTACH
|| kind != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
tgt_var->key = oldn;
tgt_var->copy_from = GOMP_MAP_COPY_FROM_P (kind);
tgt_var->always_copy_from = GOMP_MAP_ALWAYS_FROM_P (kind);
tgt_var->is_attach = false;
tgt_var->offset = newn->host_start - oldn->host_start;
/* For implicit maps, old contained in new is valid. */
bool implicit_subset = (implicit
&& newn->host_start <= oldn->host_start
&& oldn->host_end <= newn->host_end);
if (implicit_subset)
tgt_var->length = oldn->host_end - oldn->host_start;
else
tgt_var->length = newn->host_end - newn->host_start;
if (GOMP_MAP_FORCE_P (kind)
/* For implicit maps, old contained in new is valid. */
|| !(implicit_subset
/* Otherwise, new contained inside old is considered valid. */
|| (oldn->host_start <= newn->host_start
&& newn->host_end <= oldn->host_end)))
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("Trying to map into device [%p..%p) object when "
"[%p..%p) is already mapped",
(void *) newn->host_start, (void *) newn->host_end,
(void *) oldn->host_start, (void *) oldn->host_end);
}
if (GOMP_MAP_ALWAYS_TO_P (kind) || always_to_flag)
{
/* Implicit + always should not happen. If this does occur, below
address/length adjustment is a TODO. */
assert (!implicit_subset);
if (oldn->aux && oldn->aux->attach_count)
{
/* We have to be careful not to overwrite still attached pointers
during the copyback to host. */
uintptr_t addr = newn->host_start;
while (addr < newn->host_end)
{
size_t i = (addr - oldn->host_start) / sizeof (void *);
if (oldn->aux->attach_count[i] == 0)
gomp_copy_host2dev (devicep, aq,
(void *) (oldn->tgt->tgt_start
+ oldn->tgt_offset
+ addr - oldn->host_start),
(void *) addr,
sizeof (void *), false, cbuf);
addr += sizeof (void *);
}
}
else
gomp_copy_host2dev (devicep, aq,
(void *) (oldn->tgt->tgt_start + oldn->tgt_offset
+ newn->host_start - oldn->host_start),
(void *) newn->host_start,
newn->host_end - newn->host_start, false, cbuf);
}
gomp_increment_refcount (oldn, refcount_set);
}
static int
get_kind (bool short_mapkind, void *kinds, int idx)
{
if (!short_mapkind)
return ((unsigned char *) kinds)[idx];
int val = ((unsigned short *) kinds)[idx];
if (GOMP_MAP_IMPLICIT_P (val))
val &= ~GOMP_MAP_IMPLICIT;
return val;
}
static bool
get_implicit (bool short_mapkind, void *kinds, int idx)
{
if (!short_mapkind)
return false;
int val = ((unsigned short *) kinds)[idx];
return GOMP_MAP_IMPLICIT_P (val);
}
static void
gomp_map_pointer (struct target_mem_desc *tgt, struct goacc_asyncqueue *aq,
uintptr_t host_ptr, uintptr_t target_offset, uintptr_t bias,
struct gomp_coalesce_buf *cbuf,
bool allow_zero_length_array_sections)
{
struct gomp_device_descr *devicep = tgt->device_descr;
struct splay_tree_s *mem_map = &devicep->mem_map;
struct splay_tree_key_s cur_node;
cur_node.host_start = host_ptr;
if (cur_node.host_start == (uintptr_t) NULL)
{
cur_node.tgt_offset = (uintptr_t) NULL;
gomp_copy_host2dev (devicep, aq,
(void *) (tgt->tgt_start + target_offset),
(void *) &cur_node.tgt_offset, sizeof (void *),
true, cbuf);
return;
}
/* Add bias to the pointer value. */
cur_node.host_start += bias;
cur_node.host_end = cur_node.host_start;
splay_tree_key n = gomp_map_lookup (mem_map, &cur_node);
if (n == NULL)
{
if (allow_zero_length_array_sections)
cur_node.tgt_offset = cur_node.host_start;
else
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("Pointer target of array section wasn't mapped");
}
}
else
{
cur_node.host_start -= n->host_start;
cur_node.tgt_offset
= n->tgt->tgt_start + n->tgt_offset + cur_node.host_start;
/* At this point tgt_offset is target address of the
array section. Now subtract bias to get what we want
to initialize the pointer with. */
cur_node.tgt_offset -= bias;
}
gomp_copy_host2dev (devicep, aq, (void *) (tgt->tgt_start + target_offset),
(void *) &cur_node.tgt_offset, sizeof (void *),
true, cbuf);
}
static void
gomp_map_fields_existing (struct target_mem_desc *tgt,
struct goacc_asyncqueue *aq, splay_tree_key n,
size_t first, size_t i, void **hostaddrs,
size_t *sizes, void *kinds,
struct gomp_coalesce_buf *cbuf, htab_t *refcount_set)
{
struct gomp_device_descr *devicep = tgt->device_descr;
struct splay_tree_s *mem_map = &devicep->mem_map;
struct splay_tree_key_s cur_node;
int kind;
bool implicit;
const bool short_mapkind = true;
const int typemask = short_mapkind ? 0xff : 0x7;
cur_node.host_start = (uintptr_t) hostaddrs[i];
cur_node.host_end = cur_node.host_start + sizes[i];
splay_tree_key n2 = gomp_map_0len_lookup (mem_map, &cur_node);
kind = get_kind (short_mapkind, kinds, i);
implicit = get_implicit (short_mapkind, kinds, i);
if (n2
&& n2->tgt == n->tgt
&& n2->host_start - n->host_start == n2->tgt_offset - n->tgt_offset)
{
gomp_map_vars_existing (devicep, aq, n2, &cur_node, &tgt->list[i],
kind & typemask, false, implicit, cbuf,
refcount_set);
return;
}
if (sizes[i] == 0)
{
if (cur_node.host_start > (uintptr_t) hostaddrs[first - 1])
{
cur_node.host_start--;
n2 = splay_tree_lookup (mem_map, &cur_node);
cur_node.host_start++;
if (n2
&& n2->tgt == n->tgt
&& n2->host_start - n->host_start
== n2->tgt_offset - n->tgt_offset)
{
gomp_map_vars_existing (devicep, aq, n2, &cur_node, &tgt->list[i],
kind & typemask, false, implicit, cbuf,
refcount_set);
return;
}
}
cur_node.host_end++;
n2 = splay_tree_lookup (mem_map, &cur_node);
cur_node.host_end--;
if (n2
&& n2->tgt == n->tgt
&& n2->host_start - n->host_start == n2->tgt_offset - n->tgt_offset)
{
gomp_map_vars_existing (devicep, aq, n2, &cur_node, &tgt->list[i],
kind & typemask, false, implicit, cbuf,
refcount_set);
return;
}
}
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("Trying to map into device [%p..%p) structure element when "
"other mapped elements from the same structure weren't mapped "
"together with it", (void *) cur_node.host_start,
(void *) cur_node.host_end);
}
attribute_hidden void
gomp_attach_pointer (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq, splay_tree mem_map,
splay_tree_key n, uintptr_t attach_to, size_t bias,
struct gomp_coalesce_buf *cbufp,
bool allow_zero_length_array_sections)
{
struct splay_tree_key_s s;
size_t size, idx;
if (n == NULL)
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("enclosing struct not mapped for attach");
}
size = (n->host_end - n->host_start + sizeof (void *) - 1) / sizeof (void *);
/* We might have a pointer in a packed struct: however we cannot have more
than one such pointer in each pointer-sized portion of the struct, so
this is safe. */
idx = (attach_to - n->host_start) / sizeof (void *);
if (!n->aux)
n->aux = gomp_malloc_cleared (sizeof (struct splay_tree_aux));
if (!n->aux->attach_count)
n->aux->attach_count
= gomp_malloc_cleared (sizeof (*n->aux->attach_count) * size);
if (n->aux->attach_count[idx] < UINTPTR_MAX)
n->aux->attach_count[idx]++;
else
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("attach count overflow");
}
if (n->aux->attach_count[idx] == 1)
{
uintptr_t devptr = n->tgt->tgt_start + n->tgt_offset + attach_to
- n->host_start;
uintptr_t target = (uintptr_t) *(void **) attach_to;
splay_tree_key tn;
uintptr_t data;
if ((void *) target == NULL)
{
/* As a special case, allow attaching NULL host pointers. This
allows e.g. unassociated Fortran pointers to be mapped
properly. */
data = 0;
gomp_debug (1,
"%s: attaching NULL host pointer, target %p "
"(struct base %p)\n", __FUNCTION__, (void *) devptr,
(void *) (n->tgt->tgt_start + n->tgt_offset));
gomp_copy_host2dev (devicep, aq, (void *) devptr, (void *) &data,
sizeof (void *), true, cbufp);
return;
}
s.host_start = target + bias;
s.host_end = s.host_start + 1;
tn = splay_tree_lookup (mem_map, &s);
if (!tn)
{
if (allow_zero_length_array_sections)
/* When allowing attachment to zero-length array sections, we
copy the host pointer when the target region is not mapped. */
data = target;
else
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("pointer target not mapped for attach");
}
}
else
data = tn->tgt->tgt_start + tn->tgt_offset + target - tn->host_start;
gomp_debug (1,
"%s: attaching host %p, target %p (struct base %p) to %p\n",
__FUNCTION__, (void *) attach_to, (void *) devptr,
(void *) (n->tgt->tgt_start + n->tgt_offset), (void *) data);
gomp_copy_host2dev (devicep, aq, (void *) devptr, (void *) &data,
sizeof (void *), true, cbufp);
}
else
gomp_debug (1, "%s: attach count for %p -> %u\n", __FUNCTION__,
(void *) attach_to, (int) n->aux->attach_count[idx]);
}
attribute_hidden void
gomp_detach_pointer (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq, splay_tree_key n,
uintptr_t detach_from, bool finalize,
struct gomp_coalesce_buf *cbufp)
{
size_t idx;
if (n == NULL)
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("enclosing struct not mapped for detach");
}
idx = (detach_from - n->host_start) / sizeof (void *);
if (!n->aux || !n->aux->attach_count)
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("no attachment counters for struct");
}
if (finalize)
n->aux->attach_count[idx] = 1;
if (n->aux->attach_count[idx] == 0)
{
gomp_mutex_unlock (&devicep->lock);
gomp_fatal ("attach count underflow");
}
else
n->aux->attach_count[idx]--;
if (n->aux->attach_count[idx] == 0)
{
uintptr_t devptr = n->tgt->tgt_start + n->tgt_offset + detach_from
- n->host_start;
uintptr_t target = (uintptr_t) *(void **) detach_from;
gomp_debug (1,
"%s: detaching host %p, target %p (struct base %p) to %p\n",
__FUNCTION__, (void *) detach_from, (void *) devptr,
(void *) (n->tgt->tgt_start + n->tgt_offset),
(void *) target);
gomp_copy_host2dev (devicep, aq, (void *) devptr, (void *) &target,
sizeof (void *), true, cbufp);
}
else
gomp_debug (1, "%s: attach count for %p -> %u\n", __FUNCTION__,
(void *) detach_from, (int) n->aux->attach_count[idx]);
}
attribute_hidden uintptr_t
gomp_map_val (struct target_mem_desc *tgt, void **hostaddrs, size_t i)
{
if (tgt->list[i].key != NULL)
return tgt->list[i].key->tgt->tgt_start
+ tgt->list[i].key->tgt_offset
+ tgt->list[i].offset;
switch (tgt->list[i].offset)
{
case OFFSET_INLINED:
return (uintptr_t) hostaddrs[i];
case OFFSET_POINTER:
return 0;
case OFFSET_STRUCT:
return tgt->list[i + 1].key->tgt->tgt_start
+ tgt->list[i + 1].key->tgt_offset
+ tgt->list[i + 1].offset
+ (uintptr_t) hostaddrs[i]
- (uintptr_t) hostaddrs[i + 1];
default:
return tgt->tgt_start + tgt->list[i].offset;
}
}
static inline __attribute__((always_inline)) struct target_mem_desc *
gomp_map_vars_internal (struct gomp_device_descr *devicep,
struct goacc_asyncqueue *aq, size_t mapnum,
void **hostaddrs, void **devaddrs, size_t *sizes,
void *kinds, bool short_mapkind,
htab_t *refcount_set,
enum gomp_map_vars_kind pragma_kind)
{
size_t i, tgt_align, tgt_size, not_found_cnt = 0;
bool has_firstprivate = false;
bool has_always_ptrset = false;
bool openmp_p = (pragma_kind & GOMP_MAP_VARS_OPENACC) == 0;
const int rshift = short_mapkind ? 8 : 3;
const int typemask = short_mapkind ? 0xff : 0x7;
struct splay_tree_s *mem_map = &devicep->mem_map;
struct splay_tree_key_s cur_node;
struct target_mem_desc *tgt
= gomp_malloc (sizeof (*tgt) + sizeof (tgt->list[0]) * mapnum);
tgt->list_count = mapnum;
tgt->refcount = (pragma_kind & GOMP_MAP_VARS_ENTER_DATA) ? 0 : 1;
tgt->device_descr = devicep;
tgt->prev = NULL;
struct gomp_coalesce_buf cbuf, *cbufp = NULL;
if (mapnum == 0)
{