Skip to content

Commit dec7c17

Browse files
committed
[OpenACC] Prototype OpenMP extension: omp_get_mapped_hostptr
`omp_get_mapped_hostptr` is an original OpenMP extension for the sake of supporting OpenACC's `acc_hostptr`, which will be implemented in a following commit. For now, all testing of `omp_get_mapped_hostptr` will be via testing for `acc_hostptr`. That is, `omp_get_mapped_hostptr` is not well tested outside of Clacc's OpenACC runtime, so it is not yet recommended for general use in OpenMP code.
1 parent ff885c9 commit dec7c17

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

openmp/libomptarget/include/omptarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ int omp_target_associate_ptr(void *host_ptr, void *device_ptr, size_t size,
154154
size_t device_offset, int device_num);
155155
int omp_target_disassociate_ptr(void *host_ptr, int device_num);
156156
void *omp_get_mapped_ptr(const void *ptr, int device_num);
157+
void *omp_get_mapped_hostptr(const void *ptr, int device_num);
157158
size_t omp_get_accessible_buffer(const void *ptr, size_t size, int device_num,
158159
void **buffer_host, void **buffer_device);
159160
void *omp_target_map_to(void *ptr, size_t size, int device_num);

openmp/libomptarget/src/api.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ EXTERN void *omp_get_mapped_ptr(const void *ptr, int device_num) {
290290
if (device_num == omp_get_initial_device()) {
291291
DP("Call to omp_get_mapped_ptr on host, returning host pointer\n");
292292
// OpenMP 5.1, sec. 3.8.11 "omp_get_mapped_ptr", p. 431, L10-12:
293-
// Otherwise it returns the device pointer, which is ptr if device_num is
294-
// the value returned by omp_get_initial_device().
293+
// "Otherwise it returns the device pointer, which is ptr if device_num is
294+
// the value returned by omp_get_initial_device()."
295295
//
296296
// That is, the spec actually requires us to cast away const.
297297
return const_cast<void *>(ptr);
@@ -316,7 +316,7 @@ EXTERN void *omp_get_mapped_ptr(const void *ptr, int device_num) {
316316
// TODO: This seems to be implied by the named "mapped" instead of
317317
// "accessible". Or should we return the host pointer? That is, is this
318318
// supposed to be like omp_target_is_present or omp_target_is_accessible?
319-
// OpenMP 5.1 doesn't seem clear.
319+
// OpenMP 5.1 doesn't seem clear. Keep omp_get_mapped_hostptr in sync.
320320
if (IsHostPtr) {
321321
DP("Call to omp_get_mapped_ptr for unified shared memory, returning "
322322
"NULL\n");
@@ -326,6 +326,41 @@ EXTERN void *omp_get_mapped_ptr(const void *ptr, int device_num) {
326326
return TgtPtr;
327327
}
328328

329+
EXTERN void *omp_get_mapped_hostptr(const void *ptr, int device_num) {
330+
DP("Call to omp_get_mapped_hostptr for device %d and address " DPxMOD "\n",
331+
device_num, DPxPTR(ptr));
332+
333+
if (!ptr) {
334+
DP("Call to omp_get_mapped_hostptr with NULL ptr, returning NULL\n");
335+
return NULL;
336+
}
337+
338+
if (device_num == omp_get_initial_device()) {
339+
DP("Call to omp_get_mapped_hostptr for host, returning device pointer\n");
340+
// For consistency with OpenMP 5.1, sec. 3.8.11 "omp_get_mapped_ptr", p.
341+
// 431, L10-12:
342+
// "Otherwise it returns the device pointer, which is ptr if device_num is
343+
// the value returned by omp_get_initial_device()."
344+
return const_cast<void *>(ptr);
345+
}
346+
347+
RTLsMtx->lock();
348+
size_t Devices_size = Devices.size();
349+
RTLsMtx->unlock();
350+
if (Devices_size <= (size_t)device_num) {
351+
DP("Call to omp_get_mapped_hostptr with invalid device ID, returning "
352+
"NULL\n");
353+
return NULL;
354+
}
355+
356+
DeviceTy &Device = Devices[device_num];
357+
// TODO: This returns nullptr in the case of unified shared memory. This is
358+
// for consistency with the current omp_get_mapped_ptr implementation.
359+
void *HostPtr = Device.lookupHostPtr(const_cast<void *>(ptr));
360+
DP("Call to omp_get_mapped_hostptr returns " DPxMOD "\n", DPxPTR(HostPtr));
361+
return HostPtr;
362+
}
363+
329364
EXTERN size_t omp_get_accessible_buffer(
330365
const void *ptr, size_t size, int device_num, void **buffer_host,
331366
void **buffer_device) {

openmp/libomptarget/src/device.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "private.h"
1515
#include "rtl.h"
1616

17+
#include <algorithm>
1718
#include <cassert>
1819
#include <climits>
1920
#include <string>
@@ -164,6 +165,33 @@ LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
164165
return lr;
165166
}
166167

168+
void *DeviceTy::lookupHostPtr(void *TgtPtr) {
169+
uintptr_t Tp = (uintptr_t)TgtPtr;
170+
DP("Looking up mapping for TgtPtr=" DPxMOD "...\n", DPxPTR(Tp));
171+
DataMapMtx.lock();
172+
173+
HostDataToTargetListTy::iterator Itr = std::find_if(
174+
HostDataToTargetMap.begin(), HostDataToTargetMap.end(),
175+
[Tp](const HostDataToTargetTy &Entry) {
176+
uintptr_t Size = Entry.HstPtrEnd - Entry.HstPtrBegin;
177+
return Entry.TgtPtrBegin <= Tp && Tp < Entry.TgtPtrBegin + Size;
178+
});
179+
180+
void *HostPtr;
181+
if (Itr == HostDataToTargetMap.end()) {
182+
DP("Mapping does not exist\n");
183+
HostPtr = NULL;
184+
} else {
185+
uintptr_t Offset = Tp - Itr->TgtPtrBegin;
186+
uintptr_t Hp = Itr->HstPtrBegin + Offset;
187+
DP("Mapping exists with HstPtr=" DPxMOD "\n", DPxPTR(Hp));
188+
HostPtr = (void *)Hp;
189+
}
190+
191+
DataMapMtx.unlock();
192+
return HostPtr;
193+
}
194+
167195
size_t DeviceTy::getAccessibleBuffer(void *Ptr, int64_t Size, void **BufferHost,
168196
void **BufferDevice) {
169197
size_t BufferSize;

openmp/libomptarget/src/device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ struct DeviceTy {
218218

219219
uint64_t getMapEntryRefCnt(void *HstPtrBegin);
220220
LookupResult lookupMapping(void *HstPtrBegin, int64_t Size);
221+
void *lookupHostPtr(void *TgtPtr);
221222
size_t getAccessibleBuffer(void *Ptr, int64_t Size, void **BufferHost,
222223
void **BufferDevice);
223224
void *getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase, int64_t Size,

openmp/libomptarget/src/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ VERS1.0 {
3636
omp_target_associate_ptr;
3737
omp_target_disassociate_ptr;
3838
omp_get_mapped_ptr;
39+
omp_get_mapped_hostptr;
3940
omp_get_accessible_buffer;
4041
omp_target_map_to;
4142
omp_target_map_from;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@
217217
extern void __KAI_KMPC_CONVENTION omp_target_map_release(void *ptr, size_t size, int device_num);
218218
extern void __KAI_KMPC_CONVENTION omp_target_map_delete(void *ptr, size_t size, int device_num);
219219

220+
// The reverse of omp_get_mapped_ptr. The implementation is inefficient, so
221+
// it shouldn't be used in production code, but it might be useful for
222+
// debugging.
223+
extern void *__KAI_KMPC_CONVENTION omp_get_mapped_hostptr(const void *ptr, int device_num);
224+
220225
/* kmp API functions */
221226
extern int __KAI_KMPC_CONVENTION kmp_get_stacksize (void);
222227
extern void __KAI_KMPC_CONVENTION kmp_set_stacksize (int);

openmp/runtime/src/kmp_ftn_entry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,10 @@ void *FTN_STDCALL FTN_GET_MAPPED_PTR(const void *ptr, int device_num) {
10191019
return NULL;
10201020
}
10211021

1022+
void *FTN_STDCALL FTN_GET_MAPPED_HOSTPTR(const void *ptr, int device_num) {
1023+
return NULL;
1024+
}
1025+
10221026
size_t FTN_STDCALL FTN_GET_ACCESSIBLE_BUFFER(const void *ptr, size_t size,
10231027
int device_num, void **buffer_host,
10241028
void **buffer_device) {

openmp/runtime/src/kmp_ftn_os.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#define FTN_TARGET_ASSOCIATE_PTR omp_target_associate_ptr
121121
#define FTN_TARGET_DISASSOCIATE_PTR omp_target_disassociate_ptr
122122
#define FTN_GET_MAPPED_PTR omp_get_mapped_ptr
123+
#define FTN_GET_MAPPED_HOSTPTR omp_get_mapped_hostptr
123124
#define FTN_GET_ACCESSIBLE_BUFFER omp_get_accessible_buffer
124125
#endif
125126

@@ -245,6 +246,7 @@
245246
#define FTN_TARGET_ASSOCIATE_PTR omp_target_associate_ptr_
246247
#define FTN_TARGET_DISASSOCIATE_PTR omp_target_disassociate_ptr_
247248
#define FTN_GET_MAPPED_PTR omp_get_mapped_ptr_
249+
#define FTN_GET_MAPPED_HOSTPTR omp_get_mapped_hostptr_
248250
#define FTN_GET_ACCESSIBLE_BUFFER omp_get_accessible_buffer_
249251
#endif
250252

@@ -372,6 +374,7 @@
372374
#define FTN_TARGET_ASSOCIATE_PTR OMP_TARGET_ASSOCIATE_PTR
373375
#define FTN_TARGET_DISASSOCIATE_PTR OMP_TARGET_DISASSOCIATE_PTR
374376
#define FTN_GET_MAPPED_PTR OMP_GET_MAPPED_PTR
377+
#define FTN_GET_MAPPED_HOSTPTR OMP_GET_MAPPED_HOSTPTR
375378
#define FTN_GET_ACCESSIBLE_BUFFER OMP_GET_ACCESSIBLE_BUFFER
376379
#endif
377380

@@ -497,6 +500,7 @@
497500
#define FTN_TARGET_ASSOCIATE_PTR OMP_TARGET_ASSOCIATE_PTR_
498501
#define FTN_TARGET_DISASSOCIATE_PTR OMP_TARGET_DISASSOCIATE_PTR_
499502
#define FTN_GET_MAPPED_PTR OMP_GET_MAPPED_PTR_
503+
#define FTN_GET_MAPPED_HOSTPTR OMP_GET_MAPPED_HOSTPTR_
500504
#define FTN_GET_ACCESSIBLE_BUFFER OMP_GET_ACCESSIBLE_BUFFER_
501505
#endif
502506

0 commit comments

Comments
 (0)