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

Typing jit signature of readonly arrays inputs #4511

Closed
2 tasks done
mroeschke opened this issue Sep 3, 2019 · 3 comments
Closed
2 tasks done

Typing jit signature of readonly arrays inputs #4511

mroeschke opened this issue Sep 3, 2019 · 3 comments
Labels
no action required No action was needed to resolve. question Notes an issue as a question

Comments

@mroeschke
Copy link

Reporting a bug

Is there a way to type the signature of readonly arrays? I did not find a way to specify readonly on the documentation page or StackOverflow (or maybe specifying array should work on readonly arrays as well?).

In [1]: import numba

In [2]: arr = np.array([1])

In [3]: arr.setflags(write=False)

In [4]: @numba.jit((numba.int64[:],))
   ...: def f(x):
   ...:     return x
   ...:

In [5]: f(arr)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-0e64fe0f2f72> in <module>
----> 1 f(arr)

/lib/python3.7/site-packages/numba/dispatcher.py in _explain_matching_error(self, *args, **kws)
    498         msg = ("No matching definition for argument type(s) %s"
    499                % ', '.join(map(str, args)))
--> 500         raise TypeError(msg)
    501
    502     def _search_new_conversions(self, *args, **kws):

TypeError: No matching definition for argument type(s) readonly array(int64, 1d, C)

In [6]: numba.__version__
Out[6]: '0.45.1'
@stuartarchibald
Copy link
Contributor

Thanks for the report. Unfortunately there's no short cut for specifying an array that is read only, the full numba.types API calls have to be used. Here's an example:

In [1]: from numba import njit, types                                                                                           

In [2]: import numpy as np                                                                                                      

In [3]: arr = np.array([1])                                                                                                     

In [4]: arr.setflags(write=False)                                                                                               

In [5]: arr.flags                                                                                                               
Out[5]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [6]: @njit((types.Array(types.int64, 1, 'C', readonly=True),)) # this is an int64, 1D, C order, read only array
   ...: def f(x): 
   ...:     return x 
   ...:                                                                                                                         

In [7]: f(arr)                                                                                                                  
Out[7]: array([1])

In [8]: f.signatures                                                                                                            
Out[8]: [(readonly array(int64, 1d, C),)]

hope this helps.

@stuartarchibald stuartarchibald added no action required No action was needed to resolve. question Notes an issue as a question labels Sep 3, 2019
@esc
Copy link
Member

esc commented Sep 9, 2019

@mroeschke I will close this issue now as I believe this issue has been resolved. Thanks again for asking about this.

@esc esc closed this as completed Sep 9, 2019
@mroeschke
Copy link
Author

Great, thanks for your response!

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

No branches or pull requests

3 participants