0

Say we have this snippet that runs error-free:

@numba.njit()
def run(rows, columns):

    for row_index, Re in enumerate(numpy.linspace(-2, 1, rows)):
        for column_index, Im in enumerate(numpy.linspace(-1, 1, columns)):
            value = mandelbrot(Re, Im, 200)

Where mandelbrot is a function that returns a value that we then want stored in a 2D numpy array:

@numba.njit()
def run(rows, columns):
    result = numpy.zeros([rows, columns])
    
    for row_index, Re in enumerate(numpy.linspace(-2, 1, rows)):
        for column_index, Im in enumerate(numpy.linspace(-1, 1, columns)):
            result[row_index, column_index] = mandelbrot(Re, Im, 200)

    return result

This gives the following No implementation of function Function(<built-in function setitem>) found for signature: setitem(readonly array(float64, 2d, C), UniTuple(int64 x 2), int64)

It seems like numba does not enjoy assigning items. As suggested in this answer we can try using array.ravel(). However it does not help the situation, error remains unchanged.

            result.ravel()[row_index, column_index] = mandelbrot(Re, Im, 200)

That being said, I would like to know if there is a way for numba to ignore this line completely, and run in pure python.

iustin
  • 66
  • 10
  • 1
    I can't reproduce the problem (Python 3.8, Numba 0.53.1). It works just fine using a dummy `mandelbrot()` and replacing `numpy.zeros([rows, columns])` (which produces a different error) by `numpy.zeros((rows, columns))`, or better `numpy.empty((rows, columns))`. – aerobiomat Jun 22 '21 at 10:24
  • thanks, it apparently solves the problem easily, i don't know why `np.zeros([rows, cols])` hah. Do you know if i could keep a tqdm progress bar open too? – iustin Jun 22 '21 at 10:58
  • 1
    User interface operations in a JITted function may not be the best Idea. You could split your function to work by rows or other chunks. You can also [vectorize](https://stackoverflow.com/a/66941893/2148416) and [parallelize](https://stackoverflow.com/a/67505968/2148416) it... – aerobiomat Jun 22 '21 at 12:03

0 Answers0