Error while adding values in jited function

I am new to numba and used it worked quite well so far in speeding up some of my code. But i have hit a dead end in a naive implementation of a vektor-matrix multiplikation.
I have the matrix A, the vector b and the Produkt Ab. But everytime i try to add the product
of the matrix A and vector b to a temporary variable, i get the same error:

raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function() found for signature:

iadd(float64, array(float64, 1d, C))

There are 16 candidate implementations:

  • Of which 14 did not match due to:
    Overload of function ‘iadd’: File: : Line N/A.
    With argument(s): ‘(float64, array(float64, 1d, C))’:
    No match.
  • Of which 2 did not match due to:
    Operator Overload in function ‘iadd’: File: unknown: Line unknown.
    With argument(s): ‘(float64, array(float64, 1d, C))’:
    No match for registered cases:
    • (int64, int64) → int64
    • (int64, uint64) → int64
    • (uint64, int64) → int64
    • (uint64, uint64) → uint64
    • (float32, float32) → float32
    • (float64, float64) → float64
    • (complex64, complex64) → complex64
    • (complex128, complex128) → complex128

File “src/code/eigenvector-centrality/eigenvector_numba.py”, line 46:
def mat_vector_mul(A, b, Ab):

for j in numba.prange(A.shape[1]):
tmp += A[i, j] * b[i]

Code:

@numba.njit(parallel = True)
def mat_vector_mul(A, b, Ab):
tmp = 0.0
for i in numba.prange(b.shape[0]):
for j in numba.prange(A.shape[1]):
tmp += A[i, j] * b[i]
Ab[i] = tmp

I am pretty sure i am overlooking something absolutely obvious, but cannot get a hold of it. Any suggestions or help is highly appreciated! Thanks!

In general it’s best to include a complete runnable sample of the code that produces the issue, otherwise it can be difficult for people to replicate and understand the problem. I think you had some mix up with indices - the following, based on your code, executes without errors for me:

import numba
import numpy as np

np.random.seed(1)


@numba.njit(parallel=False)
def mat_vector_mul(A, b, Ab):
    for i in numba.prange(A.shape[0]):
        tmp = 0.0
        for j in numba.prange(b.shape[0]):
            tmp += A[i, j] * b[j]
        Ab[i] = tmp


A = np.random.rand(2, 3)
b = np.random.rand(3)
Ab = np.zeros(A.shape[0])

print(A)
print(b)

mat_vector_mul(A, b, Ab)

np.testing.assert_allclose(A @ b, Ab)

print(A @ b)

producing:

[[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]
[0.18626021 0.34556073 0.39676747]
[0.32663584 0.14366255]

In general, if you can paste the whole runnable code when seeking advice, then it will help others to diagnose the issue (when you post code, it needs to be enclosed in backticks so that it formats properly - see How to post code or preformatted text - tips & tricks - Discourse Meta).

Also, you can check if your code runs without the @numba.njit decorator - if you do that, do you get an error from Python that indicates an issue?