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

MAINT, ENH: concision of ufunc type/dims "dispatching" #135

Open
tylerjereddy opened this issue Dec 15, 2022 · 0 comments
Open

MAINT, ENH: concision of ufunc type/dims "dispatching" #135

tylerjereddy opened this issue Dec 15, 2022 · 0 comments
Labels
enhancement New feature or request maintenance

Comments

@tylerjereddy
Copy link
Contributor

tylerjereddy commented Dec 15, 2022

One of the concerns @gliga brought up in gh-124 code review was the lack of concision in the ufunc dispatching to workunits with regard to data types and view dimensionality--we are currently quite verbose in checking first the string name of the data type of a view and then its shape/dimensionality, before manually calling out to a workunit for that particular dimensionality and type + functionality.

One possible solution I "pitched" to Nader and Neil at the PyKokkos meeting this morning was the usage of the Python standard library inspect module to obtain a list of function names for workunits/kernels, that we could then dispatch to using some templating/string logic.

In fact, we already use a strategy similar to this to call/pre-compile most of our ufunc workunits in pre_compile_tools/pre_compile_ufuncs.py, to avoid hypothesis timeout failures related to our slow compile times when running the array API standard conformance suite.

So, very roughly/crudely, we might do something like:

from inspect import getmembers, isfunction
from pykokkos.lib import ufuncs

kernel_dict = dict(getmembers(ufuncs, isfunction))
# should be a dictionary with structure similar to:
# {function_name (string): function_object (actual workunit object)}

# workunit sketch/pseudocode

def round(view):
    out = pk.View(...)
    ndims = view.ndims
    dtype = view.dtype
    op = "round"
    # workunits are currently named like: round_impl_1d_double
    function_name_str = f"{op}_impl_{ndims}d_{dtype}"
    desired_workunit = kernel_dict[function_name_str]
    # call the kernel
    desired_workunit(tid, view, out)

Of course, this is just a pseudocode prototype, and may require some careful thought/refactoring--for example, we may want to rename workunit functions or adjust call signatures a bit for templating/dispatching convenience, and probably also separate the workunits to a separate module so we get easy access to the dictionary of function_name:function_object on import, etc.

@tylerjereddy tylerjereddy added enhancement New feature or request maintenance labels Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request maintenance
Projects
None yet
Development

No branches or pull requests

1 participant