1 // RUN: %libomptarget-compile-run-and-check-generic
2 
3 // REQUIRES: unified_shared_memory
4 // UNSUPPORTED: clang-6, clang-7, clang-8, clang-9
5 
6 // Fails on amdgpu with error: GPU Memory Error
7 // XFAIL: amdgcn-amd-amdhsa
8 
9 #include <omp.h>
10 #include <stdio.h>
11 
12 #pragma omp requires unified_shared_memory
13 
14 #define N 1024
15 
main(int argc,char * argv[])16 int main(int argc, char *argv[]) {
17   int fails;
18   void *host_alloc = 0, *device_alloc = 0;
19   int *a = (int *)malloc(N * sizeof(int));
20   int dev = omp_get_default_device();
21 
22   // Init
23   for (int i = 0; i < N; ++i) {
24     a[i] = 10;
25   }
26   host_alloc = &a[0];
27 
28   //
29   // map + target no close
30   //
31 #pragma omp target data map(tofrom : a[ : N]) map(tofrom : device_alloc)
32   {
33 #pragma omp target map(tofrom : device_alloc)
34     { device_alloc = &a[0]; }
35   }
36 
37   // CHECK: a used from unified memory.
38   if (device_alloc == host_alloc)
39     printf("a used from unified memory.\n");
40 
41   //
42   // map + target with close
43   //
44   device_alloc = 0;
45 #pragma omp target data map(close, tofrom : a[ : N]) map(tofrom : device_alloc)
46   {
47 #pragma omp target map(tofrom : device_alloc)
48     { device_alloc = &a[0]; }
49   }
50   // CHECK: a copied to device.
51   if (device_alloc != host_alloc)
52     printf("a copied to device.\n");
53 
54   //
55   // map + use_device_ptr no close
56   //
57   device_alloc = 0;
58 #pragma omp target data map(tofrom : a[ : N]) use_device_ptr(a)
59   { device_alloc = &a[0]; }
60 
61   // CHECK: a used from unified memory with use_device_ptr.
62   if (device_alloc == host_alloc)
63     printf("a used from unified memory with use_device_ptr.\n");
64 
65   //
66   // map + use_device_ptr close
67   //
68   device_alloc = 0;
69 #pragma omp target data map(close, tofrom : a[ : N]) use_device_ptr(a)
70   { device_alloc = &a[0]; }
71 
72   // CHECK: a used from device memory with use_device_ptr.
73   if (device_alloc != host_alloc)
74     printf("a used from device memory with use_device_ptr.\n");
75 
76   //
77   // map enter/exit + close
78   //
79   device_alloc = 0;
80 #pragma omp target enter data map(close, to : a[ : N])
81 
82 #pragma omp target map(from : device_alloc)
83   {
84     device_alloc = &a[0];
85     a[0] = 99;
86   }
87 
88   // 'close' is missing, so the runtime must check whether s is actually in
89   // shared memory in order to determine whether to transfer data and delete the
90   // allocation.
91 #pragma omp target exit data map(from : a[ : N])
92 
93   // CHECK: a has been mapped to the device.
94   if (device_alloc != host_alloc)
95     printf("a has been mapped to the device.\n");
96 
97   // CHECK: a[0]=99
98   // CHECK: a is present: 0
99   printf("a[0]=%d\n", a[0]);
100   printf("a is present: %d\n", omp_target_is_present(a, dev));
101 
102   free(a);
103 
104   // CHECK: Done!
105   printf("Done!\n");
106 
107   return 0;
108 }
109