Example #1
0
def math_overlay(mod, getname):
    """Register all functions listed in mathsyms and n_ary_mathsyms"""
    for symname in mathsyms + list(n_ary_mathsyms):
        if hasattr(mod, getname(symname)):
            py_func = getattr(mod, getname(symname))
            nb_func = globals()[symname]
            overlay(py_func, nb_func)
Example #2
0
def math_overlay(mod, getname):
    """Register all functions listed in mathsyms and n_ary_mathsyms"""
    for symname in mathsyms + list(n_ary_mathsyms):
        if hasattr(mod, getname(symname)):
            py_func = getattr(mod, getname(symname))
            nb_func = globals()[symname]
            overlay(py_func, nb_func)
Example #3
0
def ejit(py_cls, exc_cls):
    __all__.append(exc_cls.__name__)
    exc_cls = sjit(exc_cls)
    overlay(py_cls, exc_cls)

    @typeof.case(py_cls)
    def exc_typeof(pyval):
        return exc_cls[()]

    return exc_cls
Example #4
0
def ejit(exc_cls):
    __all__.append(exc_cls.__name__)
    py_cls = getattr(exceptions, exc_cls.__name__)
    exc_cls.layout = []
    exc_cls = sjit(exc_cls)
    overlay(py_cls, exc_cls)

    @typeof.case(py_cls)
    def exc_typeof(pyval):
        return exc_cls[()]

    return exc_cls
Example #5
0
def ejit(exc_cls):
    __all__.append(exc_cls.__name__)
    py_cls = getattr(exceptions, exc_cls.__name__)
    exc_cls.layout = []
    exc_cls = sjit(exc_cls)
    overlay(py_cls, exc_cls)

    @typeof.case(py_cls)
    def exc_typeof(pyval):
        return exc_cls[()]

    return exc_cls
Example #6
0
#===------------------------------------------------------------------===

@jit('StaticTuple[a, b] -> dtype -> Dimension[base]')
def c_layout_from_shape(shape, dtype):
    """Construct dimensions for an array of shape `shape` with a C layout"""
    dim = c_layout_from_shape(tail(shape), dtype)
    extent = head(shape)
    stride = dim.stride * dim.extent
    return Dimension(dim, extent, stride)

@jit('StaticTuple[a, EmptyTuple[]] -> dtype -> Dimension[base]')
def c_layout_from_shape(shape, dtype):
    extent = head(shape)
    return Dimension(EmptyDim(), extent, 1) # TODO: ContigDim

@jit
def product(it):
    """Take the product of the iterable"""
    prod = 1
    for x in it:
        prod *= x
    return prod

#===------------------------------------------------------------------===
# Overlays
#===------------------------------------------------------------------===

overlay(np.empty, empty)
overlay(np.empty_like, empty_like)
overlay(np.zeros_like, zeros_like)
overlay(np.ones_like,  ones_like)
Example #7
0
#===------------------------------------------------------------------===

## typeof()


def make_typeof(py_func, argtypes):
    [type] = argtypes

    @jit
    def typeof_impl(obj):
        return type

    env = fresh_env(typeof_impl, tuple(argtypes), "cpu")
    func, env = phase.ll_lower(typeof_impl, env)
    return func


opaque.implement_opaque(typeof, make_typeof)

## addressof()

#def impl_addressof(builder, argtypes, obj):
#    builder.ret(builder.addressof(obj))
#
#lowlevel_impls.add_impl(addressof, "addressof", impl_addressof)

## Overlays

# make flypy.typeof() available in flypy code
overlay(flypy.typeof, typeof)
Example #8
0
def c_layout_from_shape(shape, dtype):
    """Construct dimensions for an array of shape `shape` with a C layout"""
    dim = c_layout_from_shape(tail(shape), dtype)
    extent = head(shape)
    stride = dim.stride * dim.extent
    return Dimension(dim, extent, stride)


@jit('StaticTuple[a, EmptyTuple[]] -> dtype -> Dimension[base]')
def c_layout_from_shape(shape, dtype):
    extent = head(shape)
    return Dimension(EmptyDim(), extent, 1)  # TODO: ContigDim


@jit
def product(it):
    """Take the product of the iterable"""
    prod = 1
    for x in it:
        prod *= x
    return prod


#===------------------------------------------------------------------===
# Overlays
#===------------------------------------------------------------------===

overlay(np.empty, empty)
overlay(np.empty_like, empty_like)
overlay(np.zeros_like, zeros_like)
overlay(np.ones_like, ones_like)
Example #9
0
from flypy import overlay
from ... import jit, typeof


@jit
class NoneType(object):
    layout = []

    @jit('a -> bool')
    def __nonzero__(self):
        return False

    @jit('a -> a -> bool')
    def __eq__(self, other):
        return True

    @jit('a -> b -> bool')
    def __eq__(self, other):
        return other is None


NoneValue = NoneType()


@typeof.case(types.NoneType)
def typeof(pyval):
    return NoneType[()]


overlay(None, NoneValue)
Example #10
0
from __future__ import print_function, division, absolute_import
import types
from flypy import overlay
from ... import jit, typeof

@jit
class NoneType(object):
    layout = []

    @jit('a -> bool')
    def __nonzero__(self):
        return False

    @jit('a -> a -> bool')
    def __eq__(self, other):
        return True

    @jit('a -> b -> bool')
    def __eq__(self, other):
        return other is None


NoneValue = NoneType()


@typeof.case(types.NoneType)
def typeof(pyval):
     return NoneType[()]

overlay(None, NoneValue)
Example #11
0
#===------------------------------------------------------------------===
# Low-level implementations
#===------------------------------------------------------------------===

## typeof()

def make_typeof(py_func, argtypes):
    [type] = argtypes

    @jit
    def typeof_impl(obj):
        return type

    env = fresh_env(typeof_impl, tuple(argtypes), "cpu")
    func, env = phase.ll_lower(typeof_impl, env)
    return func

opaque.implement_opaque(typeof, make_typeof)

## addressof()

#def impl_addressof(builder, argtypes, obj):
#    builder.ret(builder.addressof(obj))
#
#lowlevel_impls.add_impl(addressof, "addressof", impl_addressof)

## Overlays

# make flypy.typeof() available in flypy code
overlay(flypy.typeof, typeof)