Skip to content

Commit dd15747

Browse files
committed
[OpenMP] Rework implementation to be conform to OpenMP 6.0
Removes restriction on Ptr to host pointers. Provides accessibility check for AMDGPU through HSA + default implementation returning false.
1 parent 712bdd1 commit dd15747

File tree

6 files changed

+35
-36
lines changed

6 files changed

+35
-36
lines changed

offload/include/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ struct DeviceTy {
158158
/// Ask the device whether the runtime should use auto zero-copy.
159159
bool useAutoZeroCopy();
160160

161-
/// Ask the device whether it supports unified memory.
162-
bool supportsUnifiedMemory();
161+
/// Ask the device whether the storage is accessible.
162+
bool isAccessiblePtr(const void *Ptr, size_t Size);
163163

164164
/// Check if there are pending images for this device.
165165
bool hasPendingImages() const { return HasPendingImages; }

offload/libomptarget/OpenMP/API.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,7 @@ EXTERN int omp_target_is_accessible(const void *Ptr, size_t Size,
225225
if (!DeviceOrErr)
226226
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
227227

228-
// TODO: Add check for openmp version compatability
229-
230-
// for OpenMP 5.1 the routine checks whether a host pointer is accessible from
231-
// the device this requires for the device to support unified shared memory
232-
if (DeviceOrErr->supportsUnifiedMemory()) {
233-
DP("Device %d supports unified memory, returning true\n", DeviceNum);
234-
return true;
235-
}
236-
237-
// TODO: Provide stubs & implementation to check whether a pointer is
238-
// accessible from a given device using hsa_amd_pointer_info for AMDGPU
239-
// implementation for OpenMP 6.x the specification is required to return true
240-
// if the accessibility of the pointer can be determined otherwise it's
241-
// allowed to return false the specification will be clarified from the
242-
// current wording
243-
244-
// functionality to check whether a device pointer is accessible from a device
245-
// (OpenMP 6.0) from the host might not be possible
246-
DP("Device %d does not support unified memory, returning false\n", DeviceNum);
247-
return false;
228+
return DeviceOrErr->isAccessiblePtr(Ptr, Size);
248229
}
249230

250231
EXTERN int omp_target_memcpy(void *Dst, const void *Src, size_t Length,

offload/libomptarget/device.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,6 @@ bool DeviceTy::useAutoZeroCopy() {
368368
return RTL->use_auto_zero_copy(RTLDeviceID);
369369
}
370370

371-
bool DeviceTy::supportsUnifiedMemory() {
372-
return RTL->supports_unified_memory(RTLDeviceID);
373-
}
371+
bool DeviceTy::isAccessiblePtr(const void *Ptr, size_t Size) {
372+
return RTL->is_accessible_ptr(RTLDeviceID, Ptr, Size);
373+
}

offload/plugins-nextgen/amdgpu/src/rtl.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3027,7 +3027,26 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
30273027
return ((IsAPU || OMPX_ApuMaps) && IsXnackEnabled);
30283028
}
30293029

3030-
bool supportsUnifiedMemoryImpl() override { return IsXnackEnabled; }
3030+
bool isAccessiblePtrImpl(const void *Ptr, size_t Size) override {
3031+
hsa_amd_pointer_info_t Info;
3032+
Info.size = sizeof(hsa_amd_pointer_info_t);
3033+
3034+
hsa_agent_t *Agents = nullptr;
3035+
uint32_t Count = 0;
3036+
hsa_status_t Status = hsa_amd_pointer_info(Ptr, &Info, malloc, &Count, &Agents);
3037+
3038+
if (Status != HSA_STATUS_SUCCESS)
3039+
return false;
3040+
3041+
// Checks if the pointer is known by HSA and accessible by the device
3042+
for(uint32_t i = 0; i < Count; i++)
3043+
if(Agents[i].handle == getAgent().handle)
3044+
return Info.sizeInBytes >= Size;
3045+
3046+
// If the pointer is unknown to HSA it's assumed a host pointer
3047+
// in that case the device can access it on unified memory support is enabled
3048+
return IsXnackEnabled;
3049+
}
30313050

30323051
/// Getters and setters for stack and heap sizes.
30333052
Error getDeviceStackSize(uint64_t &Value) override {

offload/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,9 +1093,10 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
10931093
bool useAutoZeroCopy();
10941094
virtual bool useAutoZeroCopyImpl() { return false; }
10951095

1096-
/// Returns true if the device has unified memory capabilities
1097-
bool supportsUnifiedMemory();
1098-
virtual bool supportsUnifiedMemoryImpl() { return false; }
1096+
/// Returns true if the plugin can guarantee that the associated
1097+
/// storage is accessible
1098+
bool isAccessiblePtr(const void *Ptr, size_t Size);
1099+
virtual bool isAccessiblePtrImpl(const void *Ptr, size_t Size) { return false; }
10991100

11001101
virtual Expected<omp_interop_val_t *>
11011102
createInterop(int32_t InteropType, interop_spec_t &InteropSpec) {
@@ -1527,8 +1528,8 @@ struct GenericPluginTy {
15271528
/// Returns if the plugin can support automatic copy.
15281529
int32_t use_auto_zero_copy(int32_t DeviceId);
15291530

1530-
/// Returns if the the device supports unified memory.
1531-
int32_t supports_unified_memory(int32_t DeviceId);
1531+
/// Returns if the associated storage is accessible for a given device.
1532+
int32_t is_accessible_ptr(int32_t DeviceId, const void *Ptr, size_t Size);
15321533

15331534
/// Look up a global symbol in the given binary.
15341535
int32_t get_global(__tgt_device_binary Binary, uint64_t Size,

offload/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,7 @@ Error GenericDeviceTy::syncEvent(void *EventPtr) {
16071607

16081608
bool GenericDeviceTy::useAutoZeroCopy() { return useAutoZeroCopyImpl(); }
16091609

1610-
bool GenericDeviceTy::supportsUnifiedMemory() {
1611-
return supportsUnifiedMemoryImpl();
1612-
}
1610+
bool GenericDeviceTy::isAccessiblePtr(const void *Ptr, size_t Size) { return isAccessiblePtrImpl(Ptr, Size); }
16131611

16141612
Error GenericPluginTy::init() {
16151613
if (Initialized)
@@ -2163,8 +2161,8 @@ int32_t GenericPluginTy::use_auto_zero_copy(int32_t DeviceId) {
21632161
return getDevice(DeviceId).useAutoZeroCopy();
21642162
}
21652163

2166-
int32_t GenericPluginTy::supports_unified_memory(int32_t DeviceId) {
2167-
return getDevice(DeviceId).supportsUnifiedMemory();
2164+
int32_t GenericPluginTy::is_accessible_ptr(int32_t DeviceId, const void *Ptr, size_t Size) {
2165+
return getDevice(DeviceId).isAccessiblePtr(Ptr, Size);
21682166
}
21692167

21702168
int32_t GenericPluginTy::get_global(__tgt_device_binary Binary, uint64_t Size,

0 commit comments

Comments
 (0)