Debian Codegraph @

libgomp/testsuite/libgomp.c-c++-common/target-implicit-map-3.c

raw
1 /* PR middle-end/110270 */
2
3 /* Ensure that defaultmap(default : pointer) uses correct OpenMP 5.2
4 semantic, i.e. keeping the pointer value even if not mapped;
5 before OpenMP 5.0/5.1 required that it is NULL, causing issues
6 especially with unified-shared memory but also the code below
7 shows why that's not a good idea. */
8
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <omp.h>
12
13 /* 'unified_address' is required by the OpenMP spec as only then
14 'is_device_ptr' can be left out. All our devices support this
15 while remote offloading would not. However, in practice it is
16 sufficient that the host and device pointer size is the same
17 (or the device pointer is smaller) - and then a device pointer is
18 representable and omp_target_alloc can return a bare device pointer.
19
20 We here assume that this weaker condition holds and do not
21 require: #pragma omp requires unified_address */
22
23 void
24 test_device (int dev)
25 {
26 int *p1 = (int*) 0x12345;
27 int *p1a = (int*) 0x67890;
28 int *p2 = (int*) omp_target_alloc (sizeof (int) * 5, dev);
29 int *p2a = (int*) omp_target_alloc (sizeof (int) * 10, dev);
30 intptr_t ip = (intptr_t) p2;
31 intptr_t ipa = (intptr_t) p2a;
32
33 int A[3] = {1,2,3};
34 int B[5] = {4,5,6,7,8};
35 int *p3 = &A[0];
36 int *p3a = &B[0];
37
38 #pragma omp target enter data map(to:A) device(dev)
39
40 #pragma omp target device(dev) /* defaultmap(default:pointer) */
41 {
42 /* The pointees aren't mapped. */
43 /* OpenMP 5.2 -> same value as before the target region. */
44 if ((intptr_t) p1 != 0x12345) __builtin_abort ();
45 if ((intptr_t) p2 != ip) __builtin_abort ();
46 for (int i = 0; i < 5; i++)
47 p2[i] = 13*i;
48
49 for (int i = 0; i < 10; i++)
50 ((int *)ipa)[i] = 7*i;
51
52 /* OpenMP: Mapped => must point to the corresponding device storage of 'A' */
53 if (p3[0] != 1 || p3[1] != 2 || p3[2] != 3)
54 __builtin_abort ();
55 p3[0] = -11; p3[1] = -22; p3[2] = -33;
56 }
57 #pragma omp target exit data map(from:A) device(dev)
58
59 if (p3[0] != -11 || p3[1] != -22 || p3[2] != -33)
60 __builtin_abort ();
61
62 // With defaultmap:
63
64 #pragma omp target enter data map(to:B) device(dev)
65
66 #pragma omp target device(dev) defaultmap(default:pointer)
67 {
68 /* The pointees aren't mapped. */
69 /* OpenMP 5.2 -> same value as before the target region. */
70 if ((intptr_t) p1a != 0x67890) __builtin_abort ();
71 if ((intptr_t) p2a != ipa) __builtin_abort ();
72
73 for (int i = 0; i < 5; i++)
74 ((int *)ip)[i] = 13*i;
75
76 for (int i = 0; i < 10; i++)
77 p2a[i] = 7*i;
78
79 /* OpenMP: Mapped => must point to the corresponding device storage of 'B' */
80 if (p3a[0] != 4 || p3a[1] != 5 || p3a[2] != 6 || p3a[3] != 7 || p3a[4] != 8)
81 __builtin_abort ();
82 p3a[0] = -44; p3a[1] = -55; p3a[2] = -66; p3a[3] = -77; p3a[4] = -88;
83 }
84 #pragma omp target exit data map(from:B) device(dev)
85
86 if (p3a[0] != -44 || p3a[1] != -55 || p3a[2] != -66 || p3a[3] != -77 || p3a[4] != -88)
87 __builtin_abort ();
88
89 omp_target_free (p2, dev);
90 omp_target_free (p2a, dev);
91 }
92
93 int
94 main()
95 {
96 int ntgts = omp_get_num_devices();
97 if (ntgts)
98 fprintf (stderr, "Offloading devices exist\n"); /* { dg-output "Offloading devices exist(\n|\r\n|\r)" { target offload_device } } */
99 else
100 fprintf (stderr, "Only host fallback\n"); /* { dg-output "Only host fallback(\n|\r\n|\r)" { target { ! offload_device } } } */
101
102 for (int i = 0; i <= ntgts; i++)
103 test_device (i);
104 return 0;
105 }