def optimize_chain_inner(dot_shapes): print "In optimize_chain_inner! Looking to optimize these dot calls/shapes ", dot_shapes dot_call_dfg_nodes = {dotcall_ni.dfg_node: shape for (dotcall_ni, shape) in dot_shapes.items()} for (dotcall_ni, shape) in dot_shapes.iteritems(): if len(shape) == 2: chain_for(dotcall_ni.dfg_node, shape, dot_call_dfg_nodes) # Remove incomplete chains for d in subchains: done_chains.pop(d, None) print "And here are the chains: " pprint(done_chains) for (end, inputs) in done_chains.iteritems(): for (source, shape) in inputs: assumptions[source] = "{1}.shape == %s" % str(shape) chain_inputs = [inp for (inp, shp) in inputs] chain_shapes = [shp for (inp, shp) in inputs] optimal_order = matrix_chain.matrix_chain_tree(chain_shapes) print "Optimal order", optimal_order placeholders = ["{%i}" % i for i in range(len(chain_inputs))] new_chain_expr = mult_order_to_expr(placeholders, optimal_order, end.ast_node.func.as_string()) nodes_to_replace, edges_to_replace = dfg.subgraph_between(chain_inputs, end) cprint("Going to replace a subgraph, saved as 'last_subgraph', with %s" % new_chain_expr, "red") mainvar("last_subgraph", nodes_to_replace) mainvar("input_nodes", chain_inputs) mainvar("new_chain_expr", new_chain_expr) mainvar("assumptions", assumptions) if not DRYRUN: modcode.replace_subgraph_and_code(dfg, nodes_to_replace, chain_inputs, new_chain_expr, assumptions)
def cythonify_if_numeric(func_and_var_info): try: print "cythonify_if_numeric called with", func_and_var_info var_types = {} for (ni, val) in func_and_var_info.items(): if isinstance(val, tuple): # It's variable info typ, dtype, shape = val if not is_numerical_type(typ, dtype): cprint("Can't do numerical optimization of loop because of non-numerical " +\ 'var %s (%s)'%(ni.expr, str(typ)), 'red') return False var_types[ni.expr] = val elif callable(val): if not is_numerical_callable(val): cprint("Can't do numerical optimization of loop " +\ "because of non-numerical callable " + \ str(val), 'red') return False loopfn, argnames, retnames = cythonify.loop_to_cython(dfg, loopnode, var_types) __builtin__.myloopfn = loopfn if retnames: newexpr = (''.join([r+',' for r in retnames]) + ' = ' + 'myloopfn(' + ','.join(argnames) + ')' ) else: newexpr = 'myloopfn(' + ','.join(argnames) + ')' cprint("new expression is "+ newexpr, 'blue') in_edges = dfg.get_incoming_edges(loopnode) modcode.replace_subgraph_and_code(dfg, [loopnode], [e.n1 for e in in_edges], newexpr, assumptions={}) except: logger.exception("Oh crap!")
def optimize_chain_inner(dot_shapes): print "In optimize_chain_inner! Looking to optimize these dot calls/shapes ", dot_shapes dot_call_dfg_nodes = { dotcall_ni.dfg_node: shape for (dotcall_ni, shape) in dot_shapes.items() } for (dotcall_ni, shape) in dot_shapes.iteritems(): if len(shape) == 2: chain_for(dotcall_ni.dfg_node, shape, dot_call_dfg_nodes) # Remove incomplete chains for d in subchains: done_chains.pop(d, None) print "And here are the chains: " pprint(done_chains) for (end, inputs) in done_chains.iteritems(): for (source, shape) in inputs: assumptions[source] = "{1}.shape == %s" % str(shape) chain_inputs = [inp for (inp, shp) in inputs] chain_shapes = [shp for (inp, shp) in inputs] optimal_order = matrix_chain.matrix_chain_tree(chain_shapes) print "Optimal order", optimal_order placeholders = ['{%i}' % i for i in range(len(chain_inputs))] new_chain_expr = mult_order_to_expr( placeholders, optimal_order, end.ast_node.func.as_string()) nodes_to_replace, edges_to_replace = dfg.subgraph_between( chain_inputs, end) cprint( "Going to replace a subgraph, saved as 'last_subgraph', with %s" % new_chain_expr, 'red') mainvar('last_subgraph', nodes_to_replace) mainvar('input_nodes', chain_inputs) mainvar('new_chain_expr', new_chain_expr) mainvar('assumptions', assumptions) if not DRYRUN: modcode.replace_subgraph_and_code(dfg, nodes_to_replace, chain_inputs, new_chain_expr, assumptions)
def cythonify_if_numeric(func_and_var_info): try: print "cythonify_if_numeric called with", func_and_var_info var_types = {} for (ni, val) in func_and_var_info.items(): if isinstance(val, tuple): # It's variable info typ, dtype, shape = val if not is_numerical_type(typ, dtype): cprint("Can't do numerical optimization of loop because of non-numerical " +\ 'var %s (%s)'%(ni.expr, str(typ)), 'red') return False var_types[ni.expr] = val elif callable(val): if not is_numerical_callable(val): cprint("Can't do numerical optimization of loop " +\ "because of non-numerical callable " + \ str(val), 'red') return False loopfn, argnames, retnames = cythonify.loop_to_cython( dfg, loopnode, var_types) __builtin__.myloopfn = loopfn if retnames: newexpr = (''.join([r + ',' for r in retnames]) + ' = ' + 'myloopfn(' + ','.join(argnames) + ')') else: newexpr = 'myloopfn(' + ','.join(argnames) + ')' cprint("new expression is " + newexpr, 'blue') in_edges = dfg.get_incoming_edges(loopnode) modcode.replace_subgraph_and_code(dfg, [loopnode], [e.n1 for e in in_edges], newexpr, assumptions={}) except: logger.exception("Oh crap!")