Katherine Chen
Katherine Chen

Reputation: 542

How to verify and allocate GPU allocation in Tensorflow?

My laptop is Thinkpad T470P which seems to have dual GPUs -- one is Integrated Intel HD graphic 630, and the other one is GeForce 940MX.

I installed CUDA ver. 10.1 on this machine successfully, and now I want to run a training in Tensorflow. I want to know which GPU the training is using, so I tried this:

from tensorflow.python.client import device_lib

device_lib.list_local_devices()

and this is what I got:

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 17770480900406893487, name: "/device:GPU:0"
 device_type: "GPU"
 memory_limit: 1462163865
 locality {
   bus_id: 1
   links {
   }
 }
 incarnation: 5306128727345722238
 physical_device_desc: "device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0"]

I am just curious why there are two incarnation? one has name /device:GPU:0 and the other one has name GeForce 940MX.

From my very limited knowledge, is it true that CUDA and tensorflow could only run on the GeForce one, because CUDA doesn't even support the integrated GPU?

In this case, how do I specify the tensorflow to run on the GeForce 940MX one? Since there are two names, I am not sure whether they are referring to different GPUs. Many thanks for your input!

Upvotes: 0

Views: 754

Answers (1)

Gabriel Ibagon
Gabriel Ibagon

Reputation: 442

First, CUDA is only compatible with NVIDIA graphics cards, so your Integrated Intel HD graphic 630 would not be used by TensorFlow or be listed using device_lib.list_local_devices().

The only GPU device used is the item listed with device_type: "GPU", which represents your GeForce 940MX card. I believe the other item listed refers to the same GPU card - I usually only pay attention to the items with device type = GPU. To print out those items specifically, you can use this script from https://stackoverflow.com/a/38580201/9672143:

from tensorflow.python.client import device_lib

local_device_protos = device_lib.list_local_devices()
print([x.name for x in local_device_protos if x.device_type == 'GPU'])

A useful tool is nvidia-smi on the command line. This lists the available NVIDIA GPUs, as well as which processes are using each GPU. When you start your TensorFlow/Keras program, you should see the python process appear in the bottom section of the nvidia-smi output.

To answer your last question, you can force TensorFlow to use a specific GPU using the following code BEFORE importing TF/Keras:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # gpu ID

The GPU ID can be found from the output of nvidia-smi, which displays each GPU and its associated ID.

Upvotes: 2

Related Questions