def main(): stmt0 = Assign(SymbolRef('foo'), Constant(123.4)) stmt1 = FunctionDecl(Float(), SymbolRef("bar"), [ SymbolRef("spam", Int()), SymbolRef("eggs", Long())], [String("baz")]) stmt3 = [[SymbolRef("AAAAA")]] tree = CFile("myfile", [stmt0, stmt1, stmt3]) print (to_dot(tree))
def main(): stmt0 = Assign(SymbolRef('foo'), Constant(123.4)) stmt1 = FunctionDecl(Float(), SymbolRef("bar"), [SymbolRef("spam", Int()), SymbolRef("eggs", Long())], [String("baz")]) stmt3 = [[SymbolRef("AAAAA")]] tree = CFile("myfile", [stmt0, stmt1, stmt3]) print(to_dot(tree))
def dot_ast_to_browser(ast_node, file_name): from ctree.dotgen import to_dot dot_text = to_dot(ast_node) dot_output = DotManager.run_dot(dot_text) with open(file_name, "wb") as f: f.write(dot_output) import subprocess subprocess.check_output(["open", file_name])
def dot_ast_to_browser(ast_node, file_name): from ctree.dotgen import to_dot dot_text = to_dot(ast_node) dot_output = DotManager.run_dot(dot_text) with open(file_name, "wb") as f: f.write(dot_output) import subprocess subprocess.check_output(["open", file_name])
def transform(self, py_ast, program_config): """ Convert the Python AST to a C AST according to the directions given in program_config. """ len_A, A_dtype, A_ndim, A_shape = program_config[0] A_type = NdPointer(A_dtype, A_ndim, A_shape) apply_one = PyBasicConversions().visit(py_ast.body[0]) apply_one_typesig = FuncType(A_type.get_base_type(), [A_type.get_base_type()]) apply_one.set_typesig(apply_one_typesig) apply_kernel = FunctionDecl(Void(), "apply_kernel", params=[SymbolRef("A", A_type)], defn=[ Assign(SymbolRef("i", Int()), FunctionCall(SymbolRef("get_global_id"), [Constant(0)])), If(Lt(SymbolRef("i"), Constant(len_A)), [ Assign(ArrayRef(SymbolRef("A"), SymbolRef("i")), FunctionCall(SymbolRef("apply"), [ArrayRef(SymbolRef("A"), SymbolRef("i"))])) ]) ]) # add opencl type qualifiers apply_kernel.set_kernel() apply_kernel.params[0].set_global() kernel = OclFile("kernel", [apply_one, apply_kernel]) template_args = { 'array_decl': SymbolRef("data", A_type), 'array_ref': SymbolRef("data"), 'count': Constant(len_A), 'kernel_path': kernel.get_generated_path_ref(), 'kernel_name': String(apply_kernel.name), } template_path = os.path.join(os.getcwd(), "templates", "OclDoubler.tmpl.c") control = CFile("control", [ FileTemplate(template_path, template_args), ]) tree = Project([kernel, control]) with open("graph.dot", 'w') as f: f.write( to_dot(tree) ) entry_point_typesig = FuncType(Int(), [A_type]).as_ctype() return tree, entry_point_typesig
def transform(self, py_ast, program_config): """ Convert the Python AST to a C AST according to the directions given in program_config. """ arg_config, tuner_config = program_config len_A = arg_config['A_len'] A_dtype = arg_config['A_dtype'] A_ndim = arg_config['A_ndim'] A_shape = arg_config['A_shape'] inner_type = get_ctree_type(A_dtype) array_type = NdPointer(A_dtype, A_ndim, A_shape) apply_one_typesig = FuncType(inner_type, [inner_type]) template_entries = { 'array_decl': SymbolRef("A", array_type), 'array_ref' : SymbolRef("A"), 'num_items' : Constant(len_A), } tree = CFile("generated", [ py_ast.body[0], StringTemplate("""\ void apply_all($array_decl) { for (int i = 0; i < $num_items; i++) { $array_ref[i] = apply( $array_ref[i] ); } } """, template_entries) ]) tree = PyBasicConversions().visit(tree) apply_one = tree.find(FunctionDecl, name="apply") apply_one.set_static().set_inline() apply_one.set_typesig(apply_one_typesig) with open("graph.dot", 'w') as f: f.write( to_dot(tree) ) entry_point_typesig = FuncType(Void(), [array_type]).as_ctype() return Project([tree]), entry_point_typesig
def test_c_identity(self): self.assertNotEqual(to_dot(identity_ast), "")
def test_c_gcd(self): self.assertNotEqual(to_dot(gcd_ast), "")
def test_py_fib(self): self.assertNotEqual(to_dot(get_ast(fib)), "")
def test_cl_mem_dot(self): from ctree.dotgen import to_dot to_dot(SymbolRef("foo", cl_mem()))
def test_py_identity(self): self.assertNotEqual(to_dot(get_ast(identity)), "")
def test_py_gcd(self): self.assertNotEqual(to_dot(get_ast(gcd)), "")
def dot_ast_to_image(ast_node): from ctree.dotgen import to_dot dot_text = to_dot(ast_node) return DotManager.dot_text_to_image(dot_text)
def test_c_l2norm(self): self.assertNotEqual(to_dot(l2norm_ast), "")
def test_py_identity(self): self.assertNotEqual(to_dot(get_ast(identity)), "")
def test_py_fib(self): self.assertNotEqual(to_dot(get_ast(fib)), "")
def test_c_fib(self): self.assertNotEqual(to_dot(fib_ast), "")
def test_file_template_dotgen(self): from ctree.c.nodes import String path = os.path.join(*(fixtures.__path__ + ["templates", "printf.tmpl.c"])) tree = FileTemplate(path, {'fmt': String('Hello, world!')}) to_dot(tree)
def dot_ast_to_image(ast_node): from ctree.dotgen import to_dot dot_text = to_dot(ast_node) return DotManager.dot_text_to_image(dot_text)
def test_dotgen(self): tree = StringTemplate("return $one $two", { 'one': Constant(1), 'two': Constant(2), }) dot = to_dot(tree)
def test_cl_mem_dot(self): from ctree.dotgen import to_dot to_dot(SymbolRef("foo", cl_mem()))
def test_c_l2norm(self): self.assertNotEqual(to_dot(l2norm_ast), "")
def test_c_identity(self): self.assertNotEqual(to_dot(identity_ast), "")
def test_py_gcd(self): self.assertNotEqual(to_dot(get_ast(gcd)), "")
def test_c_gcd(self): self.assertNotEqual(to_dot(gcd_ast), "")
def test_c_fib(self): self.assertNotEqual(to_dot(fib_ast), "")
def test_lol_1(self): tree = Block([[a, b]]) to_dot(tree)