1. ホーム
  2. Linux

コンピュータに利用可能なGPUデバイスがあるかどうかを確認する

2022-02-16 19:57:48
<パス

コンピュータ上で利用可能なGPUデバイスを問い合わせる

コード

def is_gpu_available(cuda_only=True):
    from tensorflow.python.client import device_lib as _device_lib
    if cuda_only:
        return any((x.device_type == 'GPU')
                   for x in _device_lib.list_local_devices())
    else:
        return any((x.device_type == 'GPU' or x.device_type == 'SYCL')
                   for x in _device_lib.list_local_devices())
print(is_gpu_available(cuda_only=True))

2020-11-29 15:55:26.179560: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-11-29 15:55:26.453227: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1650 major: 7 minor: 5 memoryClockRate(GHz): 1.56
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.25GiB
2020-11-29 15:55:26.453414: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2020-11-29 15:55:27.273123: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-11-29 15:55:27.273240: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0 
2020-11-29 15:55:27.273301: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N 
2020-11-29 15:55:27.273504: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 2941 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5)
True


結果表示

2020-11-29 15:55:26.179560: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-11-29 15:55:26.453227: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1650 major: 7 minor: 5 memoryClockRate(GHz): 1.56
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.25GiB
2020-11-29 15:55:26.453414: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2020-11-29 15:55:27.273123: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-11-29 15:55:27.273240: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0 
2020-11-29 15:55:27.273301: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N 
2020-11-29 15:55:27.273504: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 2941 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5)
True