-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cupy CUDADriverError: context is destroyed #32
Comments
I can reproduce and I don't understand the issue. Surprisingly, if you do import cupy as cp
import itk
cp.log(1/cp.ones((1200, 120)))/0.01879
device = cp.cuda.runtime.getDevice()
img = itk.CudaImage[itk.F, 3].New()
cp.cuda.runtime.setDevice(device)
cp.log(1/cp.ones((1200, 120)))/0.01879 it does not crash so |
Can you try the following piece of code and let me know if it helps? import cupy as cp
import itk
cp.log(1/cp.ones((1200, 120)))/0.01879
ctx = cp.cuda.driver.ctxGetCurrent()
img = itk.CudaImage[itk.F, 3].New()
cp.cuda.driver.ctxSetCurrent(ctx)
cp.log(1/cp.ones((1200, 120)))/0.01879 |
Thanks ! The second solution runs, and using context like this seems to work in my other codes.
So there is still a problem with context destruction. Also, I have tested with pytorch to check that the problem is not in the cupy side:
This works and no error in closing the python script. Finally, I have explored in details the src files and see that the function cudaSetDevice() initializes the primary context. I have modified the function CudaContextManager(), empty the destructor ~CudaContextManager() and remove GetCurrentContext() (and where it is called). You can check the branch cudaContextManagement to see the details. I do not know how delete the lines I'll let you get back to me and do the modifications if everything is ok. Thanks, Matthieu |
Does it also happen if you comment the |
If I only comment Matthieu |
I can't reproduce this error so I'm not sure how to help... |
Have you tried adding cp.cuda.driver.ctxSetCurrent(ctx) as the last line of the program? |
EDIT: If possible, I would still prefer the changes made in the branch cudaContextManagement as I would not have to add |
I made a PR #34 from my branch cudaContextManagement for the tests |
Use the primary context with cudaSetDevice() introduced by Cuda 7 (https://developer.download.nvidia.com/compute/cuda/7_0/Prod/doc/CUDA_Toolkit_Release_Notes.pdf) instead of a new one. cudaSetDevice is called before every memory transfer between the CPU and GPU to be sure that the context is set for the current thread, see https://developer.nvidia.com/blog/cuda-pro-tip-always-set-current-device-avoid-multithreading-bugs/.
Hello,
While using Cupy and ITK, I got a Cuda error: context is destroyed. This seems to happen when I call Cupy functions around ITK's use of Cuda.
Here is a minimal example to reproduce the error:
cuda version: 11.2
itk version : 5.4.0
cupy version : 11.3.0 (cupy-cuda112)
I would guess ITK destroy the GPU context created by Cupy.
Thanks in advance for any help,
Matthieu
The text was updated successfully, but these errors were encountered: