gota
gota

Reputation: 2659

Working and manipulating numpy arrays with numba

Why can't Numba's jit compile a simple Numpy array operation?

Here is a minimal non-working example that reproduces Numba's failure to compile

import numpy as np
from numba import jit

rows = 10
columns = 999999
A = np.empty((rows, columns))
b = np.linspace(0, 1, num=rows)

@jit(nopython=True)
def replicate(A, b):
    for i in range(A.shape[1]):
        A[:, i] = b
    return A #optional

replicate(a, b)

With the following error:

TypingError: Failed at nopython (nopython frontend)
Cannot resolve setitem: array(float64, 1d, C, nonconst)[(slice3_type, int64)] = array(float64, 1d, C, nonconst)
File "<ipython-input-32-db24fbe2922f>", line 12

Am I doing something wrong?

As an aside, I need nopython mode because in my real-life situation I need to perform array addition, multiplication with scalars and array populating with other arrays frequently. and my understanding is that in object mode I won't be able to do loop jitting and thus I won't see any real performance boost on the execution.

Upvotes: 4

Views: 3183

Answers (1)

JoshAdel
JoshAdel

Reputation: 68682

Numba doesn't support numpy slicing in nopython mode. Try unrolling the loops explicitly:

rows = 10
columns = 999999
a = np.empty((rows, columns))
b = np.linspace(0, 1, num=rows)

@jit(nopython=True)
def replicate(A, b):
    for i in xrange(A.shape[0]):
        for j in xrange(A.shape[1]):
            A[i, j] = b[i]
    return A #optional

replicate(a, b)

Upvotes: 2

Related Questions