Beispiel #1
0
def test_ccode_codegen_ast():
    assert ccode(Comment("this is a comment")) == "// this is a comment"
    assert ccode(While(abs(x) > 1,
                       [aug_assign(x, '-', 1)])) == ('while (fabs(x) > 1) {\n'
                                                     '   x -= 1;\n'
                                                     '}')
    assert ccode(Scope([AddAugmentedAssignment(x, 1)])) == ('{\n'
                                                            '   x += 1;\n'
                                                            '}')
    inp_x = Declaration(Variable(x, type=real))
    assert ccode(FunctionPrototype(real, 'pwer',
                                   [inp_x])) == 'double pwer(double x)'
    assert ccode(
        FunctionDefinition(
            real, 'pwer', [inp_x],
            [Assignment(x, x**2)])) == ('double pwer(double x){\n'
                                        '   x = pow(x, 2);\n'
                                        '}')

    # Elements of CodeBlock are formatted as statements:
    block = CodeBlock(
        x,
        Print([x, y], "%d %d"),
        FunctionCall('pwer', [x]),
        Return(x),
    )
    assert ccode(block) == '\n'.join([
        'x;',
        'printf("%d %d", x, y);',
        'pwer(x);',
        'return x;',
    ])
Beispiel #2
0
def test_ccode_codegen_ast():
    assert ccode(Comment("this is a comment")) == "// this is a comment"
    assert ccode(While(abs(x) > 1, [aug_assign(x, "-", 1)])) == (
        "while (fabs(x) > 1) {\n" "   x -= 1;\n" "}"
    )
    assert ccode(Scope([AddAugmentedAssignment(x, 1)])) == ("{\n" "   x += 1;\n" "}")
    inp_x = Declaration(Variable(x, type=real))
    assert ccode(FunctionPrototype(real, "pwer", [inp_x])) == "double pwer(double x)"
    assert ccode(
        FunctionDefinition(real, "pwer", [inp_x], [Assignment(x, x ** 2)])
    ) == ("double pwer(double x){\n" "   x = pow(x, 2);\n" "}")

    # Elements of CodeBlock are formatted as statements:
    block = CodeBlock(x, Print([x, y], "%d %d"), FunctionCall("pwer", [x]), Return(x),)
    assert ccode(block) == "\n".join(
        ["x;", 'printf("%d %d", x, y);', "pwer(x);", "return x;",]
    )
Beispiel #3
0
def test_ccode_codegen_ast():
    # Note that C only allows comments of the form /* ... */, double forward
    # slash is not standard C, and some C compilers will grind to a halt upon
    # encountering them.
    assert ccode(
        Comment("this is a comment")) == "/* this is a comment */"  # not //
    assert ccode(While(abs(x) > 1,
                       [aug_assign(x, '-', 1)])) == ('while (fabs(x) > 1) {\n'
                                                     '   x -= 1;\n'
                                                     '}')
    assert ccode(Scope([AddAugmentedAssignment(x, 1)])) == ('{\n'
                                                            '   x += 1;\n'
                                                            '}')
    inp_x = Declaration(Variable(x, type=real))
    assert ccode(FunctionPrototype(real, 'pwer',
                                   [inp_x])) == 'double pwer(double x)'
    assert ccode(
        FunctionDefinition(
            real, 'pwer', [inp_x],
            [Assignment(x, x**2)])) == ('double pwer(double x){\n'
                                        '   x = pow(x, 2);\n'
                                        '}')

    # Elements of CodeBlock are formatted as statements:
    block = CodeBlock(
        x,
        Print([x, y], "%d %d"),
        FunctionCall('pwer', [x]),
        Return(x),
    )
    assert ccode(block) == '\n'.join([
        'x;',
        'printf("%d %d", x, y);',
        'pwer(x);',
        'return x;',
    ])
Beispiel #4
0
bc_tmps = []
for e in bc_eqs2:
    bc_tmps.append(Variable(e.lhs, type=templateT).as_Declaration(value=e.rhs))

body = [tmp_init]
body.extend(bc_tmps)
body.extend([
    convert_eq_to_assignment(teq1c),
    convert_eq_to_assignment(teq2c), loop1,
    convert_eq_to_assignment(teq5c), loop2
])

algo = CodeBlock(*body)

# Set up to create a template function
tx = Pointer(x, type=templateT)
ty = Pointer(y, type=templateT)
ty2 = Pointer(y2, type=templateT)
yp0_var = Variable('yp0', type=templateT)
ypn_var = Variable('ypn', type=templateT)

tf = TemplateFunctionDefinition(void, "CubicSplineSolve",
                                [tx, ty, n, yp0_var, ypn_var, ty2],
                                [templateT], algo)

ACP = ACodePrinter()
gen_comment = Comment("Generated by gen_cubic_spline_solver.py")
cb = CodeBlock(gen_comment, tf)
s = ACP.doprint(cb)
print(s)
Beispiel #5
0
def test_Comment():
    c = Comment('foobar')
    assert c.text == 'foobar'
    assert str(c) == 'foobar'
Beispiel #6
0
    def gen_function(self, kernel_name, kernel_arg_type_name):
        main_block = []
        main_block.append(
            Comment(
                "Output iteration space %s reduced to %s iteration spaces" %
                (self._max_shape, len(self._indexes))))
        # dereference all symbols that are not indexed
        dereference = [
            symbol for symbol in self._symbol_indexes
            if not self._symbol_indexes[symbol]
        ]

        for expr_state in self._expr_states:
            free_symbols = [
                symbol for symbol in expr_state.expr.free_symbols
                if not symbol in self._temporaries
            ]
            # create substitutions for symbols that are indexed
            subs = [(symbol,
                     indexed_symbol(symbol, [
                         self._indexes[idx][1]
                         for idx in self._symbol_indexes[symbol]
                     ], [
                         self._indexes[idx][0]
                         for idx in self._symbol_indexes[symbol]
                     ])) for symbol in free_symbols
                    if self._symbol_indexes[symbol]]
            # indexed results. non indexed temps
            if not expr_state.is_intermediate:
                main_block.append(
                    Comment("Produce output symbol %s" % expr_state.symbol))
                res_symbol = IndexedBase(
                    expr_state.symbol,
                    shape=tuple(tuple(index[1] for index in self._indexes)))
                res_symbol = res_symbol[tuple(index[0]
                                              for index in self._indexes)]
            else:
                main_block.append(
                    Comment("Produce intermediate symbol %s" %
                            expr_state.symbol))
                res_symbol = expr_state.symbol
            main_block.append(
                Assignment(res_symbol, expr_state.expr.subs(subs)))

        # create all the for loops
        def func(block, idx):
            return [
                VarFor(Declaration(Variable(Symbol(idx[0]), type=uint32)),
                       VarRange(idx[2][0], idx[2][1], Integer(1)), block)
            ]

        for_block = reduce(func, self._indexes[::-1], main_block)[0]
        # Create the initial code with args assignments and paralellization calcluation
        ast = self.setup_codegen()
        # declare temporaries
        ast.extend([
            Declaration(Variable(symbol, type=int32))
            for symbol in self._temporaries
        ])
        ast.append(for_block)
        ast.append(FunctionCall("gap_waitbarrier", (Integer(0), )))
        # create function definition
        func_def = FunctionDefinition(
            VOID, kernel_name,
            [Pointer("Args", type=Type(kernel_arg_type_name))], ast)
        # generate code
        return ccode(func_def,
                     contract=False,
                     dereference=dereference,
                     user_functions=CFUNC_DEFS,
                     type_mappings=TYPE_MAPPINGS)