首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >jetson Nano安装pycuda(编译安装版)

jetson Nano安装pycuda(编译安装版)

作者头像
云深无际
发布2021-05-18 14:25:28
发布2021-05-18 14:25:28
1.9K0
举报
文章被收录于专栏:云深之无迹云深之无迹

继续解决我们的各种奇怪的问题

代码语言:javascript
复制
pip install opencv-python

现在都好智能哎,版本都识别出来了

代码语言:javascript
复制
 WARNING: The scripts f2py, f2py3 and f2py3.6 are installed in '/home/yunswj/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

会出现这个问题,我来解决一下

吐了都,连个nano也没有

这个地址,就是上面报错的路径。自己写,别抄我的

代码语言:javascript
复制
export PATH=/home/yunswj/.local/bin/:$PATH
代码语言:javascript
复制
 source ~/.bashrc

之后在生效一下。

未来会使用zsh,那就是zshrc这个东西了,都一样的玩法,脑子活点。

没有错误了

代码语言:javascript
复制
pip install ipython

没有错误,很舒服哇

装pycuda这么红,这就出事了。。。

先装nvcc,其实不是没有,就是没写路径,真拉跨

先看自己的cuda多少版本的

一定是10.2的版本

然后写入自己的路径,因为上面还配置了一个,所以这个地方就有4个

自己手打就好好的细心点

代码语言:javascript
复制
export CUBA_HOME=/usr/local/cuda-10.2
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH
export PATH=/usr/local/cuda-10.2/bin:$PATH

我是用的nano,编辑后

CTRL+X,然后y,然后再打开这个文件看一下有没有写入,最后强制的写入

输入nvcc -V看看版本,就配置好了

先看看有没有下载的工具,有的哇

代码语言:javascript
复制
https://files.pythonhosted.org/packages/5e/3f/5658c38579b41866ba21ee1b5020b8225cec86fe717e4b1c5c972de0a33c/pycuda-2019.1.2.tar.gz

这个是比较老的,我们去GitHub上面找一下最新的

代码语言:javascript
复制
sudo -H pip install jetson-stats
代码语言:javascript
复制
pip install jetson-stats
代码语言:javascript
复制
sudo jtop

这个是超酷的资源显示器,emmmmm,报错我以后再看

此时分享一段代码,控制风扇的

140这个参数是PWM,0~255之间

GitHub还是打不开,我还是就用老版本吧,又不是不能用

代码语言:javascript
复制
wget https://files.pythonhosted.org/packages/5e/3f/5658c38579b41866ba21ee1b5020b8225cec86fe717e4b1c5c972de0a33c/pycuda-2019.1.2.tar.gz

下载成功

在桌面

代码语言:javascript
复制
tar zxvf pycuda-2019.1.2.tar.gz    
cd pycuda-2019.1.2/  
python3 configure.py --cuda-root=/usr/local/cuda-10.2
sudo python3 setup.py install

输入这些,别输入错误

稍等?一会儿吧,我耍手机去了,别管电脑就好

代码语言:javascript
复制
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
from pycuda.compiler import SourceModule


mod = SourceModule("""
#define BLOCK_SIZE 16

typedef struct {
    int width;
    int height;
    int stride; 
    int __padding;    //为了和64位的elements指针对齐
    float* elements;
} Matrix;

// 读取矩阵元素
__device__ float GetElement(const Matrix A, int row, int col)
{
    return A.elements[row * A.stride + col];
}

// 赋值矩阵元素
__device__ void SetElement(Matrix A, int row, int col, float value)
{
    A.elements[row * A.stride + col] = value;
}

// 获取 16x16 的子矩阵
 __device__ Matrix GetSubMatrix(Matrix A, int row, int col) 
{
    Matrix Asub;
    Asub.width    = BLOCK_SIZE;
    Asub.height   = BLOCK_SIZE;
    Asub.stride   = A.stride;
    Asub.elements = &A.elements[A.stride * BLOCK_SIZE * row + BLOCK_SIZE * col];
    return Asub;
}

__global__ void matrix_mul(Matrix *A, Matrix *B, Matrix *C)
{
    int blockRow = blockIdx.y;
    int blockCol = blockIdx.x;
    int row = threadIdx.y;
    int col = threadIdx.x;

    Matrix Csub = GetSubMatrix(*C, blockRow, blockCol);

    // 每个线程通过累加Cvalue计算Csub的一个值
    float Cvalue = 0;

    // 为了计算Csub遍历所有需要的Asub和Bsub
    for (int m = 0; m < (A->width / BLOCK_SIZE); ++m) 
    {
        Matrix Asub = GetSubMatrix(*A, blockRow, m);
        Matrix Bsub = GetSubMatrix(*B, m, blockCol);
 
        __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
        __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
       
        As[row][col] = GetElement(Asub, row, col);
        Bs[row][col] = GetElement(Bsub, row, col);

        __syncthreads();
        
        for (int e = 0; e < BLOCK_SIZE; ++e)
            Cvalue += As[row][e] * Bs[e][col];

        __syncthreads();
    }

    SetElement(Csub, row, col, Cvalue);
}
""")


class MatrixStruct(object):
    def __init__(self, array):
        self._cptr = None

        self.shape, self.dtype = array.shape, array.dtype
        self.width = np.int32(self.shape[1])
        self.height = np.int32(self.shape[0])
        self.stride = self.width
        self.elements = cuda.to_device(array)                      # 分配内存并拷贝数组数据至device,返回其地址

    def send_to_gpu(self):
        self._cptr = cuda.mem_alloc(self.nbytes())                 # 分配一个C结构体所占的内存
        cuda.memcpy_htod(int(self._cptr), self.width.tobytes())    # 拷贝数据至device,下同
        cuda.memcpy_htod(int(self._cptr)+4, self.height.tobytes())
        cuda.memcpy_htod(int(self._cptr)+8, self.stride.tobytes())
        cuda.memcpy_htod(int(self._cptr)+16, np.intp(int(self.elements)).tobytes())

    def get_from_gpu(self):
        return cuda.from_device(self.elements, self.shape, self.dtype)  # 从device取回数组数据
   
    def nbytes(self):
        return self.width.nbytes * 4 + np.intp(0).nbytes


a = np.random.randn(400,400).astype(np.float32)
b = np.random.randn(400,400).astype(np.float32)
c = np.zeros_like(a)

A = MatrixStruct(a)
B = MatrixStruct(b)
C = MatrixStruct(c)
A.send_to_gpu()
B.send_to_gpu()
C.send_to_gpu()

matrix_mul = mod.get_function("matrix_mul")
matrix_mul(A._cptr, B._cptr, C._cptr, block=(16,16,1), grid=(25,25))
result = C.get_from_gpu()
print(np.dot(a,b))
print(result)

这个是我们用的例子

用nano写入

这里附上一个简单的快捷键

出现了这个问题,emmmm,命运多舛呀

代码语言:javascript
复制
>>> import sys
>>> sys.path
['', '/usr/lib/python38.zip', 
'/usr/lib/python3.8', 
'/usr/lib/python3.8/lib-dynload', 
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages']

这些库在这里,我们的目标就是重新清理这个库,在此之前,机器先断电一次

重启就好了

把3.8卸载

直接加大数,别的不会整了

出现这个了,轻车熟路的操作一次

累了,玩不了了。。。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云深之无迹 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档