def build_test_trees(): template_code = """ int main (int a) { printf(" Hello World!"); } """ ast = parse_source(template_code, 'helloWorld_test') Dump.save('Backends/C/tests/trees/helloWorld_tree', ast) template_code = """ int main() { int i; int sum[10]; for (i = 0; i <= 10; i++) { sum[i] = i; } #pragma omp parallel for reduction(+ : sum) for (i = 0; i <= 10; i++) { sum[i] = i; } } """ ast = parse_source(template_code, 'pragma_test') Dump.save('Backends/C/tests/trees/pragma_tree', ast) template_code = open('Backends/C/tests/codes/jacobi_big.c', 'r').read() ast = parse_source(template_code, 'jacobi_c') Dump.save('Backends/C/tests/trees/jacobi_tree', ast)
def test_mandel(self): """ Test mandel source """ template_code = open(CODE_PATH + '/mandel.c', 'r').read() ast = parse_source(template_code, 'mandel_test') new_ast = AstToIR(Writer = CUDAWriter).transform(ast) good_tree = Dump.load(TREE_PATH + '/mandel_tree') self.check_output(new_ast, good_tree)
def build_tools_tree(): """ Builds the tests AST for both pi.c and pi.cu """ template_code = ''' int main() { int f = 0; int i = 0; i = 7; } ''' new_ast = parse_source(template_code, "Tool test 1") # # Transform the C ast into the internal representation # new_ast = AstToIR(Writer = OmpWriter).transform(ast) # template_code = """ # int main() { # int f = 0; # f = 7; # } # """ # declarations = AstToIR(Writer = OmpWriter).transform(parse_source(template_code, "Tool test 2")).ext[-1].body.decls # from Tools.Tree import InsertTool, ReplaceTool, RemoveTool # InsertTool(subtree = declarations, position = "begin").apply(new_ast.ext[-1].body, 'decls') Dump.save(TREE_PATH + '/insert_tool_tree', new_ast)
def test_jacobicu(self): """ Test mutating mandel to cuda """ template_code = open(CODE_PATH + '/jacobi.c', 'r').read() ast = parse_source(template_code, 'jacobi_test') tmp = AstToIR(Writer = CUDAWriter).transform(ast) new_ast = CM_OmpParallel().apply_all(tmp) good_tree = Dump.load(TREE_PATH + '/jacobicu_tree') self.check_output(new_ast, good_tree)
def test_jacobi(self): template_code = open('Backends/C/tests/codes/jacobi_big.c', 'r').read() ast = parse_source(template_code, 'jacobi_test') self.good_tree = Dump.load('Backends/C/tests/trees/jacobi_tree') ast_str = StringIO(); good_str = StringIO(); ast.show(ast_str) self.good_tree.show(good_str) self.assertEqual(ast_str.getvalue(), good_str.getvalue())
def build_jacobi_tree(): """ Builds the tests AST for both pi.c and pi.cu """ # Pi template_code = open(CODE_PATH + '/jacobi.c', 'r').read() ast = parse_source(template_code, 'pi_test') Dump.save(TREE_PATH + '/jacobi_tree', ast) # CUDA version new_ast = AstToIR(Writer = CUDAWriter).transform(ast) tmp = CM_OmpParallel().apply_all(new_ast) Dump.save(TREE_PATH + '/jacobicu_tree', tmp)
def test_Tools(self): """ Test common tools """ template_code = ''' int main() { int i = 0; i = 7; } ''' ast = parse_source(template_code, "Tool test 1") # Transform the C ast into the internal representation new_ast = AstToIR(Writer = OmpWriter).transform(ast) template_code = """ int main() { int f = 0; f = 7; } """ tmp = AstToIR(Writer = OmpWriter).transform(parse_source(template_code, "Tool test 2")).ext[-1].body declarations = tmp.decls statements = tmp.stmts from Tools.Tree import InsertTool, ReplaceTool, RemoveTool, CloneTool InsertTool(subtree = declarations, position = "begin").apply(new_ast.ext[-1].body, 'decls') self.assertEqual(new_ast.ext[-1].body.decls[-1].parent, new_ast.ext[-1].body) # Clone the last statement # InsertTool(subtree = c_ast.Compound(stmts = [new_ast.ext[-1].body.stmts[-1].__deepcopy__({}),], decls = []), position = 'begin').apply(new_ast.ext[-1].body, 'stmts') CloneTool(original = new_ast.ext[-1].body.stmts[-1], position = 'begin').apply(new_ast.ext[-1].body, 'stmts') # Check replace tool ReplaceTool(new_node = statements[-1], old_node = new_ast.ext[-1].body.stmts[-1]).apply(new_ast.ext[-1].body, 'stmts') # Check if parent link is preserved self.assertEqual(new_ast.ext[-1].body.stmts[-1].parent, new_ast.ext[-1].body) # Check remove tool RemoveTool(target_node = new_ast.ext[-1].body.stmts[-1]).apply(new_ast.ext[-1].body, 'stmts') # Check parent links self.assertEqual(new_ast.ext[-1].body.decls[-1].parent, new_ast.ext[-1].body) # Tree must be the same as original good_tree = Dump.load(TREE_PATH + '/insert_tool_tree') self.check_output(new_ast, good_tree)
def build_mandel_tree(): """ Builds the tests AST for both mandel.c and mandel.cu """ # [CODE_PATH, TREE_PATH] = getPath( # Mandel template_code = open(CODE_PATH + '/mandel.c', 'r').read() ast = parse_source(template_code, 'mandel_test') Dump.save(TREE_PATH + '/mandel_tree', ast) tmp = AstToIR(Writer = CUDAWriter).transform(ast) # CUDA version new_ast = CM_OmpParallelFor().apply_all(tmp) Dump.save(TREE_PATH + '/mandelcu_tree', new_ast)
def test_helloWorld(self): template_code = """ int main (int a) { printf(" Hello World!"); } """ ast = parse_source(template_code, 'helloWorld_test') self.good_tree = Dump.load('Backends/C/tests/trees/helloWorld_tree') ast_str = StringIO(); good_str = StringIO(); ast.show(ast_str) self.good_tree.show(good_str) self.assertEqual(ast_str.getvalue(), good_str.getvalue())
def parse_snippet(self, template_code, subs_dir, name, show=False): """ Transform a template code snippet to the IR :param template_code: String with the source code :param subs_dir: Dictionary with substutitions :param name: Name of the template to be parsed (debugging) :param show: Show the template after variable substitution (debugging) :return: Representation of the parsed template """ subtree = None if subs_dir: template_code = TemplateParser(template_code).render(**subs_dir) if show: print " Template " + str(template_code) try: subtree = parse_source(template_code, name) except c_parser.ParseError, e: print "Parse error:" + str(e) return None
def test_pragma(self): template_code = """ int main() { int i; int sum[10]; for (i = 0; i <= 10; i++) { sum[i] = i; } #pragma omp parallel for reduction(+ : sum) for (i = 0; i <= 10; i++) { sum[i] = i; } } """ ast = parse_source(template_code, 'pragma_test') self.good_tree = Dump.load('Backends/C/tests/trees/pragma_tree') ast_str = StringIO(); good_str = StringIO(); ast.show(ast_str) self.good_tree.show(good_str) self.assertEqual(ast_str.getvalue(), good_str.getvalue())
else: print ">>> File not found!" exit() ###################### First Layer : File parsing # Parse file from Frontend.Parse import parse_source print "Parsing " + filename + " .... ", template_code = " ".join(open(filename, 'r').readlines()) ast = parse_source(template_code, filename) print " OK " print " Migrating to Internal Representation ...." from Frontend.InternalRepr import AstToIR # Transform the C ast into the internal representation new_ast = AstToIR(Writer = CUDAWriter).transform(ast) print " OK " ###################### Second Layer : Transformation tools