Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
 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)
Example #5
0
 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)
Example #6
0
 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())
Example #7
0
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)
Example #8
0
     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())
Example #9
0
     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) 
Example #10
0
 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())