When I shut down the pytorch program by kill, I encountered the problem with the GPU

DL-Chang: <p>Hello,<br>
When I shut down the pytorch program by kill, I encountered the problem with the GPU(I opened two programs at the same time and then turned off).<br>
When i use kill to close the program，GPU is still occupied.Then I found that there were two PIDs that could not kill.<br>
What’s wrong ? thank you ?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/a/a0ec18a70f87ef997ed4344843f91adb5417fb57.png" data-download-href="https://discuss.pytorch.org/uploads/default/a0ec18a70f87ef997ed4344843f91adb5417fb57" title="TIM截图20170816224626"><img src="https://discuss.pytorch.org/uploads/default/original/2X/a/a0ec18a70f87ef997ed4344843f91adb5417fb57.png" alt="TIM截图20170816224626" width="690" height="488" data-dominant-color="0E0E0E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">TIM截图20170816224626</span><span class="informations">1097×776 10.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>and my code is:</p>
<blockquote>
<pre><code>net.cuda()
net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
cudnn.benchmark = True
</code></pre>
</blockquote>

JuliousHurtado: <p>I had the same problem. The easy solution is to reset the pc or reset the memory from the GPU (that work for me):</p>
<blockquote>
<p>sudo nvidia-smi --gpu-reset</p>
</blockquote>
<p>But I don’t know why this happens, only that I have to stops the scripts using ctrl-c.</p>

DL-Chang: <p>Hi:<br>
I no longer encounter this problem when I set up to use only one GPU。</p>
<blockquote>
<p>os.environ[“CUDA_VISIBLE_DEVICES”] = “0”</p>
</blockquote>

yifita: <p>I also had the same problem. The admin had to reboot every time this happened. Something is quite wrong in the DataParallel.<br>
Also I’ve often experienced the following:<br>
running on multiple GPUs with DataParallel and DataLoader (num_worker = 16), try to terminate the process with ctrl+c then program freezes. Gpus hang, including nvidia-smi or other GPU related work.</p>

guyue_hu: <p>nearly the same problem! Every time I reboot the machine, does somebody has any good way to solve it?</p>

feipan664: <p><a class="mention" href="/u/dl-chang">@DL-Chang</a><br>
I happen to meet the issue same as you, and what I do is:</p>
<blockquote>
<p>ps aux|grep python</p>
</blockquote>
<p>And then, find out the pid whose process is occupying in the GPU memory, do</p>
<blockquote>
<p>kill -9 PID</p>
</blockquote>

Zhiqiu_Lin: <p>Same issue here! I guess DataParallel is very buggy in terms of processing kill signal. Would love to have it fixed</p>

zfzhang: <p>I am still encountering the GPU hang problem today, but my case is not Pytorch related.</p>
<p>I’m using Win7 with CUDA 10.2, writing C++ code with CUDA runtime API. Sometimes when I hit Ctrl-C, the GPU hangs and the process is in some sort of zombie state and unkillable, then I have to reboot.</p>
<p>I conjecture that when the host code is submitting a kernel job and the kill signal arrives this will happen, without concrete evidence.  If that’s the case, we might need to catch the kill signal in our code to allow graceful exit of CUDA.</p>

Noki_Lee: <p><a class="mention" href="/u/feipan664">@feipan664</a>’s solution worked. But, how can we prevent this issue in advance? PID in <code>nvidia-smi</code> shows just the main PID of the job. And, killing that just kills the main PID, remaining intertwined PIDs.</p>