kerncraft_interface.py 8.91 KB
Newer Older
Martin Bauer's avatar
Martin Bauer committed
1
from tempfile import TemporaryDirectory
Martin Bauer's avatar
Martin Bauer committed
2

Martin Bauer's avatar
Martin Bauer committed
3
import sympy as sp
Martin Bauer's avatar
Martin Bauer committed
4
import os
Martin Bauer's avatar
Martin Bauer committed
5
from collections import defaultdict
Martin Bauer's avatar
Martin Bauer committed
6
7
import subprocess
import kerncraft
Martin Bauer's avatar
Martin Bauer committed
8
import kerncraft.kernel
Julian Hammer's avatar
Julian Hammer committed
9
from typing import Optional
10
from kerncraft.iaca import iaca_analyse_instrumented_binary, iaca_instrumentation
Julian Hammer's avatar
Julian Hammer committed
11
12
from kerncraft.machinemodel import MachineModel

Martin Bauer's avatar
Martin Bauer committed
13
from pystencils.kerncraft_coupling.generate_benchmark import generate_benchmark
Julian Hammer's avatar
Julian Hammer committed
14
from pystencils.astnodes import LoopOverCoordinate, SympyAssignment, ResolvedFieldAccess, KernelFunction
Martin Bauer's avatar
Martin Bauer committed
15
from pystencils.field import get_layout_from_strides
Martin Bauer's avatar
Martin Bauer committed
16
from pystencils.sympyextensions import count_operations_in_ast
17
from pystencils.transformations import filtered_tree_iteration
Martin Bauer's avatar
Martin Bauer committed
18
from pystencils.utils import DotDict
19
import warnings
Martin Bauer's avatar
Martin Bauer committed
20
21
22
23
24
25
26


class PyStencilsKerncraftKernel(kerncraft.kernel.Kernel):
    """
    Implementation of kerncraft's kernel interface for pystencils CPU kernels.
    Analyses a list of equations assuming they will be executed on a CPU
    """
Martin Bauer's avatar
Martin Bauer committed
27
28
    LIKWID_BASE = '/usr/local/likwid'

29
30
    def __init__(self, ast: KernelFunction, machine: Optional[MachineModel] = None, assumed_layout='SoA',
                 debug_print=False):
Julian Hammer's avatar
Julian Hammer committed
31
32
33
34
35
        """Create a kerncraft kernel using a pystencils AST

        Args:
            ast: pystencils ast
            machine: kerncraft machine model - specify this if kernel needs to be compiled
36
37
38
            assumed_layout: either 'SoA' or 'AoS' - if fields have symbolic sizes the layout of the index
                    coordinates is not known. In this case either a structures of array (SoA) or
                    array of structures (AoS) layout is assumed
Julian Hammer's avatar
Julian Hammer committed
39
        """
Jan Hönig's avatar
Jan Hönig committed
40
        super(PyStencilsKerncraftKernel, self).__init__(machine)
Martin Bauer's avatar
Martin Bauer committed
41
42

        self.ast = ast
Martin Bauer's avatar
Martin Bauer committed
43
        self.temporary_dir = TemporaryDirectory()
Martin Bauer's avatar
Martin Bauer committed
44
45

        # Loops
46
47
        inner_loops = [l for l in filtered_tree_iteration(ast, LoopOverCoordinate, stop_type=SympyAssignment)
                       if l.is_innermost_loop]
Martin Bauer's avatar
Martin Bauer committed
48
        if len(inner_loops) == 0:
Martin Bauer's avatar
Martin Bauer committed
49
50
            raise ValueError("No loop found in pystencils AST")
        else:
51
52
53
            if len(inner_loops) > 1:
                warnings.warn("pystencils AST contains multiple inner loops. "
                              "Only one can be analyzed - choosing first one")
Martin Bauer's avatar
Martin Bauer committed
54
            inner_loop = inner_loops[0]
Martin Bauer's avatar
Martin Bauer committed
55
56

        self._loop_stack = []
Martin Bauer's avatar
Martin Bauer committed
57
58
59
        cur_node = inner_loop
        while cur_node is not None:
            if isinstance(cur_node, LoopOverCoordinate):
Martin Bauer's avatar
Martin Bauer committed
60
61
62
                loop_counter_sym = cur_node.loop_counter_symbol
                loop_info = (loop_counter_sym.name, cur_node.start, cur_node.stop, cur_node.step)
                self._loop_stack.append(loop_info)
Martin Bauer's avatar
Martin Bauer committed
63
            cur_node = cur_node.parent
Martin Bauer's avatar
Martin Bauer committed
64
65
66
        self._loop_stack = list(reversed(self._loop_stack))

        # Data sources & destinations
67
68
        self.sources = defaultdict(list)
        self.destinations = defaultdict(list)
Martin Bauer's avatar
Martin Bauer committed
69

Julian Hammer's avatar
Julian Hammer committed
70
71
72
73
74
75
76
77
78
        def get_layout_tuple(f):
            if f.has_fixed_shape:
                return get_layout_from_strides(f.strides)
            else:
                layout_list = list(f.layout)
                for _ in range(f.index_dimensions):
                    layout_list.insert(0 if assumed_layout == 'SoA' else -1, max(layout_list) + 1)
                return layout_list

Martin Bauer's avatar
Martin Bauer committed
79
        reads, writes = search_resolved_field_accesses_in_ast(inner_loop)
Martin Bauer's avatar
Martin Bauer committed
80
        for accesses, target_dict in [(reads, self.sources), (writes, self.destinations)]:
Martin Bauer's avatar
Martin Bauer committed
81
            for fa in accesses:
Martin Bauer's avatar
Martin Bauer committed
82
                coord = [sp.Symbol(LoopOverCoordinate.get_loop_counter_name(i), positive=True, integer=True) + off
Martin Bauer's avatar
Martin Bauer committed
83
                         for i, off in enumerate(fa.offsets)]
Martin Bauer's avatar
Martin Bauer committed
84
                coord += list(fa.idx_coordinate_values)
Julian Hammer's avatar
Julian Hammer committed
85
86
                layout = get_layout_tuple(fa.field)
                permuted_coord = [sp.sympify(coord[i]) for i in layout]
Martin Bauer's avatar
Martin Bauer committed
87
                target_dict[fa.field.name].append(permuted_coord)
Martin Bauer's avatar
Martin Bauer committed
88
89

        # Variables (arrays)
Martin Bauer's avatar
Martin Bauer committed
90
91
        fields_accessed = ast.fields_accessed
        for field in fields_accessed:
Julian Hammer's avatar
Julian Hammer committed
92
            layout = get_layout_tuple(field)
Martin Bauer's avatar
Martin Bauer committed
93
94
            permuted_shape = list(field.shape[i] for i in layout)
            self.set_variable(field.name, str(field.dtype), tuple(permuted_shape))
Martin Bauer's avatar
Martin Bauer committed
95

96
97
98
99
        for param in ast.get_parameters():
            if not param.is_field_parameter:
                self.set_variable(param.symbol.name, str(param.symbol.dtype), None)
                self.sources[param.symbol.name] = [None]
Martin Bauer's avatar
Martin Bauer committed
100
101
102
103
104

        # data type
        self.datatype = list(self.variables.values())[0][0]

        # flops
Martin Bauer's avatar
Martin Bauer committed
105
        operation_count = count_operations_in_ast(inner_loop)
Martin Bauer's avatar
Martin Bauer committed
106
        self._flops = {
Martin Bauer's avatar
Martin Bauer committed
107
108
109
            '+': operation_count['adds'],
            '*': operation_count['muls'],
            '/': operation_count['divs'],
Martin Bauer's avatar
Martin Bauer committed
110
        }
Jan Hönig's avatar
Jan Hönig committed
111
112
        for k in [k for k, v in self._flops.items() if v == 0]:
            del self._flops[k]
Martin Bauer's avatar
Martin Bauer committed
113
114
        self.check()

115
116
117
118
119
120
121
122
123
124
125
        if debug_print:
            from pprint import pprint
            print("-----------------------------  Loop Stack --------------------------")
            pprint(self._loop_stack)
            print("-----------------------------  Sources -----------------------------")
            pprint(self.sources)
            print("-----------------------------  Destinations ------------------------")
            pprint(self.destinations)
            print("-----------------------------  FLOPS -------------------------------")
            pprint(self._flops)

Jan Hönig's avatar
Jan Hönig committed
126
127
128
    def iaca_analysis(self, micro_architecture, asm_block='auto',
                      pointer_increment='auto_with_manual_fallback', verbose=False):
        compiler, compiler_args = self._machine.get_compiler()
Martin Bauer's avatar
Martin Bauer committed
129
130
        if '-std=c99' not in compiler_args:
            compiler_args += ['-std=c99']
Martin Bauer's avatar
Martin Bauer committed
131
        header_path = kerncraft.get_header_path()
Martin Bauer's avatar
Martin Bauer committed
132

Martin Bauer's avatar
Martin Bauer committed
133
        compiler_cmd = [compiler] + compiler_args + ['-I' + header_path]
Martin Bauer's avatar
Martin Bauer committed
134

Martin Bauer's avatar
Martin Bauer committed
135
136
137
        src_file = os.path.join(self.temporary_dir.name, "source.c")
        asm_file = os.path.join(self.temporary_dir.name, "source.s")
        iaca_asm_file = os.path.join(self.temporary_dir.name, "source.iaca.s")
Martin Bauer's avatar
Martin Bauer committed
138
        dummy_src_file = os.path.join(header_path, "dummy.c")
Martin Bauer's avatar
Martin Bauer committed
139
140
        dummy_asm_file = os.path.join(self.temporary_dir.name, "dummy.s")
        binary_file = os.path.join(self.temporary_dir.name, "binary")
Martin Bauer's avatar
Martin Bauer committed
141
142

        # write source code to file
Martin Bauer's avatar
Martin Bauer committed
143
144
        with open(src_file, 'w') as f:
            f.write(generate_benchmark(self.ast, likwid=False))
Martin Bauer's avatar
Martin Bauer committed
145
146

        # compile to asm files
Martin Bauer's avatar
Martin Bauer committed
147
        subprocess.check_output(compiler_cmd + [src_file, '-S', '-o', asm_file])
Martin Bauer's avatar
Martin Bauer committed
148
        subprocess.check_output(compiler_cmd + [dummy_src_file, '-S', '-o', dummy_asm_file])
Martin Bauer's avatar
Martin Bauer committed
149

Martin Bauer's avatar
Martin Bauer committed
150
151
        with open(asm_file) as read, open(iaca_asm_file, 'w') as write:
            instrumented_asm_block = iaca_instrumentation(read, write)
Martin Bauer's avatar
Martin Bauer committed
152
153

        # assemble asm files to executable
Martin Bauer's avatar
Martin Bauer committed
154
        subprocess.check_output(compiler_cmd + [iaca_asm_file, dummy_asm_file, '-o', binary_file])
Martin Bauer's avatar
Martin Bauer committed
155

Martin Bauer's avatar
Martin Bauer committed
156
        result = iaca_analyse_instrumented_binary(binary_file, micro_architecture)
Martin Bauer's avatar
Martin Bauer committed
157
    
Martin Bauer's avatar
Martin Bauer committed
158
        return result, instrumented_asm_block
Martin Bauer's avatar
Martin Bauer committed
159

Jan Hönig's avatar
Jan Hönig committed
160
161
    def build(self, lflags=None, verbose=False, openmp=False):
        # TODO do we use openmp or not???
Jan Hönig's avatar
Jan Hönig committed
162
        compiler, compiler_args = self._machine.get_compiler()
Martin Bauer's avatar
Martin Bauer committed
163
164
        if '-std=c99' not in compiler_args:
            compiler_args.append('-std=c99')
Martin Bauer's avatar
Martin Bauer committed
165
        header_path = kerncraft.get_header_path()
Martin Bauer's avatar
Martin Bauer committed
166
167
168
169

        cmd = [compiler] + compiler_args + [
            '-I' + os.path.join(self.LIKWID_BASE, 'include'),
            '-L' + os.path.join(self.LIKWID_BASE, 'lib'),
Martin Bauer's avatar
Martin Bauer committed
170
            '-I' + header_path,
Martin Bauer's avatar
Martin Bauer committed
171
172
173
            '-Wl,-rpath=' + os.path.join(self.LIKWID_BASE, 'lib'),
        ]

Martin Bauer's avatar
Martin Bauer committed
174
        dummy_src_file = os.path.join(header_path, 'dummy.c')
Martin Bauer's avatar
Martin Bauer committed
175
176
        src_file = os.path.join(self.temporary_dir.name, "source_likwid.c")
        bin_file = os.path.join(self.temporary_dir.name, "benchmark")
Martin Bauer's avatar
Martin Bauer committed
177

Martin Bauer's avatar
Martin Bauer committed
178
179
        with open(src_file, 'w') as f:
            f.write(generate_benchmark(self.ast, likwid=True))
Martin Bauer's avatar
Martin Bauer committed
180

Martin Bauer's avatar
Martin Bauer committed
181
182
        subprocess.check_output(cmd + [src_file, dummy_src_file, '-pthread', '-llikwid', '-o', bin_file])
        return bin_file
183

Martin Bauer's avatar
Martin Bauer committed
184
185

class KerncraftParameters(DotDict):
Martin Bauer's avatar
Martin Bauer committed
186
187
    def __init__(self, **kwargs):
        super(KerncraftParameters, self).__init__(**kwargs)
Martin Bauer's avatar
Martin Bauer committed
188
189
190
191
192
        self['asm_block'] = 'auto'
        self['asm_increment'] = 0
        self['cores'] = 1
        self['cache_predictor'] = 'SIM'
        self['verbose'] = 0
Jan Hönig's avatar
Jan Hönig committed
193
        self['pointer_increment'] = 'auto'
Jan Hönig's avatar
Jan Hönig committed
194
        self['iterations'] = 10
Julian Hammer's avatar
Julian Hammer committed
195
196
        self['unit'] = 'cy/CL'
        self['ignore_warnings'] = True
Jan Hönig's avatar
Jan Hönig committed
197

Martin Bauer's avatar
Martin Bauer committed
198
199
200
201

# ------------------------------------------- Helper functions ---------------------------------------------------------


Martin Bauer's avatar
Martin Bauer committed
202
def search_resolved_field_accesses_in_ast(ast):
Martin Bauer's avatar
Martin Bauer committed
203
204
205
206
207
208
209
210
211
    def visit(node, reads, writes):
        if not isinstance(node, SympyAssignment):
            for a in node.args:
                visit(a, reads, writes)
            return

        for expr, accesses in [(node.lhs, writes), (node.rhs, reads)]:
            accesses.update(expr.atoms(ResolvedFieldAccess))

Martin Bauer's avatar
Martin Bauer committed
212
213
214
    read_accesses = set()
    write_accesses = set()
    visit(ast, read_accesses, write_accesses)
Martin Bauer's avatar
Martin Bauer committed
215
    return read_accesses, write_accesses