提交 37b481c6 authored 作者: Josh Bleecher Snyder's avatar Josh Bleecher Snyder

backout ebbc8fab2c8f

上级 f6050ea8
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
// Alloc and Free // Alloc and Free
///////////////////////// /////////////////////////
static int g_gpu_shutdown = 0; static int g_gpu_context_active = 0;
/** /**
* *
...@@ -70,6 +70,10 @@ void * device_malloc(size_t size) ...@@ -70,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)
{ {
...@@ -95,16 +99,6 @@ int device_free(void *ptr) ...@@ -95,16 +99,6 @@ int device_free(void *ptr)
if(i==TABLE_SIZE) if(i==TABLE_SIZE)
printf("Unallocated unknow size!\n"); printf("Unallocated unknow size!\n");
#endif #endif
if(g_gpu_shutdown && (0 == _outstanding_mallocs[0])) {
// we're done with the gpu, and all relevant memory has been freed
// we're now free to close the cuda context; if we don't explicitly
// exit our cuda context, some systems segfault on process exit
// for as-yet unknown reasons; see
// http://groups.google.com/group/theano-users/browse_thread/thread/c351846e5cebe35f
cudaThreadExit();
}
return 0; return 0;
} }
static PyObject * static PyObject *
...@@ -1856,6 +1850,11 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args) ...@@ -1856,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");
...@@ -1899,7 +1898,8 @@ CudaNdarray_gpu_init(PyObject* _unused, PyObject* args) ...@@ -1899,7 +1898,8 @@ 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) {
g_gpu_shutdown = 1; 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论