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 declare(self, converter): """ Returns a c++ statement declaring this object. """ if self.is_scalar(): return cpp_ast.Assign( cpp_ast.Value(self.scalar_t.ctype(), self.ref_name()), cpp_ast.CNumber(0)) else: self._declare = True return cpp_ast.Expression()
def gen_array_unpack(self): ret = [ cpp_ast.Assign( cpp_ast.Pointer(cpp_ast.Value("npy_double", "_my_" + x)), cpp_ast.TypeCast( cpp_ast.Pointer(cpp_ast.Value("npy_double", "")), cpp_ast.FunctionCall(cpp_ast.CName("PyArray_DATA"), params=[cpp_ast.CName(x)]))) for x in self.argdict.keys() ] return ret
def visit_OutputAssignment(self, node): return cpp_ast.Assign(self.visit(stencil_model.OutputElement()), self.visit(node.value))
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))
def visit_Assign(self, node): lhs = self.visit(node.targets[0]) rhs = self.visit(node.value) #find or create target model #pass pointer into opencall, or do what is necessary target = None try: target = self.get_or_create_model(lhs) except ValueError: pass value = None try: value = self.get_or_create_model(rhs) except TypeError: pass if value is not None: #RHS is a data object if target is not None: if type(target) != DataModelView: raise ValueError( "'%s' refers to a real buffer and cannot be reassigned to '%s'" % (str(lhs), str(rhs))) #TODO we allways assume here that the name must be assigned, which is not always the case. name = target.name self.data_model[name] = DataModelView(value, name) return cpp_ast.Assign(cpp_ast.CName(target.ref_name()), cpp_ast.CName(value.ref_name())) elif type(lhs) == cpp_ast.CName: self.data_model[lhs.name] = DataModelView(value, lhs.name) data_type = value.scalar_t.ctype() if value.is_scalar( ) else value.scalar_t.ctype() + "*" return cpp_ast.Assign(cpp_ast.Value(data_type, lhs.name), cpp_ast.CName(value.ref_name())) else: raise ValueError( "could not assign to '%s': not a data object or name" % str(lhs)) elif type(rhs) == OpenCall: if target is None and type(lhs) == cpp_ast.CName: params = rhs.get_output_params() self.data_model[lhs.name] = target = DataModel( params['type'], params['shape'], None, lhs.name) pre = target.declare(self) post = rhs.write_to(target, self) return cpp_ast.UnbracedBlock([pre, post]) elif target: return rhs.write_to(target, self) else: raise ValueError( "could not assign to '%s': not a data object or name" % str(lhs)) elif type(rhs) == cpp_ast.CNumber: if target is None and type(lhs) == cpp_ast.CName: self.data_model[lhs.name] = target = DataModel( type(rhs.num), [1], None, lhs.name) return cpp_ast.Initializer( cpp_ast.Value(target.scalar_t.ctype(), lhs.name), rhs.num) elif target: assert target.is_scalar( ), "'%s' is not a scalar data object" % target.name assert target.scalar_t.matches( type( rhs.num ) ), \ "Type mismatch: '%s' is type '%s', but '%s' is type '%s'" % ( target.name, target.scalar_t, rhs.num, type(rhs.num ) ) return cpp_ast.Initializer( cpp_ast.Value(taget.scalar_t.ctype(), target.name), rhs.num) else: raise ValueError( "could not assign to '%s': not a data object or name" % str(lhs)) else: raise ValueError("could not assign from '%s'" % str(rhs))