【问题标题】:Numbapro: No speed-up for Matrix MultiplicationNumbapro:矩阵乘法没有加速
【发布时间】:2014-10-25 23:09:06
【问题描述】:

过去几天,我一直试图了解为什么 Numbapro(来自 Continuum Analytics, Inc. 的加速;我正在运行 30 天试用版)在我的 MacBook Pro(Intel Core i7,2.6GHz, 16GB RAM,NVIDIA GeForce GT 650M,1GB PCI 总线)。

我从 (NxM)x(MxN) 矩阵乘法的代码中举了一个例子,其中 Continuum Analytics, Inc. 声称通过 CUDA 加速计算,我比较了 CUDA.JIT 和 numpy 之间的时间。我的想法是运行例如 1e4 iterations 并且矩阵 B 每次迭代都是随机的。在我使用的以下代码下方,我引用了我获得的时间。有什么解决办法吗?谢谢!

from numbapro import *
from numba import *
import numpy as np
import math
from timeit import default_timer as timer

m=1000
n=1000
A = np.array(np.random.random((n,m)), dtype=np.float32)
C = np.empty([n,n])

iterations = 10000

start = timer()
for i in range(iterations):
    B = np.array(np.random.random((m,n)), dtype=np.float32)
    X=np.dot(A,B)
numpy_time=(timer() - start)

@cuda.jit(void(float32[:,:],float32[:,:],float32[:,:]))
def cu_square_matrix_mul(A, B, C):

    tx = cuda.threadIdx.x
    ty = cuda.threadIdx.y
    bx = cuda.blockIdx.x
    by = cuda.blockIdx.y
    bw = cuda.blockDim.x
    bh = cuda.blockDim.y
    x = tx + bx * bw
    y = ty + by * bh
    n = C.shape[0]

    if x >= n or y >= n:
        return

    cs = 0
    for i in range(n):
        cs += A[y,i]*B[i,x]
    C[y,x]= cs

    cuda.syncthreads()

blockdim = 256,3
griddim = 10,3

stream = cuda.stream()
dA = cuda.to_device(A, stream)
dC = cuda.to_device(C, stream)

start = timer()    
for i in range(iterations):
    B = np.array(np.random.random((m,n)), dtype=np.float32)
    dB = cuda.to_device(B, stream)
    cu_square_matrix_mul[griddim,blockdim,stream](dA, dB, dC) 
    dC.to_host()
    stream.synchronize()
cuda_time = (timer() - start)    

print
print("Numpy took    %f seconds" % numpy_time)
print("CUDA JIT took %f seconds, %.5fx speedup" % (cuda_time, numpy_time / cuda_time))

结果:

Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days
Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days
Vendor:  Continuum Analytics, Inc.
Package: numbapro
Message: trial mode expires in 30 days

Numpy took    378.328881 seconds
CUDA JIT took 342.723757 seconds, 1.10389x speedup

【问题讨论】:

  • 总共有多少时间花在 (1) 随机矩阵的生成 B[] (2) CPU->GPU->CPU 之间的数据传输上?我对numpy 不熟悉,但对stream.synchronize() 的调用表明该代码正在积极抑制GPU 和CPU 工作之间的重叠,以及从/到GPU 和内核执行的数据复制之间的重叠,即一切都完全同步执行。
  • from numbapro import * 导入的名称被下一次导入from numba import * 覆盖。这是故意的吗?

标签: python numpy cuda matrix-multiplication


【解决方案1】:

这是一个在 GPU 上完全简单的矩阵乘法例程,而 numpy 例程实际上是一个库调用:

X=np.dot(A,B)

可能会进行高度优化。 GPU 速度更快,这让我印象深刻。

“解决方案”是 make a call to CUBLAS 进行矩阵乘法,而不是编写自己的内核。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2017-01-25
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多