jetson nano安装pycuda

jetson nano安装pycudaJetPack4.4版本使用之前配置cuda的环境$sudonano~/.bashrcexportPATH=/usr/local/cuda-10.2/bin:$PATHexportLD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATHexportCUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.2$sudosource~/.bashrc$nvcc-V检测一下是否配置成功之后下载[p

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

JetPack4.4版本

配置cuda的环境

$ sudo nano ~/.bashrc
export PATH=/usr/local/cuda-10.2/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.2
$ sudo source ~/.bashrc
$ nvcc -V 检测一下是否配置成功

Jetbrains全家桶1年46,售后保障稳定

安装pycuda-2019

之后下载[pycuda-2019.1.2]

下载完之后解压
进入解压出来的文件

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

出现这个就说明正在编译文件安装,等待一段时间后即可安装完成。
在这里插入图片描述
安装完出现:
在这里插入图片描述
就表明安装成功了。

但是使用的时候还得配置一下一些必要的东西不然会报错:?*

FileNotFoundError: [Errno 2] No such file or directory: ‘nvcc’

将nvcc的完整路径硬编码到Pycuda的compiler.py文件中的compile_plain()
中,大约在第 73 行的位置中加入下面段代码!

nvcc = '/usr/local/cuda/bin/'+nvcc

在这里插入图片描述

更新JetPack4.6版本

4.6版本也是cuda10.2版本的,cuda配置环境都一样

安装pycuda-2021

源码也可下载【pycuda-2021】
这是pycuda的github地址:https://github.com/inducer/pycuda

测试pycuda是否安装正确的时候会报错

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import pycuda.autoinit
  File "/usr/local/lib/python3.6/dist-packages/pycuda-2021.1-py3.6-linux-aarch64.egg/pycuda/autoinit.py", line 7, in <module>
    from pycuda.tools import make_default_context  # noqa: E402
  File "/usr/local/lib/python3.6/dist-packages/pycuda-2021.1-py3.6-linux-aarch64.egg/pycuda/tools.py", line 33, in <module>
    from pycuda.compyte.dtypes import (  # noqa: F401
ModuleNotFoundError: No module named 'pycuda.compyte'

解决方案

官方解决方案【链接
不想去看的话,直接下载这个链接的源码,同下步骤进行安装即可
https://pypi.org/project/pycuda/#files

tar zxvf pycuda-2021.1.tar.gz    
cd pycuda-2021.1/  
python3 configure.py --cuda-root=/usr/local/cuda-10.2
sudo python3 setup.py install

测试dome

接下来写个矩阵运算的小demo来测试是否能真正运行:

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)

出现下面矩阵运算的结果即可说明在jetson nano上安装的pycuda成功了,之后就可以配合tensorrt使用啦!
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203655.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从开发者角度玩Windows 11

    从开发者角度玩Windows 11今天Windows11正式发布,有新的界面,有新的WindowsStore,也有新的交互,相信不少小伙伴都已经安装了Windows11或从各大媒体了解到相关的信息。作为开发者,或者你和我一样更关注Windows11给开发者带来了什么新的体验和提升。一.安装Windows11依赖于TPM2.0,什么是TPM呢?TPM技术旨在提供基于硬件的与安全性相关的功能。TPM芯片是一个安全的加密处理器,有助于执行生成、存储和限制加密密钥的使用等操作。TPM芯片包含多重物理安…

    2022年5月6日
    122
  • Python学生信息管理系统课程设计报告_python做的项目管理系统

    Python学生信息管理系统课程设计报告_python做的项目管理系统1.本人第一次学python做出来的,当时满满的成就感,当作纪念!!!!!非常简单,复制即可使用代码块importjson#把字符串类型的数据转换成Python基本数据类型或者将Python基本数据类型转换成字符串类型。deflogin_user():whileTrue:register=input(‘学生姓名:’)try:…

    2022年10月10日
    3
  • oracle创建用户并授权

    一、创建用户登录到system用户以创建其他用户创建的:createuserusernameidentifiedbypassword;二、授权在这里插入代码片

    2022年4月3日
    459
  • 数电和模电的理解「建议收藏」

    数电和模电的理解「建议收藏」模电模拟信号:随处可见的自然信号都是模拟信号,模拟信号在时间上和取值上都是连续的,画出来就是一条连续的曲线,可以完全地“模拟”自然信号。模电是指用来对模拟信号进行传输、变换、处理、放大、测量和显示等工作的电路。模拟信号是指连续变化的电信号。模拟电路是电子电路的基础,它主要包括放大电路、信号运算和处理电路、振荡电路、调制和解调电路及电源等。数电数字信号:在时间上和取值上都是不连续的。数字信号存在“采样”,数字信号的值只能在采样点变化。数字信号存在“量化”,数字信号的值只

    2022年6月20日
    47
  • Linux下如何解压.zip和.rar文件[通俗易懂]

    Linux下如何解压.zip和.rar文件[通俗易懂]Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们:1)对于ziplinux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序。它们的参数选项很多,这里只做简单介绍,举例说明一下其用法:#zipall.zip*.jpg这条命令是将所有.jpg的文件压缩成一个zip包#un…

    2022年5月23日
    36
  • centos系统安装git

    centos系统安装gitcentos安装git

    2025年5月24日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号