示例#1
0
 def simple_array_test(self):
     py_ast = ast.parse("x = 0.5 + b * c\nreturn x")
     dfdag = nes.ast_to_dfdag(py_ast, {'b':(2,'nodes','modes'),'c':(2,'nodes','modes')})
     ct_builder = nes.dfdag_to_ctree(dfdag)
     ret_sym = ct_builder.return_symbol
     ret_sym.type = np.ctypeslib.ndpointer(dtype=np.float64)()
     c_ast = CFile(body=[
             CppInclude("stdlib.h"),
             FunctionDecl(
             None, "test_fun", 
             params = [
                 SymbolRef("b", np.ctypeslib.ndpointer(dtype=np.float64)()),
                 SymbolRef("c", np.ctypeslib.ndpointer(dtype=np.float64)()),
                 SymbolRef("nodes", ctypes.c_int()),
                 SymbolRef("modes", ctypes.c_int()),
                 ret_sym
                 ],
             defn = ct_builder.c_ast
             )
         ])
     mod = JitModule()
     submod = CFile("test_fun", [c_ast], path=CONFIG.get('jit','COMPILE_PATH'))._compile(c_ast.codegen())
     mod._link_in(submod)
     entry = c_ast.find(FunctionDecl, name="test_fun")             
     c_test_fun = mod.get_callable(entry.name, entry.get_type())     
     nodes = 19
     modes = 5
     a = np.random.rand(2, nodes, modes)
     b = np.random.rand(2, nodes, modes)
     
     res = np.zeros((2,nodes,modes))
     c_test_fun(a,b,nodes,modes,res)
     self.assertTrue(np.allclose( res, 0.5+a*b)) 
示例#2
0
文件: nodes.py 项目: i-Zaak/ctree
class Project(CommonNode):
    """Holds a list files."""

    _fields = ["files"]

    def __init__(self, files=None, indent=0, compilation_dir=""):
        self.files = files if files else []
        super(Project, self).__init__()
        self.compilation_dir = compilation_dir
        self.indent = indent

    def codegen(self, indent=0):
        """
        Code generates each file in the project and links their
        bytecode together to get the master bytecode file.
        """
        from ctree.jit import JitModule

        self._module = JitModule()

        # now that we have a concrete compilation dir, resolve references to it
        from ctree.transformations import ResolveGeneratedPathRefs

        #
        # resolver = ResolveGeneratedPathRefs(self._module.compilation_dir)
        # self.files = [resolver.visit(f) for f in self.files]
        # if resolver.count:
        #     log.info("automatically resolved %d GeneratedPathRef node(s).", resolver.count)

        # transform all files into llvm modules and link them into the master module
        for f in self.files:
            submodule = f._compile(f.codegen())
            if submodule:
                self._module._link_in(submodule)
        return self._module

    @property
    def module(self):
        if self._module:
            return self._module
        return self.codegen(indent=self.indent)
示例#3
0
class Project(CommonNode):
    """Holds a list files."""
    _fields = ['files']

    def __init__(self, files=None, indent=0, compilation_dir = ''):
        self.files = files if files else []
        super(Project, self).__init__()
        self.compilation_dir = compilation_dir
        self.indent = indent

    def codegen(self, indent=0):
        """
        Code generates each file in the project and links their
        bytecode together to get the master bytecode file.
        """
        from ctree.jit import JitModule

        self._module = JitModule()

        # now that we have a concrete compilation dir, resolve references to it
        from ctree.transformations import ResolveGeneratedPathRefs
        #
        # resolver = ResolveGeneratedPathRefs(self._module.compilation_dir)
        # self.files = [resolver.visit(f) for f in self.files]
        # if resolver.count:
        #     log.info("automatically resolved %d GeneratedPathRef node(s).", resolver.count)

        # transform all files into llvm modules and link them into the master module
        for f in self.files:
            submodule = f._compile(f.codegen())
            if submodule:
                self._module._link_in(submodule)
        return self._module

    @property
    def module(self):
        if self._module:
            return self._module
        return self.codegen(indent=self.indent)
示例#4
0
文件: nodes.py 项目: leonardt/ctree
    def codegen(self, indent=0):
        """
        Code generates each file in the project and links their
        bytecode together to get the master bytecode file.
        """
        from ctree.jit import JitModule

        module = JitModule()

        # now that we have a concrete compilation dir, resolve references to it
        from ctree.transformations import ResolveGeneratedPathRefs

        resolver = ResolveGeneratedPathRefs(module.compilation_dir)
        self.files = [resolver.visit(f) for f in self.files]
        log.info("automatically resolved %d GeneratedPathRef node(s).", resolver.count)

        # transform all files into llvm modules and link them into the master module
        for f in self.files:
            submodule = f._compile(f.codegen(), module.compilation_dir)
            if submodule:
                module._link_in(submodule)
        return module
示例#5
0
文件: nodes.py 项目: lowks/ctree
    def codegen(self, indent=0):
        """
        Code generates each file in the project and links their
        bytecode together to get the master bytecode file.
        """
        from ctree.jit import JitModule

        module = JitModule()

        # now that we have a concrete compilation dir, resolve references to it
        from ctree.transformations import ResolveGeneratedPathRefs

        resolver = ResolveGeneratedPathRefs(module.compilation_dir)
        self.files = [resolver.visit(f) for f in self.files]
        log.info("automatically resolved %d GeneratedPathRef node(s).", resolver.count)

        # transform all files into llvm modules and link them into the master module
        for f in self.files:
            submodule = f._compile(f.codegen(), module.compilation_dir)
            if submodule:
                module._link_in(submodule)
        return module