astnodes.py 15.2 KB
Newer Older
1
import sympy as sp
2
from sympy.tensor import IndexedBase
3
from pystencils.field import Field
Jan Hoenig's avatar
Jan Hoenig committed
4
from pystencils.types import TypedSymbol, createType, get_type_from_sympy
5
6


7
class Node(object):
Martin Bauer's avatar
Martin Bauer committed
8
9
    """Base class for all AST nodes"""

10
11
12
13
    def __init__(self, parent=None):
        self.parent = parent

    def args(self):
Martin Bauer's avatar
Martin Bauer committed
14
        """Returns all arguments/children of this node"""
15
16
17
18
        return []

    @property
    def symbolsDefined(self):
19
        """Set of symbols which are defined by this node. """
20
21
22
        return set()

    @property
23
24
25
    def undefinedSymbols(self):
        """Symbols which are use but are not defined inside this node"""
        raise NotImplementedError()
26
27

    def atoms(self, argType):
Martin Bauer's avatar
Martin Bauer committed
28
29
30
        """
        Returns a set of all children which are an instance of the given argType
        """
31
32
33
34
35
36
37
38
39
40
41
        result = set()
        for arg in self.args:
            if isinstance(arg, argType):
                result.add(arg)
            result.update(arg.atoms(argType))
        return result


class KernelFunction(Node):

    class Argument:
42
43
        def __init__(self, name, dtype, kernelFunctionNode):
            from pystencils.transformations import symbolNameToVariableName
44
            self.name = name
45
            self.dtype = dtype
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            self.isFieldPtrArgument = False
            self.isFieldShapeArgument = False
            self.isFieldStrideArgument = False
            self.isFieldArgument = False
            self.fieldName = ""
            self.coordinate = None

            if name.startswith(Field.DATA_PREFIX):
                self.isFieldPtrArgument = True
                self.isFieldArgument = True
                self.fieldName = name[len(Field.DATA_PREFIX):]
            elif name.startswith(Field.SHAPE_PREFIX):
                self.isFieldShapeArgument = True
                self.isFieldArgument = True
                self.fieldName = name[len(Field.SHAPE_PREFIX):]
            elif name.startswith(Field.STRIDE_PREFIX):
                self.isFieldStrideArgument = True
                self.isFieldArgument = True
                self.fieldName = name[len(Field.STRIDE_PREFIX):]

66
67
68
69
70
            self.field = None
            if self.isFieldArgument:
                fieldMap = {symbolNameToVariableName(f.name): f for f in kernelFunctionNode.fieldsAccessed}
                self.field = fieldMap[self.fieldName]

71
72
73
        def __repr__(self):
            return '<{0} {1}>'.format(self.dtype, self.name)

74
    def __init__(self, body, fieldsAccessed, functionName="kernel"):
75
76
        super(KernelFunction, self).__init__()
        self._body = body
77
        body.parent = self
78
        self._parameters = None
79
        self.functionName = functionName
80
        self._body.parent = self
81
        self._fieldsAccessed = fieldsAccessed
82
83
        # these variables are assumed to be global, so no automatic parameter is generated for them
        self.globalVariables = set()
84
85
86
87
88
89

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

    @property
90
    def undefinedSymbols(self):
91
92
93
94
95
96
97
98
99
100
101
102
103
        return set()

    @property
    def parameters(self):
        self._updateParameters()
        return self._parameters

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

    @property
    def args(self):
Jan Hoenig's avatar
Jan Hoenig committed
104
        return [self._body]
105

106
107
108
109
110
    @property
    def fieldsAccessed(self):
        """Set of Field instances: fields which are accessed inside this kernel function"""
        return self._fieldsAccessed

111
    def _updateParameters(self):
112
        undefinedSymbols = self._body.undefinedSymbols - self.globalVariables
113
        self._parameters = [KernelFunction.Argument(s.name, s.dtype, self) for s in undefinedSymbols]
114
115
116
117
        self._parameters.sort(key=lambda l: (l.fieldName, l.isFieldPtrArgument, l.isFieldShapeArgument,
                                             l.isFieldStrideArgument, l.name),
                              reverse=True)

118
    def __str__(self):
119
        self._updateParameters()
120
        return '{0} {1}({2})\n{3}'.format(type(self).__name__, self.functionName, self.parameters,
Michael Kuron's avatar
Michael Kuron committed
121
                                          ("\t" + "\t".join(str(self.body).splitlines(True))))
122
123
124
125

    def __repr__(self):
        self._updateParameters()
        return '{0} {1}({2})'.format(type(self).__name__, self.functionName, self.parameters)
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

class Block(Node):
    def __init__(self, listOfNodes):
        super(Node, self).__init__()
        self._nodes = listOfNodes
        for n in self._nodes:
            n.parent = self

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

    def insertFront(self, node):
        node.parent = self
        self._nodes.insert(0, node)

Martin Bauer's avatar
Martin Bauer committed
143
    def insertBefore(self, newNode, insertBefore):
144
        newNode.parent = self
Martin Bauer's avatar
Martin Bauer committed
145
146
147
        idx = self._nodes.index(insertBefore)
        self._nodes.insert(idx, newNode)

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    def append(self, node):
        node.parent = self
        self._nodes.append(node)

    def takeChildNodes(self):
        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
    def symbolsDefined(self):
        result = set()
        for a in self.args:
            result.update(a.symbolsDefined)
        return result

    @property
176
    def undefinedSymbols(self):
177
        result = set()
178
        definedSymbols = set()
179
        for a in self.args:
180
181
182
            result.update(a.undefinedSymbols)
            definedSymbols.update(a.symbolsDefined)
        return result - definedSymbols
183

184
185
    def __str__(self):
        return ''.join('{!s}\n'.format(node) for node in self._nodes)
186
187

    def __repr__(self):
188
        return ''.join('{!r}'.format(node) for node in self._nodes)
189

190
191
192
193
194
195
196
197
198
199

class PragmaBlock(Block):
    def __init__(self, pragmaLine, listOfNodes):
        super(PragmaBlock, self).__init__(listOfNodes)
        self.pragmaLine = pragmaLine


class LoopOverCoordinate(Node):
    LOOP_COUNTER_NAME_PREFIX = "ctr"

Martin Bauer's avatar
Martin Bauer committed
200
    def __init__(self, body, coordinateToLoopOver, start, stop, step=1):
201
        self._body = body
202
        body.parent = self
203
        self._coordinateToLoopOver = coordinateToLoopOver
Martin Bauer's avatar
Martin Bauer committed
204
205
206
        self._begin = start
        self._end = stop
        self._increment = step
207
208
209
210
        self._body.parent = self
        self.prefixLines = []

    def newLoopWithDifferentBody(self, newBody):
Martin Bauer's avatar
Martin Bauer committed
211
        result = LoopOverCoordinate(newBody, self._coordinateToLoopOver, self._begin, self._end, self._increment)
Martin Bauer's avatar
Martin Bauer committed
212
        result.prefixLines = [l for l in self.prefixLines]
213
214
215
216
217
        return result

    @property
    def args(self):
        result = [self._body]
Martin Bauer's avatar
Martin Bauer committed
218
219
220
        for e in [self._begin, self._end, self._increment]:
            if hasattr(e, "args"):
                result.append(e)
221
222
223
224
225
226
227
        return result

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

    @property
Martin Bauer's avatar
Martin Bauer committed
228
229
230
231
232
233
234
235
236
237
    def start(self):
        return self._begin

    @property
    def stop(self):
        return self._end

    @property
    def step(self):
        return self._increment
238
239
240
241
242
243
244

    @property
    def coordinateToLoopOver(self):
        return self._coordinateToLoopOver

    @property
    def symbolsDefined(self):
245
246
247
248
249
250
251
252
253
        return set([self.loopCounterSymbol])

    @property
    def undefinedSymbols(self):
        result = self._body.undefinedSymbols
        for possibleSymbol in [self._begin, self._end, self._increment]:
            if isinstance(possibleSymbol, Node) or isinstance(possibleSymbol, sp.Basic):
                result.update(possibleSymbol.atoms(sp.Symbol))
        return result - set([self.loopCounterSymbol])
254

Martin Bauer's avatar
Martin Bauer committed
255
256
257
258
    @staticmethod
    def getLoopCounterName(coordinateToLoopOver):
        return "%s_%s" % (LoopOverCoordinate.LOOP_COUNTER_NAME_PREFIX, coordinateToLoopOver)

259
260
    @property
    def loopCounterName(self):
Martin Bauer's avatar
Martin Bauer committed
261
262
263
264
        return LoopOverCoordinate.getLoopCounterName(self.coordinateToLoopOver)

    @staticmethod
    def getLoopCounterSymbol(coordinateToLoopOver):
265
        return TypedSymbol(LoopOverCoordinate.getLoopCounterName(coordinateToLoopOver), 'int')
266
267
268

    @property
    def loopCounterSymbol(self):
Martin Bauer's avatar
Martin Bauer committed
269
        return LoopOverCoordinate.getLoopCounterSymbol(self.coordinateToLoopOver)
270
271
272

    @property
    def isOutermostLoop(self):
Martin Bauer's avatar
Martin Bauer committed
273
274
        from pystencils.transformations import getNextParentOfType
        return getNextParentOfType(self, LoopOverCoordinate) is None
275
276
277

    @property
    def isInnermostLoop(self):
Martin Bauer's avatar
Martin Bauer committed
278
        return len(self.atoms(LoopOverCoordinate)) == 0
279
280
281
282
283

    @property
    def coordinateToLoopOver(self):
        return self._coordinateToLoopOver

284
    def __str__(self):
285
        return 'loop:{!s} in {!s}:{!s}:{!s}\n{!s}'.format(self.loopCounterName, self.start, self.stop, self.step,
Michael Kuron's avatar
Michael Kuron committed
286
                                                          ("\t" + "\t".join(str(self.body).splitlines(True))))
287
288

    def __repr__(self):
289
        return 'loop:{!s} in {!s}:{!s}:{!s}'.format(self.loopCounterName, self.start, self.stop, self.step)
290

291
292
293
294
295
296

class SympyAssignment(Node):
    def __init__(self, lhsSymbol, rhsTerm, isConst=True):
        self._lhsSymbol = lhsSymbol
        self.rhs = rhsTerm
        self._isDeclaration = True
297
        isCast = str(self._lhsSymbol.func).lower() == 'cast' if hasattr(self._lhsSymbol, "func") else False
298
        if isinstance(self._lhsSymbol, Field.Access) or isinstance(self._lhsSymbol, IndexedBase) or isCast:
299
300
301
302
303
304
305
306
307
308
309
            self._isDeclaration = False
        self._isConst = isConst

    @property
    def lhs(self):
        return self._lhsSymbol

    @lhs.setter
    def lhs(self, newValue):
        self._lhsSymbol = newValue
        self._isDeclaration = True
310
        isCast = str(self._lhsSymbol.func).lower() == 'cast' if hasattr(self._lhsSymbol, "func") else False
311
        if isinstance(self._lhsSymbol, Field.Access) or isinstance(self._lhsSymbol, sp.Indexed) or isCast:
312
313
314
315
316
317
318
319
320
321
322
323
324
            self._isDeclaration = False

    @property
    def args(self):
        return [self._lhsSymbol, self.rhs]

    @property
    def symbolsDefined(self):
        if not self._isDeclaration:
            return set()
        return set([self._lhsSymbol])

    @property
325
    def undefinedSymbols(self):
326
        result = self.rhs.atoms(sp.Symbol)
327
328
329
330
331
332
333
        # Add loop counters if there a field accesses
        loopCounters = set()
        for symbol in result:
            if isinstance(symbol, Field.Access):
                for i in range(len(symbol.offsets)):
                    loopCounters.add(LoopOverCoordinate.getLoopCounterSymbol(i))
        result.update(loopCounters)
334
335
336
337
338
339
340
341
342
343
344
        result.update(self._lhsSymbol.atoms(sp.Symbol))
        return result

    @property
    def isDeclaration(self):
        return self._isDeclaration

    @property
    def isConst(self):
        return self._isConst

Jan Hoenig's avatar
Jan Hoenig committed
345
346
    def replace(self, child, replacement):
        if child == self.lhs:
347
348
            replacement.parent = self
            self.lhs = replacement
Jan Hoenig's avatar
Jan Hoenig committed
349
350
351
352
353
354
        elif child == self.rhs:
            replacement.parent = self
            self.rhs = replacement
        else:
            raise ValueError('%s is not in args of %s' % (replacement, self.__class__))

355
356
357
358
359
360
361
362
363
364
365
    def __repr__(self):
        return repr(self.lhs) + " = " + repr(self.rhs)


class TemporaryMemoryAllocation(Node):
    def __init__(self, typedSymbol, size):
        self.symbol = typedSymbol
        self.size = size

    @property
    def symbolsDefined(self):
366
        return set([self.symbol])
367
368

    @property
369
370
371
372
373
    def undefinedSymbols(self):
        if isinstance(self.size, sp.Basic):
            return self.size.atoms(sp.Symbol)
        else:
            return set()
374
375
376

    @property
    def args(self):
377
        return [self.symbol]
378
379
380
381


class TemporaryMemoryFree(Node):
    def __init__(self, typedSymbol):
382
        self.symbol = typedSymbol
383
384
385
386
387
388

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

    @property
389
    def undefinedSymbols(self):
390
391
392
393
394
395
        return set()

    @property
    def args(self):
        return []

Jan Hoenig's avatar
Jan Hoenig committed
396

Jan Hoenig's avatar
Jan Hoenig committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# TODO implement defined & undefinedSymbols
class Conversion(Node):
    def __init__(self, child, dtype, parent=None):
        super(Conversion, self).__init__(parent)
        self._args = [child]
        self.dtype = dtype

    @property
    def args(self):
        """Returns all arguments/children of this node"""
        return self._args

    @args.setter
    def args(self, value):
        self._args = value

    @property
    def symbolsDefined(self):
        """Set of symbols which are defined by this node. """
        return set()

    @property
    def undefinedSymbols(self):
        """Symbols which are use but are not defined inside this node"""
        raise set()

    def __repr__(self):
Jan Hoenig's avatar
Jan Hoenig committed
424
        return '(%s(%s))' % (repr(self.dtype), repr(self.args[0].dtype)) + repr(self.args)
Jan Hoenig's avatar
Jan Hoenig committed
425

Jan Hoenig's avatar
Jan Hoenig committed
426
# TODO Pow
Jan Hoenig's avatar
Jan Hoenig committed
427

Jan Hoenig's avatar
Jan Hoenig committed
428
429
430
431

_expr_dict = {'Add': ' + ', 'Mul': ' * ', 'Pow': '**'}


Jan Hoenig's avatar
Jan Hoenig committed
432
433
434
435
class Expr(Node):
    def __init__(self, args, parent=None):
        super(Expr, self).__init__(parent)
        self._args = list(args)
Jan Hoenig's avatar
Jan Hoenig committed
436
        self.dtype = None
Jan Hoenig's avatar
Jan Hoenig committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

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

    @args.setter
    def args(self, value):
        self._args = value

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

    @property
    def symbolsDefined(self):
        return set()  # Todo fix for symbol analysis

    @property
    def undefinedSymbols(self):
        return set()  # Todo fix for symbol analysis

Jan Hoenig's avatar
Jan Hoenig committed
465
    def __repr__(self):
Jan Hoenig's avatar
Jan Hoenig committed
466
        return _expr_dict[self.__class__.__name__].join(repr(arg) for arg in self.args)
Jan Hoenig's avatar
Jan Hoenig committed
467

Jan Hoenig's avatar
Jan Hoenig committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481

class Mul(Expr):
    pass


class Add(Expr):
    pass


class Pow(Expr):
    pass


class Indexed(Expr):
482
483
484
    def __init__(self, args, base, parent=None):
        super(Indexed, self).__init__(args, parent)
        self.base = base
Jan Hoenig's avatar
Jan Hoenig committed
485
486
        # Get dtype from label, and unpointer it
        self.dtype = createType(base.label.dtype.baseType)
487

Jan Hoenig's avatar
Jan Hoenig committed
488
489
    def __repr__(self):
        return '%s[%s]' % (self.args[0], self.args[1])
Jan Hoenig's avatar
Jan Hoenig committed
490

491

492
493
494
495
496
497
498
499
500
501
502
class PointerArithmetic(Expr):
    def __init__(self, args, pointer, parent=None):
        super(PointerArithmetic, self).__init__([args] + [pointer], parent)
        self.pointer = pointer
        self.offset = args
        self.dtype = pointer.dtype

    def __repr__(self):
        return '*(%s + %s)' % (self.pointer, self.args)


Jan Hoenig's avatar
Jan Hoenig committed
503
class Number(Node, sp.AtomicExpr):
Jan Hoenig's avatar
Jan Hoenig committed
504
505
    def __init__(self, number, parent=None):
        super(Number, self).__init__(parent)
Jan Hoenig's avatar
Jan Hoenig committed
506
507
508

        self.dtype, self.value = get_type_from_sympy(number)
        self._args = tuple()
Jan Hoenig's avatar
Jan Hoenig committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

    @property
    def args(self):
        """Returns all arguments/children of this node"""
        return self._args

    @property
    def symbolsDefined(self):
        """Set of symbols which are defined by this node. """
        return set()

    @property
    def undefinedSymbols(self):
        """Symbols which are use but are not defined inside this node"""
        raise set()

    def __repr__(self):
526
        return repr(self.value)
Jan Hoenig's avatar
Jan Hoenig committed
527

Jan Hoenig's avatar
Jan Hoenig committed
528
529
530
531
532
    def __float__(self):
        return float(self.value)

    def __int__(self):
        return int(self.value)
Jan Hoenig's avatar
Jan Hoenig committed
533

Jan Hoenig's avatar
Jan Hoenig committed
534