transformations.py 47.3 KB
Newer Older
1
import warnings
2
from collections import defaultdict, OrderedDict, namedtuple
3
from copy import deepcopy
Martin Bauer's avatar
Martin Bauer committed
4
from types import MappingProxyType
5

6
7
import sympy as sp
from sympy.logic.boolalg import Boolean
8
from sympy.tensor import IndexedBase
9
from pystencils.assignment import Assignment
Martin Bauer's avatar
Martin Bauer committed
10
from pystencils.field import Field, FieldType
Martin Bauer's avatar
Martin Bauer committed
11
from pystencils.data_types import TypedSymbol, PointerType, StructType, get_base_type, cast_func, \
12
    pointer_arithmetic_func, get_type_of_expression, collate_types, create_type
Martin Bauer's avatar
Martin Bauer committed
13
from pystencils.slicing import normalize_slice
Martin Bauer's avatar
Martin Bauer committed
14
import pystencils.astnodes as ast
15
16


Martin Bauer's avatar
Martin Bauer committed
17
def filtered_tree_iteration(node, node_type, stop_type=None):
18
    for arg in node.args:
Martin Bauer's avatar
Martin Bauer committed
19
        if isinstance(arg, node_type):
20
            yield arg
Martin Bauer's avatar
Martin Bauer committed
21
22
23
        elif stop_type and isinstance(node, stop_type):
            continue

Martin Bauer's avatar
Martin Bauer committed
24
        yield from filtered_tree_iteration(arg, node_type)
25
26


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def unify_shape_symbols(body, common_shape, fields):
    """Replaces symbols for array sizes to ensure they are represented by the same unique symbol.

    When creating a kernel with variable array sizes, all passed arrays must have the same size.
    This is ensured when the kernel is called. Inside the kernel this means that only on symbol has to be used instead
    of one for each field. For example shape_arr1[0]  and shape_arr2[0] must be equal, so they should also be
    represented by the same symbol.

    Args:
        body: ast node, for the kernel part where substitutions is made, is modified in-place
        common_shape: shape of the field that was chosen
        fields: all fields whose shapes should be replaced by common_shape
    """
    substitutions = {}
    for field in fields:
        assert len(field.spatial_shape) == len(common_shape)
        if not field.has_fixed_shape:
            for common_shape_component, shape_component in zip(common_shape, field.spatial_shape):
                if shape_component != common_shape_component:
                    substitutions[shape_component] = common_shape_component
    if substitutions:
        body.subs(substitutions)


Martin Bauer's avatar
Martin Bauer committed
51
def get_common_shape(field_set):
52
53
    """Takes a set of pystencils Fields and returns their common spatial shape if it exists. Otherwise
    ValueError is raised"""
Martin Bauer's avatar
Martin Bauer committed
54
55
56
57
58
59
60
61
    nr_of_fixed_shaped_fields = 0
    for f in field_set:
        if f.has_fixed_shape:
            nr_of_fixed_shaped_fields += 1

    if nr_of_fixed_shaped_fields > 0 and nr_of_fixed_shaped_fields != len(field_set):
        fixed_field_names = ",".join([f.name for f in field_set if f.has_fixed_shape])
        var_field_names = ",".join([f.name for f in field_set if not f.has_fixed_shape])
62
        msg = "Mixing fixed-shaped and variable-shape fields in a single kernel is not possible\n"
Martin Bauer's avatar
Martin Bauer committed
63
        msg += "Variable shaped: %s \nFixed shaped:    %s" % (var_field_names, fixed_field_names)
64
65
        raise ValueError(msg)

Martin Bauer's avatar
Martin Bauer committed
66
67
68
69
    shape_set = set([f.spatial_shape for f in field_set])
    if nr_of_fixed_shaped_fields == len(field_set):
        if len(shape_set) != 1:
            raise ValueError("Differently sized field accesses in loop body: " + str(shape_set))
70

Martin Bauer's avatar
Martin Bauer committed
71
    shape = list(sorted(shape_set, key=lambda e: str(e[0])))[0]
72
73
74
    return shape


75
76
77
78
79
80
81
82
83
84
85
86
87
88
def get_field_accesses(expr, result=set()):
    if isinstance(expr, Field.Access):
        result.add(expr)
        for o in expr.offsets:
            get_field_accesses(o, result)
        for i in expr.index:
            get_field_accesses(i, result)
    elif hasattr(expr, 'atoms'):
        new_accesses = expr.atoms(Field.Access)
        result.update(new_accesses)
        for a in new_accesses:
            get_field_accesses(a, result)


Martin Bauer's avatar
Martin Bauer committed
89
90
91
92
def make_loop_over_domain(body, function_name, iteration_slice=None, ghost_layers=None, loop_order=None):
    """Uses :class:`pystencils.field.Field.Access` to create (multiple) loops around given AST.

    Args:
93
        body: Block object with inner loop contents
Martin Bauer's avatar
Martin Bauer committed
94
95
96
97
98
99
100
101
102
        function_name: name of generated C function
        iteration_slice: if not None, iteration is done only over this slice of the field
        ghost_layers: a sequence of pairs for each coordinate with lower and upper nr of ghost layers
             if None, the number of ghost layers is determined automatically and assumed to be equal for a
             all dimensions
        loop_order: loop ordering from outer to inner loop (optimal ordering is same as layout)

    Returns:
        :class:`LoopOverCoordinate` instance with nested loops, ordered according to field layouts
103
104
    """
    # find correct ordering by inspecting participating FieldAccesses
105
106
107
108
    field_accesses = set()
    get_field_accesses(body, field_accesses)
    field_accesses = {e for e in field_accesses if not e.is_absolute_access}

Martin Bauer's avatar
Martin Bauer committed
109
110
111
112
113
114
115
116
    # exclude accesses to buffers from field_list, because buffers are treated separately
    field_list = [e.field for e in field_accesses if not FieldType.is_buffer(e.field)]
    fields = set(field_list)
    num_buffer_accesses = len(field_accesses) - len(field_list)

    if loop_order is None:
        loop_order = get_optimal_loop_ordering(fields)

117
118
    shape = get_common_shape(fields)
    unify_shape_symbols(body, common_shape=shape, fields=fields)
Martin Bauer's avatar
Martin Bauer committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

    if iteration_slice is not None:
        iteration_slice = normalize_slice(iteration_slice, shape)

    if ghost_layers is None:
        required_ghost_layers = max([fa.required_ghost_layers for fa in field_accesses])
        ghost_layers = [(required_ghost_layers, required_ghost_layers)] * len(loop_order)
    if isinstance(ghost_layers, int):
        ghost_layers = [(ghost_layers, ghost_layers)] * len(loop_order)

    def get_loop_stride(loop_begin, loop_end, step):
        return (loop_end - loop_begin) / step

    loop_strides = []
    loop_vars = []
    current_body = body
Martin Bauer's avatar
Martin Bauer committed
135
    for i, loop_coordinate in enumerate(reversed(loop_order)):
Martin Bauer's avatar
Martin Bauer committed
136
        if iteration_slice is None:
Martin Bauer's avatar
Martin Bauer committed
137
138
139
            begin = ghost_layers[loop_coordinate][0]
            end = shape[loop_coordinate] - ghost_layers[loop_coordinate][1]
            new_loop = ast.LoopOverCoordinate(current_body, loop_coordinate, begin, end, 1)
Martin Bauer's avatar
Martin Bauer committed
140
141
142
            current_body = ast.Block([new_loop])
            loop_strides.append(get_loop_stride(begin, end, 1))
            loop_vars.append(new_loop.loop_counter_symbol)
Martin Bauer's avatar
Martin Bauer committed
143
        else:
Martin Bauer's avatar
Martin Bauer committed
144
            slice_component = iteration_slice[loop_coordinate]
Martin Bauer's avatar
Martin Bauer committed
145
146
            if type(slice_component) is slice:
                sc = slice_component
Martin Bauer's avatar
Martin Bauer committed
147
                new_loop = ast.LoopOverCoordinate(current_body, loop_coordinate, sc.start, sc.stop, sc.step)
Martin Bauer's avatar
Martin Bauer committed
148
149
150
                current_body = ast.Block([new_loop])
                loop_strides.append(get_loop_stride(sc.start, sc.stop, sc.step))
                loop_vars.append(new_loop.loop_counter_symbol)
Martin Bauer's avatar
Martin Bauer committed
151
            else:
Martin Bauer's avatar
Martin Bauer committed
152
                assignment = ast.SympyAssignment(ast.LoopOverCoordinate.get_loop_counter_symbol(loop_coordinate),
Martin Bauer's avatar
Martin Bauer committed
153
154
                                                 sp.sympify(slice_component))
                current_body.insert_front(assignment)
155

Martin Bauer's avatar
Martin Bauer committed
156
157
158
    loop_vars = [num_buffer_accesses * var for var in loop_vars]
    ast_node = ast.KernelFunction(current_body, ghost_layers=ghost_layers, function_name=function_name, backend='cpu')
    return ast_node, loop_strides, loop_vars
159
160


Martin Bauer's avatar
Martin Bauer committed
161
def create_intermediate_base_pointer(field_access, coordinates, previous_ptr):
Martin Bauer's avatar
Martin Bauer committed
162
    r"""
163
    Addressing elements in structured arrays is done with :math:`ptr\left[ \sum_i c_i \cdot s_i \right]`
Martin Bauer's avatar
Martin Bauer committed
164
165
166
167
168
    where :math:`c_i` is the coordinate value and :math:`s_i` the stride of a coordinate.
    The sum can be split up into multiple parts, such that parts of it can be pulled before loops.
    This function creates such an access for coordinates :math:`i \in \mbox{coordinates}`.
    Returns a new typed symbol, where the name encodes which coordinates have been resolved.

169
170
171
172
173
174
175
176
177
    Args:
        field_access: instance of :class:`pystencils.field.Field.Access` which provides strides and offsets
        coordinates: mapping of coordinate ids to its value, where stride*value is calculated
        previous_ptr: the pointer which is de-referenced

    Returns
        tuple with the new pointer symbol and the calculated offset

    Examples:
Martin Bauer's avatar
Martin Bauer committed
178
        >>> field = Field.create_generic('myfield', spatial_dimensions=2, index_dimensions=1)
Martin Bauer's avatar
Martin Bauer committed
179
        >>> x, y = sp.symbols("x y")
Martin Bauer's avatar
Martin Bauer committed
180
181
        >>> prev_pointer = TypedSymbol("ptr", "double")
        >>> create_intermediate_base_pointer(field[1,-2](5), {0: x}, prev_pointer)
Martin Bauer's avatar
Martin Bauer committed
182
        (ptr_01, x*fstride_myfield[0] + fstride_myfield[0])
Martin Bauer's avatar
Martin Bauer committed
183
        >>> create_intermediate_base_pointer(field[1,-2](5), {0: x, 1 : y }, prev_pointer)
Martin Bauer's avatar
Martin Bauer committed
184
        (ptr_01_1m2, x*fstride_myfield[0] + y*fstride_myfield[1] + fstride_myfield[0] - 2*fstride_myfield[1])
Martin Bauer's avatar
Martin Bauer committed
185
    """
Martin Bauer's avatar
Martin Bauer committed
186
    field = field_access.field
187
188
    offset = 0
    name = ""
Martin Bauer's avatar
Martin Bauer committed
189
    list_to_hash = []
Martin Bauer's avatar
Martin Bauer committed
190
191
    for coordinate_id, coordinate_value in coordinates.items():
        offset += field.strides[coordinate_id] * coordinate_value
192

Martin Bauer's avatar
Martin Bauer committed
193
194
195
        if coordinate_id < field.spatial_dimensions:
            offset += field.strides[coordinate_id] * field_access.offsets[coordinate_id]
            if type(field_access.offsets[coordinate_id]) is int:
Martin Bauer's avatar
Martin Bauer committed
196
                name += "_%d%d" % (coordinate_id, field_access.offsets[coordinate_id])
197
            else:
Martin Bauer's avatar
Martin Bauer committed
198
                list_to_hash.append(field_access.offsets[coordinate_id])
199
        else:
Martin Bauer's avatar
Martin Bauer committed
200
            if type(coordinate_value) is int:
Martin Bauer's avatar
Martin Bauer committed
201
                name += "_%d%d" % (coordinate_id, coordinate_value)
202
            else:
Martin Bauer's avatar
Martin Bauer committed
203
                list_to_hash.append(coordinate_value)
204

Martin Bauer's avatar
Martin Bauer committed
205
    if len(list_to_hash) > 0:
Martin Bauer's avatar
Martin Bauer committed
206
        name += "_%0.6X" % (hash(tuple(list_to_hash)))
207

Martin Bauer's avatar
Martin Bauer committed
208
    name = name.replace("-", 'm')
Martin Bauer's avatar
Martin Bauer committed
209
210
    new_ptr = TypedSymbol(previous_ptr.name + name, previous_ptr.dtype)
    return new_ptr, offset
211
212


Martin Bauer's avatar
Martin Bauer committed
213
def parse_base_pointer_info(base_pointer_specification, loop_order, spatial_dimensions, index_dimensions):
214
    """
Martin Bauer's avatar
Martin Bauer committed
215
    Creates base pointer specification for :func:`resolve_field_accesses` function.
Martin Bauer's avatar
Martin Bauer committed
216
217
218

    Specification of how many and which intermediate pointers are created for a field access.
    For example [ (0), (2,3,)]  creates on base pointer for coordinates 2 and 3 and writes the offset for coordinate
219
    zero directly in the field access. These specifications are defined dependent on the loop ordering.
Martin Bauer's avatar
Martin Bauer committed
220
221
    This function translates more readable version into the specification above.

222
    Allowed specifications:
Martin Bauer's avatar
Martin Bauer committed
223
224
225
226
227
228
        - "spatialInner<int>" spatialInner0 is the innermost loop coordinate,
          spatialInner1 the loop enclosing the innermost
        - "spatialOuter<int>" spatialOuter0 is the outermost loop
        - "index<int>": index coordinate
        - "<int>": specifying directly the coordinate

Martin Bauer's avatar
Martin Bauer committed
229
230
231
    Args:
        base_pointer_specification: nested list with above specifications
        loop_order: list with ordering of loops from outer to inner
Martin Bauer's avatar
Martin Bauer committed
232
233
        spatial_dimensions: number of spatial dimensions
        index_dimensions: number of index dimensions
Martin Bauer's avatar
Martin Bauer committed
234
235
236

    Returns:
        list of tuples that can be passed to :func:`resolve_field_accesses`
Martin Bauer's avatar
Martin Bauer committed
237
238
239
240
241

    Examples:
        >>> parse_base_pointer_info([['spatialOuter0'], ['index0']], loop_order=[2,1,0],
        ...                         spatial_dimensions=3, index_dimensions=1)
        [[0], [3], [1, 2]]
242
243
    """
    result = []
Martin Bauer's avatar
Martin Bauer committed
244
245
    specified_coordinates = set()
    loop_order = list(reversed(loop_order))
Martin Bauer's avatar
Martin Bauer committed
246
    for spec_group in base_pointer_specification:
Martin Bauer's avatar
Martin Bauer committed
247
248
249
        new_group = []

        def add_new_element(elem):
Martin Bauer's avatar
Martin Bauer committed
250
            if elem >= spatial_dimensions + index_dimensions:
Martin Bauer's avatar
Martin Bauer committed
251
252
253
254
255
                raise ValueError("Coordinate %d does not exist" % (elem,))
            new_group.append(elem)
            if elem in specified_coordinates:
                raise ValueError("Coordinate %d specified two times" % (elem,))
            specified_coordinates.add(elem)
Martin Bauer's avatar
Martin Bauer committed
256
        for element in spec_group:
257
            if type(element) is int:
Martin Bauer's avatar
Martin Bauer committed
258
                add_new_element(element)
259
260
261
262
            elif element.startswith("spatial"):
                element = element[len("spatial"):]
                if element.startswith("Inner"):
                    index = int(element[len("Inner"):])
Martin Bauer's avatar
Martin Bauer committed
263
                    add_new_element(loop_order[index])
264
265
                elif element.startswith("Outer"):
                    index = int(element[len("Outer"):])
Martin Bauer's avatar
Martin Bauer committed
266
                    add_new_element(loop_order[-index])
267
                elif element == "all":
Martin Bauer's avatar
Martin Bauer committed
268
                    for i in range(spatial_dimensions):
Martin Bauer's avatar
Martin Bauer committed
269
                        add_new_element(i)
270
271
272
273
                else:
                    raise ValueError("Could not parse " + element)
            elif element.startswith("index"):
                index = int(element[len("index"):])
Martin Bauer's avatar
Martin Bauer committed
274
                add_new_element(spatial_dimensions + index)
275
276
277
            else:
                raise ValueError("Unknown specification %s" % (element,))

Martin Bauer's avatar
Martin Bauer committed
278
        result.append(new_group)
279

Martin Bauer's avatar
Martin Bauer committed
280
    all_coordinates = set(range(spatial_dimensions + index_dimensions))
Martin Bauer's avatar
Martin Bauer committed
281
    rest = all_coordinates - specified_coordinates
282
283
    if rest:
        result.append(list(rest))
284

285
286
287
    return result


Martin Bauer's avatar
Martin Bauer committed
288
289
def substitute_array_accesses_with_constants(ast_node):
    """Substitutes all instances of Indexed (array accesses) that are not field accesses with constants.
Martin Bauer's avatar
Martin Bauer committed
290
291
292
    Benchmarks showed that using an array access as loop bound or in pointer computations cause some compilers to do
    less optimizations.
    This transformation should be after field accesses have been resolved (since they introduce array accesses) and
293
294
295
    before constants are moved before the loops.
    """

Martin Bauer's avatar
Martin Bauer committed
296
    def handle_sympy_expression(expr, parent_block):
297
298
299
300
301
302
        """Returns sympy expression where array accesses have been replaced with constants, together with a list
        of assignments that define these constants"""
        if not isinstance(expr, sp.Expr):
            return expr

        # get all indexed expressions that are not field accesses
Martin Bauer's avatar
Martin Bauer committed
303
        indexed_expressions = [e for e in expr.atoms(sp.Indexed) if not isinstance(e, ast.ResolvedFieldAccess)]
304
305

        # special case: right hand side is a single indexed expression, then nothing has to be done
Martin Bauer's avatar
Martin Bauer committed
306
        if len(indexed_expressions) == 1 and expr == indexed_expressions[0]:
307
308
            return expr

Martin Bauer's avatar
Martin Bauer committed
309
310
        constants_definitions = []
        constant_substitutions = {}
Martin Bauer's avatar
Martin Bauer committed
311
312
        for indexed_expr in indexed_expressions:
            base, idx = indexed_expr.args
Martin Bauer's avatar
Martin Bauer committed
313
314
315
316
            typed_symbol = base.args[0]
            base_type = deepcopy(get_base_type(typed_symbol.dtype))
            base_type.const = False
            constant_replacing_indexed = TypedSymbol(typed_symbol.name + str(idx), base_type)
Martin Bauer's avatar
Martin Bauer committed
317
318
            constants_definitions.append(ast.SympyAssignment(constant_replacing_indexed, indexed_expr))
            constant_substitutions[indexed_expr] = constant_replacing_indexed
Martin Bauer's avatar
Martin Bauer committed
319
320
321
        constants_definitions.sort(key=lambda e: e.lhs.name)

        already_defined = parent_block.symbols_defined
Martin Bauer's avatar
Martin Bauer committed
322
323
324
        for new_assignment in constants_definitions:
            if new_assignment.lhs not in already_defined:
                parent_block.insert_before(new_assignment, ast_node)
Martin Bauer's avatar
Martin Bauer committed
325
326
327
328
329
330
331
332
333
334
335

        return expr.subs(constant_substitutions)

    if isinstance(ast_node, ast.SympyAssignment):
        ast_node.rhs = handle_sympy_expression(ast_node.rhs, ast_node.parent)
        ast_node.lhs = handle_sympy_expression(ast_node.lhs, ast_node.parent)
    elif isinstance(ast_node, ast.LoopOverCoordinate):
        ast_node.start = handle_sympy_expression(ast_node.start, ast_node.parent)
        ast_node.stop = handle_sympy_expression(ast_node.stop, ast_node.parent)
        ast_node.step = handle_sympy_expression(ast_node.step, ast_node.parent)
        substitute_array_accesses_with_constants(ast_node.body)
336
    else:
Martin Bauer's avatar
Martin Bauer committed
337
338
        for a in ast_node.args:
            substitute_array_accesses_with_constants(a)
339

Martin Bauer's avatar
Martin Bauer committed
340

Martin Bauer's avatar
Martin Bauer committed
341
342
def resolve_buffer_accesses(ast_node, base_buffer_index, read_only_field_names=set()):
    def visit_sympy_expr(expr, enclosing_block, sympy_assignment):
343
        if isinstance(expr, Field.Access):
Martin Bauer's avatar
Martin Bauer committed
344
            field_access = expr
345
346

            # Do not apply transformation if field is not a buffer
Martin Bauer's avatar
Martin Bauer committed
347
            if not FieldType.is_buffer(field_access.field):
348
349
                return expr

Martin Bauer's avatar
Martin Bauer committed
350
            buffer = field_access.field
351

352
            dtype = PointerType(buffer.dtype, const=buffer.name in read_only_field_names, restrict=False)
Martin Bauer's avatar
Martin Bauer committed
353
            field_ptr = TypedSymbol("%s%s" % (Field.DATA_PREFIX, symbol_name_to_variable_name(buffer.name)), dtype)
354

Martin Bauer's avatar
Martin Bauer committed
355
356
            buffer_index = base_buffer_index
            if len(field_access.index) > 1:
357
358
                raise RuntimeError('Only indexing dimensions up to 1 are currently supported in buffers!')

Martin Bauer's avatar
Martin Bauer committed
359
360
361
            if len(field_access.index) > 0:
                cell_index = field_access.index[0]
                buffer_index += cell_index
362

Martin Bauer's avatar
Martin Bauer committed
363
364
            result = ast.ResolvedFieldAccess(field_ptr, buffer_index, field_access.field, field_access.offsets,
                                             field_access.index)
365

Martin Bauer's avatar
Martin Bauer committed
366
            return visit_sympy_expr(result, enclosing_block, sympy_assignment)
367
368
369
370
        else:
            if isinstance(expr, ast.ResolvedFieldAccess):
                return expr

Martin Bauer's avatar
Martin Bauer committed
371
            new_args = [visit_sympy_expr(e, enclosing_block, sympy_assignment) for e in expr.args]
372
            kwargs = {'evaluate': False} if type(expr) in (sp.Add, sp.Mul, sp.Piecewise) else {}
Martin Bauer's avatar
Martin Bauer committed
373
374
375
376
377
378
379
380
            return expr.func(*new_args, **kwargs) if new_args else expr

    def visit_node(sub_ast):
        if isinstance(sub_ast, ast.SympyAssignment):
            enclosing_block = sub_ast.parent
            assert type(enclosing_block) is ast.Block
            sub_ast.lhs = visit_sympy_expr(sub_ast.lhs, enclosing_block, sub_ast)
            sub_ast.rhs = visit_sympy_expr(sub_ast.rhs, enclosing_block, sub_ast)
381
        else:
Martin Bauer's avatar
Martin Bauer committed
382
383
            for i, a in enumerate(sub_ast.args):
                visit_node(a)
384

Martin Bauer's avatar
Martin Bauer committed
385
    return visit_node(ast_node)
386

387

Martin Bauer's avatar
Martin Bauer committed
388
def resolve_field_accesses(ast_node, read_only_field_names=set(),
Martin Bauer's avatar
Martin Bauer committed
389
390
                           field_to_base_pointer_info=MappingProxyType({}),
                           field_to_fixed_coordinates=MappingProxyType({})):
Martin Bauer's avatar
Martin Bauer committed
391
392
393
    """
    Substitutes :class:`pystencils.field.Field.Access` nodes by array indexing

394
395
396
397
398
399
    Args:
        ast_node: the AST root
        read_only_field_names: set of field names which are considered read-only
        field_to_base_pointer_info: a list of tuples indicating which intermediate base pointers should be created
                                    for details see :func:`parse_base_pointer_info`
        field_to_fixed_coordinates: map of field name to a tuple of coordinate symbols. Instead of using the loop
Martin Bauer's avatar
Martin Bauer committed
400
                                    counters to index the field these symbols are used as coordinates
401
402
403

    Returns
        transformed AST
Martin Bauer's avatar
Martin Bauer committed
404
    """
Martin Bauer's avatar
Martin Bauer committed
405
406
    field_to_base_pointer_info = OrderedDict(sorted(field_to_base_pointer_info.items(), key=lambda pair: pair[0]))
    field_to_fixed_coordinates = OrderedDict(sorted(field_to_fixed_coordinates.items(), key=lambda pair: pair[0]))
407

Martin Bauer's avatar
Martin Bauer committed
408
    def visit_sympy_expr(expr, enclosing_block, sympy_assignment):
409
        if isinstance(expr, Field.Access):
Martin Bauer's avatar
Martin Bauer committed
410
            field_access = expr
Martin Bauer's avatar
Martin Bauer committed
411
            field = field_access.field
412

Martin Bauer's avatar
Martin Bauer committed
413
            if field_access.indirect_addressing_fields:
414
415
                new_offsets = tuple(visit_sympy_expr(off, enclosing_block, sympy_assignment)
                                    for off in field_access.offsets)
Martin Bauer's avatar
Martin Bauer committed
416
417
418
419
420
                new_indices = tuple(visit_sympy_expr(ind, enclosing_block, sympy_assignment)
                                    if isinstance(ind, sp.Basic) else ind
                                    for ind in field_access.index)
                field_access = Field.Access(field_access.field, new_offsets,
                                            new_indices, field_access.is_absolute_access)
421

Martin Bauer's avatar
Martin Bauer committed
422
423
            if field.name in field_to_base_pointer_info:
                base_pointer_info = field_to_base_pointer_info[field.name]
424
            else:
Martin Bauer's avatar
Martin Bauer committed
425
                base_pointer_info = [list(range(field.index_dimensions + field.spatial_dimensions))]
426

427
            dtype = PointerType(field.dtype, const=field.name in read_only_field_names, restrict=False)
Martin Bauer's avatar
Martin Bauer committed
428
            field_ptr = TypedSymbol("%s%s" % (Field.DATA_PREFIX, symbol_name_to_variable_name(field.name)), dtype)
429

Martin Bauer's avatar
Martin Bauer committed
430
431
432
433
            def create_coordinate_dict(group_param):
                coordinates = {}
                for e in group_param:
                    if e < field.spatial_dimensions:
Martin Bauer's avatar
Martin Bauer committed
434
                        if field.name in field_to_fixed_coordinates:
435
436
437
438
                            if not field_access.is_absolute_access:
                                coordinates[e] = field_to_fixed_coordinates[field.name][e]
                            else:
                                coordinates[e] = 0
439
                        else:
Martin Bauer's avatar
Martin Bauer committed
440
441
442
443
                            if not field_access.is_absolute_access:
                                coordinates[e] = ast.LoopOverCoordinate.get_loop_counter_symbol(e)
                            else:
                                coordinates[e] = 0
Martin Bauer's avatar
Martin Bauer committed
444
                        coordinates[e] *= field.dtype.item_size
445
                    else:
446
                        if isinstance(field.dtype, StructType):
Martin Bauer's avatar
Martin Bauer committed
447
                            assert field.index_dimensions == 1
Martin Bauer's avatar
Martin Bauer committed
448
449
                            accessed_field_name = field_access.index[0]
                            assert isinstance(accessed_field_name, str)
Martin Bauer's avatar
Martin Bauer committed
450
                            coordinates[e] = field.dtype.get_element_offset(accessed_field_name)
451
                        else:
Martin Bauer's avatar
Martin Bauer committed
452
                            coordinates[e] = field_access.index[e - field.spatial_dimensions]
453

Martin Bauer's avatar
Martin Bauer committed
454
                return coordinates
455

Martin Bauer's avatar
Martin Bauer committed
456
            last_pointer = field_ptr
457

Martin Bauer's avatar
Martin Bauer committed
458
459
            for group in reversed(base_pointer_info[1:]):
                coord_dict = create_coordinate_dict(group)
Martin Bauer's avatar
Martin Bauer committed
460
                new_ptr, offset = create_intermediate_base_pointer(field_access, coord_dict, last_pointer)
Martin Bauer's avatar
Martin Bauer committed
461
462
463
464
                if new_ptr not in enclosing_block.symbols_defined:
                    new_assignment = ast.SympyAssignment(new_ptr, last_pointer + offset, is_const=False)
                    enclosing_block.insert_before(new_assignment, sympy_assignment)
                last_pointer = new_ptr
465

Martin Bauer's avatar
Martin Bauer committed
466
            coord_dict = create_coordinate_dict(base_pointer_info[0])
Martin Bauer's avatar
Martin Bauer committed
467
            _, offset = create_intermediate_base_pointer(field_access, coord_dict, last_pointer)
Martin Bauer's avatar
Martin Bauer committed
468
469
            result = ast.ResolvedFieldAccess(last_pointer, offset, field_access.field,
                                             field_access.offsets, field_access.index)
470

Martin Bauer's avatar
Martin Bauer committed
471
472
            if isinstance(get_base_type(field_access.field.dtype), StructType):
                new_type = field_access.field.dtype.get_element_type(field_access.index[0])
Martin Bauer's avatar
Martin Bauer committed
473
                result = cast_func(result, new_type)
474

Martin Bauer's avatar
Martin Bauer committed
475
            return visit_sympy_expr(result, enclosing_block, sympy_assignment)
476
        else:
Martin Bauer's avatar
Martin Bauer committed
477
478
            if isinstance(expr, ast.ResolvedFieldAccess):
                return expr
479

Martin Bauer's avatar
Martin Bauer committed
480
            new_args = [visit_sympy_expr(e, enclosing_block, sympy_assignment) for e in expr.args]
481
            kwargs = {'evaluate': False} if type(expr) in (sp.Add, sp.Mul, sp.Piecewise) else {}
Martin Bauer's avatar
Martin Bauer committed
482
483
484
485
486
487
488
489
            return expr.func(*new_args, **kwargs) if new_args else expr

    def visit_node(sub_ast):
        if isinstance(sub_ast, ast.SympyAssignment):
            enclosing_block = sub_ast.parent
            assert type(enclosing_block) is ast.Block
            sub_ast.lhs = visit_sympy_expr(sub_ast.lhs, enclosing_block, sub_ast)
            sub_ast.rhs = visit_sympy_expr(sub_ast.rhs, enclosing_block, sub_ast)
490
        else:
Martin Bauer's avatar
Martin Bauer committed
491
492
            for i, a in enumerate(sub_ast.args):
                visit_node(a)
493

Martin Bauer's avatar
Martin Bauer committed
494
    return visit_node(ast_node)
495
496


Martin Bauer's avatar
Martin Bauer committed
497
def move_constants_before_loop(ast_node):
498
499
    """Moves :class:`pystencils.ast.SympyAssignment` nodes out of loop body if they are iteration independent.

Martin Bauer's avatar
Martin Bauer committed
500
    Call this after creating the loop structure with :func:`make_loop_over_domain`
Martin Bauer's avatar
Martin Bauer committed
501
    """
Martin Bauer's avatar
Martin Bauer committed
502
    def find_block_to_move_to(node):
Martin Bauer's avatar
Martin Bauer committed
503
504
        """
        Traverses parents of node as long as the symbols are independent and returns a (parent) block
505
        the assignment can be safely moved to
Martin Bauer's avatar
Martin Bauer committed
506
507
508
        :param node: SympyAssignment inside a Block
        :return blockToInsertTo, childOfBlockToInsertBefore
        """
509
510
        assert isinstance(node.parent, ast.Block)

Martin Bauer's avatar
Martin Bauer committed
511
512
        last_block = node.parent
        last_block_child = node
513
        element = node.parent
Martin Bauer's avatar
Martin Bauer committed
514
        prev_element = node
515
516
        while element:
            if isinstance(element, ast.Block):
Martin Bauer's avatar
Martin Bauer committed
517
518
                last_block = element
                last_block_child = prev_element
519
520

            if isinstance(element, ast.Conditional):
Martin Bauer's avatar
Martin Bauer committed
521
                critical_symbols = element.condition_expr.atoms(sp.Symbol)
522
            else:
Martin Bauer's avatar
Martin Bauer committed
523
524
                critical_symbols = element.symbols_defined
            if node.undefined_symbols.intersection(critical_symbols):
525
                break
Martin Bauer's avatar
Martin Bauer committed
526
            prev_element = element
527
            element = element.parent
Martin Bauer's avatar
Martin Bauer committed
528
        return last_block, last_block_child
529

Martin Bauer's avatar
Martin Bauer committed
530
531
    def check_if_assignment_already_in_block(assignment, target_block):
        for arg in target_block.args:
532
533
534
535
536
537
            if type(arg) is not ast.SympyAssignment:
                continue
            if arg.lhs == assignment.lhs:
                return arg
        return None

Martin Bauer's avatar
Martin Bauer committed
538
    def get_blocks(node, result_list):
539
        if isinstance(node, ast.Block):
Martin Bauer's avatar
Martin Bauer committed
540
            result_list.append(node)
541
542
        if isinstance(node, ast.Node):
            for a in node.args:
Martin Bauer's avatar
Martin Bauer committed
543
                get_blocks(a, result_list)
544

Martin Bauer's avatar
Martin Bauer committed
545
546
547
    all_blocks = []
    get_blocks(ast_node, all_blocks)
    for block in all_blocks:
Martin Bauer's avatar
Martin Bauer committed
548
        children = block.take_child_nodes()
549
        for child in children:
550
551
552
            target, child_to_insert_before = find_block_to_move_to(child)
            if target == block:     # movement not possible
                target.append(child)
553
            else:
554
555
                if isinstance(child, ast.SympyAssignment):
                    exists_already = check_if_assignment_already_in_block(child, target)
556
                else:
557
                    exists_already = False
Martin Bauer's avatar
Martin Bauer committed
558

559
560
                if not exists_already:
                    target.insert_before(child, child_to_insert_before)
Martin Bauer's avatar
Martin Bauer committed
561
562
                elif exists_already and exists_already.rhs == child.rhs:
                    pass
563
                else:
Martin Bauer's avatar
Martin Bauer committed
564
                    block.append(child)  # don't move in this case - better would be to rename symbol
565
566


Martin Bauer's avatar
Martin Bauer committed
567
def split_inner_loop(ast_node: ast.Node, symbol_groups):
Martin Bauer's avatar
Martin Bauer committed
568
569
    """
    Splits inner loop into multiple loops to minimize the amount of simultaneous load/store streams
570

Martin Bauer's avatar
Martin Bauer committed
571
572
573
574
575
    Args:
        ast_node: AST root
        symbol_groups: sequence of symbol sequences: for each symbol sequence a new inner loop is created which
                       updates these symbols and their dependent symbols. Symbols which are in none of the symbolGroups
                       and which no symbol in a symbol group depends on, are not updated!
Martin Bauer's avatar
Martin Bauer committed
576
    """
Martin Bauer's avatar
Martin Bauer committed
577
578
579
580
581
582
583
584
585
586
587
588
589
    all_loops = ast_node.atoms(ast.LoopOverCoordinate)
    inner_loop = [l for l in all_loops if l.is_innermost_loop]
    assert len(inner_loop) == 1, "Error in AST: multiple innermost loops. Was split transformation already called?"
    inner_loop = inner_loop[0]
    assert type(inner_loop.body) is ast.Block
    outer_loop = [l for l in all_loops if l.is_outermost_loop]
    assert len(outer_loop) == 1, "Error in AST, multiple outermost loops."
    outer_loop = outer_loop[0]

    symbols_with_temporary_array = OrderedDict()
    assignment_map = OrderedDict((a.lhs, a) for a in inner_loop.body.args)

    assignment_groups = []
Martin Bauer's avatar
Martin Bauer committed
590
    for symbol_group in symbol_groups:
591
        # get all dependent symbols
Martin Bauer's avatar
Martin Bauer committed
592
        symbols_to_process = list(symbol_group)
Martin Bauer's avatar
Martin Bauer committed
593
594
595
596
        symbols_resolved = set()
        while symbols_to_process:
            s = symbols_to_process.pop()
            if s in symbols_resolved:
597
598
                continue

Martin Bauer's avatar
Martin Bauer committed
599
            if s in assignment_map:  # if there is no assignment inside the loop body it is independent already
Martin Bauer's avatar
Martin Bauer committed
600
601
602
                for new_symbol in assignment_map[s].rhs.atoms(sp.Symbol):
                    if type(new_symbol) is not Field.Access and new_symbol not in symbols_with_temporary_array:
                        symbols_to_process.append(new_symbol)
Martin Bauer's avatar
Martin Bauer committed
603
            symbols_resolved.add(s)
604

Martin Bauer's avatar
Martin Bauer committed
605
        for symbol in symbol_group:
606
607
            if type(symbol) is not Field.Access:
                assert type(symbol) is TypedSymbol
Martin Bauer's avatar
Martin Bauer committed
608
609
                new_ts = TypedSymbol(symbol.name, PointerType(symbol.dtype))
                symbols_with_temporary_array[symbol] = IndexedBase(new_ts, shape=(1,))[inner_loop.loop_counter_symbol]
610

Martin Bauer's avatar
Martin Bauer committed
611
612
613
614
        assignment_group = []
        for assignment in inner_loop.body.args:
            if assignment.lhs in symbols_resolved:
                new_rhs = assignment.rhs.subs(symbols_with_temporary_array.items())
Martin Bauer's avatar
Martin Bauer committed
615
                if type(assignment.lhs) is not Field.Access and assignment.lhs in symbol_group:
616
                    assert type(assignment.lhs) is TypedSymbol
Martin Bauer's avatar
Martin Bauer committed
617
618
                    new_ts = TypedSymbol(assignment.lhs.name, PointerType(assignment.lhs.dtype))
                    new_lhs = IndexedBase(new_ts, shape=(1,))[inner_loop.loop_counter_symbol]
619
                else:
Martin Bauer's avatar
Martin Bauer committed
620
621
622
                    new_lhs = assignment.lhs
                assignment_group.append(ast.SympyAssignment(new_lhs, new_rhs))
        assignment_groups.append(assignment_group)
623

Martin Bauer's avatar
Martin Bauer committed
624
625
    new_loops = [inner_loop.new_loop_with_different_body(ast.Block(group)) for group in assignment_groups]
    inner_loop.parent.replace(inner_loop, ast.Block(new_loops))
626

Martin Bauer's avatar
Martin Bauer committed
627
628
    for tmp_array in symbols_with_temporary_array:
        tmp_array_pointer = TypedSymbol(tmp_array.name, PointerType(tmp_array.dtype))
Martin Bauer's avatar
Martin Bauer committed
629
630
631
632
        alloc_node = ast.TemporaryMemoryAllocation(tmp_array_pointer, inner_loop.stop, inner_loop.start)
        free_node = ast.TemporaryMemoryFree(alloc_node)
        outer_loop.parent.insert_front(alloc_node)
        outer_loop.parent.append(free_node)
633
634


Martin Bauer's avatar
Martin Bauer committed
635
def cut_loop(loop_node, cutting_points):
Martin Bauer's avatar
Martin Bauer committed
636
637
638
639
640
641
642
643
644
645
    """Cuts loop at given cutting points.

    One loop is transformed into len(cuttingPoints)+1 new loops that range from
    old_begin to cutting_points[1], ..., cutting_points[-1] to old_end

    Modifies the ast in place

    Returns:
        list of new loop nodes
    """
Martin Bauer's avatar
Martin Bauer committed
646
    if loop_node.step != 1:
647
        raise NotImplementedError("Can only split loops that have a step of 1")
Martin Bauer's avatar
Martin Bauer committed
648
649
650
    new_loops = []
    new_start = loop_node.start
    cutting_points = list(cutting_points) + [loop_node.stop]
Martin Bauer's avatar
Martin Bauer committed
651
652
    for new_end in cutting_points:
        if new_end - new_start == 1:
Martin Bauer's avatar
Martin Bauer committed
653
654
655
            new_body = deepcopy(loop_node.body)
            new_body.subs({loop_node.loop_counter_symbol: new_start})
            new_loops.append(new_body)
Martin Bauer's avatar
Martin Bauer committed
656
657
        elif new_end - new_start == 0:
            pass
658
        else:
Martin Bauer's avatar
Martin Bauer committed
659
660
            new_loop = ast.LoopOverCoordinate(deepcopy(loop_node.body), loop_node.coordinate_to_loop_over,
                                              new_start, new_end, loop_node.step)
Martin Bauer's avatar
Martin Bauer committed
661
            new_loops.append(new_loop)
Martin Bauer's avatar
Martin Bauer committed
662
        new_start = new_end
Martin Bauer's avatar
Martin Bauer committed
663
    loop_node.parent.replace(loop_node, new_loops)
Martin Bauer's avatar
Martin Bauer committed
664
    return new_loops
665
666


667
668
def simplify_conditionals(node: ast.Node, loop_counter_simplification: bool=False) -> None:
    """Removes conditionals that are always true/false.
669
670

    Args:
671
672
673
674
675
676
        node: ast node, all descendants of this node are simplified
        loop_counter_simplification: if enabled, tries to detect if a conditional is always true/false
                                     depending on the surrounding loop. For example if the surrounding loop goes from
                                     x=0 to 10 and the condition is x < 0, it is removed.
                                     This analysis needs the integer set library (ISL) islpy, so it is not done by
                                     default.
677
    """
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    for conditional in node.atoms(ast.Conditional):
        conditional.condition_expr = sp.simplify(conditional.condition_expr)
        if conditional.condition_expr == sp.true:
            conditional.parent.replace(conditional, [conditional.true_block])
        elif conditional.condition_expr == sp.false:
            conditional.parent.replace(conditional, [conditional.false_block] if conditional.false_block else [])
        elif loop_counter_simplification:
            try:
                # noinspection PyUnresolvedReferences
                from pystencils.integer_set_analysis import simplify_loop_counter_dependent_conditional
                simplify_loop_counter_dependent_conditional(conditional)
            except ImportError:
                warnings.warn("Integer simplifications in conditionals skipped, because ISLpy package not installed")


def cleanup_blocks(node: ast.Node) -> None:
694
695
696
697
698
    """Curly Brace Removal: Removes empty blocks, and replaces blocks with a single child by its child """
    if isinstance(node, ast.SympyAssignment):
        return
    elif isinstance(node, ast.Block):
        for a in list(node.args):
Martin Bauer's avatar
Martin Bauer committed
699
            cleanup_blocks(a)
700
701
702
703
704
        if len(node.args) <= 1 and isinstance(node.parent, ast.Block):
            node.parent.replace(node, node.args)
            return
    else:
        for a in node.args:
Martin Bauer's avatar
Martin Bauer committed
705
            cleanup_blocks(a)
706
707


Martin Bauer's avatar
Martin Bauer committed
708
def symbol_name_to_variable_name(symbol_name):
709
    """Replaces characters which are allowed in sympy symbol names but not in C/C++ variable names"""
Martin Bauer's avatar
Martin Bauer committed
710
    return symbol_name.replace("^", "_")
711
712


713
714
715
716
717
718
719
720
721
722
723
724
class KernelConstraintsCheck:
    """Checks if the input to create_kernel is valid.

    Test the following conditions:

    - SSA Form for pure symbols:
        -  Every pure symbol may occur only once as left-hand-side of an assignment
        -  Every pure symbol that is read, may not be written to later
    - Independence / Parallelization condition:
        - a field that is written may only be read at exact the same spatial position

    (Pure symbols are symbols that are not Field.Accesses)
Martin Bauer's avatar
Martin Bauer committed
725
    """
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
    FieldAndIndex = namedtuple('FieldAndIndex', ['field', 'index'])

    def __init__(self, type_for_symbol, check_independence_condition):
        self._type_for_symbol = type_for_symbol
        self._defined_pure_symbols = set()
        self._accessed_pure_symbols = set()

        self._field_writes = defaultdict(set)
        self.fields_read = set()
        self.check_independence_condition = check_independence_condition

    def process_assignment(self, assignment):
        # for checks it is crucial to process rhs before lhs to catch e.g. a = a + 1
        new_rhs = self.process_expression(assignment.rhs)
        new_lhs = self._process_lhs(assignment.lhs)
        return ast.SympyAssignment(new_lhs, new_rhs)

743
    def process_expression(self, rhs, type_constants=True):
744
745
        self._update_accesses_rhs(rhs)
        if isinstance(rhs, Field.Access):
Martin Bauer's avatar
Martin Bauer committed
746
            self.fields_read.add(rhs.field)
Martin Bauer's avatar
Martin Bauer committed
747
            self.fields_read.update(rhs.indirect_addressing_fields)
748
749
750
751
752
            return rhs
        elif isinstance(rhs, TypedSymbol):
            return rhs
        elif isinstance(rhs, sp.Symbol):
            return TypedSymbol(symbol_name_to_variable_name(rhs.name), self._type_for_symbol[rhs.name])
753
        elif type_constants and isinstance(rhs, sp.Number):
754
755
            return cast_func(rhs, create_type(self._type_for_symbol['_constant']))
        elif isinstance(rhs, sp.Mul):
756
            new_args = [self.process_expression(arg, type_constants) if arg not in (-1, 1) else arg for arg in rhs.args]
757
            return rhs.func(*new_args) if new_args else rhs
758
759
        elif isinstance(rhs, sp.Indexed):
            return rhs
760
761
762
        else:
            if isinstance(rhs, sp.Pow):
                # don't process exponents -> they should remain integers
763
                return sp.Pow(self.process_expression(rhs.args[0], type_constants), rhs.args[1])
764
            else:
765
                new_args = [self.process_expression(arg, type_constants) for arg in rhs.args]
766
                return rhs.func(*new_args) if new_args else rhs
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784

    @property
    def fields_written(self):
        return set(k.field for k, v in self._field_writes.items() if len(v))

    def _process_lhs(self, lhs):
        assert isinstance(lhs, sp.Symbol)
        self._update_accesses_lhs(lhs)
        if not isinstance(lhs, Field.Access) and not isinstance(lhs, TypedSymbol):
            return TypedSymbol(lhs.name, self._type_for_symbol[lhs.name])
        else:
            return lhs

    def _update_accesses_lhs(self, lhs):
        if isinstance(lhs, Field.Access):
            fai = self.FieldAndIndex(lhs.field, lhs.index)
            self._field_writes[fai].add(lhs.offsets)
            if len(self._field_writes[fai]) > 1:
785
                raise ValueError("Field {} is written at two different locations".format(lhs.field.name))
786
787
        elif isinstance(lhs, sp.Symbol):
            if lhs in self._defined_pure_symbols:
788
                raise ValueError("Assignments not in SSA form, multiple assignments to {}".format(lhs.name))
789
            if lhs in self._accessed_pure_symbols:
790
                raise ValueError("Symbol {} is written, after it has been read".format(lhs.name))
791
792
793
794
795
796
797
798
            self._defined_pure_symbols.add(lhs)

    def _update_accesses_rhs(self, rhs):
        if isinstance(rhs, Field.Access) and self.check_independence_condition:
            writes = self._field_writes[self.FieldAndIndex(rhs.field, rhs.index)]
            for write_offset in writes:
                assert len(writes) == 1
                if write_offset != rhs.offsets:
799
800
                    raise ValueError("Violation of loop independence condition. Field "
                                     "{} is read at {} and written at {}".format(rhs.field, rhs.offsets, write_offset))
801
802
803
804
805
806
807
808
            self.fields_read.add(rhs.field)
        elif isinstance(rhs, sp.Symbol):
            self._accessed_pure_symbols.add(rhs)


def add_types(eqs, type_for_symbol, check_independence_condition):
    """Traverses AST and replaces every :class:`sympy.Symbol` by a :class:`pystencils.typedsymbol.TypedSymbol`.

Martin Bauer's avatar
Martin Bauer committed
809
810
    Additionally returns sets of all fields which are read/written

811
812
813
814
815
816
817
818
819
    Args:
        eqs: list of equations
        type_for_symbol: dict mapping symbol names to types. Types are strings of C types like 'int' or 'double'
        check_independence_condition: check that loop iterations are independent - this has to be skipped for indexed
                                      kernels

    Returns:
        ``fields_read, fields_written, typed_equations`` set of read fields, set of written fields,
         list of equations where symbols have been replaced by typed symbols
Martin Bauer's avatar
Martin Bauer committed
820
    """
Martin Bauer's avatar
Martin Bauer committed
821
822
    if isinstance(type_for_symbol, str) or not hasattr(type_for_symbol, '__getitem__'):
        type_for_symbol = typing_from_sympy_inspection(eqs, type_for_symbol)
823

824
    check = KernelConstraintsCheck(type_for_symbol, check_independence_condition)
825

Martin Bauer's avatar
Martin Bauer committed
826
827
828
829
    def visit(obj):
        if isinstance(obj, list) or isinstance(obj, tuple):
            return [visit(e) for e in obj]
        if isinstance(obj, sp.Eq) or isinstance(obj, ast.SympyAssignment) or isinstance(obj, Assignment):
830
            return check.process_assignment(obj)
Martin Bauer's avatar
Martin Bauer committed
831
        elif isinstance(obj, ast.Conditional):
Martin Bauer's avatar
Martin Bauer committed
832
            false_block = None if obj.false_block is None else visit(obj.false_block)
833
            return ast.Conditional(check.process_expression(obj.condition_expr, type_constants=False),
Martin Bauer's avatar
Martin Bauer committed
834
                                   true_block=visit(obj.true_block), false_block=false_block)
Martin Bauer's avatar
Martin Bauer committed
835
836
        elif isinstance(obj, ast.Block):
            return ast.Block([visit(e) for e in obj.args])
837
        elif isinstance(obj, ast.Node) and not isinstance(obj, ast.LoopOverCoordinate):
Martin Bauer's avatar
Martin Bauer committed
838
            return obj
839
840
        else:
            raise ValueError("Invalid object in kernel " + str(type(obj)))
841

Martin Bauer's avatar
Martin Bauer committed
842
    typed_equations = visit(eqs)
843

844
    return check.fields_read, check.fields_written, typed_equations
845
846


Martin Bauer's avatar
Martin Bauer committed
847
def insert_casts(node):
848
    """Checks the types and inserts casts and pointer arithmetic where necessary.
Martin Bauer's avatar
Martin Bauer committed
849

850
851
852
853
854
    Args:
        node: the head node of the ast

    Returns:
        modified AST
Martin Bauer's avatar
Martin Bauer committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
    """
    def cast(zipped_args_types, target_dtype):
        """
        Adds casts to the arguments if their type differs from the target type
        :param zipped_args_types: a zipped list of args and types
        :param target_dtype: The target data type
        :return: args with possible casts
        """
        casted_args = []
        for argument, data_type in zipped_args_types:
            if data_type.numpy_dtype != target_dtype.numpy_dtype:  # ignoring const
                casted_args.append(cast_func(argument, target_dtype))
            else:
                casted_args.append(argument)
        return casted_args

    def pointer_arithmetic(expr_args):
        """
        Creates a valid pointer arithmetic function
        :param expr_args: Arguments of the add expression
        :return: pointer_arithmetic_func
        """
        pointer = None
        new_args = []
        for arg, data_type in expr_args:
            if data_type.func is PointerType:
                assert pointer is None
                pointer = arg
        for arg, data_type in expr_args:
            if arg != pointer:
                assert data_type.is_int() or data_type.is_uint()
                new_args.append(arg)
        new_args = sp.Add(*new_args) if len(new_args) > 0 else new_args
        return pointer_arithmetic_func(pointer, new_args)

890
    if isinstance(node, sp.AtomicExpr) or isinstance(node, cast_func):
Martin Bauer's avatar
Martin Bauer committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
        return node
    args = []
    for arg in node.args:
        args.append(insert_casts(arg))
    # TODO indexed, LoopOverCoordinate
    if node.func in (sp.Add, sp.Mul, sp.Or, sp.And, sp.Pow, sp.Eq, sp.Ne, sp.Lt, sp.Le, sp.Gt, sp.Ge):
        # TODO optimize pow, don't cast integer on double
        types = [get_type_of_expression(arg) for arg in args]
        assert len(types) > 0
        target = collate_types(types)
        zipped = list(zip(args, types))
        if target.func is PointerType:
            assert node.func is sp.Add
            return pointer_arithmetic(zipped)
        else:
            return node.func(*cast(zipped, target))
    elif node.func is ast.SympyAssignment:
        lhs = args[0]
        rhs = args[1]
        target = get_type_of_expression(lhs)
        if target.func is PointerType:
            return node.func(*args)  # TODO fix, not complete
        else:
            return node.func(lhs, *cast([(rhs, get_type_of_expression(rhs))], target))
    elif node.func is ast.ResolvedFieldAccess:
        return node
    elif node.func is ast.Block:
        for old_arg, new_arg in zip(node.args, args):
            node.replace(old_arg, new_arg)
        return node
    elif node.func is ast.LoopOverCoordinate:
        for old_arg, new_arg in zip(node.args, args):
            node.replace(old_arg, new_arg)
        return node
    elif node.func is sp.Piecewise:
        expressions = [expr for (expr, _) in args]
        types = [get_type_of_expression(expr) for expr in expressions]
        target = collate_types(types)
        zipped = list(zip(expressions, types))
        casted_expressions = cast(zipped, target)
        args = [arg.func(*[expr, arg.cond]) for (arg, expr) in zip(args, casted_expressions)]

    return node.func(*args)


936
937
938
939
940
941
942
943
def remove_conditionals_in_staggered_kernel(function_node: ast.KernelFunction) -> None:
    """Removes conditionals of a kernel that iterates over staggered positions by splitting the loops at last element"""

    all_inner_loops = [l for l in function_node.atoms(ast.LoopOverCoordinate) if l.is_innermost_loop]
    assert len(all_inner_loops) == 1, "Transformation works only on kernels with exactly one inner loop"
    inner_loop = all_inner_loops.pop()

    for loop in parents_of_type(inner_loop, ast.LoopOverCoordinate, include_current=True):
944
        cut_loop(loop, [loop.stop - 1])
945

946
    simplify_conditionals(function_node.body, loop_counter_simplification=True)
947
    cleanup_blocks(function_node.body)
Martin Bauer's avatar
Martin Bauer committed
948

949
950
951
952
    move_constants_before_loop(function_node.body)
    cleanup_blocks(function_node.body)


Martin Bauer's avatar
Martin Bauer committed
953
954
955
# --------------------------------------- Helper Functions -------------------------------------------------------------


Martin Bauer's avatar
Martin Bauer committed
956
def typing_from_sympy_inspection(eqs, default_type="double"):
Martin Bauer's avatar
Martin Bauer committed
957
958
959
    """
    Creates a default symbol name to type mapping.
    If a sympy Boolean is assigned to a symbol it is assumed to be 'bool' otherwise the default type, usually ('double')
960
961
962
963
964
965

    Args:
        eqs: list of equations
        default_type: the type for non-boolean symbols
    Returns:
        dictionary, mapping symbol name to type
Martin Bauer's avatar
Martin Bauer committed
966
    """
Martin Bauer's avatar
Martin Bauer committed
967
    result = defaultdict(lambda: default_type)
968
    for eq in eqs:
969
970
        if isinstance(eq, ast.Node):
            continue
971
972
973
        # problematic case here is when rhs is a symbol: then it is impossible to decide here without
        # further information what type the left hand side is - default fallback is the dict value then
        if isinstance(eq.rhs, Boolean) and not isinstance(eq.rhs, sp.Symbol):
974
975
976
977
            result[eq.lhs.name] = "bool"
    return result


Martin Bauer's avatar
Martin Bauer committed
978
def get_next_parent_of_type(node, parent_type):
979
980
981
982
    """Returns the next parent node of given type or None, if root is reached.

    Traverses the AST nodes parents until a parent of given type was found.
    If no such parent is found, None is returned
Martin Bauer's avatar
Martin Bauer committed
983
    """
984
985
    parent = node.parent
    while parent is not None:
Martin Bauer's avatar
Martin Bauer committed
986
        if isinstance(parent, parent_type):
987
988
989
990
991
            return parent
        parent = parent.parent
    return None


992
def parents_of_type(node, parent_type, include_current=False):
993
    """Generator for all parent nodes of given type"""
994
995
996
997
998
999
1000
    parent = node if include_current else node.parent