def pyx_codegen( algo, defs, program_model, pyx_header, vd_type=None, md_type=None, pregel_combine=False, ): """Transfer python to cython code with :code:`grape.GRAPECompiler`. Args: algo: class defination of algorithm. defs: list of function to be transfer. program_model: ProgramModel, 'Pregel' or 'PIE'. pyx_header: LinesWrapper, list of pyx source code. vd_type (str): vertex data type. md_type (str): message type. pregel_combine (bool): combinator in pregel model. """ class_name = getattr(algo, "__name__") compiler = GRAPECompiler(class_name, vd_type, md_type, program_model) pyx_body = LinesWrapper() for func_name in defs.keys(): func = getattr(algo, func_name) cycode = compiler.run(func, pyx_header) pyx_body.putline(cycode) # append code body # pyx_wrapper['pyx_code_body'].extend(pyx_code_body) wrap_init(algo, program_model, pyx_header, pyx_body, vd_type, md_type, pregel_combine)
def _pie_wrapper(vd_type, md_type, algo): if hasattr(algo, "__decorated__"): raise RuntimeError("Can't decorate on decorated class.") if not inspect.isclass(algo): raise ValueError( 'The decorator "pie" must be used on a class definition') defs = OrderedDict({ name: member for name, member in inspect.getmembers(algo) if name not in _BASE_MEMBERS and inspect.isroutine(member) }) _check_and_reorder(PIE_NECESSARY_DEFS, algo, defs) pyx_header = LinesWrapper() pyx_header.putline("from pie cimport AdjList") pyx_header.putline("from pie cimport Context") pyx_header.putline("from pie cimport Fragment") pyx_header.putline("from pie cimport PIEAggregateType") pyx_header.putline("from pie cimport Vertex") pyx_header.putline("from pie cimport VertexArray") pyx_header.putline("from pie cimport VertexRange") pyx_header.putline("from pie cimport MessageStrategy") pyx_header.putline("from pie cimport to_string") pyx_header.putline("") for line in _common_imports: pyx_header.putline(line) pyx_codegen(algo, defs, ProgramModel.PIE, pyx_header, vd_type, md_type) # pyx_codegen(algo, defs, pyx_wrapper, vd_type, md_type) return algo
def _pregel_wrapper(vd_type, md_type, algo): if hasattr(algo, "__decorated__"): raise RuntimeError("Can't decorate on decorated class.") if not inspect.isclass(algo): raise ValueError( 'The decorator "pie" must be used on a class definition') defs = OrderedDict({ name: member for name, member in inspect.getmembers(algo) if name not in _BASE_MEMBERS and inspect.isroutine(member) }) _check_and_reorder(PREGEL_NECESSARY_DEFS, algo, defs) enable_combine = False if PREGEL_COMBINE_DEF in defs.keys(): enable_combine = True pyx_header = LinesWrapper() pyx_header.putline("from pregel cimport Context") pyx_header.putline("from pregel cimport MessageIterator") pyx_header.putline("from pregel cimport PregelAggregatorType") pyx_header.putline("from pregel cimport Vertex") pyx_header.putline("from pregel cimport to_string") pyx_header.putline("") for line in _common_imports: pyx_header.putline(line) pyx_codegen( algo, defs, ProgramModel.Pregel, pyx_header, vd_type, md_type, enable_combine, ) return algo