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

Add or document signature syntax for zero dimensional arrays #3838

Open
cems2 opened this issue Mar 6, 2019 · 3 comments
Open

Add or document signature syntax for zero dimensional arrays #3838

cems2 opened this issue Mar 6, 2019 · 3 comments

Comments

@cems2
Copy link

cems2 commented Mar 6, 2019

Problem Being addressed.

Numba has a text syntax for function signatures such as
'f8' or 'float32[:,:]'

However there is no simple text syntax for other arrays such as zero dimensional arrays

You can do this using a poorly documented alternative syntax of using explicit types in the signature rather than the simplified text string convention. For example
@nb.njit(nb.types.Array(nb.int64, 0, "C")())

Feature request

Add syntax for at least the Zero dimensional case since this is actually pretty common when using cuda. (because you can store scalars on the GPU if you make them zero dimensional arrays).

Here's a possible syntax that might be worth considering.

'float32[]' # empty brackets without any colons would imply zero dimensions

A final matter to consider would be if one wants to also worry about fortran versus C order arrays. My vote would be not to worry about that since it would complicate the syntax. But if you chose to do so then a possible ansatz would be to replace the ":" colon sigils with the letter F like this:

'float32[F,F]' # where F is the signal for a fortran order 2 D array

Note that zero and 1D arrays are identical in C and F

@stuartarchibald
Copy link
Contributor

Thanks for the suggestion. Some background:

Essentially, the string syntax works by just evaluating the string using Python's eval with numba.types.__dict__ as globals, e.g. eval('f8()', numba.types.__dict__). The way e.g. float32[:, :] ends up as a types.Array(float32, 2, A) is through an overload of the __getitem__ syntax (and slice objects) on the numba.types.Type to instantiate the appropriate type, similarly, f8() ends up as Signature(float64, (), None) (repr: () -> float64) through an overload of the __call__ method on the numba.types.Type. As a result, because e.g. float32[] # empty brackets without any colons would imply zero dimensions is not valid Python syntax, it would not be possible to support this without having a parser to handle such cases.

All this said, some other syntax could be overloaded e.g. float32[0] could indicate a 0-d float32 array, and more generally a type[x] indicating a types.Array(type, x, "A") type. Something similar for Fortran/C ordering could be permitted too. The code in question is here, if you wanted to experiment:

def __getitem__(self, args):
"""
Return an array of this type.
"""
from . import Array
ndim, layout = self._determine_array_spec(args)
return Array(dtype=self, ndim=ndim, layout=layout)
def _determine_array_spec(self, args):
# XXX non-contiguous by default, even for 1d arrays,
# doesn't sound very intuitive
if isinstance(args, (tuple, list)):
ndim = len(args)
if args[0].step == 1:
layout = 'F'
elif args[-1].step == 1:
layout = 'C'
else:
layout = 'A'
elif isinstance(args, slice):
ndim = 1
if args.step == 1:
layout = 'C'
else:
layout = 'A'
else:
ndim = 1
layout = 'A'
return ndim, layout

@cems2
Copy link
Author

cems2 commented Apr 17, 2019 via email

@stuartarchibald
Copy link
Contributor

@cems2 I would suggest opening a new issue, ideally with sample code demonstrating the problem, and someone will take a look (to get a faster response, also include a description of the hardware on which it is running). Sounds like it might be resource contention/exhaustion. You'll note in the issue tracker that there's a CUDA label and a lot of issues found under it https://github.com/numba/numba/issues?q=is%3Aissue+is%3Aopen+label%3ACUDA Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants