def gen_loops(self, node): dim = len(self.output_grid.shape) ret_node = None cur_node = None for d in xrange(dim): dim_var = self.gen_fresh_var() self.dim_vars.append(dim_var) initial = cpp_ast.CNumber(self.output_grid.ghost_depth) end = cpp_ast.CNumber(self.output_grid.shape[d] - self.output_grid.ghost_depth - 1) increment = cpp_ast.CNumber(1) if d == 0: ret_node = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node = ret_node elif d == dim - 2: cur_node.body = StencilConvertASTCilk.CilkFor( dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node = cur_node.body else: cur_node.body = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node = cur_node.body return (cur_node, ret_node)
def visit_interior_kernel(self, node): cur_node, ret_node = self.gen_loops(node) body = cpp_ast.Block() self.output_index_var = cpp_ast.CName(self.gen_fresh_var()) body.append(cpp_ast.Value("int", self.output_index_var)) body.append( cpp_ast.Assign( self.output_index_var, self.gen_array_macro(self.output_grid_name, [cpp_ast.CName(x) for x in self.dim_vars]))) replaced_body = None for gridname in self.argdict.keys(): replaced_body = [ ast_tools.ASTNodeReplacer(ast.Name(gridname, None), ast.Name("_my_" + gridname, None)).visit(x) for x in node.body ] body.extend([self.visit(x) for x in replaced_body]) cur_node.body = body return ret_node
def visit_StencilModel(self, node): self.argdict = dict() for i in range(len(node.input_grids)): self.var_names.append(node.input_grids[i].name) self.argdict[node.input_grids[i].name] = self.input_grids[i] self.argdict[self.output_grid_name] = self.output_grid assert node.border_kernel.body == [], 'Border kernels not yet implemented' func_name = "kernel" arg_names = [x.name for x in node.input_grids] + [self.output_grid_name] args = [ cpp_ast.Pointer(cpp_ast.Value("PyObject", x)) for x in arg_names ] body = cpp_ast.Block() # generate the code to unpack arrays into C++ pointers and macros for accessing # the arrays body.extend([self.gen_array_macro_definition(x) for x in self.argdict]) body.extend(self.gen_array_unpack()) body.append(self.visit_interior_kernel(node.interior_kernel)) return cpp_ast.FunctionBody( cpp_ast.FunctionDeclaration(cpp_ast.Value("void", func_name), args), body)
def gen_loops(self, node): dim = len(self.output_grid.shape) ret_node = None cur_node = None def add_one(n): if self.inject_failure == 'loop_off_by_one': return cpp_ast.CNumber(n.num + 1) else: return n for d in xrange(dim): dim_var = self.gen_fresh_var() self.dim_vars.append(dim_var) initial = cpp_ast.CNumber(self.output_grid.ghost_depth) end = cpp_ast.CNumber(self.output_grid.shape[d] - self.output_grid.ghost_depth - 1) increment = cpp_ast.CNumber(1) if d == 0: ret_node = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node = ret_node elif d == dim - 2: # add OpenMP parallel pragma to 2nd innermost loop pragma = cpp_ast.Pragma("omp parallel for") for_node = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node.body = cpp_ast.Block(contents=[pragma, for_node]) cur_node = for_node elif d == dim - 1: # add ivdep pragma to innermost node pragma = cpp_ast.Pragma("ivdep") for_node = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node.body = cpp_ast.Block(contents=[pragma, for_node]) cur_node = for_node else: cur_node.body = cpp_ast.For(dim_var, add_one(initial), add_one(end), increment, cpp_ast.Block()) cur_node = cur_node.body return (cur_node, ret_node)
def visit_FunctionDef(self, node): declarator = cpp_ast.Value('void', node.name) args = self.visit(node.args) body = cpp_ast.Block([self.visit(x) for x in node.body]) return declarator, args, body
def visit_For(self, node): _iters = self.visit(node.iter) targets = self.visit(node.target) #are we iterating over a data object? if type(_iters) is cpp_ast.CName and _iters.name in self.data_model: _iters = [_iters] targets = [targets] #co-iterating over a set of datasets! if type(_iters) is list: #set up the loop variable and weight self.loopvar.append(self.loopvar[-1] + 'i') loopvar = cpp_ast.CName(self.loopvar[-1]) body = [] weight_loop = self.weighted and reduce(lambda a, b: a or b, [ (_iter.name in self.data_model and self.data_model[_iter.name].should_subsample) for _iter in _iters ]) if weight_loop: self.aggregate_on(loopvar) # add the temporary children to the data model for target, _iter in zip(targets, _iters): if not (type(_iter) is cpp_ast.CName and _iter.name in self.data_model): raise ValueError( "Cannot iterate over unknown data object: '%s'" % _iter.name) parent_model = self.data_model[_iter.name] if type(target) is not cpp_ast.CName: raise ValueError("Not a valid iteration target: '%s'" % str(target)) if target.name in self.data_model: raise ValueError("Cannot reuse iteration target: '%s'" % target.name) target_model = self.data_model[ target.name] = parent_model.branch(name=target.name) ctype = target_model.scalar_t.ctype() decl = cpp_ast.Value( ctype, target.name) if target_model.is_scalar( ) else cpp_ast.Pointer(cpp_ast.Value(ctype, target.name)) init = parent_model.declare_child(target_model, loopvar) #init = cpp_ast.Subscript( _iter, loopvar) if target_model.is_scalar() \ # else cpp_ast.BinOp( _iter, '+', cpp_ast.BinOp( loopvar , '*', parent_model.element_size() ) ) body.append(cpp_ast.Assign(decl, init)) # visit the body of the for body += [self.visit(x) for x in node.body] # generate the C for intializer ret = cpp_ast.RawFor( cpp_ast.Assign( cpp_ast.Value( 'int', loopvar ), cpp_ast.CNumber(0) ), \ cpp_ast.Compare( loopvar, '<', cpp_ast.CNumber( self.data_model[ _iter.name ].dimensions[0] ) ), \ cpp_ast.UnaryOp( '++', loopvar ), cpp_ast.Block( body ) ) # remove temporary children from data model and otherwise clean up self.loopvar = self.loopvar[:-1] for target in targets: del self.data_model[target.name] if weight_loop: self.aggregate_off(loopvar) # return return ret #or perhaps, a known range. elif isinstance(_iters, cpp_ast.CNumber): return cpp_ast.RawFor( cpp_ast.Assign( cpp_ast.Value( 'int', targets ), cpp_ast.CNumber(0) ), \ cpp_ast.Compare( targets, '<', _iter ), cpp_ast.UnaryOp( '++', target ), \ cpp_ast.Block([ self.visit( x ) for x in node.body ] ) ) else: raise ValueError( 'Loop iterand "%s" is not a numeric expression or known data object' % str(_iters))