def visit_Assign(self, node): self.generic_visit(node) extra_assign = [node] for i, t in enumerate(node.targets): if isinstance(t, ast.Tuple) or isinstance(t, ast.List): renamings = dict() self.traverse_tuples(t, (), renamings) if renamings: self.counter += 1 gtarget = "{0}{1}{2}".format( NormalizeTuples.tuple_name, self.counter, i) node.targets[i] = ast.Name(gtarget, node.targets[i].ctx) metadata.add(node.targets[i], metadata.LocalVariable()) for rename, state in sorted(renamings.iteritems()): nnode = reduce( lambda x, y: ast.Subscript( x, ast.Index(ast.Num(y)), ast.Load()), state, ast.Name(gtarget, ast.Load())) if isinstance(rename, str): extra_assign.append( ast.Assign( [ast.Name(rename, ast.Store())], nnode)) else: extra_assign.append(ast.Assign([rename], nnode)) return (ast.If(ast.Num(1), extra_assign, []) if len(extra_assign) > 1 else extra_assign)
def visit_For(self, node): target = node.target if isinstance(target, ast.Tuple) or isinstance(target, ast.List): renamings = dict() self.traverse_tuples(target, (), renamings) if renamings: self.counter += 1 gtarget = "{0}{1}".format( NormalizeTuples.tuple_name, self.counter ) node.target = ast.Name(gtarget, node.target.ctx) metadata.add(node.target, metadata.LocalVariable()) for rename, state in sorted(renamings.iteritems()): nnode = reduce( lambda x, y: ast.Subscript( x, ast.Index(ast.Num(y)), ast.Load()), state, ast.Name(gtarget, ast.Load())) if isinstance(rename, str): node.body.insert(0, ast.Assign( [ast.Name(rename, ast.Store())], nnode) ) else: node.body.insert(0, ast.Assign([rename], nnode)) self.generic_visit(node) return node
def attach_data(self, node): '''Generic method called for visit_XXXX() with XXXX in GatherOMPData.statements list ''' if self.current: for curr in self.current: md = OMPDirective(curr) metadata.add(node, md) self.current = list() # add a Pass to hold some directives for field_name, field in ast.iter_fields(node): if field_name in GatherOMPData.statement_lists: if (field and isinstance(field[-1], ast.Expr) and self.isompdirective(field[-1].value)): field.append(ast.Pass()) self.generic_visit(node) # add an If to hold scoping OpenMP directives directives = metadata.get(node, OMPDirective) field_names = {n for n, _ in ast.iter_fields(node)} has_no_scope = field_names.isdisjoint(GatherOMPData.statement_lists) if directives and has_no_scope: # some directives create a scope, but the holding stmt may not # artificially create one here if needed sdirective = ''.join(d.s for d in directives) scoping = ('parallel', 'task', 'section') if any(s in sdirective for s in scoping): node = ast.If(ast.Num(1), [node], []) return node
def visit_Attribute(self, node): if node.attr in attributes: out = ast.Subscript( node.value, ast.Index(ast.Num(attributes[node.attr][1].val)), node.ctx) metadata.add(out, metadata.Attribute()) return out else: return node
def visit_AnyComp(self, node, comp_type, comp_module, comp_method): node.elt = self.visit(node.elt) name = "{0}_comprehension{1}".format(comp_type, self.count) self.count += 1 args = self.passmanager.gather(ImportedIds, node, self.ctx) self.count_iter = 0 starget = "__target" body = reduce(self.nest_reducer, reversed(node.generators), ast.Expr( ast.Call( ast.Attribute( ast.Name(comp_module, ast.Load()), comp_method, ast.Load()), [ast.Name(starget, ast.Load()), node.elt], [], None, None ) ) ) # add extra metadata to this node metadata.add(body, metadata.Comprehension(starget)) init = ast.Assign( [ast.Name(starget, ast.Store())], ast.Call( ast.Attribute( ast.Name('__builtin__', ast.Load()), comp_type, ast.Load() ), [], [], None, None) ) result = ast.Return(ast.Name(starget, ast.Load())) sargs = sorted(ast.Name(arg, ast.Param()) for arg in args) fd = ast.FunctionDef(name, ast.arguments(sargs, None, None, []), [init, body, result], []) self.ctx.module.body.append(fd) return ast.Call( ast.Name(name, ast.Load()), [ast.Name(arg.id, ast.Load()) for arg in sargs], [], None, None ) # no sharing !
def attach_data(self, node): if self.current: for curr in self.current: md = OMPDirective(curr) metadata.add(node, md) self.current = list() # add a Pass to hold some directives for field_name, field in ast.iter_fields(node): if field_name in GatherOMPData.statement_lists: if (field and isinstance(field[-1], ast.Expr) and self.isompdirective(field[-1].value)): field.append(ast.Pass()) self.generic_visit(node) return node
def visit_AnyComp(self, node): self.generic_visit(node.elt) generators = [self.visit(n) for n in node.generators] nnode = node for i, g in enumerate(generators): if isinstance(g, tuple): gtarget = "{0}{1}".format(g[0], i) nnode.generators[i].target = ast.Name( gtarget, nnode.generators[i].target.ctx) metadata.add( nnode.generators[i].target, metadata.LocalVariable()) nnode = _ConvertToTuple(gtarget, g[1]).visit(nnode) node.elt = nnode.elt node.generators = nnode.generators return node
def visit_AnyComp(self, node, *fields): for field in fields: setattr(node, field, self.visit(getattr(node, field))) generators = map(self.visit, node.generators) nnode = node for i, g in enumerate(generators): if isinstance(g, tuple): gtarget = "{0}{1}".format(g[0], i) nnode.generators[i].target = ast.Name( gtarget, nnode.generators[i].target.ctx) metadata.add( nnode.generators[i].target, metadata.LocalVariable()) nnode = _ConvertToTuple(gtarget, g[1]).visit(nnode) for field in fields: setattr(node, field, getattr(nnode, field)) node.generators = nnode.generators return node
def visit_For(self, node): target = node.target if isinstance(target, ast.Tuple) or isinstance(target, ast.List): renamings = dict() self.traverse_tuples(target, (), renamings) if renamings: elems = [x[0] for x in sorted(renamings.items(), key=itemgetter(1))] self.counter += 1 newname = "{0}{1}".format( NormalizeTuples.tuple_name, self.counter) node.target = ast.Name(newname, node.target.ctx) metadata.add(node.target, metadata.LocalVariable()) node.body.insert(0, ast.Assign([ ast.Tuple( [ast.Name(x, ast.Store()) for x in elems], ast.Store())], ast.Name(newname, ast.Load()) ) ) self.generic_visit(node) return node
def nest_reducer(self, x, g): def wrap_in_ifs(node, ifs): return reduce(lambda n, if_: ast.If(if_, [n], []), ifs, node) metadata.add(g.target, metadata.LocalVariable()) return ast.For(g.target, g.iter, [wrap_in_ifs(x, g.ifs)], [])