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

printf that can be called from Numba jit-decorated function #5679

Open
pearu opened this issue May 7, 2020 · 3 comments
Open

printf that can be called from Numba jit-decorated function #5679

pearu opened this issue May 7, 2020 · 3 comments
Assignees

Comments

@pearu
Copy link
Contributor

pearu commented May 7, 2020

Feature request

Having the following implementation

from numba import extending, cgutils, types
@extending.intrinsic
def printf(typingctx, format_type, *args):
    """printf that can be called from Numba jit-decorated functions.
    """
    if isinstance(format_type, types.StringLiteral):
        sig = nb_types.void(format_type, types.BaseTuple.from_types(args))
        def codegen(context, builder, signature, args):
            cgutils.printf(builder, format_type.literal_value, *args[1:]) 
        return sig, codegen

one can call printf from a jit-decorated function. For instance,

from numba import njit
@njit
def foo(a, b):
    c = 1.3
    printf("in foo, a=%p, b=%i, c=%e\n", a, b, c)

and then

>>> foo(1, 2)
in foo, a=0x1, b=2, c=1.300000e+00

I have found it useful for debugging. If there is interest, where such a codelet should land in numba?

@stuartarchibald
Copy link
Contributor

Thanks for the suggestion. I think this is a good thing to have, but should probably be @overload'd or @register_jitable'd and made to work "kind of the same" when JIT is disabled (i.e. might have to replace the format specifiers to make it work in CPython). As to location, how about numba.misc.utils or similar? There's numba.misc.special already but it's not like this is some special token, it's a useful piece of additional functionality.

@quantfreedom
Copy link

@pearu ... does this still work ... i copied what you did and nothing was returned and it also seems that they moved cgutils

@pearu
Copy link
Contributor Author

pearu commented Oct 23, 2023

@QuantFreedom1022 not likely, for instance, cgutils now lives under numba.core. However, it should be possible to fix the feature for newer numba versions.

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

3 participants