指定显卡的三种方式

第一种 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
相关推荐
EterNity_TiMe_6 小时前
跨地域实时共创,使用白板协作不卡壳得最佳解决方案。
人工智能·web·cpolar
吴佳浩 Alben6 小时前
OpenClaw 2026.3.2 — 2026.3.8 权限变更与安全加固
人工智能·安全·语言模型
xjf77116 小时前
通义灵码Agent闭环工作流
人工智能·ai文档
AAwangzong6 小时前
OpenClaw引爆工业AI革命:从跟风部署到系统进化的四重重构
人工智能
Spaceack6 小时前
《养虾者说》
人工智能·openclaw
墨染天姬6 小时前
【AI】基于qwen:14b微调小说专家
人工智能
zhangfeng11336 小时前
2026年国际国外top10 top20流行的ai ide,
ide·人工智能
Eward-an7 小时前
华为ModelEngine全流程评测:从智能体开发到应用编排,解锁企业级AI开发新范式
大数据·人工智能
liliwoliliwo7 小时前
深度学习--CNN
人工智能·深度学习