xref: /llvm-project/offload/test/unified_shared_memory/close_manual.c (revision 904b1a850536d273b0e11bd17a7ea642ba3b5bc4)
1 // RUN: %libomptarget-compile-run-and-check-generic
2 
3 // REQUIRES: unified_shared_memory
4 
5 #include <omp.h>
6 #include <stdio.h>
7 
8 // ---------------------------------------------------------------------------
9 // Various definitions copied from OpenMP RTL
10 
11 extern void __tgt_target_data_begin(int64_t device_id, int32_t arg_num,
12                                     void **args_base, void **args,
13                                     int64_t *arg_sizes, int64_t *arg_types);
14 
15 extern void __tgt_target_data_end(int64_t device_id, int32_t arg_num,
16                                   void **args_base, void **args,
17                                   int64_t *arg_sizes, int64_t *arg_types);
18 
19 // End of definitions copied from OpenMP RTL.
20 // ---------------------------------------------------------------------------
21 
22 #pragma omp requires unified_shared_memory
23 
24 #define N 1024
25 
main(int argc,char * argv[])26 int main(int argc, char *argv[]) {
27   int fails;
28   void *host_alloc = 0, *device_alloc = 0;
29   int *a = (int *)malloc(N * sizeof(int));
30 
31   // Init
32   for (int i = 0; i < N; ++i) {
33     a[i] = 10;
34   }
35   host_alloc = &a[0];
36 
37 // Dummy target region that ensures the runtime library is loaded when
38 // the target data begin/end functions are manually called below.
39 #pragma omp target
40   {}
41 
42   // Manual calls
43   int device_id = omp_get_default_device();
44   int arg_num = 1;
45   void **args_base = (void **)&a;
46   void **args = (void **)&a;
47   int64_t arg_sizes[arg_num];
48 
49   arg_sizes[0] = sizeof(int) * N;
50 
51   int64_t arg_types[arg_num];
52 
53   // Ox400 enables the CLOSE map type in the runtime:
54   // OMP_TGT_MAPTYPE_CLOSE = 0x400
55   // OMP_TGT_MAPTYPE_TO    = 0x001
56   arg_types[0] = 0x400 | 0x001;
57 
58   device_alloc = host_alloc;
59 
60   __tgt_target_data_begin(device_id, arg_num, args_base, args, arg_sizes,
61                           arg_types);
62 
63 #pragma omp target data use_device_ptr(a)
64   { device_alloc = a; }
65 
66   __tgt_target_data_end(device_id, arg_num, args_base, args, arg_sizes,
67                         arg_types);
68 
69   // CHECK: a was copied to the device
70   if (device_alloc != host_alloc)
71     printf("a was copied to the device\n");
72 
73   free(a);
74 
75   // CHECK: Done!
76   printf("Done!\n");
77 
78   return 0;
79 }
80