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

Ability to register a non-static type resolution function and a multi-purpose loop #41

Open
hameerabbasi opened this issue Jun 7, 2019 · 2 comments

Comments

@hameerabbasi
Copy link
Contributor

Let's suppose you want to define a gufunc from within Python. We need two parts: The type resolution function, and the actual loop. So for example:

def typeres(*input_types):
    # Perform some operation with input types.
    return output_types

def loop(*all_allocated_arrays):
    # Perform all operations, store in last arrray(s) as output.

For example, divmod:

def typeres(*input_types):
    if len(input_types) != 2 or not all(t.fits('... * int64') for t in input_types):
         raise TypeError('Type resolution error.')
    return 'int64', 'int64'

def loop(in1, in2, out1, out2):
    out1 = in1 / in2
    out2 = in1 % in2

divmod = define_gufunc(typeres, loop)

So, the type resolution function will be called and then all the output arrays allocated. Let's say we pass in two 5 * int64 arrays. And then, loop gets passed in all four 5 * int64 arrays, and there's no looping in Python, only in C.

Then, what happens is: out1, out2 is returned from the gufunc.

@skrah
Copy link
Member

skrah commented Jun 21, 2019

It looks like the example relies on the primitives // and %, so that would be already possible:

>>> def f(a, b):
...     out1 = a // b
...     out2 = a % b
...     return out1, out2
... 
>>> x = array([8, 9, 10])
>>> y = array([3, 4, 5])
>>> f(x, y)
(array([2, 2, 2], type='3 * int64'), array([2, 1, 0], type='3 * int64')

@hameerabbasi
Copy link
Contributor Author

This is a simple example of how to define a gufunc from Python. It'd be nice to have this be possible in general.

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

No branches or pull requests

2 participants