|
| 1 | +// RUN: %libomptarget-compilexx-run-and-check-generic |
| 2 | + |
| 3 | +// XFAIL: * |
| 4 | + |
| 5 | +#include <omp.h> |
| 6 | +#include <stdio.h> |
| 7 | + |
| 8 | +// Test for various cases of use_device_addr on an array-section. |
| 9 | +// The corresponding data is not previously mapped. |
| 10 | + |
| 11 | +// Note that this tests for the current behavior wherein if a lookup fails, |
| 12 | +// the runtime returns nullptr, instead of the original host-address. |
| 13 | +// That was compatible with OpenMP 5.0, where it was a user error if |
| 14 | +// corresponding storage didn't exist, but with 5.1+, the runtime needs to |
| 15 | +// return the host address, as it needs to assume that the host-address is |
| 16 | +// device-accessible, as the user has guaranteed it. |
| 17 | +// Once the runtime returns the original host-address when the lookup fails, the |
| 18 | +// test will need to be updated. |
| 19 | + |
| 20 | +int g, h[10]; |
| 21 | +int *ph = &h[0]; |
| 22 | + |
| 23 | +struct S { |
| 24 | + int *paa[10][10]; |
| 25 | + |
| 26 | + void f1(int i) { |
| 27 | + paa[0][2] = &g; |
| 28 | + |
| 29 | + int *original_ph3 = &ph[3]; |
| 30 | + int **original_paa02 = &paa[0][2]; |
| 31 | + |
| 32 | +// (A) No corresponding map, lookup should fail. |
| 33 | +// CHECK: A: 1 1 1 |
| 34 | +#pragma omp target data use_device_addr(ph[3 : 4]) |
| 35 | + { |
| 36 | + int *mapped_ptr_ph3 = |
| 37 | + (int *)omp_get_mapped_ptr(original_ph3, omp_get_default_device()); |
| 38 | + printf("A: %d %d %d\n", mapped_ptr_ph3 == nullptr, |
| 39 | + mapped_ptr_ph3 != original_ph3, &ph[3] == (int *)nullptr + 3); |
| 40 | + } |
| 41 | + |
| 42 | +// (B) use_device_addr/map: different operands, same base-pointer. |
| 43 | +// use_device_addr operand within mapped address range. |
| 44 | +// CHECK: B: 1 1 1 |
| 45 | +#pragma omp target data map(ph[2 : 3]) use_device_addr(ph[3 : 1]) |
| 46 | + { |
| 47 | + int *mapped_ptr_ph4 = |
| 48 | + (int *)omp_get_mapped_ptr(original_ph3 + 1, omp_get_default_device()); |
| 49 | + printf("B: %d %d %d\n", mapped_ptr_ph4 != nullptr, |
| 50 | + mapped_ptr_ph4 != original_ph3 + 1, &ph[4] == mapped_ptr_ph4); |
| 51 | + } |
| 52 | + |
| 53 | +// (C) use_device_addr/map: different base-pointers. |
| 54 | +// No corresponding storage, lookup should fail. |
| 55 | +// CHECK: C: 1 1 1 |
| 56 | +#pragma omp target data map(ph) use_device_addr(ph[3 : 4]) |
| 57 | + { |
| 58 | + int *mapped_ptr_ph3 = |
| 59 | + (int *)omp_get_mapped_ptr(original_ph3, omp_get_default_device()); |
| 60 | + printf("C: %d %d %d\n", mapped_ptr_ph3 == nullptr, |
| 61 | + mapped_ptr_ph3 != original_ph3, &ph[3] == (int *)nullptr + 3); |
| 62 | + } |
| 63 | + |
| 64 | +// (D) use_device_addr/map: one of two maps with matching base-pointer. |
| 65 | +// use_device_addr operand within mapped address range of second map, |
| 66 | +// lookup should succeed. |
| 67 | +// CHECK: D: 1 1 1 |
| 68 | +#pragma omp target data map(ph) map(ph[2 : 5]) use_device_addr(ph[3 : 4]) |
| 69 | + { |
| 70 | + int *mapped_ptr_ph3 = |
| 71 | + (int *)omp_get_mapped_ptr(original_ph3, omp_get_default_device()); |
| 72 | + printf("D: %d %d %d\n", mapped_ptr_ph3 != nullptr, |
| 73 | + mapped_ptr_ph3 != original_ph3, &ph[3] == mapped_ptr_ph3); |
| 74 | + } |
| 75 | + |
| 76 | +// (E) No corresponding map, lookup should fail |
| 77 | +// CHECK: E: 1 1 1 |
| 78 | +#pragma omp target data use_device_addr(paa[0]) |
| 79 | + { |
| 80 | + int **mapped_ptr_paa02 = |
| 81 | + (int **)omp_get_mapped_ptr(original_paa02, omp_get_default_device()); |
| 82 | + printf("E: %d %d %d\n", mapped_ptr_paa02 == nullptr, |
| 83 | + mapped_ptr_paa02 != original_paa02, |
| 84 | + &paa[0][2] == (int **)nullptr + 2); |
| 85 | + } |
| 86 | + |
| 87 | +// (F) use_device_addr/map: different operands, same base-array. |
| 88 | +// use_device_addr within mapped address range. Lookup should succeed. |
| 89 | +// CHECK: F: 1 1 1 |
| 90 | +#pragma omp target data map(paa) use_device_addr(paa[0]) |
| 91 | + { |
| 92 | + int **mapped_ptr_paa02 = |
| 93 | + (int **)omp_get_mapped_ptr(original_paa02, omp_get_default_device()); |
| 94 | + printf("F: %d %d %d\n", mapped_ptr_paa02 != nullptr, |
| 95 | + mapped_ptr_paa02 != original_paa02, |
| 96 | + &paa[0][2] == mapped_ptr_paa02); |
| 97 | + } |
| 98 | + |
| 99 | +// (G) use_device_addr/map: different operands, same base-array. |
| 100 | +// use_device_addr extends beyond existing mapping. Not spec compliant. |
| 101 | +// But the lookup succeeds because we use the base-address for translation. |
| 102 | +// CHECK: G: 1 1 1 |
| 103 | +#pragma omp target data map(paa[0][4]) use_device_addr(paa[0]) |
| 104 | + { |
| 105 | + int **mapped_ptr_paa04 = (int **)omp_get_mapped_ptr( |
| 106 | + original_paa02 + 2, omp_get_default_device()); |
| 107 | + printf("G: %d %d %d\n", mapped_ptr_paa04 != nullptr, |
| 108 | + mapped_ptr_paa04 != original_paa02 + 2, |
| 109 | + &paa[0][4] == mapped_ptr_paa04); |
| 110 | + } |
| 111 | + |
| 112 | + int *original_paa020 = &paa[0][2][0]; |
| 113 | + int **original_paa0 = (int **)&paa[0]; |
| 114 | + |
| 115 | +// (H) use_device_addr/map: different base-pointers. |
| 116 | +// No corresponding storage for use_device_addr opnd, lookup should fail. |
| 117 | +// CHECK: H: 1 1 1 |
| 118 | +#pragma omp target data map(paa[0][2][0]) use_device_addr(paa[0]) |
| 119 | + { |
| 120 | + int **mapped_ptr_paa020 = |
| 121 | + (int **)omp_get_mapped_ptr(original_paa020, omp_get_default_device()); |
| 122 | + int **mapped_ptr_paa0 = |
| 123 | + (int **)omp_get_mapped_ptr(original_paa0, omp_get_default_device()); |
| 124 | + printf("H: %d %d %d\n", mapped_ptr_paa020 != nullptr, |
| 125 | + mapped_ptr_paa0 == nullptr, &paa[0] == nullptr); |
| 126 | + } |
| 127 | + |
| 128 | +// (I) use_device_addr/map: one map with different, one with same base-ptr. |
| 129 | +// Lookup should succeed. |
| 130 | +// CHECK: I: 1 1 1 |
| 131 | +#pragma omp target data map(paa[0][2][0]) map(paa[0]) use_device_addr(paa[0][2]) |
| 132 | + { |
| 133 | + int **mapped_ptr_paa02 = |
| 134 | + (int **)omp_get_mapped_ptr(original_paa02, omp_get_default_device()); |
| 135 | + printf("I: %d %d %d\n", mapped_ptr_paa02 != nullptr, |
| 136 | + mapped_ptr_paa02 != original_paa02, |
| 137 | + &paa[0][2] == mapped_ptr_paa02); |
| 138 | + } |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +S s1; |
| 143 | +int main() { s1.f1(1); } |
0 commit comments