Search code examples
pythonnumba

Process finished with exit code -1073741819 (0xC0000005) with numba.jit


I am running a python script using jit compilation for a function with 3 nested for loops ON WINDOWS (The problem described in the following only appears on WINDOWS, not when run on LINUX!). Minimum working example:

import numpy as np
from numba import jit

@jit(nopython=True)
def compute_loop(a, b, c):
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            S0 = b[i, j]
            for k in range(1, 5):
                print('Entered last loop')
                if b[i + k, j + k] / S0 < 0.05:
                    c[i, j] = np.sum(S0 * b[i:i + k, j:j + k])
                    break
                if k == 4:
                    c[i, j] = np.sum(S0 * b[i:i + k, j:j + k])
    print('End')
    return c

def main():
    size_i = 261
    size_j = 510
    print(size_i*size_j)
    a = np.random.rand(size_i, size_j)
    b = np.random.rand(size_i, size_j)
    c = np.zeros((size_i, size_j))
    compute_loop(a, b, c)


if __name__ == '__main__':
    main()

When I run this with size_i>260, the script interrupts with the exit code Process finished with exit code -1073741819 (0xC0000005) without any further error messages or traceback. The exit happens before exiting the last for loop, meaning it will print 'Entered last for loop' many times but eventually just yield the exit code without warning and without ever reaching the return statement (or for that matter the print command 'End').

The code runs fine when size_i<=260 OR when I use the function without jit OR when I run it on LINUX!

So it seems to depend on the matrix size of the input matrices, whether or not this works. Is still do not understand how to fix it though...

Python Versions I tried:

  • 3.7.5 on system wide installation but within a virtual environment with numba 0.54.1 and numpy 1.20.3.
  • 3.7.7 (on a different PC) with a user installation and virtual environment and same version of numba and numpy Either way leads to the same exit code without error/traceback.

What else I tried:

  • Running in debug/not debug mode makes no difference either.
  • Running the script from powershell and cmd as admin but it crashes (after reaching the same location in the code) without any error/exit code displayed.

Previous posts: I found a few posts with the same exit code, but none of them is related to numba. So far I am clueless. If anyone has any suggestion?


Solution

  • There is an out of bounds on b[i + k, j + k]. This is visible without Numba. Indeed, Numpy (version 1.20.3) explicitly reports the error:

    IndexError: index 510 is out of bounds for axis 1 with size 510
    

    Numba assumes the indices are valid, does not reports such errors and can crash if they are not valid (since checking indices is expensive). Note that you can use assertions to check indices yourself (using assert).