inducer / inducer/pycuda

GPU memory allocated by make_context cannot be released when exception.

Open
#335 11 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
2.1k
Forks
298
Avg merge
4m
Merged PRs (30d)
1

Description

**Describe the bug**
I want to initialize as many cuda contexts as possible in a multi-threaded environment, but when cuda.Device(0).make_context() throws an exception, the GPU memory allocated by make_context cannot be released.

**To Reproduce**
```
import time
import logging

import pycuda.driver as cuda
# from PyQt5.QtCore import QThread

class GpuContext(object):
def __init__(self, name=None):
self.logging = logging.getLogger('GpuContext - ' + name)
self.cuda_context = None
self.cuda_context = cuda.Device(0).make_context()

def __del__(self):
if self.cuda_context:
try:
self.cuda_context.pop()
except:
self.logging.exception('self.cuda_context.pop()')

try:
self.cuda_context.detach()
except:
self.logging.exception('self.cuda_context.detach() error')

try:
del self.cuda_context
self.cuda_context = None
except:
self.logging.exception('del self.cuda_context error')

# class GpuThread(QThread):
# def __init__(self, gpuContext, parent=None):
# super().__init__(parent)
# self.gpuContext = gpuContext
#
# def run(self):
# pass

if __name__ == '__main__':
cuda.init()
gupContexts = []
for i in range(100):
try:
gpuContext = GpuContext(str(i))
gupContexts.append(gpuContext)
except:
logging.exception('init error')
break

while len(gupContexts) > 0:
gpuContext = gupContexts.pop()
del gpuContext

while True:
print('main')
time.sleep(1)

```
**Environment (please complete the following information):**
- OS: ubuntu18
- CUDA version: 10.2, V10.2.89
- CUDA driver version: 460.91.03
- PyCUDA version: pycuda-2021.1
- Python version: 3.6.9

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.