How do I know current available GPUs Tensorflow 2.x by Python script?
February 07 2020
Once you have done the Tensorflow setup with GPU, ensure you have the latest TensorFlow 2.x GPU installed in your GPU supporting machine,
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
Will get an output looks like,
2020-02-07 10:44:15.761280: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2300040000 Hz
2020-02-07 10:44:15.763028: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5c2d6c0 executing computations on platform Host. Devices:
2020-02-07 10:44:15.763059: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
2020-02-07 10:44:15.765442: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-02-07 10:44:33.006792: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
...
2020-02-07 10:44:33.155688: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Tesla K80, Compute Capability 3.7
2020-02-07 10:44:33.155708: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (1): Tesla K80, Compute Capability 3.7
...
2020-02-07 10:44:33.169253: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-02-07 10:44:33.170324: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 1 with properties:
name: Tesla K80 major: 3 minor: 7 memoryClockRate(GHz): 0.8235
pciBusID: 0000:00:18.0
...
2020-02-07 10:44:33.206065: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-02-07 10:44:33.207126: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0, 1, 2, 3, 4, 5, 6, 7
2020-02-07 10:44:33.207189: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-02-07 10:44:33.218583: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-07 10:44:33.218612: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0 1 2 3 4 5 6 7
2020-02-07 10:44:33.218632: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N Y Y Y Y Y Y Y
2020-02-07 10:44:33.218652: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 1: Y N Y Y Y Y Y Y
2020-02-07 10:44:33.218672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 2: Y Y N Y Y Y Y Y
2020-02-07 10:44:33.218692: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 3: Y Y Y N Y Y Y Y
2020-02-07 10:44:33.218711: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 4: Y Y Y Y N Y Y Y
2020-02-07 10:44:33.218732: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 5: Y Y Y Y Y N Y Y
2020-02-07 10:44:33.218751: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 6: Y Y Y Y Y Y N Y
2020-02-07 10:44:33.218764: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 7: Y Y Y Y Y Y Y N
2020-02-07 10:44:33.220442: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
...
2020-02-07 10:44:33.241225: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:7 with 10799 MB memory) -> physical GPU (device: 7, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 11141795535244912227
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 13880322513183122131
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
...
incarnation: 4837154604343532221
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 11323765556
locality {
bus_id: 1
links {
link {
device_id: 1
type: "StreamExecutor"
strength: 1
}
link {
device_id: 2
type: "StreamExecutor"
strength: 1
}
link {
device_id: 3
type: "StreamExecutor"
strength: 1
}
link {
device_id: 4
type: "StreamExecutor"
strength: 1
}
link {
device_id: 5
type: "StreamExecutor"
strength: 1
}
link {
device_id: 6
type: "StreamExecutor"
strength: 1
}
link {
device_id: 7
type: "StreamExecutor"
strength: 1
}
}
}
...
2020-02-07 10:45:37.587838: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-02-07 10:45:37.588896: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0, 1, 2, 3, 4, 5, 6, 7
Num GPUs Available: 8
Comments (0)