Skip to content

Commit 198fde1

Browse files
committed
[OpenACC] Prototype OpenMP 5.1's omp_target_is_accessible
The OpenMP 5.1 `omp_target_is_accessible` function will be used in a future patch to support OpenACC's `acc_is_present` function, which currently uses Clacc's OpenMP extension `omp_target_range_is_present` instead, but it's better to depend on standard OpenMP functions where possible. For now, all testing of `omp_target_is_accessible` will be via testing for `acc_is_present`. That is, `omp_target_is_accessible` is not well tested outside of Clacc's OpenACC runtime, so it is not yet recommended for general use in OpenMP code.
1 parent 0952621 commit 198fde1

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

openmp/libomptarget/include/omptarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ int omp_get_num_devices(void);
141141
int omp_get_initial_device(void);
142142
void *omp_target_alloc(size_t size, int device_num);
143143
void omp_target_free(void *device_ptr, int device_num);
144+
// FIXME: OpenMP 5.0 and 5.1 say const void *ptr.
144145
int omp_target_is_present(void *ptr, int device_num);
146+
int omp_target_is_accessible(const void *ptr, size_t size, int device_num);
145147
typedef enum omp_present_t {
146148
omp_present_none = 0,
147149
omp_present_partial = 1,

openmp/libomptarget/src/api.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,67 @@ EXTERN int omp_target_is_present(void *ptr, int device_num) {
216216
return rc;
217217
}
218218

219+
EXTERN int omp_target_is_accessible(const void *ptr, size_t size,
220+
int device_num) {
221+
// OpenMP 5.1, sec. 3.8.4 "omp_target_is_accessible", p. 417, L21-22:
222+
// "This routine returns true if the storage of size bytes starting at the
223+
// address given by ptr is accessible from device device_num. Otherwise, it
224+
// returns false."
225+
//
226+
// The meaning of "accessible" for unified shared memory is established in
227+
// OpenMP 5.1, sec. 2.5.1 "requires directive". More generally, the specified
228+
// host memory is accessible if it can be accessed from the device either
229+
// directly (because of unified shared memory or because device_num is the
230+
// value returned by omp_get_initial_device()) or indirectly (because it's
231+
// mapped to the device).
232+
DP("Call to omp_target_is_accessible for device %d and address " DPxMOD "\n",
233+
device_num, DPxPTR(ptr));
234+
235+
// FIXME: Is this right?
236+
//
237+
// Null pointer is permitted:
238+
//
239+
// OpenMP 5.1, sec. 3.8.4 "omp_target_is_accessible", p. 417, L15:
240+
// "The value of ptr must be a valid host pointer or NULL (or C_NULL_PTR, for
241+
// Fortran)."
242+
//
243+
// However, I found no specification of behavior in this case.
244+
// omp_target_is_present has the same problem and is implemented the same way.
245+
// Should size have any effect on the result when ptr is NULL?
246+
if (!ptr) {
247+
DP("Call to omp_target_is_accessible with NULL ptr, returning false\n");
248+
return false;
249+
}
250+
251+
if (device_num == omp_get_initial_device()) {
252+
DP("Call to omp_target_is_accessible on host, returning true\n");
253+
return true;
254+
}
255+
256+
RTLsMtx->lock();
257+
size_t Devices_size = Devices.size();
258+
RTLsMtx->unlock();
259+
if (Devices_size <= (size_t)device_num) {
260+
DP("Call to omp_target_is_accessible with invalid device ID, returning "
261+
"false\n");
262+
return false;
263+
}
264+
265+
DeviceTy &Device = Devices[device_num];
266+
bool IsLast; // not used
267+
bool IsHostPtr; // not used
268+
// TODO: How does the spec intend for the size=0 case to be handled?
269+
// Currently, we return true if we would return true for size=1 (ptr is within
270+
// a range that's accessible). Does the spec clarify this somewhere?
271+
void *TgtPtr = Device.getTgtPtrBegin(const_cast<void *>(ptr), size, IsLast,
272+
/*UpdateRefCount=*/false,
273+
/*UseHoldRefCount=*/false, IsHostPtr,
274+
/*MustContain=*/true);
275+
int rc = (TgtPtr != NULL);
276+
DP("Call to omp_target_is_accessible returns %d\n", rc);
277+
return rc;
278+
}
279+
219280
EXTERN omp_present_t omp_target_range_is_present(void *ptr, size_t size,
220281
int device_num) {
221282
DP("Call to omp_target_range_is_present for device %d and address " DPxMOD

openmp/libomptarget/src/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ VERS1.0 {
3030
omp_target_alloc;
3131
omp_target_free;
3232
omp_target_is_present;
33+
omp_target_is_accessible;
3334
omp_target_range_is_present;
3435
omp_target_memcpy;
3536
omp_target_memcpy_rect;

openmp/runtime/src/include/omp.h.var

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
extern void* __KAI_KMPC_CONVENTION omp_target_alloc(size_t, int);
143143
extern void __KAI_KMPC_CONVENTION omp_target_free(void *, int);
144144
extern int __KAI_KMPC_CONVENTION omp_target_is_present(void *, int);
145+
extern int __KAI_KMPC_CONVENTION omp_target_is_accessible(const void *, size_t, int);
145146
extern int __KAI_KMPC_CONVENTION omp_target_memcpy(void *, void *, size_t, size_t, size_t, int, int);
146147
extern int __KAI_KMPC_CONVENTION omp_target_memcpy_rect(void *, void *, size_t, int, const size_t *,
147148
const size_t *, const size_t *, const size_t *, const size_t *, int, int);

openmp/runtime/src/kmp_ftn_entry.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,11 @@ void FTN_STDCALL FTN_TARGET_FREE(void *device_ptr, int device_num) {}
986986

987987
int FTN_STDCALL FTN_TARGET_IS_PRESENT(void *ptr, int device_num) { return 0; }
988988

989+
int FTN_STDCALL FTN_TARGET_IS_ACCESSIBLE(const void *ptr, size_t size,
990+
int device_num) {
991+
return 0;
992+
}
993+
989994
typedef enum omp_present_t {
990995
omp_present_none = 0,
991996
omp_present_partial = 1,

openmp/runtime/src/kmp_ftn_os.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
#define FTN_TARGET_ALLOC omp_target_alloc
115115
#define FTN_TARGET_FREE omp_target_free
116116
#define FTN_TARGET_IS_PRESENT omp_target_is_present
117+
#define FTN_TARGET_IS_ACCESSIBLE omp_target_is_accessible
117118
#define FTN_TARGET_RANGE_IS_PRESENT omp_target_range__is_present
118119
#define FTN_TARGET_MEMCPY omp_target_memcpy
119120
#define FTN_TARGET_MEMCPY_RECT omp_target_memcpy_rect
@@ -238,6 +239,7 @@
238239
#define FTN_TARGET_ALLOC omp_target_alloc_
239240
#define FTN_TARGET_FREE omp_target_free_
240241
#define FTN_TARGET_IS_PRESENT omp_target_is_present_
242+
#define FTN_TARGET_IS_ACCESSIBLE omp_target_is_accessible_
241243
#define FTN_TARGET_RANGE_IS_PRESENT omp_target_range_is_present_
242244
#define FTN_TARGET_MEMCPY omp_target_memcpy_
243245
#define FTN_TARGET_MEMCPY_RECT omp_target_memcpy_rect_
@@ -364,6 +366,7 @@
364366
#define FTN_TARGET_ALLOC OMP_TARGET_ALLOC
365367
#define FTN_TARGET_FREE OMP_TARGET_FREE
366368
#define FTN_TARGET_IS_PRESENT OMP_TARGET_IS_PRESENT
369+
#define FTN_TARGET_IS_ACCESSIBLE OMP_TARGET_IS_ACCESSIBLE
367370
#define FTN_TARGET_RANGE_IS_PRESENT OMP_TARGET_RANGE_IS_PRESENT
368371
#define FTN_TARGET_MEMCPY OMP_TARGET_MEMCPY
369372
#define FTN_TARGET_MEMCPY_RECT OMP_TARGET_MEMCPY_RECT
@@ -488,6 +491,7 @@
488491
#define FTN_TARGET_ALLOC OMP_TARGET_ALLOC_
489492
#define FTN_TARGET_FREE OMP_TARGET_FREE_
490493
#define FTN_TARGET_IS_PRESENT OMP_TARGET_IS_PRESENT_
494+
#define FTN_TARGET_IS_ACCESSIBLE OMP_TARGET_IS_ACCESSIBLE
491495
#define FTN_TARGET_RANGE_IS_PRESENT OMP_TARGET_RANGE_IS_PRESENT_
492496
#define FTN_TARGET_MEMCPY OMP_TARGET_MEMCPY_
493497
#define FTN_TARGET_MEMCPY_RECT OMP_TARGET_MEMCPY_RECT_

0 commit comments

Comments
 (0)