Beispiel #1
0
def jit(restype=None,
        argtypes=None,
        backend='ast',
        target='cpu',
        nopython=False,
        **kws):
    """
    Compile a function given the input and return types.

    There are multiple ways to specify the type signature:

    * Using the restype and argtypes arguments, passing Numba types.

    * By constructing a Numba function type and passing that as the
      first argument to the decorator.  You can create a function type
      by calling an exisiting Numba type, which is the return type,
      and the arguments to that call define the argument types.  For
      example, ``f8(f8)`` would create a Numba function type that
      takes a single double-precision floating point value argument,
      and returns a double-precision floating point value.

    * As above, but using a string instead of a constructed function
      type.  Example: ``jit("f8(f8)")``.

    If backend='bytecode' the bytecode translator is used, if
    backend='ast' the AST translator is used.  By default, the AST
    translator is used.  *Note that the bytecode translator is
    deprecated as of the 0.3 release.*
    """
    kws.update(nopython=nopython, backend=backend)
    if isinstance(restype, CLASS_TYPES):
        cls = restype
        env = kws.pop('env',
                      None) or environment.NumbaEnvironment.get_environment(
                          kws.get('env_name', None))
        return jit_extension_class(cls, kws, env)

    # Called with f8(f8) syntax which returns a dictionary of argtypes and restype
    if isinstance(restype, typesystem.function):
        if argtypes is not None:
            raise TypeError(
                "Cannot use both calling syntax and argtypes keyword")
        argtypes = restype.args
        restype = restype.return_type
    # Called with a string like 'f8(f8)'
    elif isinstance(restype, str) and argtypes is None:
        signature = process_signature(restype, kws.get('name', None))
        name, restype, argtypes = (signature.name, signature.return_type,
                                   signature.args)
        if name is not None:
            kws['func_name'] = name
    if restype is not None:
        kws['restype'] = restype
    if argtypes is not None:
        kws['argtypes'] = argtypes

    return jit_targets[target, backend](**kws)
Beispiel #2
0
def export(signature, env_name=None, env=None, **kws):
    """
    Construct a decorator that takes a function and exports one

    A signature is a string with

    name ret_type(arg_type, argtype, ...)
    """
    if env is None:
        env = environment.NumbaEnvironment.get_environment(env_name)
    return _internal_export(env, process_signature(signature), **kws)
Beispiel #3
0
def export(signature, env_name=None, env=None, **kws):
    """
    Construct a decorator that takes a function and exports one

    A signature is a string with

    name ret_type(arg_type, argtype, ...)
    """
    if env is None:
        env = environment.NumbaEnvironment.get_environment(env_name)
    return _internal_export(env, process_signature(signature), **kws)
Beispiel #4
0
def jit(restype=None, argtypes=None, backend='ast', target='cpu', nopython=False,
        **kws):
    """
    Compile a function given the input and return types.

    There are multiple ways to specify the type signature:

    * Using the restype and argtypes arguments, passing Numba types.

    * By constructing a Numba function type and passing that as the
      first argument to the decorator.  You can create a function type
      by calling an exisiting Numba type, which is the return type,
      and the arguments to that call define the argument types.  For
      example, ``f8(f8)`` would create a Numba function type that
      takes a single double-precision floating point value argument,
      and returns a double-precision floating point value.

    * As above, but using a string instead of a constructed function
      type.  Example: ``jit("f8(f8)")``.

    If backend='bytecode' the bytecode translator is used, if
    backend='ast' the AST translator is used.  By default, the AST
    translator is used.  *Note that the bytecode translator is
    deprecated as of the 0.3 release.*
    """
    kws.update(nopython=nopython, backend=backend)
    if isinstance(restype, CLASS_TYPES):
        cls = restype
        env = kws.pop('env', None) or environment.NumbaEnvironment.get_environment(
                                                         kws.get('env_name', None))
        return jit_extension_class(cls, kws, env)

    # Called with f8(f8) syntax which returns a dictionary of argtypes and restype
    if isinstance(restype, typesystem.function):
        if argtypes is not None:
            raise TypeError("Cannot use both calling syntax and argtypes keyword")
        argtypes = restype.args
        restype = restype.return_type
    # Called with a string like 'f8(f8)'
    elif isinstance(restype, str) and argtypes is None:
        signature = process_signature(restype, kws.get('name', None))
        name, restype, argtypes = (signature.name, signature.return_type,
                                   signature.args)
        if name is not None:
            kws['func_name'] = name
    if restype is not None:
        kws['restype'] = restype
    if argtypes is not None:
        kws['argtypes'] = argtypes

    return jit_targets[target, backend](**kws)
Beispiel #5
0
def parse_restype(visit_func, decorator, jit_args):
    restype_node = jit_args['restype']

    if restype_node is not None:
        restype = assert_constant(visit_func, decorator, restype_node)
        if isinstance(restype, (str, unicode)):
            signature = utils.process_signature(restype)
            restype = signature.return_type
            argtypes = signature.args
            check_valid_argtype(restype_node, restype)
            for argtype in argtypes:
                check_valid_argtype(restype_node, argtype)
            restype = restype(*argtypes)
        else:
            check_valid_argtype(restype_node, restype)
    else:
        raise error.NumbaError(restype_node, "Return type expected")

    return restype
Beispiel #6
0
def parse_restype(visit_func, decorator, jit_args):
    restype_node = jit_args['restype']

    if restype_node is not None:
        restype = assert_constant(visit_func, decorator, restype_node)
        if isinstance(restype, (str, unicode)):
            signature = utils.process_signature(restype)
            restype = signature.return_type
            argtypes = signature.args
            check_valid_argtype(restype_node, restype)
            for argtype in argtypes:
                check_valid_argtype(restype_node, argtype)
            restype = restype(*argtypes)
        else:
            check_valid_argtype(restype_node, restype)
    else:
        raise error.NumbaError(restype_node, "Return type expected")

    return restype
Beispiel #7
0
 def _export(func):
     for signature in signatures:
         tocall = _internal_export(env, process_signature(signature), **kws)
         tocall(func)
     return func
Beispiel #8
0
 def _export(func):
     for signature in signatures:
         tocall = _internal_export(env, process_signature(signature), **kws)
         tocall(func)
     return func