@@ -165,45 +165,25 @@ struct ur_context_handle_t_ {
165
165
};
166
166
167
167
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
174
173
class ScopedContext {
175
- hipCtx_t Original;
176
- bool NeedToRecover;
177
-
178
174
public:
179
- ScopedContext (ur_device_handle_t hDevice) : NeedToRecover{false } {
175
+ ScopedContext (ur_device_handle_t hDevice) {
176
+ hipCtx_t Original{};
180
177
181
178
if (!hDevice) {
182
179
throw UR_RESULT_ERROR_INVALID_DEVICE;
183
180
}
184
181
185
- // FIXME when multi device context are supported in HIP adapter
186
182
hipCtx_t Desired = hDevice->getNativeContext ();
187
183
UR_CHECK_ERROR (hipCtxGetCurrent (&Original));
188
184
if (Original != Desired) {
189
185
// Sets the desired context as the active one for the thread
190
186
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));
207
187
}
208
188
}
209
189
};
0 commit comments