Skip to content

Commit 2b77f79

Browse files
committed
Don't reinstate old context
1 parent e413c6c commit 2b77f79

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

source/adapters/hip/context.hpp

+7-27
Original file line numberDiff line numberDiff line change
@@ -165,45 +165,25 @@ struct ur_context_handle_t_ {
165165
};
166166

167167
namespace {
168-
/// RAII type to guarantee recovering original HIP context
169-
/// Scoped context is used across all UR HIP plugin implementation
170-
/// to activate the UR Context on the current thread, matching the
171-
/// HIP driver semantics where the context used for the HIP Driver
172-
/// API is the one active on the thread.
173-
/// The implementation tries to avoid replacing the hipCtx_t if it cans
168+
/// Scoped context is used across all UR HIP plugin implementation to activate
169+
/// the native Context on the current thread. The ScopedContext does not
170+
/// reinstate the previous context as all operations in the hip adapter that
171+
/// require an active context, set the active context and don't rely on context
172+
/// reinstation
174173
class ScopedContext {
175-
hipCtx_t Original;
176-
bool NeedToRecover;
177-
178174
public:
179-
ScopedContext(ur_device_handle_t hDevice) : NeedToRecover{false} {
175+
ScopedContext(ur_device_handle_t hDevice) {
176+
hipCtx_t Original{};
180177

181178
if (!hDevice) {
182179
throw UR_RESULT_ERROR_INVALID_DEVICE;
183180
}
184181

185-
// FIXME when multi device context are supported in HIP adapter
186182
hipCtx_t Desired = hDevice->getNativeContext();
187183
UR_CHECK_ERROR(hipCtxGetCurrent(&Original));
188184
if (Original != Desired) {
189185
// Sets the desired context as the active one for the thread
190186
UR_CHECK_ERROR(hipCtxSetCurrent(Desired));
191-
if (Original == nullptr) {
192-
// No context is installed on the current thread
193-
// This is the most common case. We can activate the context in the
194-
// thread and leave it there until all the UR context referring to the
195-
// same underlying HIP context are destroyed. This emulates
196-
// the behaviour of the HIP runtime api, and avoids costly context
197-
// switches. No action is required on this side of the if.
198-
} else {
199-
NeedToRecover = true;
200-
}
201-
}
202-
}
203-
204-
~ScopedContext() {
205-
if (NeedToRecover) {
206-
UR_CHECK_ERROR(hipCtxSetCurrent(Original));
207187
}
208188
}
209189
};

0 commit comments

Comments
 (0)