0

I need to optimize my code for my matrix multiplication program. When using @jit(nopython=True) for the same program I get 346 ms as the speed but I'm trying to get it faster by using Cuda.

I appreciate any help you can give me!

Here is the error and the code:

---> 19 @vectorize(['(float32, float32, float32)'], target='cuda') 
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function len>) found for signature:
 
>>> len(float32)
 
There are 16 candidate implementations:
      - Of which 16 did not match due to:
      Overload of function 'len': File: <numerous>: Line N/A.
        With argument(s): '(float32)':
       No match.
from numba import cuda, float32, prange

matrix1 = cp.random.uniform(1,10,size=(1000,1000), dtype=np.float64)
matrix2 = cp.random.uniform(1,10, size=(1000,1000), dtype=np.float64)
rmatrix = cp.zeros(shape=(1000,1000), dtype=np.float64)


#multiplication function


@vectorize(['(float32, float32, float32)'], target='cuda')
def gpu_matrix_multiplication(matrix1,matrix2,rmatrix):
  for i in prange(len(matrix1)):
    for j in prange(len(matrix2[0])):
      for k in prange(len(matrix2)):
        rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]

#Calculate running time                                                                                                             
%timeit gpu_matrix_multiplication(matrix1,matrix2,rmatrix)```

  • 1
    you are generally confused about the rules for usage of `vectorize`. It only works for elementwise operations, and the particular definition of "elementwise operations" here precludes the possibility of using it for an operation like matrix multiply. Likewise, this misunderstanding is leading directly to the error you cite. The `matrix1` item in the vectorize routine is *not a matrix*. You cannot index into it. It is a scalar. There are a number of other problems with your approach, but this lack of grasp of the basic concept is the most serious. – Robert Crovella Apr 30 '22 at 18:10
  • 1
    [here](https://numba.pydata.org/numba-doc/dev/cuda/examples.html) is a documented example of how to do matrix multiplication using numba/cuda. – Robert Crovella Apr 30 '22 at 18:11

0 Answers0