提交 76d73bb5 authored 作者: Josh Bleecher Snyder's avatar Josh Bleecher Snyder

Prevent cuda memory-freeing errors on shutdown.

Track whether there is a gpu context active; if not, don't try to free memory. Note that trying to free the memory and then just not displaying the error doesn't work, since the act of trying to free the memory creates a new gpu context.
上级 7ddca5a0
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
// Alloc and Free // Alloc and Free
///////////////////////// /////////////////////////
static int g_gpu_context_active = 0;
/** /**
* *
...@@ -69,6 +70,10 @@ void * device_malloc(size_t size) ...@@ -69,6 +70,10 @@ void * device_malloc(size_t size)
} }
int device_free(void *ptr) int device_free(void *ptr)
{ {
// if there is no gpu context, the call to cudaFree will fail; skip it entirely
if(!g_gpu_context_active) {
return 0;
}
cudaError_t err = cudaFree(ptr); cudaError_t err = cudaFree(ptr);
if (cudaSuccess != err) if (cudaSuccess != err)
{ {
...@@ -1845,6 +1850,11 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args) ...@@ -1845,6 +1850,11 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args)
"Unable to get the number of gpus available: %s", "Unable to get the number of gpus available: %s",
cudaGetErrorString(cudaGetLastError())); cudaGetErrorString(cudaGetLastError()));
} }
// as soon as the first successful call to a cuda* function is made, a
// gpu context has been created
g_gpu_context_active = 1;
if(deviceCount <= 0) { if(deviceCount <= 0) {
return PyErr_Format(PyExc_EnvironmentError, return PyErr_Format(PyExc_EnvironmentError,
"Can't use the GPU, no devices support CUDA"); "Can't use the GPU, no devices support CUDA");
...@@ -1889,6 +1899,7 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args) ...@@ -1889,6 +1899,7 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args)
PyObject * PyObject *
CudaNdarray_gpu_shutdown(PyObject* _unused, PyObject* _unused_args) { CudaNdarray_gpu_shutdown(PyObject* _unused, PyObject* _unused_args) {
cudaThreadExit(); cudaThreadExit();
g_gpu_context_active = 0; // context has now been closed down
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论