def test_find_loop_blocks3(): import os def ps(loops): return 42.0, 42.1 def f(loops): benchtime, stones = ps(abs(loops)) s = "" # annotator happiness if loops >= 0: s = ( "RPystone(%s) time for %d passes = %f" % (23, loops, benchtime) + "\n" + ("This machine benchmarks at %f pystones/second" % stones) ) os.write(1, s) if loops == 12345: f(loops - 1) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_find_loop_blocks3(): import os def ps(loops): return 42.0, 42.1 def f(loops): benchtime, stones = ps(abs(loops)) s = '' # annotator happiness if loops >= 0: s = ("RPystone(%s) time for %d passes = %f" % (23, loops, benchtime) + '\n' + ("This machine benchmarks at %f pystones/second" % stones)) os.write(1, s) if loops == 12345: f(loops - 1) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_find_loop_blocks_simple(): def f(a): if a <= 0: return 1 return f(a - 1) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_find_loop_blocks_simple(): def f(a): if a <= 0: return 1 return f(a - 1) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_find_loop_blocks(): def f(k): result = 0 for i in range(k): result += 1 for j in range(k): result += 1 return result t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 4
def test_find_loop_blocks(): def f(k): result = 0 for i in range(k): result += 1 for j in range(k): result += 1 return result t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 4
def measure_median_execution_cost(graph): blocks = [] blockmap = {} for block in graph.iterblocks(): blockmap[block] = len(blocks) blocks.append(block) loops = find_loop_blocks(graph) M = sparsemat.SparseMatrix(len(blocks)) vector = [] for i, block in enumerate(blocks): vector.append(block_weight(block)) M[i, i] = 1 if block.exits: if block not in loops: current_loop_start = None else: current_loop_start = loops[block] loop_exits = [] for link in block.exits: if (link.target in loops and loops[link.target] is current_loop_start): loop_exits.append(link) if len(loop_exits) and len(loop_exits) < len(block.exits): f = 0.3 / (len(block.exits) - len(loop_exits)) b = 0.7 / len(loop_exits) else: b = f = 1.0 / len(block.exits) for link in block.exits: if (link.target in loops and loops[link.target] is current_loop_start): M[i, blockmap[link.target]] -= b else: M[i, blockmap[link.target]] -= f try: Solution = M.solve(vector) except ValueError: return sys.maxint else: res = Solution[blockmap[graph.startblock]] assert res >= 0 return res
def measure_median_execution_cost(graph): blocks = [] blockmap = {} for block in graph.iterblocks(): blockmap[block] = len(blocks) blocks.append(block) loops = find_loop_blocks(graph) M = sparsemat.SparseMatrix(len(blocks)) vector = [] for i, block in enumerate(blocks): vector.append(block_weight(block)) M[i, i] = 1 if block.exits: if block not in loops: current_loop_start = None else: current_loop_start = loops[block] loop_exits = [] for link in block.exits: if (link.target in loops and loops[link.target] is current_loop_start): loop_exits.append(link) if len(loop_exits) and len(loop_exits) < len(block.exits): f = 0.3 / (len(block.exits) - len(loop_exits)) b = 0.7 / len(loop_exits) else: b = f = 1.0 / len(block.exits) for link in block.exits: if (link.target in loops and loops[link.target] is current_loop_start): M[i, blockmap[link.target]] -= b else: M[i, blockmap[link.target]] -= f try: Solution = M.solve(vector) except ValueError: return sys.maxint else: res = Solution[blockmap[graph.startblock]] assert res >= 0 return res
def test_find_loop_blocks2(): class A: pass def f(n): a1 = A() a1.x = 1 a2 = A() a2.x = 2 if n > 0: a = a1 else: a = a2 return a.x t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0
def test_find_loop_blocks2(): class A: pass def f(n): a1 = A() a1.x = 1 a2 = A() a2.x = 2 if n > 0: a = a1 else: a = a2 return a.x t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() graph = graphof(t, f) backedges = find_backedges(graph) assert backedges == [] loop_blocks = find_loop_blocks(graph) assert len(loop_blocks) == 0