static */
| 368 | } |
| 369 | |
| 370 | /* static */ port::Status GpuDriver::CreateContext( |
| 371 | int device_ordinal, CUdevice device, const DeviceOptions& device_options, |
| 372 | GpuContext** context) { |
| 373 | *context = nullptr; |
| 374 | |
| 375 | int flags = 0; |
| 376 | if (!DeviceOptionsToContextFlags(device_options, &flags)) { |
| 377 | LOG(WARNING) << "could not convert all device options into context flags"; |
| 378 | } |
| 379 | |
| 380 | CUresult res; |
| 381 | CUcontext former_context; |
| 382 | CUcontext new_context; |
| 383 | |
| 384 | unsigned int former_primary_context_flags; |
| 385 | int former_primary_context_is_active; |
| 386 | CHECK_EQ(CUDA_SUCCESS, |
| 387 | cuDevicePrimaryCtxGetState(device, &former_primary_context_flags, |
| 388 | &former_primary_context_is_active)); |
| 389 | if (former_primary_context_flags != flags) { |
| 390 | if (former_primary_context_is_active) { |
| 391 | LOG(ERROR) |
| 392 | << "The primary context is active and has a different flag set (" |
| 393 | << former_primary_context_flags << ") than the desired flag set (" |
| 394 | << flags << ")."; |
| 395 | } else { |
| 396 | CHECK_EQ(CUDA_SUCCESS, cuDevicePrimaryCtxSetFlags(device, flags)); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | former_context = cuda::CurrentContextOrDie(); |
| 401 | res = cuDevicePrimaryCtxRetain(&new_context, device); |
| 402 | if (res == CUDA_SUCCESS) { |
| 403 | if (CudaPrimaryContextsMgr::Add(new_context, device)) { |
| 404 | LOG(INFO) << "Cuda add device primary context " << new_context; |
| 405 | } else { |
| 406 | CHECK_EQ(CUDA_SUCCESS, cuDevicePrimaryCtxRelease(device)); |
| 407 | res = cuCtxCreate(&new_context, flags, device); |
| 408 | LOG(INFO) << "Cuda primary context is used, cuCtxCreate new context " |
| 409 | << new_context; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (former_context != nullptr) { |
| 414 | CUdevice former_device; |
| 415 | if (cuCtxGetDevice(&former_device) == CUDA_SUCCESS) { |
| 416 | if (former_device == device) { |
| 417 | if (former_context == new_context) { |
| 418 | VLOG(2) << "The primary context " << former_context << " for device " |
| 419 | << device |
| 420 | << " exists before initializing the StreamExecutor."; |
| 421 | } else { |
| 422 | LOG(WARNING) << "A non-primary context " << former_context |
| 423 | << " for device " << device |
| 424 | << " exists before initializing the StreamExecutor. The " |
| 425 | << "primary context is now " << new_context << ". We " |
| 426 | << "haven't verified StreamExecutor works with that."; |
| 427 | } |
nothing calls this directly
no test coverage detected