kerncraft_interface.py 7.88 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
9
10
import kerncraft.kernel
from kerncraft.machinemodel import MachineModel
from kerncraft.models import ECM, Benchmark
11
from kerncraft.iaca import iaca_analyse_instrumented_binary, iaca_instrumentation
12
from pystencils.kerncraft_coupling.generate_benchmark import generateBenchmark
Martin Bauer's avatar
Martin Bauer committed
13
from pystencils.astnodes import LoopOverCoordinate, SympyAssignment, ResolvedFieldAccess
Martin Bauer's avatar
Martin Bauer committed
14
15
from pystencils.field import getLayoutFromStrides
from pystencils.sympyextensions import countNumberOfOperationsInAst
Martin Bauer's avatar
Martin Bauer committed
16
17
18
19
20
21
22
23
from pystencils.utils import DotDict


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
24
25
    LIKWID_BASE = '/usr/local/likwid'

Jan Hönig's avatar
Jan Hönig committed
26
27
    def __init__(self, ast, machine=None):
        super(PyStencilsKerncraftKernel, self).__init__(machine)
Martin Bauer's avatar
Martin Bauer committed
28
29

        self.ast = ast
Martin Bauer's avatar
Martin Bauer committed
30
        self.temporaryDir = TemporaryDirectory()
Martin Bauer's avatar
Martin Bauer committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

        # Loops
        innerLoops = [l for l in ast.atoms(LoopOverCoordinate) if l.isInnermostLoop]
        if len(innerLoops) == 0:
            raise ValueError("No loop found in pystencils AST")
        elif len(innerLoops) > 1:
            raise ValueError("pystencils AST contains multiple inner loops - only one can be analyzed")
        else:
            innerLoop = innerLoops[0]

        self._loop_stack = []
        curNode = innerLoop
        while curNode is not None:
            if isinstance(curNode, LoopOverCoordinate):
                loopCounterSym = curNode.loopCounterSymbol
                loopInfo = (loopCounterSym.name, curNode.start, curNode.stop, curNode.step)
                self._loop_stack.append(loopInfo)
            curNode = curNode.parent
        self._loop_stack = list(reversed(self._loop_stack))

        # Data sources & destinations
52
53
        self.sources = defaultdict(list)
        self.destinations = defaultdict(list)
Martin Bauer's avatar
Martin Bauer committed
54
55

        reads, writes = searchResolvedFieldAccessesInAst(innerLoop)
56
        for accesses, targetDict in [(reads, self.sources), (writes, self.destinations)]:
Martin Bauer's avatar
Martin Bauer committed
57
            for fa in accesses:
58
                coord = [sp.Symbol(LoopOverCoordinate.getLoopCounterName(i), positive=True, integer=True) + off
Martin Bauer's avatar
Martin Bauer committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
                         for i, off in enumerate(fa.offsets)]
                coord += list(fa.idxCoordinateValues)
                layout = getLayoutFromStrides(fa.field.strides)
                permutedCoord = [coord[i] for i in layout]
                targetDict[fa.field.name].append(permutedCoord)

        # Variables (arrays)
        fieldsAccessed = ast.fieldsAccessed
        for field in fieldsAccessed:
            layout = getLayoutFromStrides(field.strides)
            permutedShape = list(field.shape[i] for i in layout)
            self.set_variable(field.name, str(field.dtype), permutedShape)

        for param in ast.parameters:
            if not param.isFieldArgument:
                self.set_variable(param.name, str(param.dtype), None)
75
                self.sources[param.name] = [None]
Martin Bauer's avatar
Martin Bauer committed
76
77
78
79
80
81
82
83
84
85
86

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

        # flops
        operationCount = countNumberOfOperationsInAst(innerLoop)
        self._flops = {
            '+': operationCount['adds'],
            '*': operationCount['muls'],
            '/': operationCount['divs'],
        }
Jan Hönig's avatar
Jan Hönig committed
87
88
        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
89
90
        self.check()

Jan Hönig's avatar
Jan Hönig committed
91
92
93
    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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        if '-std=c99' not in compiler_args:
            compiler_args += ['-std=c99']
        headerPath = kerncraft.get_header_path()
    
        compilerCmd = [compiler] + compiler_args + ['-I' + headerPath]
    
        srcFile = os.path.join(self.temporaryDir.name, "source.c")
        asmFile = os.path.join(self.temporaryDir.name, "source.s")
        dummySrcFile = os.path.join(headerPath, "dummy.c")
        dummyAsmFile = os.path.join(self.temporaryDir.name, "dummy.s")
        binaryFile = os.path.join(self.temporaryDir.name, "binary")

        # write source code to file
        with open(srcFile, 'w') as f:
            f.write(generateBenchmark(self.ast, likwid=False))

        # compile to asm files
        subprocess.check_output(compilerCmd + [srcFile,      '-S', '-o', asmFile])
        subprocess.check_output(compilerCmd + [dummySrcFile, '-S', '-o', dummyAsmFile])

114
        instrumentedAsmBlock = iaca_instrumentation(asmFile)
Martin Bauer's avatar
Martin Bauer committed
115
116
117
118
119
120
121
122

        # assemble asm files to executable
        subprocess.check_output(compilerCmd + [asmFile, dummyAsmFile, '-o', binaryFile])

        result = iaca_analyse_instrumented_binary(binaryFile, micro_architecture)
    
        return result, instrumentedAsmBlock

Jan Hönig's avatar
Jan Hönig committed
123
124
    def build(self, lflags=None, verbose=False):
        compiler, compiler_args = self._machine.get_compiler()
Martin Bauer's avatar
Martin Bauer committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        if '-std=c99' not in compiler_args:
            compiler_args.append('-std=c99')
        headerPath = kerncraft.get_header_path()

        cmd = [compiler] + compiler_args + [
            '-I' + os.path.join(self.LIKWID_BASE, 'include'),
            '-L' + os.path.join(self.LIKWID_BASE, 'lib'),
            '-I' + headerPath,
            '-Wl,-rpath=' + os.path.join(self.LIKWID_BASE, 'lib'),
        ]

        dummySrcFile = os.path.join(headerPath, 'dummy.c')
        srcFile = os.path.join(self.temporaryDir.name, "source_likwid.c")
        binFile = os.path.join(self.temporaryDir.name, "benchmark")

        with open(srcFile, 'w') as f:
            f.write(generateBenchmark(self.ast, likwid=True))

Jan Hönig's avatar
Jan Hönig committed
143
144
        print(generateBenchmark(self.ast, likwid=True))

Martin Bauer's avatar
Martin Bauer committed
145
146
        subprocess.check_output(cmd + [srcFile, dummySrcFile, '-pthread', '-llikwid', '-o', binFile])
        return binFile
147

Martin Bauer's avatar
Martin Bauer committed
148
149
150
151
152
153
154
155

class KerncraftParameters(DotDict):
    def __init__(self):
        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
156
        self['pointer_increment'] = 'auto'
Martin Bauer's avatar
Martin Bauer committed
157
158


Martin Bauer's avatar
Martin Bauer committed
159
160
161
162
163
164
165
class Analysis(object):
    def __init__(self, ast, kerncraftMachineModel, AnalysisClass, args):
        self.ast = ast

        if not isinstance(kerncraftMachineModel, MachineModel):
            kerncraftMachineModel = MachineModel(kerncraftMachineModel)

Jan Hönig's avatar
Jan Hönig committed
166
        self.analysis = AnalysisClass(PyStencilsKerncraftKernel(self.ast, kerncraftMachineModel),
Martin Bauer's avatar
Martin Bauer committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
                                      kerncraftMachineModel,
                                      args=args)
        self.analysis.analyze()

    @property
    def results(self):
        return self.analysis.results


class EcmAnalysis(Analysis):

    def __init__(self, ast, kerncraftMachineModel, cachePredictor='SIM'):
        args = KerncraftParameters()
        args['cache_predictor'] = cachePredictor
        super(EcmAnalysis, self).__init__(ast, kerncraftMachineModel, ECM, args)

    def _repr_html(self):
        pass


class BenchmarkAnalysis(Analysis):

    def __init__(self, ast, kerncraftMachineModel):
190
        super(BenchmarkAnalysis, self).__init__(ast, kerncraftMachineModel, Benchmark, KerncraftParameters())
Martin Bauer's avatar
Martin Bauer committed
191
192
193
194
195

    def _repr_html(self):
        pass


Martin Bauer's avatar
Martin Bauer committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# ------------------------------------------- Helper functions ---------------------------------------------------------


def searchResolvedFieldAccessesInAst(ast):
    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))

    readAccesses = set()
    writeAccesses = set()
    visit(ast, readAccesses, writeAccesses)
    return readAccesses, writeAccesses