cbackend.py 16.7 KB
Newer Older
Martin Bauer's avatar
Martin Bauer committed
1
import sympy as sp
Martin Bauer's avatar
Martin Bauer committed
2
3
from collections import namedtuple
from sympy.core import S
Martin Bauer's avatar
Martin Bauer committed
4
from typing import Optional, Set
Martin Bauer's avatar
Martin Bauer committed
5

Martin Bauer's avatar
Martin Bauer committed
6
7
try:
    from sympy.printing.ccode import C99CodePrinter as CCodePrinter
Martin Bauer's avatar
Martin Bauer committed
8
9
except ImportError:
    from sympy.printing.ccode import CCodePrinter  # for sympy versions < 1.1
Martin Bauer's avatar
Martin Bauer committed
10

Martin Bauer's avatar
Martin Bauer committed
11
12
from pystencils.integer_functions import bitwise_xor, bit_shift_right, bit_shift_left, bitwise_and, \
    bitwise_or, modulo_floor
13
from pystencils.astnodes import Node, ResolvedFieldAccess, SympyAssignment
Martin Bauer's avatar
Martin Bauer committed
14
from pystencils.data_types import create_type, PointerType, get_type_of_expression, VectorType, cast_func
Martin Bauer's avatar
Martin Bauer committed
15
from pystencils.backends.simd_instruction_sets import selected_instruction_set
16

Martin Bauer's avatar
Martin Bauer committed
17
__all__ = ['generate_c', 'CustomCppCode', 'PrintNode', 'get_headers', 'CustomSympyPrinter']
18

Martin Bauer's avatar
Martin Bauer committed
19

Martin Bauer's avatar
Martin Bauer committed
20
def generate_c(ast_node: Node, signature_only: bool = False, use_float_constants: Optional[bool] = None) -> str:
Martin Bauer's avatar
Martin Bauer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
    """Prints an abstract syntax tree node as C or CUDA code.

    This function does not need to distinguish between C, C++ or CUDA code, it just prints 'C-like' code as encoded
    in the abstract syntax tree (AST). The AST is built differently for C or CUDA by calling different create_kernel
    functions.

    Args:
        ast_node:
        signature_only:
        use_float_constants:

    Returns:
        C-like code for the ast node and its descendants
Martin Bauer's avatar
Martin Bauer committed
34
    """
Martin Bauer's avatar
Martin Bauer committed
35
36
37
38
    if use_float_constants is None:
        field_types = set(o.field.dtype for o in ast_node.atoms(ResolvedFieldAccess))
        double = create_type('double')
        use_float_constants = double not in field_types
39

Martin Bauer's avatar
Martin Bauer committed
40
    vector_is = selected_instruction_set['double']
Martin Bauer's avatar
Martin Bauer committed
41
42
43
    printer = CBackend(constants_as_floats=use_float_constants, signature_only=signature_only,
                       vector_instruction_set=vector_is)
    return printer(ast_node)
44
45


Martin Bauer's avatar
Martin Bauer committed
46
47
def get_headers(ast_node: Node) -> Set[str]:
    """Return a set of header files, necessary to compile the printed C-like code."""
48
49
    headers = set()

Martin Bauer's avatar
Martin Bauer committed
50
51
52
53
    if hasattr(ast_node, 'headers'):
        headers.update(ast_node.headers)
    elif isinstance(ast_node, SympyAssignment):
        if type(get_type_of_expression(ast_node.rhs)) is VectorType:
Martin Bauer's avatar
Martin Bauer committed
54
            headers.update(selected_instruction_set['double']['headers'])
55

Martin Bauer's avatar
Martin Bauer committed
56
    for a in ast_node.args:
57
        if isinstance(a, Node):
Martin Bauer's avatar
Martin Bauer committed
58
            headers.update(get_headers(a))
59
60

    return headers
61
62


63
64
65
66
# --------------------------------------- Backend Specific Nodes -------------------------------------------------------


class CustomCppCode(Node):
Martin Bauer's avatar
Martin Bauer committed
67
68
    def __init__(self, code, symbols_read, symbols_defined, parent=None):
        super(CustomCppCode, self).__init__(parent=parent)
69
        self._code = "\n" + code
Martin Bauer's avatar
Martin Bauer committed
70
71
        self._symbolsRead = set(symbols_read)
        self._symbolsDefined = set(symbols_defined)
72
        self.headers = []
73
74
75
76
77
78
79
80
81
82

    @property
    def code(self):
        return self._code

    @property
    def args(self):
        return []

    @property
Martin Bauer's avatar
Martin Bauer committed
83
    def symbols_defined(self):
84
85
86
        return self._symbolsDefined

    @property
Martin Bauer's avatar
Martin Bauer committed
87
88
    def undefined_symbols(self):
        return self.symbols_defined - self._symbolsRead
89
90
91


class PrintNode(CustomCppCode):
Martin Bauer's avatar
Martin Bauer committed
92
93
94
95
    # noinspection SpellCheckingInspection
    def __init__(self, symbol_to_print):
        code = '\nstd::cout << "%s  =  " << %s << std::endl; \n' % (symbol_to_print.name, symbol_to_print.name)
        super(PrintNode, self).__init__(code, symbols_read=[symbol_to_print], symbols_defined=set())
96
        self.headers.append("<iostream>")
97
98
99
100


# ------------------------------------------- Printer ------------------------------------------------------------------

101

Martin Bauer's avatar
Martin Bauer committed
102
103
# noinspection PyPep8Naming
class CBackend:
104

Martin Bauer's avatar
Martin Bauer committed
105
106
107
108
    def __init__(self, constants_as_floats=False, sympy_printer=None,
                 signature_only=False, vector_instruction_set=None):
        if sympy_printer is None:
            if vector_instruction_set is not None:
Martin Bauer's avatar
Martin Bauer committed
109
                self.sympy_printer = VectorizedCustomSympyPrinter(vector_instruction_set, constants_as_floats)
110
            else:
Martin Bauer's avatar
Martin Bauer committed
111
                self.sympy_printer = CustomSympyPrinter(constants_as_floats)
112
        else:
Martin Bauer's avatar
Martin Bauer committed
113
            self.sympy_printer = sympy_printer
114

Martin Bauer's avatar
Martin Bauer committed
115
        self._vectorInstructionSet = vector_instruction_set
116
        self._indent = "   "
Martin Bauer's avatar
Martin Bauer committed
117
        self._signatureOnly = signature_only
118
119

    def __call__(self, node):
Martin Bauer's avatar
Martin Bauer committed
120
121
        prev_is = VectorType.instruction_set
        VectorType.instruction_set = self._vectorInstructionSet
122
        result = str(self._print(node))
Martin Bauer's avatar
Martin Bauer committed
123
        VectorType.instruction_set = prev_is
124
        return result
125
126
127

    def _print(self, node):
        for cls in type(node).__mro__:
Martin Bauer's avatar
Martin Bauer committed
128
129
130
131
            method_name = "_print_" + cls.__name__
            if hasattr(self, method_name):
                return getattr(self, method_name)(node)
        raise NotImplementedError("CBackend does not support node of type " + str(type(node)))
132
133

    def _print_KernelFunction(self, node):
Martin Bauer's avatar
Martin Bauer committed
134
        function_arguments = ["%s %s" % (str(s.dtype), s.name) for s in node.parameters]
Martin Bauer's avatar
Martin Bauer committed
135
        func_declaration = "FUNC_PREFIX void %s(%s)" % (node.function_name, ", ".join(function_arguments))
136
        if self._signatureOnly:
Martin Bauer's avatar
Martin Bauer committed
137
            return func_declaration
138

139
        body = self._print(node.body)
Martin Bauer's avatar
Martin Bauer committed
140
        return func_declaration + "\n" + body
141
142

    def _print_Block(self, node):
Martin Bauer's avatar
Martin Bauer committed
143
144
        block_contents = "\n".join([self._print(child) for child in node.args])
        return "{\n%s\n}" % (self._indent + self._indent.join(block_contents.splitlines(True)))
145
146

    def _print_PragmaBlock(self, node):
Martin Bauer's avatar
Martin Bauer committed
147
        return "%s\n%s" % (node.pragma_line, self._print_Block(node))
148
149

    def _print_LoopOverCoordinate(self, node):
Martin Bauer's avatar
Martin Bauer committed
150
        counter_symbol = node.loop_counter_name
Martin Bauer's avatar
Martin Bauer committed
151
152
153
154
        start = "int %s = %s" % (counter_symbol, self.sympy_printer.doprint(node.start))
        condition = "%s < %s" % (counter_symbol, self.sympy_printer.doprint(node.stop))
        update = "%s += %s" % (counter_symbol, self.sympy_printer.doprint(node.step),)
        loop_str = "for (%s; %s; %s)" % (start, condition, update)
155

Martin Bauer's avatar
Martin Bauer committed
156
        prefix = "\n".join(node.prefix_lines)
157
158
        if prefix:
            prefix += "\n"
Martin Bauer's avatar
Martin Bauer committed
159
        return "%s%s\n%s" % (prefix, loop_str, self._print(node.body))
160
161

    def _print_SympyAssignment(self, node):
Martin Bauer's avatar
Martin Bauer committed
162
163
        if node.is_declaration:
            data_type = "const " + str(node.lhs.dtype) + " " if node.is_const else str(node.lhs.dtype) + " "
164
165
            return "%s%s = %s;" % (data_type, self.sympy_printer.doprint(node.lhs),
                                   self.sympy_printer.doprint(node.rhs))
166
        else:
Martin Bauer's avatar
Martin Bauer committed
167
            lhs_type = get_type_of_expression(node.lhs)
Martin Bauer's avatar
Martin Bauer committed
168
            if type(lhs_type) is VectorType and node.lhs.func == cast_func:
Martin Bauer's avatar
Martin Bauer committed
169
170
                return self._vectorInstructionSet['storeU'].format("&" + self.sympy_printer.doprint(node.lhs.args[0]),
                                                                   self.sympy_printer.doprint(node.rhs)) + ';'
171
            else:
Martin Bauer's avatar
Martin Bauer committed
172
                return "%s = %s;" % (self.sympy_printer.doprint(node.lhs), self.sympy_printer.doprint(node.rhs))
173
174

    def _print_TemporaryMemoryAllocation(self, node):
Martin Bauer's avatar
Martin Bauer committed
175
176
        return "%s %s = new %s[%s];" % (node.symbol.dtype, self.sympy_printer.doprint(node.symbol.name),
                                        node.symbol.dtype.base_type, self.sympy_printer.doprint(node.size))
177
178

    def _print_TemporaryMemoryFree(self, node):
Martin Bauer's avatar
Martin Bauer committed
179
        return "delete [] %s;" % (self.sympy_printer.doprint(node.symbol.name),)
180

Martin Bauer's avatar
Martin Bauer committed
181
182
    @staticmethod
    def _print_CustomCppCode(node):
183
        return node.code
184

185
    def _print_Conditional(self, node):
Martin Bauer's avatar
Martin Bauer committed
186
187
        condition_expr = self.sympy_printer.doprint(node.condition_expr)
        true_block = self._print_Block(node.true_block)
Martin Bauer's avatar
Martin Bauer committed
188
        result = "if (%s)\n%s " % (condition_expr, true_block)
Martin Bauer's avatar
Martin Bauer committed
189
190
        if node.false_block:
            false_block = self._print_Block(node.false_block)
Martin Bauer's avatar
Martin Bauer committed
191
            result += "else " + false_block
192
193
        return result

194
195
196
197

# ------------------------------------------ Helper function & classes -------------------------------------------------


Martin Bauer's avatar
Martin Bauer committed
198
# noinspection PyPep8Naming
199
class CustomSympyPrinter(CCodePrinter):
Martin Bauer's avatar
Martin Bauer committed
200

Martin Bauer's avatar
Martin Bauer committed
201
202
    def __init__(self, constants_as_floats=False):
        self._constantsAsFloats = constants_as_floats
Martin Bauer's avatar
Martin Bauer committed
203
204
        super(CustomSympyPrinter, self).__init__()

205
206
207
    def _print_Pow(self, expr):
        """Don't use std::pow function, for small integer exponents, write as multiplication"""
        if expr.exp.is_integer and expr.exp.is_number and 0 < expr.exp < 8:
208
            return "(" + self._print(sp.Mul(*[expr.base] * expr.exp, evaluate=False)) + ")"
209
210
211
212
213
        else:
            return super(CustomSympyPrinter, self)._print_Pow(expr)

    def _print_Rational(self, expr):
        """Evaluate all rationals i.e. print 0.25 instead of 1.0/4.0"""
Martin Bauer's avatar
Martin Bauer committed
214
215
216
217
        res = str(expr.evalf().num)
        if self._constantsAsFloats:
            res += "f"
        return res
218
219
220
221
222
223
224
225

    def _print_Equality(self, expr):
        """Equality operator is not printable in default printer"""
        return '((' + self._print(expr.lhs) + ") == (" + self._print(expr.rhs) + '))'

    def _print_Piecewise(self, expr):
        """Print piecewise in one line (remove newlines)"""
        result = super(CustomSympyPrinter, self)._print_Piecewise(expr)
Martin Bauer's avatar
Martin Bauer committed
226
227
228
229
230
231
232
        return result.replace("\n", "")

    def _print_Float(self, expr):
        res = str(expr)
        if self._constantsAsFloats:
            res += "f"
        return res
233

234
    def _print_Function(self, expr):
Martin Bauer's avatar
Martin Bauer committed
235
        function_map = {
Martin Bauer's avatar
Martin Bauer committed
236
237
238
239
240
            bitwise_xor: '^',
            bit_shift_right: '>>',
            bit_shift_left: '<<',
            bitwise_or: '|',
            bitwise_and: '&',
Martin Bauer's avatar
Martin Bauer committed
241
        }
Martin Bauer's avatar
Martin Bauer committed
242
243
        if hasattr(expr, 'to_c'):
            return expr.to_c(self._print)
Martin Bauer's avatar
Martin Bauer committed
244
        if expr.func == cast_func:
Martin Bauer's avatar
Martin Bauer committed
245
246
            arg, data_type = expr.args
            return "*((%s)(& %s))" % (PointerType(data_type), self._print(arg))
Martin Bauer's avatar
Martin Bauer committed
247
248
249
        elif expr.func == modulo_floor:
            assert all(get_type_of_expression(e).is_int() for e in expr.args)
            return "({dtype})({0} / {1}) * {1}".format(*expr.args, dtype=get_type_of_expression(expr.args[0]))
Martin Bauer's avatar
Martin Bauer committed
250
251
        elif expr.func in function_map:
            return "(%s %s %s)" % (self._print(expr.args[0]), function_map[expr.func], self._print(expr.args[1]))
252
        else:
253
            return super(CustomSympyPrinter, self)._print_Function(expr)
Martin Bauer's avatar
Martin Bauer committed
254

255

Martin Bauer's avatar
Martin Bauer committed
256
# noinspection PyPep8Naming
257
258
259
class VectorizedCustomSympyPrinter(CustomSympyPrinter):
    SummandInfo = namedtuple("SummandInfo", ['sign', 'term'])

Martin Bauer's avatar
Martin Bauer committed
260
261
    def __init__(self, instruction_set, constants_as_floats=False):
        super(VectorizedCustomSympyPrinter, self).__init__(constants_as_floats)
Martin Bauer's avatar
Martin Bauer committed
262
        self.instruction_set = instruction_set
263

Martin Bauer's avatar
Martin Bauer committed
264
265
266
267
    def _scalarFallback(self, func_name, expr, *args, **kwargs):
        expr_type = get_type_of_expression(expr)
        if type(expr_type) is not VectorType:
            return getattr(super(VectorizedCustomSympyPrinter, self), func_name)(expr, *args, **kwargs)
268
        else:
Martin Bauer's avatar
Martin Bauer committed
269
            assert self.instruction_set['width'] == expr_type.width
270
271
            return None

272
    def _print_Function(self, expr):
Martin Bauer's avatar
Martin Bauer committed
273
        if expr.func == cast_func:
Martin Bauer's avatar
Martin Bauer committed
274
275
            arg, data_type = expr.args
            if type(data_type) is VectorType:
276
                if type(arg) is ResolvedFieldAccess:
Martin Bauer's avatar
Martin Bauer committed
277
                    return self.instruction_set['loadU'].format("& " + self._print(arg))
278
                else:
Martin Bauer's avatar
Martin Bauer committed
279
                    return self.instruction_set['makeVec'].format(self._print(arg))
280
281
282

        return super(VectorizedCustomSympyPrinter, self)._print_Function(expr)

283
284
285
286
287
    def _print_And(self, expr):
        result = self._scalarFallback('_print_And', expr)
        if result:
            return result

Martin Bauer's avatar
Martin Bauer committed
288
289
290
291
        arg_strings = [self._print(a) for a in expr.args]
        assert len(arg_strings) > 0
        result = arg_strings[0]
        for item in arg_strings[1:]:
Martin Bauer's avatar
Martin Bauer committed
292
            result = self.instruction_set['&'].format(result, item)
293
294
295
296
297
298
299
        return result

    def _print_Or(self, expr):
        result = self._scalarFallback('_print_Or', expr)
        if result:
            return result

Martin Bauer's avatar
Martin Bauer committed
300
301
302
303
        arg_strings = [self._print(a) for a in expr.args]
        assert len(arg_strings) > 0
        result = arg_strings[0]
        for item in arg_strings[1:]:
Martin Bauer's avatar
Martin Bauer committed
304
            result = self.instruction_set['|'].format(result, item)
305
306
        return result

307
    def _print_Add(self, expr, order=None):
308
309
310
        result = self._scalarFallback('_print_Add', expr)
        if result:
            return result
311
312
313
314

        summands = []
        for term in expr.args:
            if term.func == sp.Mul:
Martin Bauer's avatar
Martin Bauer committed
315
                sign, t = self._print_Mul(term, inside_add=True)
316
317
318
319
320
321
322
323
324
325
326
327
328
            else:
                t = self._print(term)
                sign = 1
            summands.append(self.SummandInfo(sign, t))
        # Use positive terms first
        summands.sort(key=lambda e: e.sign, reverse=True)
        # if no positive term exists, prepend a zero
        if summands[0].sign == -1:
            summands.insert(0, self.SummandInfo(1, "0"))

        assert len(summands) >= 2
        processed = summands[0].term
        for summand in summands[1:]:
Martin Bauer's avatar
Martin Bauer committed
329
            func = self.instruction_set['-'] if summand.sign == -1 else self.instruction_set['+']
330
331
332
            processed = func.format(processed, summand.term)
        return processed

333
    def _print_Pow(self, expr):
334
335
336
        result = self._scalarFallback('_print_Pow', expr)
        if result:
            return result
337
338
339
340
341

        if expr.exp.is_integer and expr.exp.is_number and 0 < expr.exp < 8:
            return "(" + self._print(sp.Mul(*[expr.base] * expr.exp, evaluate=False)) + ")"
        else:
            if expr.exp == -1:
Martin Bauer's avatar
Martin Bauer committed
342
343
                one = self.instruction_set['makeVec'].format(1.0)
                return self.instruction_set['/'].format(one, self._print(expr.base))
344
            elif expr.exp == 0.5:
Martin Bauer's avatar
Martin Bauer committed
345
                return self.instruction_set['sqrt'].format(self._print(expr.base))
346
347
348
            else:
                raise ValueError("Generic exponential not supported")

Martin Bauer's avatar
Martin Bauer committed
349
350
351
352
    def _print_Mul(self, expr, inside_add=False):
        # noinspection PyProtectedMember
        from sympy.core.mul import _keep_coeff

353
354
355
        result = self._scalarFallback('_print_Mul', expr)
        if result:
            return result
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

        c, e = expr.as_coeff_Mul()
        if c < 0:
            expr = _keep_coeff(-c, e)
            sign = -1
        else:
            sign = 1

        a = []  # items in the numerator
        b = []  # items that are in the denominator (if any)

        # Gather args for numerator/denominator
        for item in expr.as_ordered_factors():
            if item.is_commutative and item.is_Pow and item.exp.is_Rational and item.exp.is_negative:
                if item.exp != -1:
                    b.append(sp.Pow(item.base, -item.exp, evaluate=False))
                else:
                    b.append(sp.Pow(item.base, -item.exp))
            else:
                a.append(item)

        a = a or [S.One]

        a_str = [self._print(x) for x in a]
        b_str = [self._print(x) for x in b]

        result = a_str[0]
        for item in a_str[1:]:
Martin Bauer's avatar
Martin Bauer committed
384
            result = self.instruction_set['*'].format(result, item)
385
386
387
388

        if len(b) > 0:
            denominator_str = b_str[0]
            for item in b_str[1:]:
Martin Bauer's avatar
Martin Bauer committed
389
390
                denominator_str = self.instruction_set['*'].format(denominator_str, item)
            result = self.instruction_set['/'].format(result, denominator_str)
391

Martin Bauer's avatar
Martin Bauer committed
392
        if inside_add:
393
394
395
            return sign, result
        else:
            if sign < 0:
Martin Bauer's avatar
Martin Bauer committed
396
                return self.instruction_set['*'].format(self._print(S.NegativeOne), result)
397
398
399
            else:
                return result

400
    def _print_Relational(self, expr):
401
402
403
        result = self._scalarFallback('_print_Relational', expr)
        if result:
            return result
Martin Bauer's avatar
Martin Bauer committed
404
        return self.instruction_set[expr.rel_op].format(self._print(expr.lhs), self._print(expr.rhs))
405
406

    def _print_Equality(self, expr):
407
408
409
        result = self._scalarFallback('_print_Equality', expr)
        if result:
            return result
Martin Bauer's avatar
Martin Bauer committed
410
        return self.instruction_set['=='].format(self._print(expr.lhs), self._print(expr.rhs))
411
412

    def _print_Piecewise(self, expr):
413
414
415
        result = self._scalarFallback('_print_Piecewise', expr)
        if result:
            return result
416

Martin Bauer's avatar
Martin Bauer committed
417
        if expr.args[-1].cond.args[0] is not sp.sympify(True):
418
419
420
421
422
423
424
425
426
            # We need the last conditional to be a True, otherwise the resulting
            # function may not return a result.
            raise ValueError("All Piecewise expressions must contain an "
                             "(expr, True) statement to be used as a default "
                             "condition. Without one, the generated "
                             "expression may not evaluate to anything under "
                             "some condition.")

        result = self._print(expr.args[-1][0])
Martin Bauer's avatar
Martin Bauer committed
427
        for true_expr, condition in reversed(expr.args[:-1]):
Martin Bauer's avatar
Martin Bauer committed
428
            # noinspection SpellCheckingInspection
Martin Bauer's avatar
Martin Bauer committed
429
            result = self.instruction_set['blendv'].format(result, self._print(true_expr), self._print(condition))
430
        return result