astnodes.py 24 KB
Newer Older
1
2
from typing import Any, List, Optional, Sequence, Set, Union

3
import jinja2
4
import sympy as sp
5
6

from pystencils.data_types import TypedSymbol, cast_func, create_type
7
from pystencils.field import Field
8
9
from pystencils.kernelparameters import (FieldPointerSymbol, FieldShapeSymbol,
                                         FieldStrideSymbol)
Martin Bauer's avatar
Martin Bauer committed
10
from pystencils.sympyextensions import fast_subs
Martin Bauer's avatar
Martin Bauer committed
11
12

NodeOrExpr = Union['Node', sp.Expr]
13
14


15
class Node:
Martin Bauer's avatar
Martin Bauer committed
16
    """Base class for all AST nodes."""
Martin Bauer's avatar
Martin Bauer committed
17

Martin Bauer's avatar
Martin Bauer committed
18
    def __init__(self, parent: Optional['Node'] = None):
19
20
        self.parent = parent

Martin Bauer's avatar
Martin Bauer committed
21
22
23
    @property
    def args(self) -> List[NodeOrExpr]:
        """Returns all arguments/children of this node."""
Martin Bauer's avatar
Martin Bauer committed
24
        raise NotImplementedError()
25
26

    @property
Martin Bauer's avatar
Martin Bauer committed
27
28
    def symbols_defined(self) -> Set[sp.Symbol]:
        """Set of symbols which are defined by this node."""
Martin Bauer's avatar
Martin Bauer committed
29
        raise NotImplementedError()
30
31

    @property
Martin Bauer's avatar
Martin Bauer committed
32
33
    def undefined_symbols(self) -> Set[sp.Symbol]:
        """Symbols which are used but are not defined inside this node."""
34
        raise NotImplementedError()
35

Martin Bauer's avatar
Martin Bauer committed
36
    def subs(self, subs_dict) -> None:
Martin Bauer's avatar
Martin Bauer committed
37
        """Inplace! substitute, similar to sympy's but modifies the AST inplace."""
38
        for a in self.args:
Martin Bauer's avatar
Martin Bauer committed
39
            a.subs(subs_dict)
40

41
42
43
44
    @property
    def func(self):
        return self.__class__

Martin Bauer's avatar
Martin Bauer committed
45
46
    def atoms(self, arg_type) -> Set[Any]:
        """Returns a set of all descendants recursively, which are an instance of the given type."""
47
48
        result = set()
        for arg in self.args:
Martin Bauer's avatar
Martin Bauer committed
49
            if isinstance(arg, arg_type):
50
                result.add(arg)
Martin Bauer's avatar
Martin Bauer committed
51
            result.update(arg.atoms(arg_type))
52
53
54
        return result


55
class Conditional(Node):
Martin Bauer's avatar
Martin Bauer committed
56
57
58
59
60
61
62
63
64
65
66
    """Conditional that maps to a 'if' statement in C/C++.

    Try to avoid using this node inside of loops, since currently this construction can not be vectorized.
    Consider using assignments with sympy.Piecewise in this case.

    Args:
        condition_expr: sympy relational expression
        true_block: block which is run if conditional is true
        false_block: optional block which is run if conditional is false
    """

Martin Bauer's avatar
Martin Bauer committed
67
68
    def __init__(self, condition_expr: sp.Basic, true_block: Union['Block', 'SympyAssignment'],
                 false_block: Optional['Block'] = None) -> None:
Martin Bauer's avatar
Martin Bauer committed
69
70
        super(Conditional, self).__init__(parent=None)

Martin Bauer's avatar
Martin Bauer committed
71
        self.condition_expr = condition_expr
Martin Bauer's avatar
Martin Bauer committed
72
73

        def handle_child(c):
74
75
76
77
78
79
80
            if c is None:
                return None
            if not isinstance(c, Block):
                c = Block([c])
            c.parent = self
            return c

Martin Bauer's avatar
Martin Bauer committed
81
82
        self.true_block = handle_child(true_block)
        self.false_block = handle_child(false_block)
83

Martin Bauer's avatar
Martin Bauer committed
84
85
    def subs(self, subs_dict):
        self.true_block.subs(subs_dict)
Martin Bauer's avatar
Martin Bauer committed
86
        if self.false_block:
Martin Bauer's avatar
Martin Bauer committed
87
88
            self.false_block.subs(subs_dict)
        self.condition_expr = self.condition_expr.subs(subs_dict)
89
90
91

    @property
    def args(self):
Martin Bauer's avatar
Martin Bauer committed
92
93
94
        result = [self.condition_expr, self.true_block]
        if self.false_block:
            result.append(self.false_block)
95
96
97
        return result

    @property
Martin Bauer's avatar
Martin Bauer committed
98
    def symbols_defined(self):
99
100
101
        return set()

    @property
Martin Bauer's avatar
Martin Bauer committed
102
    def undefined_symbols(self):
Martin Bauer's avatar
Martin Bauer committed
103
104
105
106
        result = self.true_block.undefined_symbols
        if self.false_block:
            result.update(self.false_block.undefined_symbols)
        result.update(self.condition_expr.atoms(sp.Symbol))
107
108
109
        return result

    def __str__(self):
Martin Bauer's avatar
Martin Bauer committed
110
        return 'if:({!s}) '.format(self.condition_expr)
111
112

    def __repr__(self):
Martin Bauer's avatar
Martin Bauer committed
113
        return 'if:({!r}) '.format(self.condition_expr)
114

115
116
117
118
119
120
121
122
    def replace_by_true_block(self):
        """Replaces the conditional by its True block"""
        self.parent.replace(self, [self.true_block])

    def replace_by_false_block(self):
        """Replaces the conditional by its False block"""
        self.parent.replace(self, [self.false_block] if self.false_block else [])

123

124
125
class KernelFunction(Node):

126
127
128
129
130
131
132
133
134
135
136
    class Parameter:
        """Function parameter.

        Each undefined symbol in a `KernelFunction` node becomes a parameter to the function.
        Parameters are either symbols introduced by the user that never occur on the left hand side of an
        Assignment, or are related to fields/arrays passed to the function.

        A parameter consists of the typed symbol (symbol property). For field related parameters this is a symbol
        defined in pystencils.kernelparameters.
        If the parameter is related to one or multiple fields, these fields are referenced in the fields property.
        """
137

138
139
140
        def __init__(self, symbol, fields):
            self.symbol = symbol  # type: TypedSymbol
            self.fields = fields  # type: Sequence[Field]
141

142
        def __repr__(self):
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            return repr(self.symbol)

        @property
        def is_field_stride(self):
            return isinstance(self.symbol, FieldStrideSymbol)

        @property
        def is_field_shape(self):
            return isinstance(self.symbol, FieldShapeSymbol)

        @property
        def is_field_pointer(self):
            return isinstance(self.symbol, FieldPointerSymbol)

        @property
        def is_field_parameter(self):
            return self.is_field_pointer or self.is_field_shape or self.is_field_stride

        @property
        def field_name(self):
            return self.fields[0].name
164

165
    def __init__(self, body, target, backend, compile_function, ghost_layers, function_name="kernel"):
166
167
        super(KernelFunction, self).__init__()
        self._body = body
168
        body.parent = self
Martin Bauer's avatar
Martin Bauer committed
169
        self.function_name = function_name
170
        self._body.parent = self
Martin Bauer's avatar
Martin Bauer committed
171
        self.ghost_layers = ghost_layers
172
173
        self._target = target
        self._backend = backend
174
        # these variables are assumed to be global, so no automatic parameter is generated for them
Martin Bauer's avatar
Martin Bauer committed
175
        self.global_variables = set()
Martin Bauer's avatar
Martin Bauer committed
176
        self.instruction_set = None  # used in `vectorize` function to tell the backend which i.s. (SSE,AVX) to use
177
178
179
180
181
182
183
184
185
186
187
188
        # function that compiles the node to a Python callable, is set by the backends
        self._compile_function = compile_function

    @property
    def target(self):
        """Currently either 'cpu' or 'gpu' """
        return self._target

    @property
    def backend(self):
        """Backend for generating the code e.g. 'llvm', 'c', 'cuda' """
        return self._backend
189
190

    @property
Martin Bauer's avatar
Martin Bauer committed
191
    def symbols_defined(self):
192
193
194
        return set()

    @property
Martin Bauer's avatar
Martin Bauer committed
195
    def undefined_symbols(self):
196
197
198
199
200
201
        return set()

    @property
    def body(self):
        return self._body

Martin Bauer's avatar
Martin Bauer committed
202
203
204
205
206
    @body.setter
    def body(self, value):
        self._body = value
        self._body.parent = self

207
208
    @property
    def args(self):
209
        return self._body,
210

211
    @property
212
    def fields_accessed(self) -> Set['ResolvedFieldAccess']:
213
        """Set of Field instances: fields which are accessed inside this kernel function"""
Martin Bauer's avatar
Martin Bauer committed
214
        return set(o.field for o in self.atoms(ResolvedFieldAccess))
215

216
217
218
219
220
    def get_parameters(self) -> Sequence['KernelFunction.Parameter']:
        """Returns list of parameters for this function.

        This function is expensive, cache the result where possible!
        """
221
        field_map = {f.name: f for f in self.fields_accessed}
222
223
224
225
226
227
228

        def get_fields(symbol):
            if hasattr(symbol, 'field_name'):
                return field_map[symbol.field_name],
            elif hasattr(symbol, 'field_names'):
                return tuple(field_map[fn] for fn in symbol.field_names)
            return ()
229

230
231
        argument_symbols = self._body.undefined_symbols - self.global_variables
        parameters = [self.Parameter(symbol, get_fields(symbol)) for symbol in argument_symbols]
232
233
        if hasattr(self, 'indexing'):
            parameters += [self.Parameter(s, []) for s in self.indexing.symbolic_parameters()]
234
235
        parameters.sort(key=lambda p: p.symbol.name)
        return parameters
236

237
    def __str__(self):
238
239
        params = [p.symbol for p in self.get_parameters()]
        return '{0} {1}({2})\n{3}'.format(type(self).__name__, self.function_name, params,
Michael Kuron's avatar
Michael Kuron committed
240
                                          ("\t" + "\t".join(str(self.body).splitlines(True))))
241
242

    def __repr__(self):
243
244
        params = [p.symbol for p in self.get_parameters()]
        return '{0} {1}({2})'.format(type(self).__name__, self.function_name, params)
245

246
247
248
249
250
    def compile(self, *args, **kwargs):
        if self._compile_function is None:
            raise ValueError("No compile-function provided for this KernelFunction node")
        return self._compile_function(self, *args, **kwargs)

251

Martin Bauer's avatar
Martin Bauer committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
class SkipIteration(Node):
    @property
    def args(self):
        return []

    @property
    def symbols_defined(self):
        return set()

    @property
    def undefined_symbols(self):
        return set()


266
class Block(Node):
Martin Bauer's avatar
Martin Bauer committed
267
268
269
    def __init__(self, nodes: List[Node]):
        super(Block, self).__init__()
        self._nodes = nodes
270
        self.parent = None
271
272
273
274
275
276
277
        for n in self._nodes:
            n.parent = self

    @property
    def args(self):
        return self._nodes

Martin Bauer's avatar
Martin Bauer committed
278
279
280
281
    def subs(self, subs_dict) -> None:
        for a in self.args:
            a.subs(subs_dict)

Martin Bauer's avatar
Martin Bauer committed
282
    def insert_front(self, node):
283
284
285
        node.parent = self
        self._nodes.insert(0, node)

Martin Bauer's avatar
Martin Bauer committed
286
287
288
    def insert_before(self, new_node, insert_before):
        new_node.parent = self
        idx = self._nodes.index(insert_before)
289
290

        # move all assignment (definitions to the top)
Martin Bauer's avatar
Martin Bauer committed
291
        if isinstance(new_node, SympyAssignment) and new_node.is_declaration:
Martin Bauer's avatar
Martin Bauer committed
292
293
294
295
296
297
            while idx > 0:
                pn = self._nodes[idx - 1]
                if isinstance(pn, LoopOverCoordinate) or isinstance(pn, Conditional):
                    idx -= 1
                else:
                    break
Martin Bauer's avatar
Martin Bauer committed
298
        self._nodes.insert(idx, new_node)
Martin Bauer's avatar
Martin Bauer committed
299

300
    def append(self, node):
301
302
303
304
305
306
307
        if isinstance(node, list) or isinstance(node, tuple):
            for n in node:
                n.parent = self
                self._nodes.append(n)
        else:
            node.parent = self
            self._nodes.append(node)
308

Martin Bauer's avatar
Martin Bauer committed
309
    def take_child_nodes(self):
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        tmp = self._nodes
        self._nodes = []
        return tmp

    def replace(self, child, replacements):
        idx = self._nodes.index(child)
        del self._nodes[idx]
        if type(replacements) is list:
            for e in replacements:
                e.parent = self
            self._nodes = self._nodes[:idx] + replacements + self._nodes[idx:]
        else:
            replacements.parent = self
            self._nodes.insert(idx, replacements)

    @property
Martin Bauer's avatar
Martin Bauer committed
326
    def symbols_defined(self):
327
328
        result = set()
        for a in self.args:
Martin Bauer's avatar
Martin Bauer committed
329
            result.update(a.symbols_defined)
330
331
332
        return result

    @property
Martin Bauer's avatar
Martin Bauer committed
333
    def undefined_symbols(self):
334
        result = set()
Martin Bauer's avatar
Martin Bauer committed
335
        defined_symbols = set()
336
        for a in self.args:
Martin Bauer's avatar
Martin Bauer committed
337
338
            result.update(a.undefined_symbols)
            defined_symbols.update(a.symbols_defined)
Martin Bauer's avatar
Martin Bauer committed
339
        return result - defined_symbols
340

341
    def __str__(self):
Martin Bauer's avatar
Martin Bauer committed
342
        return "Block " + ''.join('{!s}\n'.format(node) for node in self._nodes)
343
344

    def __repr__(self):
Martin Bauer's avatar
Martin Bauer committed
345
        return "Block"
346

347
348

class PragmaBlock(Block):
Martin Bauer's avatar
Martin Bauer committed
349
350
    def __init__(self, pragma_line, nodes):
        super(PragmaBlock, self).__init__(nodes)
Martin Bauer's avatar
Martin Bauer committed
351
        self.pragma_line = pragma_line
Martin Bauer's avatar
Martin Bauer committed
352
        for n in nodes:
Martin Bauer's avatar
Martin Bauer committed
353
354
355
            n.parent = self

    def __repr__(self):
Martin Bauer's avatar
Martin Bauer committed
356
        return self.pragma_line
357
358
359
360


class LoopOverCoordinate(Node):
    LOOP_COUNTER_NAME_PREFIX = "ctr"
Martin Bauer's avatar
Martin Bauer committed
361
    BlOCK_LOOP_COUNTER_NAME_PREFIX = "_blockctr"
362

Martin Bauer's avatar
Martin Bauer committed
363
    def __init__(self, body, coordinate_to_loop_over, start, stop, step=1, is_block_loop=False):
Martin Bauer's avatar
Martin Bauer committed
364
        super(LoopOverCoordinate, self).__init__(parent=None)
365
        self.body = body
366
        body.parent = self
Martin Bauer's avatar
Martin Bauer committed
367
        self.coordinate_to_loop_over = coordinate_to_loop_over
368
369
370
371
        self.start = start
        self.stop = stop
        self.step = step
        self.body.parent = self
Martin Bauer's avatar
Martin Bauer committed
372
        self.prefix_lines = []
Martin Bauer's avatar
Martin Bauer committed
373
        self.is_block_loop = is_block_loop
374

Martin Bauer's avatar
Martin Bauer committed
375
    def new_loop_with_different_body(self, new_body):
Martin Bauer's avatar
Martin Bauer committed
376
377
        result = LoopOverCoordinate(new_body, self.coordinate_to_loop_over, self.start, self.stop,
                                    self.step, self.is_block_loop)
Martin Bauer's avatar
Martin Bauer committed
378
        result.prefix_lines = [l for l in self.prefix_lines]
379
380
        return result

Martin Bauer's avatar
Martin Bauer committed
381
382
    def subs(self, subs_dict):
        self.body.subs(subs_dict)
383
        if hasattr(self.start, "subs"):
Martin Bauer's avatar
Martin Bauer committed
384
            self.start = self.start.subs(subs_dict)
385
        if hasattr(self.stop, "subs"):
Martin Bauer's avatar
Martin Bauer committed
386
            self.stop = self.stop.subs(subs_dict)
387
        if hasattr(self.step, "subs"):
Martin Bauer's avatar
Martin Bauer committed
388
            self.step = self.step.subs(subs_dict)
389

390
391
    @property
    def args(self):
392
393
        result = [self.body]
        for e in [self.start, self.stop, self.step]:
Martin Bauer's avatar
Martin Bauer committed
394
395
            if hasattr(e, "args"):
                result.append(e)
396
397
        return result

398
399
400
401
402
403
404
405
406
407
    def replace(self, child, replacement):
        if child == self.body:
            self.body = replacement
        elif child == self.start:
            self.start = replacement
        elif child == self.step:
            self.step = replacement
        elif child == self.stop:
            self.stop = replacement

408
    @property
Martin Bauer's avatar
Martin Bauer committed
409
410
    def symbols_defined(self):
        return {self.loop_counter_symbol}
411
412

    @property
Martin Bauer's avatar
Martin Bauer committed
413
414
    def undefined_symbols(self):
        result = self.body.undefined_symbols
Martin Bauer's avatar
Martin Bauer committed
415
416
417
        for possible_symbol in [self.start, self.stop, self.step]:
            if isinstance(possible_symbol, Node) or isinstance(possible_symbol, sp.Basic):
                result.update(possible_symbol.atoms(sp.Symbol))
Martin Bauer's avatar
Martin Bauer committed
418
        return result - {self.loop_counter_symbol}
419

Martin Bauer's avatar
Martin Bauer committed
420
    @staticmethod
Martin Bauer's avatar
Martin Bauer committed
421
422
    def get_loop_counter_name(coordinate_to_loop_over):
        return "%s_%s" % (LoopOverCoordinate.LOOP_COUNTER_NAME_PREFIX, coordinate_to_loop_over)
Martin Bauer's avatar
Martin Bauer committed
423

Martin Bauer's avatar
Martin Bauer committed
424
425
426
427
    @staticmethod
    def get_block_loop_counter_name(coordinate_to_loop_over):
        return "%s_%s" % (LoopOverCoordinate.BlOCK_LOOP_COUNTER_NAME_PREFIX, coordinate_to_loop_over)

428
    @property
Martin Bauer's avatar
Martin Bauer committed
429
    def loop_counter_name(self):
Martin Bauer's avatar
Martin Bauer committed
430
431
432
433
        if self.is_block_loop:
            return LoopOverCoordinate.get_block_loop_counter_name(self.coordinate_to_loop_over)
        else:
            return LoopOverCoordinate.get_loop_counter_name(self.coordinate_to_loop_over)
Martin Bauer's avatar
Martin Bauer committed
434

435
    @staticmethod
Martin Bauer's avatar
Martin Bauer committed
436
    def is_loop_counter_symbol(symbol):
437
438
439
        prefix = LoopOverCoordinate.LOOP_COUNTER_NAME_PREFIX
        if not symbol.name.startswith(prefix):
            return None
Martin Bauer's avatar
Martin Bauer committed
440
        if symbol.dtype != create_type('int'):
441
            return None
Martin Bauer's avatar
Martin Bauer committed
442
        coordinate = int(symbol.name[len(prefix) + 1:])
443
444
        return coordinate

Martin Bauer's avatar
Martin Bauer committed
445
    @staticmethod
Martin Bauer's avatar
Martin Bauer committed
446
447
    def get_loop_counter_symbol(coordinate_to_loop_over):
        return TypedSymbol(LoopOverCoordinate.get_loop_counter_name(coordinate_to_loop_over), 'int')
448

Martin Bauer's avatar
Martin Bauer committed
449
450
451
452
    @staticmethod
    def get_block_loop_counter_symbol(coordinate_to_loop_over):
        return TypedSymbol(LoopOverCoordinate.get_block_loop_counter_name(coordinate_to_loop_over), 'int')

453
    @property
Martin Bauer's avatar
Martin Bauer committed
454
    def loop_counter_symbol(self):
Martin Bauer's avatar
Martin Bauer committed
455
456
457
458
        if self.is_block_loop:
            return self.get_block_loop_counter_symbol(self.coordinate_to_loop_over)
        else:
            return self.get_loop_counter_symbol(self.coordinate_to_loop_over)
459
460

    @property
Martin Bauer's avatar
Martin Bauer committed
461
    def is_outermost_loop(self):
Martin Bauer's avatar
Martin Bauer committed
462
463
        from pystencils.transformations import get_next_parent_of_type
        return get_next_parent_of_type(self, LoopOverCoordinate) is None
464
465

    @property
Martin Bauer's avatar
Martin Bauer committed
466
    def is_innermost_loop(self):
Martin Bauer's avatar
Martin Bauer committed
467
        return len(self.atoms(LoopOverCoordinate)) == 0
468

469
    def __str__(self):
Martin Bauer's avatar
Martin Bauer committed
470
471
472
        return 'for({!s}={!s}; {!s}<{!s}; {!s}+={!s})\n{!s}'.format(self.loop_counter_name, self.start,
                                                                    self.loop_counter_name, self.stop,
                                                                    self.loop_counter_name, self.step,
473
                                                                    ("\t" + "\t".join(str(self.body).splitlines(True))))
474
475

    def __repr__(self):
Martin Bauer's avatar
Martin Bauer committed
476
477
478
        return 'for({!s}={!s}; {!s}<{!s}; {!s}+={!s})'.format(self.loop_counter_name, self.start,
                                                              self.loop_counter_name, self.stop,
                                                              self.loop_counter_name, self.step)
479

480
481

class SympyAssignment(Node):
Martin Bauer's avatar
Martin Bauer committed
482
483
    def __init__(self, lhs_symbol, rhs_expr, is_const=True):
        super(SympyAssignment, self).__init__(parent=None)
Martin Bauer's avatar
Martin Bauer committed
484
        self._lhs_symbol = lhs_symbol
Martin Bauer's avatar
Martin Bauer committed
485
        self.rhs = rhs_expr
Martin Bauer's avatar
Martin Bauer committed
486
        self._is_const = is_const
Martin Bauer's avatar
Martin Bauer committed
487
488
489
490
491
492
493
494
        self._is_declaration = self.__is_declaration()

    def __is_declaration(self):
        if isinstance(self._lhs_symbol, cast_func):
            return False
        if any(isinstance(self._lhs_symbol, c) for c in (Field.Access, sp.Indexed, TemporaryMemoryAllocation)):
            return False
        return True
495
496
497

    @property
    def lhs(self):
Martin Bauer's avatar
Martin Bauer committed
498
        return self._lhs_symbol
499
500

    @lhs.setter
Martin Bauer's avatar
Martin Bauer committed
501
    def lhs(self, new_value):
Martin Bauer's avatar
Martin Bauer committed
502
        self._lhs_symbol = new_value
Martin Bauer's avatar
Martin Bauer committed
503
        self._is_declaration = self.__is_declaration()
504

Martin Bauer's avatar
Martin Bauer committed
505
506
507
    def subs(self, subs_dict):
        self.lhs = fast_subs(self.lhs, subs_dict)
        self.rhs = fast_subs(self.rhs, subs_dict)
508

509
510
    @property
    def args(self):
Martin Bauer's avatar
Martin Bauer committed
511
        return [self._lhs_symbol, self.rhs]
512
513

    @property
Martin Bauer's avatar
Martin Bauer committed
514
    def symbols_defined(self):
Martin Bauer's avatar
Martin Bauer committed
515
        if not self._is_declaration:
516
            return set()
Martin Bauer's avatar
Martin Bauer committed
517
        return {self._lhs_symbol}
518
519

    @property
Martin Bauer's avatar
Martin Bauer committed
520
    def undefined_symbols(self):
521
        result = self.rhs.atoms(sp.Symbol)
522
        # Add loop counters if there a field accesses
Martin Bauer's avatar
Martin Bauer committed
523
        loop_counters = set()
524
525
526
        for symbol in result:
            if isinstance(symbol, Field.Access):
                for i in range(len(symbol.offsets)):
Martin Bauer's avatar
Martin Bauer committed
527
528
                    loop_counters.add(LoopOverCoordinate.get_loop_counter_symbol(i))
        result.update(loop_counters)
Martin Bauer's avatar
Martin Bauer committed
529
        result.update(self._lhs_symbol.atoms(sp.Symbol))
530
531
532
        return result

    @property
Martin Bauer's avatar
Martin Bauer committed
533
    def is_declaration(self):
Martin Bauer's avatar
Martin Bauer committed
534
        return self._is_declaration
535
536

    @property
Martin Bauer's avatar
Martin Bauer committed
537
    def is_const(self):
Martin Bauer's avatar
Martin Bauer committed
538
        return self._is_const
539

Jan Hoenig's avatar
Jan Hoenig committed
540
541
    def replace(self, child, replacement):
        if child == self.lhs:
542
543
            replacement.parent = self
            self.lhs = replacement
Jan Hoenig's avatar
Jan Hoenig committed
544
545
546
547
548
549
        elif child == self.rhs:
            replacement.parent = self
            self.rhs = replacement
        else:
            raise ValueError('%s is not in args of %s' % (replacement, self.__class__))

550
    def __repr__(self):
551
        return repr(self.lhs) + " ← " + repr(self.rhs)
552

Martin Bauer's avatar
Martin Bauer committed
553
554
555
    def _repr_html_(self):
        printed_lhs = sp.latex(self.lhs)
        printed_rhs = sp.latex(self.rhs)
Martin Bauer's avatar
Martin Bauer committed
556
        return "${printed_lhs} \\leftarrow {printed_rhs}$".format(printed_lhs=printed_lhs, printed_rhs=printed_rhs)
Martin Bauer's avatar
Martin Bauer committed
557

558

Martin Bauer's avatar
Martin Bauer committed
559
class ResolvedFieldAccess(sp.Indexed):
Martin Bauer's avatar
Martin Bauer committed
560
    def __new__(cls, base, linearized_index, field, offsets, idx_coordinate_values):
561
562
        if not isinstance(base, sp.IndexedBase):
            base = sp.IndexedBase(base, shape=(1,))
Martin Bauer's avatar
Martin Bauer committed
563
        obj = super(ResolvedFieldAccess, cls).__new__(cls, base, linearized_index)
Martin Bauer's avatar
Martin Bauer committed
564
565
        obj.field = field
        obj.offsets = offsets
Martin Bauer's avatar
Martin Bauer committed
566
        obj.idx_coordinate_values = idx_coordinate_values
Martin Bauer's avatar
Martin Bauer committed
567
568
569
570
571
        return obj

    def _eval_subs(self, old, new):
        return ResolvedFieldAccess(self.args[0],
                                   self.args[1].subs(old, new),
Martin Bauer's avatar
Martin Bauer committed
572
                                   self.field, self.offsets, self.idx_coordinate_values)
Martin Bauer's avatar
Martin Bauer committed
573

Martin Bauer's avatar
Martin Bauer committed
574
575
576
577
578
    def fast_subs(self, substitutions):
        if self in substitutions:
            return substitutions[self]
        return ResolvedFieldAccess(self.args[0].subs(substitutions),
                                   self.args[1].subs(substitutions),
Martin Bauer's avatar
Martin Bauer committed
579
                                   self.field, self.offsets, self.idx_coordinate_values)
Martin Bauer's avatar
Martin Bauer committed
580
581

    def _hashable_content(self):
Martin Bauer's avatar
Martin Bauer committed
582
        super_class_contents = super(ResolvedFieldAccess, self)._hashable_content()
Martin Bauer's avatar
Martin Bauer committed
583
        return super_class_contents + tuple(self.offsets) + (repr(self.idx_coordinate_values), hash(self.field))
Martin Bauer's avatar
Martin Bauer committed
584
585

    @property
Martin Bauer's avatar
Martin Bauer committed
586
    def typed_symbol(self):
Martin Bauer's avatar
Martin Bauer committed
587
588
589
590
        return self.base.label

    def __str__(self):
        top = super(ResolvedFieldAccess, self).__str__()
Martin Bauer's avatar
Martin Bauer committed
591
        return "%s (%s)" % (top, self.typed_symbol.dtype)
Martin Bauer's avatar
Martin Bauer committed
592
593

    def __getnewargs__(self):
Martin Bauer's avatar
Martin Bauer committed
594
        return self.base, self.indices[0], self.field, self.offsets, self.idx_coordinate_values
Martin Bauer's avatar
Martin Bauer committed
595
596


597
class TemporaryMemoryAllocation(Node):
Martin Bauer's avatar
Martin Bauer committed
598
599
600
601
602
603
604
605
606
    """Node for temporary memory buffer allocation.

    Always allocates aligned memory.

    Args:
        typed_symbol: symbol used as pointer (has to be typed)
        size: number of elements to allocate
        align_offset: the align_offset's element is aligned
    """
607

Martin Bauer's avatar
Martin Bauer committed
608
    def __init__(self, typed_symbol: TypedSymbol, size, align_offset):
Martin Bauer's avatar
Martin Bauer committed
609
610
        super(TemporaryMemoryAllocation, self).__init__(parent=None)
        self.symbol = typed_symbol
611
        self.size = size
Martin Bauer's avatar
Martin Bauer committed
612
613
        self.headers = ['<stdlib.h>']
        self._align_offset = align_offset
614
615

    @property
Martin Bauer's avatar
Martin Bauer committed
616
617
    def symbols_defined(self):
        return {self.symbol}
618
619

    @property
Martin Bauer's avatar
Martin Bauer committed
620
    def undefined_symbols(self):
621
622
623
624
        if isinstance(self.size, sp.Basic):
            return self.size.atoms(sp.Symbol)
        else:
            return set()
625
626
627

    @property
    def args(self):
628
        return [self.symbol]
629

Martin Bauer's avatar
Martin Bauer committed
630
631
632
633
634
635
    def offset(self, byte_alignment):
        """Number of ELEMENTS to skip for a pointer that is aligned to byte_alignment."""
        np_dtype = self.symbol.dtype.base_type.numpy_dtype
        assert byte_alignment % np_dtype.itemsize == 0
        return -self._align_offset % (byte_alignment / np_dtype.itemsize)

636
637

class TemporaryMemoryFree(Node):
Martin Bauer's avatar
Martin Bauer committed
638
    def __init__(self, alloc_node):
Martin Bauer's avatar
Martin Bauer committed
639
        super(TemporaryMemoryFree, self).__init__(parent=None)
Martin Bauer's avatar
Martin Bauer committed
640
641
642
643
644
645
646
647
        self.alloc_node = alloc_node

    @property
    def symbol(self):
        return self.alloc_node.symbol

    def offset(self, byte_alignment):
        return self.alloc_node.offset(byte_alignment)
648
649

    @property
Martin Bauer's avatar
Martin Bauer committed
650
    def symbols_defined(self):
651
652
653
        return set()

    @property
Martin Bauer's avatar
Martin Bauer committed
654
    def undefined_symbols(self):
655
656
657
658
659
        return set()

    @property
    def args(self):
        return []
Martin Bauer's avatar
Martin Bauer committed
660
661
662
663
664


def early_out(condition):
    from pystencils.cpu.vectorization import vec_all
    return Conditional(vec_all(condition), Block([SkipIteration()]))
665
666
667
668
669
670
671
672
673
674
675


class DestructuringBindingsForFieldClass(Node):
    """
    Defines all variables needed for describing a field (shape, pointer, strides)
    """
    CLASS_TO_MEMBER_DICT = {
        FieldPointerSymbol: "data",
        FieldShapeSymbol: "shape",
        FieldStrideSymbol: "stride"
    }
676
677
678
679
680
681
    CLASS_NAME_TEMPLATE = jinja2.Template("Field<{{ dtype }}, {{ ndim }}>")

    @property
    def fields_accessed(self) -> Set['ResolvedFieldAccess']:
        """Set of Field instances: fields which are accessed inside this kernel function"""
        return set(o.field for o in self.atoms(ResolvedFieldAccess))
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701

    def __init__(self, body):
        super(DestructuringBindingsForFieldClass, self).__init__()
        self.headers = ['<Field.h>']
        self.body = body

    @property
    def args(self) -> List[NodeOrExpr]:
        """Returns all arguments/children of this node."""
        return set()

    @property
    def symbols_defined(self) -> Set[sp.Symbol]:
        """Set of symbols which are defined by this node."""
        undefined_field_symbols = {s for s in self.body.undefined_symbols
                                   if isinstance(s, (FieldPointerSymbol, FieldShapeSymbol, FieldStrideSymbol))}
        return undefined_field_symbols

    @property
    def undefined_symbols(self) -> Set[sp.Symbol]:
702
        field_map = {f.name: f for f in self.fields_accessed}
703
704
705
        undefined_field_symbols = self.symbols_defined
        corresponding_field_names = {s.field_name for s in undefined_field_symbols if hasattr(s, 'field_name')}
        corresponding_field_names |= {s.field_names[0] for s in undefined_field_symbols if hasattr(s, 'field_names')}
706
707
        return {TypedSymbol(f, self.CLASS_NAME_TEMPLATE.render(dtype=field_map[f].dtype, ndim=field_map[f].ndim) + '&')
                for f in corresponding_field_names} | \
708
709
710
711
712
713
714
715
716
717
718
719
            (self.body.undefined_symbols - undefined_field_symbols)

    def subs(self, subs_dict) -> None:
        """Inplace! substitute, similar to sympy's but modifies the AST inplace."""
        self.body.subs(subs_dict)

    @property
    def func(self):
        return self.__class__

    def atoms(self, arg_type) -> Set[Any]:
        return self.body.atoms(arg_type) | {s for s in self.symbols_defined if isinstance(s, arg_type)}