Example #1
0
    def fromobject(tuple, type):
        head, tail = type.parameters
        hd = fromobject(tuple[0], head)
        if tuple[1:]:
            tl = fromobject(tuple[1:], tail)
        else:
            tl = EmptyTuple()

        return StaticTuple(hd, tl)
Example #2
0
    def fromobject(tuple, type):
        head, tail = type.parameters
        hd = fromobject(tuple[0], head)
        if tuple[1:]:
            tl = fromobject(tuple[1:], tail)
        else:
            tl = EmptyTuple()

        return StaticTuple(hd, tl)
Example #3
0
def fromnumpy(ndarray, ty, boundscheck=False):
    """Build an NDArray from a numpy ndarray"""
    # Compute steps
    itemsize = ndarray.dtype.itemsize
    steps = tuple(stride // itemsize for stride in ndarray.strides)

    # Check that strides are divisible by itemsize. We need to do this to
    # allow computation on Pointer[T] instead of Pointer[char], which we
    # need since we cannot access 'T' in __getitem__ to cast the Pointer[char]
    # back to Pointer[T]
    if any(stride % itemsize for stride in ndarray.strides):
        raise ValueError("Cannot handle non-element size strides "
                         "(e.g. views in record arrays)")

    # Build array object
    data = fromobject(ndarray.ctypes.data, Pointer[flypy.types.int8])

    dims = EmptyDim()
    for extent, stride in reversed(zip(ndarray.shape, steps)):
        dimcls = Dimension
        #dimcls = DimensionContig if stride == 1 else Dimension
        dims = dimcls(dims, extent, stride)

        if boundscheck:
            dims = BoundsCheck(dims)

    base_type = ty.parameters[0]
    return NDArray(data, dims, base_type)
Example #4
0
def fromnumpy(ndarray, ty, keepalive, boundscheck=False):
    """Build an NDArray from a numpy ndarray"""
    # Compute steps
    itemsize = ndarray.dtype.itemsize
    steps = tuple(stride // itemsize for stride in ndarray.strides)

    # Check that strides are divisible by itemsize. We need to do this to
    # allow computation on Pointer[T] instead of Pointer[char], which we
    # need since we cannot access 'T' in __getitem__ to cast the Pointer[char]
    # back to Pointer[T]
    if any(stride % itemsize for stride in ndarray.strides):
        raise ValueError("Cannot handle non-element size strides "
                         "(e.g. views in record arrays)")

    # Build array object
    data = fromobject(ndarray.ctypes.data, Pointer[flypy.types.int8], keepalive)

    dims = EmptyDim()
    for extent, stride in reversed(zip(ndarray.shape, steps)):
        dimcls = Dimension
        #dimcls = DimensionContig if stride == 1 else Dimension
        dims = dimcls(dims, extent, stride)

        if boundscheck:
            dims = BoundsCheck(dims)

    base_type = ty.parameters[0]
    return NDArray(data, dims, base_type)
Example #5
0
    def test_representation(self):
        "ctypes"
        ty = typeof((1, 2, 3))
        obj = fromobject((1, 2, 3), ty)
        keepalive = []
        rep = toctypes(obj, ty, keepalive)
        rep = CTypesStruct(rep)

        # print(rep) -> { tl:{ tl:{ tl:{ dummy:0 }, hd:3 }, hd:2 }, hd:1 }
        self.assertEqual(rep.hd, 1)
        self.assertEqual(rep.tl.hd, 2)
        self.assertEqual(rep.tl.tl.hd, 3)
Example #6
0
    def test_representation(self):
        "ctypes"
        ty = typeof((1, 2, 3))
        obj = fromobject((1, 2, 3), ty)
        keepalive = []
        rep = toctypes(obj, ty, keepalive)
        rep = CTypesStruct(rep)

        # print(rep) -> { tl:{ tl:{ tl:{ dummy:0 }, hd:3 }, hd:2 }, hd:1 }
        self.assertEqual(rep.hd, 1)
        self.assertEqual(rep.tl.hd, 2)
        self.assertEqual(rep.tl.tl.hd, 3)
Example #7
0
def make_applier(f, *subterms):
    """Create a blaze function application term"""
    @jit('Apply[subterms]')
    class Apply(object):
        """Function application term"""

        layout = [('subterms', 'subterms')]  # Tuple of sub-expressions

        @jit
        def apply(self, args):
            args = eval_subterms(self.subterms, args)
            return f(*args)

        def __repr__(self):
            return "Apply(%s, %s)" % (f.py_func.__name__, self.subterms)

    # NOTE: flypy doesn't reconstruct flypy objects recursively, it only
    #       acts on roots!
    subterms = fromobject(subterms, typeof(subterms))
    return Apply(subterms)
Example #8
0
def make_applier(f, *subterms):
    """Create a blaze function application term"""

    @jit('Apply[subterms]')
    class Apply(object):
        """Function application term"""

        layout = [('subterms', 'subterms')] # Tuple of sub-expressions

        @jit
        def apply(self, args):
            args = eval_subterms(self.subterms, args)
            return f(*args)

        def __repr__(self):
            return "Apply(%s, %s)" % (f.py_func.__name__, self.subterms)

    # NOTE: flypy doesn't reconstruct flypy objects recursively, it only
    #       acts on roots!
    subterms = fromobject(subterms, typeof(subterms))
    return Apply(subterms)
Example #9
0
def interpret(nb_func, run_phase, args, debug=False, tracer=None):
    """Interpret and return result"""
    # Translate flypy function
    argtypes = [typeof(arg) for arg in args]
    env = environment.fresh_env(nb_func, argtypes, target="cpu")
    f, env = run_phase(nb_func, env)

    if debug:
        header(f)

    if tracer is None:
        # Set up tracer to trace interpretation
        if debug:
            tracer = tracing.Tracer()
        else:
            tracer = tracing.DummyTracer()

    newargs = [conversion.fromobject(arg, typeof(arg)) for arg in args]

    # Interpret function
    env.setdefault('interp.handlers', handlers(run_phase, env))
    return interp.run(f, env, args=newargs, tracer=tracer)
Example #10
0
def interpret(nb_func, run_phase, args, debug=False, tracer=None):
    """Interpret and return result"""
    # Translate flypy function
    argtypes = [typeof(arg) for arg in args]
    env = environment.fresh_env(nb_func, argtypes, target="cpu")
    f, env = run_phase(nb_func, env)

    if debug:
        header(f)

    if tracer is None:
        # Set up tracer to trace interpretation
        if debug:
            tracer = tracing.Tracer()
        else:
            tracer = tracing.DummyTracer()

    newargs = [conversion.fromobject(arg, typeof(arg)) for arg in args]

    # Interpret function
    env.setdefault('interp.handlers', handlers(run_phase, env))
    return interp.run(f, env, args=newargs, tracer=tracer)
Example #11
0
def rewrite_constants(func, env):
    """
    Rewrite constants with user-defined types to IR constants. Also rewrite
    constants of builtins to instances of flypy classes.

        e.g. constant(None)  -> constant(NoneValue)
             constant("foo") -> constant(Bytes("foo"))
    """
    if env['flypy.state.opaque']:
        return

    context = env['flypy.typing.context']

    for op in func.ops:
        if op.opcode == 'exc_catch':
            continue
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            ty = context[c]

            # Python -> flypy (if not already)
            flypy_obj = fromobject(c.const, ty)
            # flypy -> ctypes
            ctype_obj = toctypes(flypy_obj, ty, _keep_alive)
            if byref(ty):
                ctype_obj = ctypes.pointer(ctype_obj)
            # ctypes -> pykit
            new_const = from_ctypes_value(ctype_obj)

            context[new_const] = ty
            new_constants.append(new_const)

            _keep_alive.extend([ctype_obj, c.const])

        substitute_args(op, constants, new_constants)
Example #12
0
def rewrite_constants(func, env):
    """
    Rewrite constants with user-defined types to IR constants. Also rewrite
    constants of builtins to instances of flypy classes.

        e.g. constant(None)  -> constant(NoneValue)
             constant("foo") -> constant(Bytes("foo"))
    """
    if env['flypy.state.opaque']:
        return

    context = env['flypy.typing.context']

    for op in func.ops:
        if op.opcode == 'exc_catch':
            continue
        constants = collect_constants(op)
        new_constants = []
        for c in constants:
            ty = context[c]

            # Python -> flypy (if not already)
            flypy_obj = fromobject(c.const, ty, _keep_alive)
            # flypy -> ctypes
            ctype_obj = toctypes(flypy_obj, ty, _keep_alive)
            if byref(ty):
                ctype_obj = ctypes.pointer(ctype_obj)
            # ctypes -> pykit
            new_const = from_ctypes_value(ctype_obj)

            context[new_const] = ty
            new_constants.append(new_const)

            _keep_alive.extend([ctype_obj, c.const])

        substitute_args(op, constants, new_constants)
Example #13
0
def tonb(tup):
    return fromobject(tup, typeof(tup))
Example #14
0
def tonb(tup):
    return fromobject(tup, typeof(tup))