def _extract_time_invariants(self, cluster, template, with_cse=True, costmodel=None, **kwargs): """ Extract time-invariant subexpressions, and assign them to temporaries. """ make = lambda: Scalar(name=template(), dtype=cluster.dtype).indexify() rule = iq_timeinvariant(cluster.trace) costmodel = costmodel or (lambda e: estimate_cost(e) > 0) processed, found = xreplace_constrained(cluster.exprs, make, rule, costmodel) if with_cse: leaves = [i for i in processed if i not in found] # Search for common sub-expressions amongst them (and only them) found = common_subexprs_elimination(found, make) # Some temporaries may be droppable at this point processed = compact_temporaries(found, leaves) return cluster.rebuild(processed)
def test_xreplace_constrained_time_invariants(tu, tv, tw, ti0, ti1, t0, t1, exprs, expected): exprs = EVAL(exprs, tu, tv, tw, ti0, ti1, t0, t1) make = lambda i: Scalar(name='r%d' % i).indexify() processed, found = xreplace_constrained( exprs, make, iq_timeinvariant(TemporariesGraph(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))
def test_xreplace_constrained_time_invariants(tu, tv, tw, ti0, ti1, t0, t1, exprs, expected): exprs = EVAL(exprs, tu, tv, tw, ti0, ti1, t0, t1) counter = generator() make = lambda: Scalar(name='r%d' % counter()).indexify() processed, found = xreplace_constrained(exprs, make, iq_timeinvariant(FlowGraph(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))
def _extract_time_invariants(self, cluster, template, with_cse=True, **kwargs): """ Extract time-invariant subexpressions, and assign them to temporaries. """ make = lambda: Scalar(name=template(), dtype=cluster.dtype).indexify() rule = iq_timeinvariant(cluster.trace) costmodel = lambda e: estimate_cost(e) > 0 processed, found = xreplace_constrained(cluster.exprs, make, rule, costmodel) if with_cse: leaves = [i for i in processed if i not in found] # Search for common sub-expressions amongst them (and only them) found = common_subexprs_elimination(found, make) # Some temporaries may be droppable at this point processed = compact_temporaries(found, leaves) return cluster.rebuild(processed)