指定显卡的三种方式

第一种 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
相关推荐
小龙报8 分钟前
【Coze-AI智能体平台】Coze智能体实操:翻译助手从工作流搭建到应用发布全流程详解
人工智能·深度学习·计算机视觉·chatgpt·语音识别·文心一言·集成学习
IT_陈寒5 小时前
React Hooks闭包陷阱:你以为的state可能早就过期了
前端·人工智能·后端
Thomas.Sir6 小时前
第一章:Agent智能体开发实战之【初步认识 LlamaIndex:从入门到实操】
人工智能·python·ai·检索增强·llama·llamaindex
笨笨饿6 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
boy快快长大6 小时前
【大模型应用开发】记忆
人工智能
LaughingZhu7 小时前
Product Hunt 每日热榜 | 2026-04-05
前端·数据库·人工智能·经验分享·神经网络
OPHKVPS7 小时前
GoBruteforcer(GoBrut)僵尸网络新攻势:AI 生成弱配置成“帮凶”,瞄准加密货币及区块链数据库
网络·人工智能·区块链
打乒乓球只会抽7 小时前
AI Agent:大模型+工具的智能革命
人工智能
Pelb7 小时前
求导 y = f(x) = x^2
人工智能·深度学习·神经网络·数学建模
workflower7 小时前
注塑机行业目前自动化现状分析
运维·人工智能·语言模型·自动化·集成测试·软件工程·软件需求