Exemplo n.º 1
0
    def extract(cls, n, context, min_cost, cluster, sregistry):
        make = lambda: Scalar(name=sregistry.make_name(), dtype=cluster.dtype
                              ).indexify()

        exclude = {
            i.source.indexed
            for i in cluster.scope.d_flow.independent()
        }
        rule0 = lambda e: not e.free_symbols & exclude
        rule1 = make_is_time_invariant(context)
        rule2 = lambda e: estimate_cost(e, True) >= min_cost
        rule = lambda e: rule0(e) and rule1(e) and rule2(e)

        extracted = []
        mapper = OrderedDict()
        for e in cluster.exprs:
            for i in search(e, rule, 'all', 'dfs_first_hit'):
                if i not in mapper:
                    symbol = make()
                    mapper[i] = symbol
                    extracted.append(e.func(symbol, i))

        processed = [uxreplace(e, mapper) for e in cluster.exprs]

        return extracted + processed, extracted
Exemplo n.º 2
0
    def _extract_rule(cls, context, min_cost, cluster):
        exclude = {
            i.source.indexed
            for i in cluster.scope.d_flow.independent()
        }
        rule0 = lambda e: not e.free_symbols & exclude
        rule1 = make_is_time_invariant(context)
        rule2 = lambda e: estimate_cost(e, True) >= min_cost

        return lambda e: rule0(e) and rule1(e) and rule2(e)
Exemplo n.º 3
0
    def callbacks_invariants(context, n):
        min_cost_inv = min_cost['invariants']
        if callable(min_cost_inv):
            min_cost_inv = min_cost_inv(n)

        extractor = make_is_time_invariant(context)
        model = lambda e: estimate_cost(e, True) >= min_cost_inv
        ignore_collected = lambda g: False
        selector = lambda c, n: c >= min_cost_inv and n >= 1
        return extractor, model, ignore_collected, selector
Exemplo n.º 4
0
def extract_time_invariants(cluster, template, *args):
    """
    Extract time-invariant subexpressions, and assign them to temporaries.
    """
    make = lambda: Scalar(name=template(), dtype=cluster.dtype).indexify()
    rule = make_is_time_invariant(cluster.exprs)
    costmodel = lambda e: estimate_cost(e, True) >= MIN_COST_ALIAS_INV
    processed, found = yreplace(cluster.exprs,
                                make,
                                rule,
                                costmodel,
                                eager=True)

    return cluster.rebuild(processed)
Exemplo n.º 5
0
def test_yreplace_time_invariants(exprs, expected):
    grid = Grid((3, 3, 3))
    dims = grid.dimensions
    tu = TimeFunction(name="tu", grid=grid, space_order=4).indexify()
    tv = TimeFunction(name="tv", grid=grid, space_order=4).indexify()
    tw = TimeFunction(name="tw", grid=grid, space_order=4).indexify()
    ti0 = Array(name='ti0', shape=(3, 5, 7), dimensions=dims).indexify()
    ti1 = Array(name='ti1', shape=(3, 5, 7), dimensions=dims).indexify()
    t0 = Scalar(name='t0').indexify()
    t1 = Scalar(name='t1').indexify()
    exprs = EVAL(exprs, tu, tv, tw, ti0, ti1, t0, t1)
    counter = generator()
    make = lambda: Scalar(name='r%d' % counter()).indexify()
    processed, found = yreplace(exprs, make, make_is_time_invariant(exprs),
                                lambda i: estimate_cost(i) > 0)
    assert len(found) == len(expected)
    assert all(str(i.rhs) == j for i, j in zip(found, expected))
Exemplo n.º 6
0
def extract(exprs, aliases):
    """
    Extract the candidate aliases.
    """
    is_time_invariant = make_is_time_invariant(exprs)
    time_invariants = {e.rhs: is_time_invariant(e) for e in exprs}

    processed = []
    candidates = OrderedDict()
    for e in exprs:
        # Cost check (to keep the memory footprint under control)
        naliases = len(aliases.get(e.rhs))
        cost = estimate_cost(e, True) * naliases
        test0 = lambda: cost >= MIN_COST_ALIAS and naliases > 1
        test1 = lambda: cost >= MIN_COST_ALIAS_INV and time_invariants[e.rhs]
        if test0() or test1():
            candidates[e.rhs] = e.lhs
        else:
            processed.append(e)

    return candidates, processed
Exemplo n.º 7
0
 def callbacks_invariants(context, *args):
     extractor = make_is_time_invariant(context)
     model = lambda e: estimate_cost(e, True) >= MIN_COST_ALIAS_INV
     ignore_collected = lambda g: False
     selector = lambda c, n: c >= MIN_COST_ALIAS_INV and n >= 1
     return extractor, model, ignore_collected, selector
Exemplo n.º 8
0
 def extractor(context):
     is_time_invariant = make_is_time_invariant(context)
     return lambda e: is_time_invariant(e)
Exemplo n.º 9
0
 def extractor(context):
     return make_is_time_invariant(context)