Skip to content

Commit 7e38b39

Browse files
JackAKirkldrumm
authored andcommitted
Remove deprecated hip APIs.
Mostly these APIs were completely redundant already. HIP amd doesn't have have a context concept. Signed-off-by: JackAKirk <[email protected]>
1 parent 6c2329e commit 7e38b39

16 files changed

+86
-136
lines changed

source/adapters/hip/command_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
882882

883883
try {
884884
std::unique_ptr<ur_event_handle_t_> RetImplEvent{nullptr};
885-
ScopedContext Active(hQueue->getDevice());
885+
ScopedDevice Active(hQueue->getDevice());
886886
uint32_t StreamToken;
887887
ur_stream_guard Guard;
888888
hipStream_t HIPStream = hQueue->getNextComputeStream(

source/adapters/hip/context.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ ur_context_handle_t_::getOwningURPool(umf_memory_pool_t *UMFPool) {
3232
return nullptr;
3333
}
3434

35-
/// Create a UR HIP context.
36-
///
37-
/// By default creates a scoped context and keeps the last active HIP context
38-
/// on top of the HIP context stack.
35+
/// Create a UR context.
3936
///
4037
UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
4138
uint32_t DeviceCount, const ur_device_handle_t *phDevices,
@@ -44,7 +41,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
4441

4542
std::unique_ptr<ur_context_handle_t_> ContextPtr{nullptr};
4643
try {
47-
// Create a scoped context.
44+
// Create a context.
4845
ContextPtr = std::unique_ptr<ur_context_handle_t_>(
4946
new ur_context_handle_t_{phDevices, DeviceCount});
5047
*phContext = ContextPtr.release();
@@ -115,8 +112,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextGetNativeHandle(
115112
ur_context_handle_t hContext, ur_native_handle_t *phNativeContext) {
116113
// FIXME: this entry point has been deprecated in the SYCL RT and should be
117114
// changed to unsupported once the deprecation period has elapsed
118-
*phNativeContext = reinterpret_cast<ur_native_handle_t>(
119-
hContext->getDevices()[0]->getNativeContext());
115+
// The below is extremely dodgy but is the equivalent for what went before
116+
// for continuity: apparently some users may be somehow using this API
117+
// currently, despite it not being well defined. This API should not have been
118+
// implemented in the HIP backend. hipCtx_t is not natively supported by amd
119+
// devices and is meaningless for our purposes; all hipCtx_t APIs were added
120+
// for cuda compatibility only and are deprecated by HIP.
121+
122+
hipCtx_t *Ctx;
123+
UR_CHECK_ERROR(hipCtxGetCurrent(Ctx));
124+
*phNativeContext = reinterpret_cast<ur_native_handle_t>(Ctx);
120125
return UR_RESULT_SUCCESS;
121126
}
122127

source/adapters/hip/context.hpp

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,6 @@
1919

2020
typedef void (*ur_context_extended_deleter_t)(void *UserData);
2121

22-
/// UR context mapping to a HIP context object.
23-
///
24-
/// There is no direct mapping between a HIP context and a UR context.
25-
/// The main differences are described below:
26-
///
27-
/// <b> HIP context vs UR context </b>
28-
///
29-
/// One of the main differences between the UR API and the HIP driver API is
30-
/// that the second modifies the state of the threads by assigning
31-
/// \c hipCtx_t objects to threads. \c hipCtx_t objects store data associated
32-
/// with a given device and control access to said device from the user side.
33-
/// UR API context are objects that are passed to functions, and not bound
34-
/// to threads.
35-
///
36-
/// Since the \c ur_context_handle_t can contain multiple devices, and a \c
37-
/// hipCtx_t refers to only a single device, the \c hipCtx_t is more tightly
38-
/// coupled to a \c ur_device_handle_t than a \c ur_context_handle_t. In order
39-
/// to remove some ambiguities about the different semantics of \c
40-
/// \c ur_context_handle_t and native \c hipCtx_t, we access the native \c
41-
/// hipCtx_t solely through the \c ur_device_handle_t class, by using the object
42-
/// \ref ScopedContext, which sets the active device (by setting the active
43-
/// native \c hipCtx_t).
44-
///
45-
/// <b> Primary vs User-defined \c hipCtx_t </b>
46-
///
47-
/// HIP has two different types of \c hipCtx_t, the Primary context, which is
48-
/// usable by all threads on a given process for a given device, and the
49-
/// aforementioned custom \c hipCtx_t s. The HIP documentation, confirmed with
50-
/// performance analysis, suggest using the Primary context whenever possible.
5122
///
5223
/// <b> Destructor callback </b>
5324
///
@@ -76,24 +47,14 @@ struct ur_context_handle_t_ {
7647
void operator()() { Function(UserData); }
7748
};
7849

79-
using native_type = hipCtx_t;
80-
8150
std::vector<ur_device_handle_t> Devices;
8251

8352
std::atomic_uint32_t RefCount;
8453

8554
ur_context_handle_t_(const ur_device_handle_t *Devs, uint32_t NumDevices)
86-
: Devices{Devs, Devs + NumDevices}, RefCount{1} {
87-
for (auto &Dev : Devices) {
88-
urDeviceRetain(Dev);
89-
}
90-
};
55+
: Devices{Devs, Devs + NumDevices}, RefCount{1} {};
9156

92-
~ur_context_handle_t_() {
93-
for (auto &Dev : Devices) {
94-
urDeviceRelease(Dev);
95-
}
96-
}
57+
~ur_context_handle_t_() {}
9758

9859
void invokeExtendedDeleters() {
9960
std::lock_guard<std::mutex> Guard(Mutex);
@@ -136,28 +97,3 @@ struct ur_context_handle_t_ {
13697
std::vector<deleter_data> ExtendedDeleters;
13798
std::set<ur_usm_pool_handle_t> PoolHandles;
13899
};
139-
140-
namespace {
141-
/// Scoped context is used across all UR HIP plugin implementation to activate
142-
/// the native Context on the current thread. The ScopedContext does not
143-
/// reinstate the previous context as all operations in the hip adapter that
144-
/// require an active context, set the active context and don't rely on context
145-
/// reinstation
146-
class ScopedContext {
147-
public:
148-
ScopedContext(ur_device_handle_t hDevice) {
149-
hipCtx_t Original{};
150-
151-
if (!hDevice) {
152-
throw UR_RESULT_ERROR_INVALID_DEVICE;
153-
}
154-
155-
hipCtx_t Desired = hDevice->getNativeContext();
156-
UR_CHECK_ERROR(hipCtxGetCurrent(&Original));
157-
if (Original != Desired) {
158-
// Sets the desired context as the active one for the thread
159-
UR_CHECK_ERROR(hipCtxSetCurrent(Desired));
160-
}
161-
}
162-
};
163-
} // namespace

source/adapters/hip/device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(ur_device_handle_t hDevice,
10771077
return UR_RESULT_SUCCESS;
10781078

10791079
ur_event_handle_t_::native_type Event;
1080-
ScopedContext Active(hDevice);
1080+
ScopedDevice Active(hDevice);
10811081

10821082
if (pDeviceTimestamp) {
10831083
UR_CHECK_ERROR(hipEventCreateWithFlags(&Event, hipEventDefault));

source/adapters/hip/device.hpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ struct ur_device_handle_t_ {
2424
native_type HIPDevice;
2525
std::atomic_uint32_t RefCount;
2626
ur_platform_handle_t Platform;
27-
hipCtx_t HIPContext;
2827
hipEvent_t EvBase; // HIP event used as base counter
2928
uint32_t DeviceIndex;
3029

@@ -37,11 +36,10 @@ struct ur_device_handle_t_ {
3736
int ConcurrentManagedAccess{0};
3837

3938
public:
40-
ur_device_handle_t_(native_type HipDevice, hipCtx_t Context,
41-
hipEvent_t EvBase, ur_platform_handle_t Platform,
42-
uint32_t DeviceIndex)
43-
: HIPDevice(HipDevice), RefCount{1}, Platform(Platform),
44-
HIPContext(Context), EvBase(EvBase), DeviceIndex(DeviceIndex) {
39+
ur_device_handle_t_(native_type HipDevice, hipEvent_t EvBase,
40+
ur_platform_handle_t Platform, uint32_t DeviceIndex)
41+
: HIPDevice(HipDevice), RefCount{1}, Platform(Platform), EvBase(EvBase),
42+
DeviceIndex(DeviceIndex) {
4543

4644
UR_CHECK_ERROR(hipDeviceGetAttribute(
4745
&MaxWorkGroupSize, hipDeviceAttributeMaxThreadsPerBlock, HIPDevice));
@@ -61,9 +59,7 @@ struct ur_device_handle_t_ {
6159
HIPDevice));
6260
}
6361

64-
~ur_device_handle_t_() noexcept(false) {
65-
UR_CHECK_ERROR(hipDevicePrimaryCtxRelease(HIPDevice));
66-
}
62+
~ur_device_handle_t_() noexcept(false) {}
6763

6864
native_type get() const noexcept { return HIPDevice; };
6965

@@ -73,8 +69,6 @@ struct ur_device_handle_t_ {
7369

7470
uint64_t getElapsedTime(hipEvent_t) const;
7571

76-
hipCtx_t getNativeContext() const noexcept { return HIPContext; };
77-
7872
// Returns the index of the device relative to the other devices in the same
7973
// platform
8074
uint32_t getIndex() const noexcept { return DeviceIndex; };
@@ -97,3 +91,20 @@ struct ur_device_handle_t_ {
9791
};
9892

9993
int getAttribute(ur_device_handle_t Device, hipDeviceAttribute_t Attribute);
94+
95+
namespace {
96+
/// Scoped Device is used across all UR HIP plugin implementation to activate
97+
/// the native Device on the current thread. The ScopedDevice does not
98+
/// reinstate the previous device as all operations in the hip adapter that
99+
/// require an active device, set the active device and don't rely on device
100+
/// reinstation
101+
class ScopedDevice {
102+
public:
103+
ScopedDevice(ur_device_handle_t hDevice) {
104+
if (!hDevice) {
105+
throw UR_RESULT_ERROR_INVALID_DEVICE;
106+
}
107+
hipSetDevice(hDevice->get());
108+
}
109+
};
110+
} // namespace

0 commit comments

Comments
 (0)