1. cudamat简介
cudamat是一个python语言下,利用NVIDIA的cuda sdk 进行矩阵运算加速的库。对于不熟悉cuda编程的程序员来说,这是一个非常方便的GPU加速方案。很多工程和项目中都使用了cudamat,例如gnumpy,deepnet等。
2. 安装
cudamat的github地址如下:https://github.com/cudamat/cudamat。
下载完成后,运行命令 python setup.py install来进行安装。
windows下安装需要将“cl.exe”加入path之中,另外会提示你安装vc的python编译器,依照提示下载安装即可。
3. 基本矩阵运算
import numpy as np
import cudamat as cm
cm.cublas_init()
# create two random matrices and copy them to the GPU
a = cm.CUDAMatrix(np.random.rand(32, 256))
b = cm.CUDAMatrix(np.random.rand(256, 32))
# perform calculations on the GPU
c = cm.dot(a, b)
d = c.sum(axis = 0)
# copy d back to the host (CPU) and print
print(d.asarray())
如以上代码所示,cudamat的基本使用方法是利用cm.CUDAMatrix(A)来讲矩阵A转换成GPU里的矩阵,进而进行各种运算。cudamat提供了多种矩阵运算的接口&#