| 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 | #include <assert.h> |
| 7 | #include <omp.h> |
| 8 | #include <stdio.h> |
| 9 | |
| 10 | #pragma omp requires unified_shared_memory |
| 11 | |
| 12 | int main(int argc, char *argv[]) { |
| 13 | int dev = omp_get_default_device(); |
| 14 | int x = 10; |
| 15 | int *x_dev = (int *)omp_target_alloc(sizeof x, dev); |
| 16 | assert(x_dev && "expected omp_target_alloc to succeed" ); |
| 17 | int rc = omp_target_associate_ptr(&x, x_dev, sizeof x, 0, dev); |
| 18 | assert(!rc && "expected omp_target_associate_ptr to succeed" ); |
| 19 | |
| 20 | // To determine whether x needs to be transferred, the runtime cannot simply |
| 21 | // check whether unified shared memory is enabled and the 'close' modifier is |
| 22 | // specified. It must check whether x was previously placed in device memory |
| 23 | // by, for example, omp_target_associate_ptr. |
| 24 | #pragma omp target map(always, tofrom : x) |
| 25 | x += 1; |
| 26 | |
| 27 | // CHECK: x=11 |
| 28 | printf(format: "x=%d\n" , x); |
| 29 | // CHECK: present: 1 |
| 30 | printf(format: "present: %d\n" , omp_target_is_present(&x, dev)); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |