我正在使用 C++ 开发一个大型 CUDA 应用程序,该应用程序运行各种模型,需要完全释放所有 GPU 内存,否则其他操作将失败。
我能够在关闭所有 tf 会话并运行 cudaDeviceReset() 后释放所有内存,但之后我无法运行任何新的张量流代码,并且会话创建将返回 nullptrs。我之前和之后都尝试过 cudaDeviceSynchronize() ,认为这会有所帮助,但我没有运气。
我认为对 InitMain 的调用会重新初始化张量流,但如果我在重置后再次调用,似乎不会。是否有一个特定的入口点我可以调用来基本上“重新开始”并使 TensorFlow 再次能够使用 GPU 并生成会话?
我使用的是 C++,而不是 Python。下面的示例显示了我运行 cudeDeviceReset() 然后尝试用 C++ 打开新的 TensorFlow 会话后 TensorFlow 所显示的内容。
2018-10-04 17:01:19.225505: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-10-04 17:01:19.326074: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-10-04 17:01:19.326091: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0
2018-10-04 17:01:19.326095: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N
2018-10-04 17:01:19.326215: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 9446 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
2018-10-04 17:01:19.326554: E tensorflow/stream_executor/cuda/cuda_driver.cc:785] failed to memset memory: CUDA_ERROR_INVALID_VALUE
2018-10-04 17:01:19.326593: E tensorflow/core/common_runtime/direct_session.cc:154] Failed precondition: Failed to memcopy into scratch buffer for device 0
它可能是相关的,但第一次运行在开始时有额外的行,就好像一次性初始化可能已经运行一样。第二轮缺少这些线条。下面是第一个的样子。
2018-10-04 17:01:17.253809: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-10-04 17:01:17.254173: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6325
pciBusID: 0000:01:00.0
totalMemory: 10.92GiB freeMemory: 9.76GiB
2018-10-04 17:01:17.254185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-10-04 17:01:17.413712: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-10-04 17:01:17.413733: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0
2018-10-04 17:01:17.413737: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N
2018-10-04 17:01:17.413888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 9446 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
我至少已经弄清楚如何正确释放内存,并且 nvidia-smi 显示我的应用程序在重置后按预期删除内存,但如果我之后无法执行任何操作,它仍然没有用。
我有一个关于我使用哪个代码来建立会话的问题。我没有使用任何自定义会话选项。
tensorflow::NewSession(tensorflow::SessionOptions());
基本的复制是简单地将图形加载到会话中,关闭会话和图形,释放指针,调用 cudaDeviceReset() 并再次尝试打开另一个会话,这将抛出 nullptrs 和上述错误消息。