@@ -112,45 +112,25 @@ struct ur_context_handle_t_ {
112112};
113113
114114namespace {
115- // / RAII type to guarantee recovering original HIP context
116- // / Scoped context is used across all UR HIP plugin implementation
117- // / to activate the UR Context on the current thread, matching the
118- // / HIP driver semantics where the context used for the HIP Driver
119- // / API is the one active on the thread.
120- // / The implementation tries to avoid replacing the hipCtx_t if it cans
115+ // / Scoped context is used across all UR HIP plugin implementation to activate
116+ // / the native Context on the current thread. The ScopedContext does not
117+ // / reinstate the previous context as all operations in the hip adapter that
118+ // / require an active context, set the active context and don't rely on context
119+ // / reinstation
121120class ScopedContext {
122- hipCtx_t Original;
123- bool NeedToRecover;
124-
125121public:
126- ScopedContext (ur_device_handle_t hDevice) : NeedToRecover{false } {
122+ ScopedContext (ur_device_handle_t hDevice) {
123+ hipCtx_t Original{};
127124
128125 if (!hDevice) {
129126 throw UR_RESULT_ERROR_INVALID_DEVICE;
130127 }
131128
132- // FIXME when multi device context are supported in HIP adapter
133129 hipCtx_t Desired = hDevice->getNativeContext ();
134130 UR_CHECK_ERROR (hipCtxGetCurrent (&Original));
135131 if (Original != Desired) {
136132 // Sets the desired context as the active one for the thread
137133 UR_CHECK_ERROR (hipCtxSetCurrent (Desired));
138- if (Original == nullptr ) {
139- // No context is installed on the current thread
140- // This is the most common case. We can activate the context in the
141- // thread and leave it there until all the UR context referring to the
142- // same underlying HIP context are destroyed. This emulates
143- // the behaviour of the HIP runtime api, and avoids costly context
144- // switches. No action is required on this side of the if.
145- } else {
146- NeedToRecover = true ;
147- }
148- }
149- }
150-
151- ~ScopedContext () {
152- if (NeedToRecover) {
153- UR_CHECK_ERROR (hipCtxSetCurrent (Original));
154134 }
155135 }
156136};
0 commit comments