Pawel
Pawel

Reputation: 41

Numbapro: No speed-up for Matrix Multiplication

For last couple of days I've been trying to understand why Numbapro (Accelerate from Continuum Analytics, Inc.; I'm running a 30day trial version) does not accelerate on my MacBook Pro (Intel Core i7, 2.6GHz, 16GB RAM with NVIDIA GeForce GT 650M, 1GB on PCI bus).

I took one of the examples from the codes for (NxM)x(MxN) matrix multiplication where Continuum Analytics, Inc. claims acceleration of computation via CUDA and I compared the times between CUDA.JIT and numpy. My idea is to run e.g 1e4 iterations and matrix B is randomised every iteration. Below the following code I used, I quote times I obtained. Is there any solution for that? Thanks!

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))

results in:

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

Upvotes: 2

Views: 1096

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151972

This is a completely naive matrix multiplication routine on the GPU, whereas the numpy routine, being effectively a library call:

X=np.dot(A,B)

is likely to be highly optimized. I'm impressed that the GPU is faster at all.

The "solution" would be to make a call to CUBLAS for the matrix mutliplication, rather than writing your own kernel.

Upvotes: 3

Related Questions