Example: using Numba for matrix-matrix multiplication
import numpy as np
import numba
@numba.guvectorize([numba.void(numba.float64[:,:], numba.float64[:,:], numba.float64[:,:])], '(m,l),(l,n)->(m,n)', target='cuda')
def matmul_numba_gpu(A, B, C):
for i in range(A.shape[0]):
for j in range(B.shape[1]):
tmp = 0.0
for k in range(B.shape[0]):
tmp += A[i, k] * B[k, j]
C[i,j] += tmp
- Complexity and power:
@vectorize
is simpler and designed for element-by-element operations. @guvectorize
is more powerful and flexible, designed for operations that span multiple elements and possibly multiple dimensions.
- Use cases: Use
@vectorize
for simple, scalar output operations and @guvectorize
for more complex array operations that might involve slicing, dicing, or reductions across dimensions.