开发环境配置
本节将指导你在不同操作系统上配置 CUDA 开发环境。
系统要求
硬件要求
- GPU:NVIDIA 显卡,支持 CUDA 计算能力 3.0 及以上
- 查看显卡计算能力:NVIDIA GPU 计算能力列表
- 内存:建议 8GB 以上
- 磁盘空间:至少 10GB 可用空间(包含 CUDA Toolkit 和示例)
软件要求
- 操作系统:Windows 10/11、Ubuntu 18.04+、CentOS 7+ 等
- C/C++ 编译器:
- Windows:Visual Studio 2019/2022
- Linux:GCC 7.0+ 或 Clang
- 显卡驱动:NVIDIA 驱动程序(建议最新版本)
Windows 环境配置
1. 安装 Visual Studio
CUDA 需要配合 Visual Studio 使用:
- 下载并安装 Visual Studio Community(免费版本即可)
- 安装时选择「使用 C++ 的桌面开发」工作负载
- 确保安装了 Windows SDK
2. 安装 CUDA Toolkit
- 访问 CUDA Toolkit 下载页面
- 选择对应的操作系统版本
- 下载并运行安装程序
- 选择「精简」安装模式(推荐)或「自定义」安装
安装完成后,CUDA Toolkit 默认安装在:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.x
3. 验证安装
打开命令提示符或 PowerShell,执行:
nvcc --version
输出类似:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
...
Cuda compilation tools, release 12.x, V12.x.xxx
检查显卡驱动和 GPU 信息:
nvidia-smi
输出类似:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 535.xx.xx Driver Version: 535.xx.xx CUDA Version: 12.x |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 On | N/A |
| 30% 45C P8 15W / 250W | 512MiB / 8192MiB | 4% Default |
+-------------------------------+----------------------+----------------------+
4. 配置环境变量
安装程序通常会自动配置,如果没有,请手动添加:
CUDA_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.x
PATH 添加: %CUDA_PATH%\bin
5. 运行示例程序
CUDA Toolkit 附带了大量示例程序:
cd "C:\ProgramData\NVIDIA Corporation\CUDA Samples\v12.x\1_Utilities\deviceQuery"
# 使用 Visual Studio 打开并编译,或使用命令行
nvcc deviceQuery.cpp -o deviceQuery.exe
.\deviceQuery.exe
Linux 环境配置
1. 安装显卡驱动
Ubuntu 系统:
# 添加 NVIDIA 驱动 PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
# 安装推荐驱动
sudo ubuntu-drivers autoinstall
# 或手动安装特定版本
sudo apt install nvidia-driver-535
# 重启系统
sudo reboot
验证驱动安装:
nvidia-smi
2. 安装 CUDA Toolkit
方法一:使用 apt 仓库(推荐)
# 添加 NVIDIA 包仓库
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
# 安装 CUDA Toolkit
sudo apt install cuda
# 安装开发工具包(可选)
sudo apt install cuda-toolkit-12-x
方法二:使用 runfile
# 下载 runfile
wget https://developer.download.nvidia.com/compute/cuda/12.x.x/local_installers/cuda_12.x.x_linux.run
# 运行安装程序
sudo sh cuda_12.x.x_linux.run
3. 配置环境变量
编辑 ~/.bashrc 或 ~/.zshrc:
export CUDA_HOME=/usr/local/cuda
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
使配置生效:
source ~/.bashrc
4. 验证安装
nvcc --version
nvidia-smi
5. 编译运行示例
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
开发工具推荐
IDE 选择
| IDE | 平台 | 特点 |
|---|---|---|
| Visual Studio | Windows | 官方支持最好,调试功能强大 |
| VS Code | 跨平台 | 轻量级,插件丰富 |
| CLion | 跨平台 | JetBrains 出品,智能补全 |
| Nsight Eclipse | Linux | NVIDIA 官方 IDE |
VS Code 配置
推荐安装以下扩展:
- CUDA C++ - CUDA 语法高亮
- C/C++ Extension Pack - C++ 开发支持
- CodeLLDB - 调试支持
创建 .vscode/tasks.json 用于编译:
{
"version": "2.0.0",
"tasks": [
{
"label": "compile cuda",
"type": "shell",
"command": "nvcc",
"args": [
"-g",
"-G",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"problemMatcher": ["$gcc"]
}
]
}
编译第一个 CUDA 程序
创建文件 hello.cu:
#include <stdio.h>
__global__ void hello_cuda() {
printf("Hello from GPU thread %d!\n", threadIdx.x);
}
int main() {
printf("Hello from CPU!\n");
hello_cuda<<<1, 5>>>();
cudaDeviceSynchronize();
return 0;
}
编译并运行:
nvcc hello.cu -o hello
./hello
输出:
Hello from CPU!
Hello from GPU thread 0!
Hello from GPU thread 1!
Hello from GPU thread 2!
Hello from GPU thread 3!
Hello from GPU thread 4!
常见问题
1. nvcc 找不到
确认 CUDA bin 目录在 PATH 中:
echo $PATH
2. 驱动版本不兼容
更新显卡驱动到最新版本,或安装与驱动兼容的 CUDA 版本。
3. 编译错误:unsupported GNU version
GCC 版本过高,安装较低版本的 GCC:
sudo apt install gcc-9 g++-9
sudo ln -sf /usr/bin/gcc-9 /usr/local/cuda/bin/gcc
sudo ln -sf /usr/bin/g++-9 /usr/local/cuda/bin/g++
4. 运行时错误:invalid device ordinal
检查 GPU 设备 ID 是否正确,使用 cudaGetDeviceCount() 查询设备数量。
下一步
环境配置完成后,让我们开始学习 CUDA 编程模型,理解 Host 与 Device 的协作方式。