我试图用anaconda加速快速计算矩阵.我从非常基本的例子开始:乘以2个矩阵.
我的目标是以某种方式获得GPU倍增,这比通常的numpy.dot更好
这是我的基本示例,基于此documentation.
from numbapro import guvectorize
from numpy import arange
@guvectorize(['void(float32[:,:], float32[:,:], float32[:,:])'], '(m,n),(n,p)->(m,p)', target='gpu')
def matmul(A, B, C):
m, n = A.shape
n, p = B.shape
for i in range(m):
for j in range(p):
C[i, j] = 0
for k in range(n):
C[i, j] += A[i, k] * B[k, j]
import numpy as np
import time
for dim in [50, 100, 200]:
rnd = np.random.RandomState(0)
a = rnd.rand(dim, dim).astype(np.float32)
b = rnd.rand(dim, dim).astype(np.float32)
resgpu = np.zeros_like(a)
start = time.time()
rescpu = np.dot(a, b)
print('CPU:', time.time() - start)
start = time.time()
resgpu = matmul(a, b)
print('GPU:', time.time() - start)
print(np.allclose(rescpu, resgpu))
print(np.allclose(resgpu, rescpu))
结果太糟糕了:GPU比CPU慢得多
CPU: 0.00011801719665527344
GPU: 0.05677294731140137
True
True
CPU: 0.00011205673217773438
GPU: 0.3881375789642334
True
True
CPU: 0.00038933753967285156
GPU: 3.018171787261963
True
True
当然我理解内部的numpy实现已经很好地优化了,但我希望anaconda官方的例子是好的.我使用python 3.4.3并使用这两个帮助库有错误:http://www.cs.toronto.edu/~tijmen/gnumpy.html和https://github.com/rctn/gpupy
我应该说用gpupy我在python 2.7上成功加速了.
所以我的问题是:如何通过GPU使用矩阵乘法比numpy-CPU更好? anaconda官方示例有什么问题,如果有一个python3工作库允许以numpy方式使用GPU?
===
结果
不幸的是,python 3没有简单而好的方法,而是使用2.7
感谢@rth for recommendedint awesome library scikits.cuda
一些基准测试(使用anaconda mkl测试,所以numpy也很快)
dim = 10000
rnd = np.random.RandomState(0)
a = rnd.rand(dim, dim).astype(np.float32)
b = rnd.rand(dim, dim).astype(np.float32)
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)
start = time.time()
rescpu = np.dot(a, b)
print 'CPU:', time.time() - start
start = time.time()
resgpu = culinalg.dot(a_gpu, b_gpu)
print 'GPU:', time.time() - start
resgpu = resgpu.get()
print np.allclose(rescpu, resgpu)
print np.allclose(resgpu, rescpu)
结果
CPU: 16.4765479565
GPU: 0.000520944595337