怎么用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)
相关推荐
小白快快跑哦4 分钟前
python-字符串全解(六):正则表达式-量词
python·正则表达式·字符串
禹凕21 分钟前
机器学习之Selenium(Machina Learning about Selenium)
爬虫·python·selenium·测试工具·机器学习
一直在努力的小宁1 小时前
【阅读笔记】具身智能的真机数采,到了分水岭
人工智能·深度学习·机器学习·agent·具身智能·vlm·vln
AI人工智能+1 小时前
营业执照识别技术通过图像预处理、版面定位、深度学习OCR、NLP语义解析与智能校验五步流程,实现对倾斜、反光、遮挡等复杂照片的毫秒级精准识别
人工智能·深度学习·自然语言处理·营业执照识别
ebiobiz2 小时前
基于 GD32 Embedded Builder (GEB) 与 Nimmake 的 MCU 工程搭建指南
c++·python·单片机·嵌入式硬件·mcu
码农幻想梦2 小时前
深度学习与神经网络(二)
人工智能·深度学习·神经网络
老歌老听老掉牙3 小时前
穿越时空的物流密码:VRP与VRPTW的深度剖析及实战求解
python·物流·vrp
FYKJ_20103 小时前
django电影推荐系统55066-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
问天_观心3 小时前
大模型训练与推理优化(二)
人工智能·深度学习·学习·大模型·transformer
ShyanZh3 小时前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式