astnodes.py 12.6 KB
Newer Older
1
import sympy as sp
2
import textwrap as textwrap
3
4
from sympy.tensor import IndexedBase, Indexed
from pystencils.field import Field
Jan Hoenig's avatar
Jan Hoenig committed
5
from pystencils.types import TypedSymbol, DataType
6
7


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

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

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

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

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

    def atoms(self, argType):
Martin Bauer's avatar
Martin Bauer committed
29
30
31
        """
        Returns a set of all children which are an instance of the given argType
        """
32
33
34
35
36
37
38
39
40
41
42
        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:
43
44
        def __init__(self, name, dtype, kernelFunctionNode):
            from pystencils.transformations import symbolNameToVariableName
45
            self.name = name
46
            self.dtype = dtype
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            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):]

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

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

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

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

    @property
91
    def undefinedSymbols(self):
92
93
94
95
96
97
98
99
100
101
102
103
104
        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
105
        return [self._body]
106

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

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

119
    def __str__(self):
120
        self._updateParameters()
121
        return '{0} {1}({2})\n{3}'.format(type(self).__name__, self.functionName, self.parameters,
122
123
124
125
126
                                          textwrap.indent(str(self.body), '\t'))

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

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

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
144
    def insertBefore(self, newNode, insertBefore):
145
        newNode.parent = self
Martin Bauer's avatar
Martin Bauer committed
146
147
148
        idx = self._nodes.index(insertBefore)
        self._nodes.insert(idx, newNode)

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
176
    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
177
    def undefinedSymbols(self):
178
        result = set()
179
        definedSymbols = set()
180
        for a in self.args:
181
182
183
            result.update(a.undefinedSymbols)
            definedSymbols.update(a.symbolsDefined)
        return result - definedSymbols
184

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

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

191
192
193
194
195
196
197
198
199
200

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

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

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

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

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

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

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

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

    @property
    def symbolsDefined(self):
246
247
248
249
250
251
252
253
254
        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])
255

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

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

    @staticmethod
    def getLoopCounterSymbol(coordinateToLoopOver):
Jan Hoenig's avatar
Jan Hoenig committed
266
        return TypedSymbol(LoopOverCoordinate.getLoopCounterName(coordinateToLoopOver), DataType('int'))
267
268
269

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

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

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

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

285
    def __str__(self):
286
        return 'loop:{!s} in {!s}:{!s}:{!s}\n{!s}'.format(self.loopCounterName, self.start, self.stop, self.step,
287
288
289
                                                          textwrap.indent(str(self.body), '\t'))

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

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

class SympyAssignment(Node):
    def __init__(self, lhsSymbol, rhsTerm, isConst=True):
        self._lhsSymbol = lhsSymbol
        self.rhs = rhsTerm
        self._isDeclaration = True
        if isinstance(self._lhsSymbol, Field.Access) or isinstance(self._lhsSymbol, IndexedBase):
            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
        if isinstance(self._lhsSymbol, Field.Access) or isinstance(self._lhsSymbol, sp.Indexed):
311
312
313
314
315
316
317
318
319
320
321
322
323
            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
324
    def undefinedSymbols(self):
325
        result = self.rhs.atoms(sp.Symbol)
326
327
328
329
330
331
332
        # 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)
333
334
335
336
337
338
339
340
341
342
343
        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
344
345
346
347
348
349
350
351
352
    def replace(self, child, replacement):
        if child == self.lhs:
            self.lhs = child
        elif child == self.rhs:
            replacement.parent = self
            self.rhs = replacement
        else:
            raise ValueError('%s is not in args of %s' % (replacement, self.__class__))

353
354
355
356
357
358
359
360
361
362
363
    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):
364
        return set([self.symbol])
365
366

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

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


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

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

    @property
387
    def undefinedSymbols(self):
388
389
390
391
392
393
        return set()

    @property
    def args(self):
        return []

Jan Hoenig's avatar
Jan Hoenig committed
394
395
396
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

# TODO everything which is not Atomic expression: Pow)

class Expr(Node):
    def __init__(self, args, parent=None):
        super(Expr, self).__init__(parent)
        self._args = list(args)

    @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


class Mul(Expr):
    pass


class Add(Expr):
    pass


class Pow(Expr):
    pass


class Indexed(Expr):
    pass