Numba and NumPy don't execute the following foo
function in the same way:
from numba import jit
import numpy as np
@jit
def foo(a):
a[:] = a[::-1] # reverse the array
a = np.array([0, 1, 2])
foo(a)
print(a)
With NumPy (without @jit
) it prints [2, 1, 0]
, while with Numba (with @jit
) it prints [2, 1, 2]
. It looks like Numba modifies the array in-place, which leads in data corruption. It is easy to work around by making a copy of the array:
a[:] = a[::-1].copy()
But is this the desired behavior? Shouldn't Numba and NumPy give the same result?
I am using Numba v0.26.0 in Python 3.5.2.
This is a known issue (https://github.com/numba/numba/issues/1960) and it was fixed in numba 0.27. Following NumPy behavior, the fix detects for overlap and makes temporary copies to avoid corrupting the data.