指定显卡的三种方式

第一种 os.environ"CUDA_VISIBLE_DEVICES" = "1"

python 复制代码
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import torch

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))

结果

物理 GPU 1 被映射为逻辑 GPU 0

python 复制代码
1
0
NVIDIA GeForce RTX 4090

第二种 命令行 CUDA_VISIBLE_DEVICES=1 python cuda_test.py

python 复制代码
import torch

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))

结果

物理 GPU 1 被映射为逻辑 GPU 0

python 复制代码
1
0
NVIDIA GeForce RTX 4090

第三种 torch.cuda.set_device(1)

python 复制代码
import torch
torch.cuda.set_device(1)

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))
print(torch.cuda.get_device_name(1))

结果

设置任意cuda:1设备为当前cuda设备

python 复制代码
2
1
NVIDIA GeForce RTX 4090
NVIDIA GeForce RTX 4090
相关推荐
AI多Agent协作实战派6 小时前
【AI探索历程17】从“给自己用“到想“让别人用“
人工智能
吴佳浩6 小时前
Memory Provider 实战:Hermes、Mem0、Honcho、Hindsight 为什么都这样设计?
人工智能·agent·ai编程
吴佳浩6 小时前
企业级 Agent Memory 选型指南:如何构建可扩展的 Memory Service?
人工智能·agent·ai编程
北方的银狐-Zero7 小时前
ERP 管执行,数据管标准,OntoL本体 管思考:企业智能化升级的规划图
大数据·人工智能·本体论
浅安的邂逅8 小时前
260918-白帽把 OpenAI 论坛“打穿“了:攻防赛漏洞、账户被 Claude 入侵、模型偷偷掩盖不当行为
人工智能·大模型·ai编程·ai模型·行业动态
jinyishu_8 小时前
认识 AI Agent:概念、架构、设计模式与主流框架
人工智能
Hrain-AI8 小时前
AI 爬虫三档授权工程落地:搜索放行、训练拦截、Agent 分层(附脚本)
人工智能·elasticsearch·milvus
sunhy_csdn8 小时前
“词码”作为 Token 候选译名
人工智能
Hrain-AI8 小时前
多 Agent 并行不打架:worktree 隔离与反馈回流落地(附脚本)
网络·数据库·人工智能·架构