怎么用python代码查看可用的gpu,然后指定可用的gpu运行

查看gpu

python 复制代码
import torch

# 检查CUDA是否可用
if torch.cuda.is_available():
    # 获取GPU设备数量
    device_count = torch.cuda.device_count()

    # 列出可用的GPU设备
    for i in range(device_count):
        print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
else:
    print("CUDA is not available. No GPU devices found.")

输出结果

设置指定gpu运行

指定上面输出的5号卡

python 复制代码
if torch.cuda.is_available():
    # 指定要使用的GPU设备编号
    device = torch.device("cuda:5")
    print(f"Using GPU {device} - {torch.cuda.get_device_name(device)}")
else:
    print("CUDA is not available. No GPU devices found.")

查看gpu的内存情况等

python 复制代码
import subprocess

# 执行nvidia-smi命令以获取GPU信息
nvidia_smi_output = subprocess.check_output("nvidia-smi", shell=True).decode()

# 切分输出为每个GPU的信息
gpu_info = nvidia_smi_output.strip().split('\n\n')

# 遍历每个GPU的信息
for i, info in enumerate(gpu_info):
    print(f"GPU {i}:")
    print(info)
相关推荐
nothing&nowhere5 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田5 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang6 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
飞猪~9 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_9563198810 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python
是Dream呀11 小时前
基于深度学习的人类行为识别算法研究
人工智能·深度学习·算法
人工智能培训11 小时前
破解数据发展瓶颈 激活数字经济新动能
大数据·数据库·人工智能·深度学习·神经网络·机器学习·生成对抗网络
绝世番茄11 小时前
鸿蒙 HarmonyOS ArkTS 音乐播放器界面布局深度学习指南
深度学习·华为·list·harmonyos·鸿蒙
happyprince11 小时前
03_NVIDIA_ModelOpt-量化算法深入
人工智能·深度学习·算法