I have 3 gpu, why torch.cuda.device_count() only return '1'

username: foxet
cooked: <p>i have 3 gpu(CUDA_VISIBLE_DEVICES=0,1,2),<br>
why torch.cuda.device_count() only return ‘1’</p>

username: QuantScientist
cooked: <p>Run this:<br>
<a href="https://github.com/QuantScientist/Deep-Learning-Boot-Camp/blob/master/day%2002%20PyTORCH%20and%20PyCUDA/PyTorch/01%20PyTorch%20GPU%20support%20test.ipynb" class="onebox" target="_blank" rel="nofollow noopener">https://github.com/QuantScientist/Deep-Learning-Boot-Camp/blob/master/day%2002%20PyTORCH%20and%20PyCUDA/PyTorch/01%20PyTorch%20GPU%20support%20test.ipynb</a></p>
<p>Show us the output,</p>

username: foxet
cooked: <pre><code class="lang-python">% reset -f
from __future__ import print_function
from __future__ import division
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
</code></pre>
<pre><code class="lang-python">import torch
import sys
print('__Python VERSION:', sys.version)
print('__pyTorch VERSION:', torch.__version__)
print('__CUDA VERSION')
from subprocess import call
# call(["nvcc", "--version"]) does not work
! nvcc --version
print('__CUDNN VERSION:', torch.backends.cudnn.version())
print('__Number CUDA Devices:', torch.cuda.device_count())
print('__Devices')
call(["nvidia-smi", "--format=csv", "--query-gpu=index,name,driver_version,memory.total,memory.used,memory.free"])
print('Active CUDA Device: GPU', torch.cuda.current_device())

print ('Available devices ', torch.cuda.device_count())
print ('Current cuda device ', torch.cuda.current_device())


</code></pre>
<pre><code>__Python VERSION: 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
__pyTorch VERSION: 0.2.0_4
__CUDA VERSION
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61
__CUDNN VERSION: 6021
__Number CUDA Devices: 3
__Devices
Active CUDA Device: GPU 0
Available devices  3
Current cuda device  0
</code></pre>
<pre><code class="lang-python">use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
Tensor = FloatTensor

</code></pre>
<pre><code class="lang-python">import pycuda
from pycuda import compiler
import pycuda.driver as drv

drv.init()
print("%d device(s) found." % drv.Device.count())
           
for ordinal in range(drv.Device.count()):
    dev = drv.Device(ordinal)
    print (ordinal, dev.name())
</code></pre>
<pre><code>3 device(s) found.
0 GeForce GTX 1080 Ti
1 GeForce GTX 1080 Ti
2 GeForce GTX 1080 Ti
</code></pre>
<pre><code class="lang-python">from pycuda import gpuarray
from pycuda.curandom import rand as curand
 # -- initialize the device
import pycuda.autoinit

height = 100
width = 200
X = curand((height, width), np.float32)
X.flags.c_contiguous 
print (type(X))
</code></pre>
<pre><code>&lt;class 'pycuda.gpuarray.GPUArray'&gt;
</code></pre>
<pre><code class="lang-python">torch.cuda.device_count()
</code></pre>
<pre><code>3
</code></pre>
<pre><code class="lang-python">
</code></pre>
<pre><code class="lang-python">
</code></pre>
<pre><code class="lang-python">
</code></pre>
<pre><code class="lang-python">
</code></pre>

username: foxet
cooked: <p>in this file, torch.cuda.device_count() works fine.</p>
<p>seems just the code i worked on can’t return the right number of cuda device. don’t know why</p>

username: foxet
cooked: <p>after i duplicate that file, it works …thankyou…</p>

username: QuantScientist
cooked: <p>My pleasure, let me know if you need anything else <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: mikey_t
cooked: <p>I’m having the same problem and I’m wondering if there have been any updates to make it easier for pytorch to find my gpus. I have two:</p>
<blockquote>
<p>Microsoft Remote Display Adapter               0<br>
NVIDIA GeForce RTX 2070 SUPER    4293918720 4095<br>
Microsoft Remote Display Adapter               0<br>
NVIDIA GeForce RTX 2070 SUPER    4293918720 4095</p>
</blockquote>
<p>But when I run <code>torch.cuda.device_count()</code>, I get <code>1</code>. Everythinig runs smoothly on the one gpu, but I’d like to utilize both. I tried following the advice above, but the link to the jupyter notebook appears to be broken and when I run the code pasted in this question, I can’t install the package <code>pycuda</code>. Since this is from three years ago, I’m wondering if there has been an update.</p>

username: drevicko
cooked: <p>Do you have CUDA_VISIBLE_DEVICES set in the environment from which you launch your program (or by some script that launches your python program)? That makes other devices disappear from subsequent cuda calls, including from pytorch. <a href="https://discuss.pytorch.org/t/cuda-visible-devices-make-gpu-disappear/21439">This</a> illustrates nicely how CUDA_VISIBLE_DEVICES works.</p>