首先,你要有个N卡,如果没有的话。。。。。。。。
安装
安装环境: Ubuntu 18.04
查看显卡
lspci | grep -i nvidia
将nouveau添加黑名单
# 查看
lsmod | grep -i nouveau
# 创建黑名单
sudo vi /etc/modprobe.d/blacklist-nouveau.conf
# 添加黑名单内容
blacklist nouveau
options nouveau modeset=0
# 生效
sudo update-initramfs -u
#重启
sudo reboot
安装依赖
sudo apt-get install build-essential
sudo apt-get install linux-headers-$(uname -r)
下载CUDA SDK
- 下载地址
- 历史版本下载
推荐方式--runfile(local)
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
- 缺点: 安装包大,下载时间长
- 优点: 可直接一次安装驱动和SDK,避免驱动版本和SDK不兼容的情况(相信我,不兼容这个情况是个坑,深不见底)。
安装
sudo sh cuda_10.2.89_440.33.01_linux.run
检查
检查驱动
nvidia-smi
会输出当前显卡信息
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.39 Driver Version: 418.39 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:04:00.0 Off | N/A |
| 24% 42C P0 54W / 250W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
检查SDK
进入安装目录cd /usr/local/cuda/bin
(默认安装目录)。
./nvcc --version
输出版本信息:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130
配置环境变量
系统级
sudo vim /etc/environment
添加CUDA信息
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda/bin"
用户级
添加到当前用户
- bash
vim ~/.bashrc
- zsh
vim ~/.zshrc
添加CUDA信息
export CUDA_BIN=/usr/local/cuda/bin
export CUDA_LIB=/usr/local/cuda/lib64
export PATH=${PATH}:${CUDA_BIN}
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_LIB}
别忘了source
。
检查
nvcc --version
创建一个CUDA的CMake项目
代码
// @file main.cu
#include
__global__ void hello_world(){
unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
printf("[ %d ] hello world\n", idx);
}
int main() {
hello_world<<1, 1 >>>();
cudaDeviceSynchronize();
return 0;
}
CMakeList.txt
cmake_minimum_required(VERSION 3.17)
project(cuda_01_hello_world CUDA)
set(CMAKE_CUDA_STANDARD 14)
add_executable(cuda_01_hello_world main.cu)
set_target_properties(
cuda_01_hello_world
PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
编译
mkdir build && cd build
cmake ..
./cuda_01_hello_world
输出
小朋友你是否有很多的问号?
__global__
是什么?idx
是怎么算出来的?<<<1, 1>>
是什么?cudaDeviceSynchronize()
是什么?- 是谁打印的“hello world”?
且听下回分解
拓展阅读
- 《Learn CUDA Programming》:chapter 1 - 4
- An Even Easier Introduction to CUDA
- https://developer.nvidia.com/blog/even-easier-introduction-cuda/
- Building Cross-Platform CUDA Applications with CMake
- https://developer.nvidia.com/blog/building-cuda-applications-cmake/
- CUDA Quick Start Guide,各平台安装。
- https://docs.nvidia.com/cuda/archive/11.1.0/cuda-quick-start-guide/index.html#abstract