Potential for a memory leak when using Primary Context
- Dominant language
- Python
- Stars
- 2.1k
- Forks
- 298
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Description
I am working with a group that is using PyCUDA in a task-based parallel application. We therefore don't have a "main" thread that can "own" the PyCUDA primary context. Instead we found a solution where we push/pop the Primary Context on the context stack:
```python
dev = drv.Device(DEV_ID)
ctx = dev.retain_primary_context()
ctx.push()
# ... do work
ctx.pop()
```
The problem with this approach is that any variables referring to GPU memory are still in scope when `ctx.pop()` is called. This appears to result in their not being free'd on the device, leaking memory. Explicitly calling `del` on these variables does not stop the memory leak.
Our solution has been to create an execution-guard, enuring that all GPU variables are out of scope before `ctx.pop()` is called:
```python
class GPUTaskWrapper(object):
def __init__(self, thunk):
self.thunk = thunk
def __call__(self, *args, **kwargs):
dev = drv.Device(DEV_ID)
ctx = dev.retain_primary_context()
ctx.push()
try:
return self.thunk(*args, **kwargs)
finally:
ctx.pop()
def gpu_task_wrapper(thunk):
return GPUTaskWrapper(thunk)
@gpu_task_wrapper
def work_function():
# ... do work
```
@elliottslaughter gets credit for making this work.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.