@@ -112,45 +112,25 @@ struct ur_context_handle_t_ {
112
112
};
113
113
114
114
namespace {
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
121
120
class ScopedContext {
122
- hipCtx_t Original;
123
- bool NeedToRecover;
124
-
125
121
public:
126
- ScopedContext (ur_device_handle_t hDevice) : NeedToRecover{false } {
122
+ ScopedContext (ur_device_handle_t hDevice) {
123
+ hipCtx_t Original{};
127
124
128
125
if (!hDevice) {
129
126
throw UR_RESULT_ERROR_INVALID_DEVICE;
130
127
}
131
128
132
- // FIXME when multi device context are supported in HIP adapter
133
129
hipCtx_t Desired = hDevice->getNativeContext ();
134
130
UR_CHECK_ERROR (hipCtxGetCurrent (&Original));
135
131
if (Original != Desired) {
136
132
// Sets the desired context as the active one for the thread
137
133
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));
154
134
}
155
135
}
156
136
};
0 commit comments