Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numba could not report the error of out of array in njit? #8914

Closed
chen-erqi opened this issue Apr 24, 2023 · 6 comments
Closed

Numba could not report the error of out of array in njit? #8914

chen-erqi opened this issue Apr 24, 2023 · 6 comments
Labels
question Notes an issue as a question

Comments

@chen-erqi
Copy link

My code is here:

@numba.njit
def f():
    a = np.zeros(10)
    print(a[10])

f()

this program can work and print "1.3351156154062717e-306"

the numba version is 0.54.0
python version is 3.8.10
numpy version is 1.20.3

@gmarkall
Copy link
Member

An array with 10 elements has indices 0-9 - if you print the element at index 10 this is a value beyond the end of the array. You can catch errors like this with the boundscheck kwarg to the @njit decorator:

import numba
import numpy as np

@numba.njit(boundscheck=True)
def f():
    a = np.zeros(10)
    print(a[10])

f()

prints:

$ python repro.py 
debug: IndexError: index 10 is out of bounds for axis 0 with size 10
Traceback (most recent call last):
  File "/home/gmarkall/numbadev/issues/8914/repro.py", line 10, in <module>
    f()
IndexError: index is out of bounds

Generally boundscheck=True is slower than running without it, so it would usually only be used for debugging purposes.

@gmarkall gmarkall added the question Notes an issue as a question label Apr 24, 2023
@esc
Copy link
Member

esc commented Apr 24, 2023

Generally boundscheck=True is slower than running without it, so it would usually only be used for debugging purposes.

Indeed, the relevant docs are here: https://numba.pydata.org/numba-doc/dev/reference/pysemantics.html?highlight=boundscheck#bounds-checking

@chen-erqi
Copy link
Author

Generally boundscheck=True is slower than running without it, so it would usually only be used for debugging purposes.

Indeed, the relevant docs are here: https://numba.pydata.org/numba-doc/dev/reference/pysemantics.html?highlight=boundscheck#bounds-checking

thanks! I learn a lot from the docs.

@chen-erqi
Copy link
Author

An array with 10 elements has indices 0-9 - if you print the element at index 10 this is a value beyond the end of the array. You can catch errors like this with the boundscheck kwarg to the @njit decorator:

import numba
import numpy as np

@numba.njit(boundscheck=True)
def f():
    a = np.zeros(10)
    print(a[10])

f()

prints:

$ python repro.py 
debug: IndexError: index 10 is out of bounds for axis 0 with size 10
Traceback (most recent call last):
  File "/home/gmarkall/numbadev/issues/8914/repro.py", line 10, in <module>
    f()
IndexError: index is out of bounds

Generally boundscheck=True is slower than running without it, so it would usually only be used for debugging purposes.

thanks a lot.
Futhermore, could you please explain why numba is not design with a boundary check, which seems like a fundmental work of coding.

@gmarkall
Copy link
Member

Futhermore, could you please explain why numba is not design with a boundary check, which seems like a fundmental work of coding.

Because it's faster to omit the boundscheck - it's not designed without it, it's just that you have to opt-in to it.

@chen-erqi
Copy link
Author

thanks a lot!
I learn a lot from these setting and will consider how to apply it to accelerate my structure of evolutionary algorithm.
In addition, I sincerely thanks all members of numba, because of numba, i can accelerate my code and finally my papar is accepted by a journal with if=8.653.
repect to you all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Notes an issue as a question
Projects
None yet
Development

No branches or pull requests

3 participants