- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 NumbaPro 的 cuda 扩展来乘以大型数组矩阵。最后我想要的是将大小为 NxN 的矩阵乘以一个对角矩阵,该矩阵将作为一维矩阵输入(因此,我发现 a.dot(numpy.diagflat(b)) 与* b).但是,我收到一个不提供任何信息的断言错误。
如果我将两个一维数组矩阵相乘,我只能避免这个断言错误,但这不是我想要做的。
from numbapro import vectorize, cuda
from numba import f4,f8
import numpy as np
def generate_input(n):
import numpy as np
A = np.array(np.random.sample((n,n)))
B = np.array(np.random.sample(n) + 10)
return A, B
def product(a, b):
return a * b
def main():
cu_product = vectorize([f4(f4, f4), f8(f8, f8)], target='gpu')(product)
N = 1000
A, B = generate_input(N)
D = np.empty(A.shape)
stream = cuda.stream()
with stream.auto_synchronize():
dA = cuda.to_device(A, stream)
dB = cuda.to_device(B, stream)
dD = cuda.to_device(D, stream, copy=False)
cu_product(dA, dB, out=dD, stream=stream)
dD.to_host(stream)
if __name__ == '__main__':
main()
这是我的终端输出的内容:
Traceback (most recent call last):
File "cuda_vectorize.py", line 32, in <module>
main()
File "cuda_vectorize.py", line 28, in main
cu_product(dA, dB, out=dD, stream=stream)
File "/opt/anaconda1anaconda2anaconda3/lib/python2.7/site-packages/numbapro/_cudadispatch.py", line 109, in __call__
File "/opt/anaconda1anaconda2anaconda3/lib/python2.7/site-packages/numbapro/_cudadispatch.py", line 191, in _arguments_requirement
AssertionError
最佳答案
问题是您正在对采用非标量参数的函数使用 vectorize
。 NumbaPro 的vectorize
的想法是,它将标量函数作为输入,并生成一个将标量运算并行应用于向量的所有元素的函数。请参阅 NumbaPro documentation。
你的函数接受一个矩阵和一个向量,它们绝对不是标量。 [编辑] 您可以使用 NumbaPro 的 cuBLAS 包装器或编写您自己的简单内核函数在 GPU 上做您想做的事。这是一个演示两者的示例。注意将需要 NumbaPro 0.12.2 或更高版本(在此编辑时刚刚发布)。
from numbapro import jit, cuda
from numba import float32
import numbapro.cudalib.cublas as cublas
import numpy as np
from timeit import default_timer as timer
def generate_input(n):
A = np.array(np.random.sample((n,n)), dtype=np.float32)
B = np.array(np.random.sample(n), dtype=A.dtype)
return A, B
@cuda.jit(argtypes=[float32[:,:], float32[:,:], float32[:]])
def diagproduct(c, a, b):
startX, startY = cuda.grid(2)
gridX = cuda.gridDim.x * cuda.blockDim.x;
gridY = cuda.gridDim.y * cuda.blockDim.y;
height, width = c.shape
for y in range(startY, height, gridY):
for x in range(startX, width, gridX):
c[y, x] = a[y, x] * b[x]
def main():
N = 1000
A, B = generate_input(N)
D = np.empty(A.shape, dtype=A.dtype)
E = np.zeros(A.shape, dtype=A.dtype)
F = np.empty(A.shape, dtype=A.dtype)
start = timer()
E = np.dot(A, np.diag(B))
numpy_time = timer() - start
blas = cublas.api.Blas()
start = timer()
blas.gemm('N', 'N', N, N, N, 1.0, np.diag(B), A, 0.0, D)
cublas_time = timer() - start
diff = np.abs(D-E)
print("Maximum CUBLAS error %f" % np.max(diff))
blockdim = (32, 8)
griddim = (16, 16)
start = timer()
dA = cuda.to_device(A)
dB = cuda.to_device(B)
dF = cuda.to_device(F, copy=False)
diagproduct[griddim, blockdim](dF, dA, dB)
dF.to_host()
cuda_time = timer() - start
diff = np.abs(F-E)
print("Maximum CUDA error %f" % np.max(diff))
print("Numpy took %f seconds" % numpy_time)
print("CUBLAS took %f seconds, %0.2fx speedup" % (cublas_time, numpy_time / cublas_time))
print("CUDA JIT took %f seconds, %0.2fx speedup" % (cuda_time, numpy_time / cuda_time))
if __name__ == '__main__':
main()
内核明显更快,因为 SGEMM 执行全矩阵乘法 (O(n^3)),并将对角线扩展为全矩阵。 diagproduct
函数更智能。它只是对每个矩阵元素进行一次乘法运算,而不会将对角线扩展为完整矩阵。以下是 N=1000 在我的 NVIDIA Tesla K20c GPU 上的结果:
Maximum CUBLAS error 0.000000
Maximum CUDA error 0.000000
Numpy took 0.024535 seconds
CUBLAS took 0.010345 seconds, 2.37x speedup
CUDA JIT took 0.004857 seconds, 5.05x speedup
时间包括进出 GPU 的所有副本,这对于小型矩阵来说是一个严重的瓶颈。如果我们将 N 设置为 10,000 并再次运行,我们将获得更大的加速:
Maximum CUBLAS error 0.000000
Maximum CUDA error 0.000000
Numpy took 7.245677 seconds
CUBLAS took 1.371524 seconds, 5.28x speedup
CUDA JIT took 0.264598 seconds, 27.38x speedup
然而,对于非常小的矩阵,CUBLAS SGEMM 具有优化路径,因此更接近 CUDA 性能。这里,N=100
Maximum CUBLAS error 0.000000
Maximum CUDA error 0.000000
Numpy took 0.006876 seconds
CUBLAS took 0.001425 seconds, 4.83x speedup
CUDA JIT took 0.001313 seconds, 5.24x speedup
关于python - Anaconda 的 NumbaPro CUDA 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153985/
我正在使用以下脚本以静默模式在机器上安装(重新安装)最新的 anaconda。它已经运行了很长时间,成功安装了anaconda。然而,它最近坏了,现在不是安装所有的 anaconda 包,而是最终安装
我最近运行了 conda update --all,现在每次我打开一个新终端时,我都会在顶部看到这些错误行: ERROR: This cross-compiler package contains n
如果你这样做 conda info --envs您将获得在 anaconda 中创建的环境列表 有没有办法做到这一点,但还要获得这些环境的创建日期? 最佳答案 envs=`conda env list
我的电脑:Mac Book Pro、OS X Yosemite 我正在尝试使用以下命令卸载 anaconda。 rm -rf ~/anaconda 但是,我收到了很多行“权限被拒绝”的信息,看起来像这
我尝试安装 GraphLab Create,因此我创建了一个新的 Anaconda 环境,但它显示缺少依赖项。我不知道如何删除它。 终端: C:\Users\PC>conda create -n gl
我最近通过使用以下命令中断了Anaconda Navigator的安装: conda update --all -y 显然,这并不少见,这是重新安装Anaconda并从头开始创建新环境的好借口。 但是
我想更新我当前使用的所有包。进入 anaconda 提示符并输入 conda update --all 返回消息“所有请求的包都已安装”。事实并非如此,因为我的包裹中几乎有一半已经过时了。我知道 an
我使用的是 64 位 Windows 10 机器。我试图按照 this DataCamp 中提到的步骤安装 Anaconda页。我期待一个 .exe 文件,相反,我得到了一个 .pkg 文件。我不知道
我收到此错误: C:\Users>conda install pytorch torchvision -c soumith Fetching package metadata ............
我需要从 Windows 10 笔记本电脑上卸载 Anaconda 中的 RStudio,但无法使其正常工作。以下是原因以及我迄今为止所做的尝试。 尝试从 Anaconda 启动 RStudio De
我通过 Anaconda 使用 Rstudio 一段时间了,一切都很顺利。但是,在没有任何先前问题的情况下,当我尝试运行 Rstudio 时,会弹出一个导航窗口,询问保存下载文件的目录 我只是尝试选择
与这个问题部分相关 [现在从 SO 中删除] 但现在只专注于在 Anaconda 中安装 Anaconda 目录中不存在的包 here .一个例子可以是 COIN-OR CLP 包 here在 R 中
只是想知道更新 Anaconda 和 Conda 安装和虚拟环境的正确方法是什么。这是我一步一步的困惑: 当我运行命令时 conda update anaconda ,它更新/降级了很多包。 然后我跑
我想我需要在他们最近的许可证更改后获得 Anaconda 的商业许可证。我很好奇,访问 conda-forge 存储库是否需要商业许可证,因为它是一个社区存储库?还是仅适用于主要的 anaconda
显然,Anaconda 有一个不同的金字塔包,它是针对 Web 框架的。 https://anaconda.org/anaconda/pyramid arima Pyramid 的用户指南建议使用 p
我关注了以下instructions安装 Jupyter 笔记本(单用户模式)。 但是,我实际上需要安装多用户(Jupyter-hub)应用程序。 如何卸载此应用程序? 仅供引用 - 使用 Cento
我最近尝试为我的 Windows 10 笔记本电脑安装 Anaconda。不幸的是,这似乎不起作用,因为我的计算机上似乎没有实际的 Anaconda 应用程序,而只是桌面上的文件夹和文件的集合。我认为
我的机器上安装了 Anaconda 3.5 版,但我决定卸载它(通过控制面板)并下载 2.7 版。我正在使用 Windows 7。 但是,在安装快要结束时我收到一条错误消息,我收到一个弹出窗口说:无法
我最近重置了我的笔记本电脑并在其中安装了 anaconda,我尝试打开 jupyter 实验室但它给了我错误 Error executing Jupyter command 'lab': [WinEr
我使用 Anaconda Navigator 从不同环境启动 Jupyter。 Jupyter 笔记本的默认目录(在 Jupyter 启动后显示)是我的主目录(在 Windows 7 中)。 是否可以
我是一名优秀的程序员,十分优秀!