Beispiel #1
0
    def AAppl(self, app):
        "Look for unops, binops and reductions and anything else we can handle"
        if paterm.matches('Arithmetic;*', app.spine):
            opname = app.args[0].label.lower()
            op = self.opname_to_astop.get(opname, None)
            type = plan.get_datashape(app)
            is_array = type.shape or self.nesting_level
            if op is not None and is_array and len(app.args) == 3: # args = [op, lhs, rhs]
                return self.handle_arithmetic(app, op)

        elif paterm.matches('Slice;*', app.spine):
            array, start, stop, step = app.args
            if all(paterm.matches("None;*", op) for op in (start, stop, step)):
                return self.visit(array)

        elif paterm.matches("Assign;*", app.spine):
            return self.match_assignment(app)

        elif paterm.matches("Array;*", app.spine) and self.nesting_level:
            self.maybe_operand(app)
            if self.nesting_level:
                return None
            return app

        return self.unhandled(app)
Beispiel #2
0
    def AAppl(self, app):
        "Look for unops, binops and reductions and anything else we can handle"
        if paterm.matches('Arithmetic;*', app.spine):
            opname = app.args[0].label.lower()
            op = self.opname_to_astop.get(opname, None)
            type = plan.get_datashape(app)
            is_array = type.shape or self.nesting_level
            if op is not None and is_array and len(
                    app.args) == 3:  # args = [op, lhs, rhs]
                return self.handle_arithmetic(app, op)

        elif paterm.matches('Slice;*', app.spine):
            array, start, stop, step = app.args
            if all(paterm.matches("None;*", op) for op in (start, stop, step)):
                return self.visit(array)

        elif paterm.matches("Assign;*", app.spine):
            return self.match_assignment(app)

        elif paterm.matches("Array;*", app.spine) and self.nesting_level:
            self.maybe_operand(app)
            if self.nesting_level:
                return None
            return app

        return self.unhandled(app)
Beispiel #3
0
    def dispatch(self, aterm):
        # canidate functions, functions matching the signature of
        # the term
        c = [f for f, sig in self.funs.iteritems() if matches(sig, aterm)]

        if len(c) == 0:
            raise NoDispatch(aterm)

        # the canidate which has the minimal cost function
        costs = [(f, self.costs[f](aterm)) for f in c]

        return min(costs, key=lambda x: x[1])
Beispiel #4
0
    def AAppl(self, app):
        "Look for unops, binops and reductions and anything else we can handle"
        if paterm.matches('Arithmetic;*', app.spine):
            opname = app.args[0].lower()
            op = self.opname_to_astop.get(opname, None)
            args = app.args[1:]
            if op is not None and len(args) == 2:
                self.nesting_level += 1
                self.visit(args)
                self.nesting_level -= 1

                left, right = self.result
                result = ast.BinOp(left=left, op=op(), right=right)
                return self.register(app, result)
        elif paterm.matches('Slice;*', app.spine):
            array, start, stop, step = app.args
            if all(paterm.matches("None;*", op) for op in (start, stop, step)):
                return self.visit(array)
        elif paterm.matches("Assign;*", app.spine):
            return self.match_assignment(app)

        return self.unhandled(app)
Beispiel #5
0
    def AAppl(self, node):
        """
        Executor(op1, op2, ..., opN, lhs_op?){'backend', executor_id, has_lhs}
        """
        # print node.annotation.meta, node.annotation

        if paterm.matches("Array;*", node.spine):
            id = node.annotation.meta[0]
            return self.arrays[id.label]
        else:
            assert  paterm.matches("Executor;*", node.spine), node

            backend, executor_id, has_lhs = node.annotation.meta
            executor = self.executors[executor_id.label]
            operands = self.visit(node.args)
            if has_lhs:
                lhs_op = operands.pop()
            else:
                # FIXME: !
                raise NotImplementedError

            executors.execute(executor, operands, lhs_op)
            return lhs_op
Beispiel #6
0
def unannotate_dtype(aterm):
    """ Takes a term with a datashape annotation and returns the NumPy
    dtype associate with it

    >>> term
    x{dshape("2, 2, int32")}
    >>> unannotate_dtype(term)
    int32
    """
    # unpack the annotation {'s': 'int32'}
    unpack = paterm.matches('dshape(s);*', aterm.annotation['type'])
    ds_str = unpack['s']
    dshape = datashape(ds_str.s)

    dtype = coretypes.to_dtype(dshape)
    return dtype
Beispiel #7
0
def unannotate_dtype(aterm):
    """ Takes a term with a datashape annotation and returns the NumPy
    dtype associate with it

    >>> term
    x{dshape("2, 2, int32")}
    >>> unannotate_dtype(term)
    int32
    """
    # unpack the annotation {'s': 'int32'}
    unpack = paterm.matches('dshape(s);*', aterm.annotation['type'])
    ds_str = unpack['s']
    dshape = datashape(ds_str.s)

    dtype = coretypes.to_dtype(dshape)
    return dtype