Skip to content

Commit 5f3ac7d

Browse files
committed
[OpenACC] Prototype OpenMP extension: omp_get_accessible_buffer
`omp_get_accessible_buffer` is an original OpenMP extension that will be made use of by future patches to improve various OpenACC routines' handling of shared memory. For now, all testing of `omp_get_accessible_buffer` will be via testing of OpenACC routines. That is, `omp_get_accessible_buffer` routines are not well tested outside of Clacc's OpenACC runtime, so they are not yet recommended for general use in OpenMP code.
1 parent c31584a commit 5f3ac7d

File tree

8 files changed

+130
-0
lines changed

8 files changed

+130
-0
lines changed

openmp/libomptarget/include/omptarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ int omp_target_associate_ptr(void *host_ptr, void *device_ptr, size_t size,
161161
size_t device_offset, int device_num);
162162
int omp_target_disassociate_ptr(void *host_ptr, int device_num);
163163
void *omp_get_mapped_ptr(const void *ptr, int device_num);
164+
size_t omp_get_accessible_buffer(const void *ptr, size_t size, int device_num,
165+
void **buffer_host, void **buffer_device);
164166
void *omp_target_map_to(void *, size_t, int);
165167
void omp_target_map_from(void *, size_t, int);
166168
void omp_target_map_from_delete(void *, size_t, int);

openmp/libomptarget/src/api.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,53 @@ EXTERN void *omp_get_mapped_ptr(const void *ptr, int device_num) {
367367
return TgtPtr;
368368
}
369369

370+
EXTERN size_t omp_get_accessible_buffer(
371+
const void *ptr, size_t size, int device_num, void **buffer_host,
372+
void **buffer_device) {
373+
DP("Call to omp_get_accessible_buffer for address " DPxMOD ", size %zu, and "
374+
"device %d\n",
375+
DPxPTR(ptr), size, device_num);
376+
377+
if (device_num == omp_get_initial_device()) {
378+
DP("Call to omp_get_accessible_buffer for initial device, returning "
379+
"SIZE_MAX\n");
380+
if (buffer_host)
381+
*buffer_host = nullptr;
382+
if (buffer_device)
383+
*buffer_device = nullptr;
384+
return SIZE_MAX;
385+
}
386+
387+
RTLsMtx->lock();
388+
size_t Devices_size = Devices.size();
389+
RTLsMtx->unlock();
390+
if (Devices_size <= (size_t)device_num) {
391+
DP("Call to omp_get_accessible_buffer with invalid device ID, returning "
392+
"0\n");
393+
if (buffer_host)
394+
*buffer_host = nullptr;
395+
if (buffer_device)
396+
*buffer_device = nullptr;
397+
return 0;
398+
}
399+
400+
if (!ptr) {
401+
DP("Call to omp_get_accessible_buffer with NULL ptr, returning SIZE_MAX\n");
402+
if (buffer_host)
403+
*buffer_host = nullptr;
404+
if (buffer_device)
405+
*buffer_device = nullptr;
406+
return SIZE_MAX;
407+
}
408+
409+
DeviceTy &Device = Devices[device_num];
410+
size_t BufferSize = Device.getAccessibleBuffer(const_cast<void *>(ptr),
411+
/*Size=*/size, buffer_host,
412+
buffer_device);
413+
DP("Call to omp_get_accessible_buffer returns %zu\n", BufferSize);
414+
return BufferSize;;
415+
}
416+
370417
EXTERN int omp_target_memcpy(void *dst, void *src, size_t length,
371418
size_t dst_offset, size_t src_offset, int dst_device, int src_device) {
372419
DP("Call to omp_target_memcpy, dst device %d, src device %d, "

openmp/libomptarget/src/device.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,41 @@ LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
164164
return lr;
165165
}
166166

167+
size_t DeviceTy::getAccessibleBuffer(void *Ptr, int64_t Size, void **BufferHost,
168+
void **BufferDevice) {
169+
size_t BufferSize;
170+
DataMapMtx.lock();
171+
LookupResult lr = lookupMapping(Ptr, Size);
172+
if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
173+
auto &HT = *lr.Entry;
174+
BufferSize = HT.HstPtrEnd - HT.HstPtrBegin;
175+
DP("Overlapping mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin="
176+
DPxMOD ", " "Size=%" PRId64 ", RefCount=%s\n",
177+
DPxPTR(HT.HstPtrBegin), DPxPTR(HT.TgtPtrBegin), BufferSize,
178+
HT.isRefCountInf() ? "INF" : std::to_string(HT.getRefCount()).c_str());
179+
if (BufferHost)
180+
*BufferHost = (void *)HT.HstPtrBegin;
181+
if (BufferDevice)
182+
*BufferDevice = (void *)HT.TgtPtrBegin;
183+
} else if (RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) {
184+
DP("Unified shared memory\n");
185+
BufferSize = SIZE_MAX;
186+
if (BufferHost)
187+
*BufferHost = nullptr;
188+
if (BufferDevice)
189+
*BufferDevice = nullptr;
190+
} else {
191+
DP("Host memory is inaccessible from device\n");
192+
BufferSize = 0;
193+
if (BufferHost)
194+
*BufferHost = nullptr;
195+
if (BufferDevice)
196+
*BufferDevice = nullptr;
197+
}
198+
DataMapMtx.unlock();
199+
return BufferSize;
200+
}
201+
167202
// Used by targetDataBegin
168203
// Return the target pointer begin (where the data will be moved).
169204
// Allocate memory if this is the first occurrence of this mapping.

openmp/libomptarget/src/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ struct DeviceTy {
218218

219219
uint64_t getMapEntryRefCnt(void *HstPtrBegin);
220220
LookupResult lookupMapping(void *HstPtrBegin, int64_t Size);
221+
size_t getAccessibleBuffer(void *Ptr, int64_t Size, void **BufferHost,
222+
void **BufferDevice);
221223
void *getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase, int64_t Size,
222224
bool &IsNew, bool &IsHostPtr, bool IsImplicit,
223225
bool UpdateRefCount, bool HasCloseModifier,

openmp/libomptarget/src/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ VERS1.0 {
3737
omp_target_associate_ptr;
3838
omp_target_disassociate_ptr;
3939
omp_get_mapped_ptr;
40+
omp_get_accessible_buffer;
4041
omp_target_map_to;
4142
omp_target_map_from;
4243
omp_target_map_from_delete;

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,39 @@
174174
extern omp_present_t __KAI_KMPC_CONVENTION
175175
omp_target_range_is_present(void *, size_t, int);
176176

177+
// Find any mapped or unified shared memory buffer that overlaps with the
178+
// specified host memory range.
179+
//
180+
// ptr and size specify a host memory range to look up. size=0 has the
181+
// same effect as size=1: it looks for a single mapped address instead of a
182+
// larger range.
183+
//
184+
// Skip assignments to *buffer_host or *buffer_device described below if
185+
// buffer_host or buffer_device is NULL, respectively.
186+
//
187+
// Evaluate the results in the following order:
188+
// - If device_num is invalid, set *buffer_host and *buffer_device to NULL,
189+
// and return 0.
190+
// - If ptr == NULL (regardless of size), or if the specified host memory
191+
// range is accessible from the specified device but not via any mapped
192+
// buffer (because of unified shared memory or because device_num is the
193+
// value returned by omp_get_initial_device()), set *buffer_host and
194+
// *buffer_device to NULL, and return SIZE_MAX. FIXME: This handling of
195+
// ptr = NULL is inconsistent with the current implementation of
196+
// omp_target_is_accessible in LLVM, but OpenMP 5.1 is unclear about the
197+
// right behavior. Make them consistent once we know which is right.
198+
// - If no byte of the specified host memory range is accessible from the
199+
// specified device, set *buffer_host and *buffer_device to NULL, and
200+
// return 0.
201+
// - Set *buffer_host to the start of the earliest (that is, smallest
202+
// starting address) host memory buffer that contains at least one
203+
// byte within the specified host memory range and that is mapped to the
204+
// specified device, set *buffer_device to the start of the mapped device
205+
// buffer, and return the size of that buffer.
206+
extern size_t omp_get_accessible_buffer(
207+
const void *ptr, size_t size, int device_num, void **buffer_host,
208+
void **buffer_device);
209+
177210
// Same as an OpenMP "target enter data" construct with:
178211
// - A device clause for the specified device number.
179212
// - A map clause with:

openmp/runtime/src/kmp_ftn_entry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,12 @@ int FTN_STDCALL FTN_TARGET_DISASSOCIATE_PTR(void *host_ptr, int device_num) {
10291029
void *FTN_STDCALL FTN_GET_MAPPED_PTR(const void *ptr, int device_num) {
10301030
return NULL;
10311031
}
1032+
1033+
size_t FTN_STDCALL FTN_GET_ACCESSIBLE_BUFFER(const void *ptr, size_t size,
1034+
int device_num, void **buffer_host,
1035+
void **buffer_device) {
1036+
return 0;
1037+
}
10321038
#endif // defined(KMP_STUB)
10331039

10341040
#ifdef KMP_STUB

openmp/runtime/src/kmp_ftn_os.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
#define FTN_TARGET_ASSOCIATE_PTR omp_target_associate_ptr
122122
#define FTN_TARGET_DISASSOCIATE_PTR omp_target_disassociate_ptr
123123
#define FTN_GET_MAPPED_PTR omp_get_mapped_ptr
124+
#define FTN_GET_ACCESSIBLE_BUFFER omp_get_accessible_buffer
124125
#endif
125126

126127
#define FTN_CONTROL_TOOL omp_control_tool
@@ -246,6 +247,7 @@
246247
#define FTN_TARGET_ASSOCIATE_PTR omp_target_associate_ptr_
247248
#define FTN_TARGET_DISASSOCIATE_PTR omp_target_disassociate_ptr_
248249
#define FTN_GET_MAPPED_PTR omp_get_mapped_ptr_
250+
#define FTN_GET_ACCESSIBLE_BUFFER omp_get_accessible_buffer_
249251
#endif
250252

251253
#define FTN_CONTROL_TOOL omp_control_tool_
@@ -373,6 +375,7 @@
373375
#define FTN_TARGET_ASSOCIATE_PTR OMP_TARGET_ASSOCIATE_PTR
374376
#define FTN_TARGET_DISASSOCIATE_PTR OMP_TARGET_DISASSOCIATE_PTR
375377
#define FTN_GET_MAPPED_PTR OMP_GET_MAPPED_PTR
378+
#define FTN_GET_ACCESSIBLE_BUFFER OMP_GET_ACCESSIBLE_BUFFER
376379
#endif
377380

378381
#define FTN_CONTROL_TOOL OMP_CONTROL_TOOL
@@ -498,6 +501,7 @@
498501
#define FTN_TARGET_ASSOCIATE_PTR OMP_TARGET_ASSOCIATE_PTR_
499502
#define FTN_TARGET_DISASSOCIATE_PTR OMP_TARGET_DISASSOCIATE_PTR_
500503
#define FTN_GET_MAPPED_PTR OMP_GET_MAPPED_PTR_
504+
#define FTN_GET_ACCESSIBLE_BUFFER OMP_GET_ACCESSIBLE_BUFFER_
501505
#endif
502506

503507
#define FTN_CONTROL_TOOL OMP_CONTROL_TOOL_

0 commit comments

Comments
 (0)