def get_elwise_module_descriptor(arguments, operation, name="kernel"): from codepy.bpl import BoostPythonModule from codepy.cgen import FunctionBody, FunctionDeclaration, \ Value, POD, Struct, For, Initializer, Include, Statement, \ Line, Block S = Statement mod = BoostPythonModule() mod.add_to_preamble([ Include("pyublas/numpy.hpp"), ]) mod.add_to_module([ S("namespace ublas = boost::numeric::ublas"), S("using namespace pyublas"), Line(), ]) body = Block([ Initializer( Value("numpy_array<%s >::iterator" % dtype_to_ctype(varg.dtype), varg.name), "args.%s_ary.begin()" % varg.name) for varg in arguments if isinstance(varg, VectorArg)] +[Initializer( sarg.declarator(), "args." + sarg.name) for sarg in arguments if isinstance(sarg, ScalarArg)] ) body.extend([ Line(), For("unsigned i = 0", "i < codepy_length", "++i", Block([S(operation)]) ) ]) arg_struct = Struct("arg_struct", [arg.declarator() for arg in arguments]) mod.add_struct(arg_struct, "ArgStruct") mod.add_to_module([Line()]) mod.add_function( FunctionBody( FunctionDeclaration( Value("void", name), [POD(numpy.uintp, "codepy_length"), Value("arg_struct", "args")]), body)) return mod
def declarator(self): return Value("numpy_array<%s >" % dtype_to_ctype(self.dtype), self.name+"_ary")