| 166 | } // namespace |
| 167 | |
| 168 | ScopedActivateContext::ScopedActivateContext(GpuContext* cuda_context) { |
| 169 | if (FLAGS_gpuexec_cuda_sync_around_driver_calls) SynchronizeOrDie(); |
| 170 | |
| 171 | auto* tls = &tls_data.get(); |
| 172 | tls->depth++; |
| 173 | if (tls->id == cuda_context->id()) { |
| 174 | if (kVerifyGpuContext) { |
| 175 | CHECK_EQ(CurrentContext(), cuda_context->context()); |
| 176 | } |
| 177 | DCHECK_EQ(CurrentContext(), cuda_context->context()); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | VLOG(3) << "ScopedActivateContext switching context from " << tls->id |
| 182 | << " to " << cuda_context->id(); |
| 183 | |
| 184 | to_restore_ = (tls->depth == 1 ? nullptr : tls->context); |
| 185 | |
| 186 | // Set the context and update thread local. |
| 187 | FAIL_IF_CUDA_RES_ERROR(cuCtxSetCurrent(cuda_context->context()), |
| 188 | "Failed setting context"); |
| 189 | tls->id = cuda_context->id(); |
| 190 | tls->context = cuda_context; |
| 191 | } |
| 192 | |
| 193 | ScopedActivateContext::~ScopedActivateContext() { |
| 194 | if (FLAGS_gpuexec_cuda_sync_around_driver_calls) SynchronizeOrDie(); |
nothing calls this directly
no test coverage detected