在jetson nano上使用CUDA
我使用的是jetsonnano 4GB的版本的,按照网上教的用TF烧录镜像,这点很多教程都要教,就不赘述了,唯一会踩坑的是最好使用USB3.0的读卡器,不然烧录会有问题。
jetson nano烧录镜像后是自带cuda的,不需要安装,输入
nvcc -V
提示"nvcc command not found" ,说明cuda还没加入路径中,则需要配置一下环境
利用vim打开 ~ 路径下.bashrc文件:
sudo vi ~/.bashrc
文件的最后添加以下三行:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda
重新执行.bashrc文件,直接生效;
source ~./bashrc
输入nvcc -V命令进行测试,如果显示如下信息,证明修改正确。
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sun_Sep_30_21:09:22_CDT_2018
Cuda compilation tools, release 10.0, V10.0.166
注意jetson nano不支持nvidia-smi命令,所以可以装一个jtop来代替,具体看下面这个博客
NVIDIA Jetson AGX Xavier 安装及使用jtop_这也太南了趴的博客-CSDN博客
接下来写hello world
新建一个helloWorld.cu文件,输入
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
__global__ void mykernel(void) {
int col_index = threadIdx.x + blockIdx.x * blockDim.x;
int row_index = threadIdx.y + blockIdx.y * blockDim.y;
printf("hello from (%d,%d) \n",row_index,col_index);
}
int main(void) {
dim3 grid(2,2);
dim3 block(1,4);
mykernel<<<grid, block>>>();
// synchronize the device
cudaDeviceSynchronize();
}
接下来终端输入
nvcc helloWorld.cu -o helloWorld
./helloWorld
会出现下面这样子就算成功。
hello from (4,0)
hello from (5,0)
hello from (6,0)
hello from (7,0)
hello from (0,0)
hello from (1,0)
hello from (2,0)
hello from (3,0)
hello from (4,1)
hello from (5,1)
hello from (6,1)
hello from (7,1)
hello from (0,1)
hello from (1,1)
hello from (2,1)
hello from (3,1)