test_kerncraft_coupling.py 6.3 KB
Newer Older
1
2
import numpy as np
import pytest
Martin Bauer's avatar
Martin Bauer committed
3
import sympy as sp
4
from pathlib import Path
5

6
7
from kerncraft.kernel import KernelCode
from kerncraft.machinemodel import MachineModel
8
from kerncraft.models import ECM, ECMData, Benchmark
9

Markus Holzer's avatar
Markus Holzer committed
10
from pystencils import Assignment, Field, fields
Martin Bauer's avatar
Martin Bauer committed
11
12
from pystencils.cpu import create_kernel
from pystencils.kerncraft_coupling import KerncraftParameters, PyStencilsKerncraftKernel
13
14
from pystencils.kerncraft_coupling.generate_benchmark import generate_benchmark, run_c_benchmark
from pystencils.timeloop import TimeLoop
15

16
17
SCRIPT_FOLDER = Path(__file__).parent
INPUT_FOLDER = SCRIPT_FOLDER / "kerncraft_inputs"
18
19


20
@pytest.mark.kerncraft
21
def test_compilation():
22
    machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
23
    machine = MachineModel(path_to_yaml=machine_file_path)
24

25
    kernel_file_path = INPUT_FOLDER / "2d-5pt.c"
26
    with open(kernel_file_path) as kernel_file:
27
        reference_kernel = KernelCode(kernel_file.read(), machine=machine, filename=kernel_file_path)
28
29
30
        reference_kernel.get_kernel_header(name='test_kernel')
        reference_kernel.get_kernel_code(name='test_kernel')
        reference_kernel.get_main_code(kernel_function_name='test_kernel')
31
32
33
34
35
36
37
38
39
40
41
42
43

    size = [30, 50, 3]
    arr = np.zeros(size)
    a = Field.create_from_numpy_array('a', arr, index_dimensions=1)
    b = Field.create_from_numpy_array('b', arr, index_dimensions=1)
    s = sp.Symbol("s")
    rhs = a[0, -1](0) + a[0, 1] + a[-1, 0] + a[1, 0]
    update_rule = Assignment(b[0, 0], s * rhs)
    ast = create_kernel([update_rule])
    mine = generate_benchmark(ast, likwid=False)
    print(mine)


44
@pytest.mark.kerncraft
45
def analysis(kernel, model='ecmdata'):
46
    machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
47
    machine = MachineModel(path_to_yaml=machine_file_path)
48
    if model == 'ecmdata':
49
        model = ECMData(kernel, machine, KerncraftParameters())
50
    elif model == 'ecm':
51
        model = ECM(kernel, machine, KerncraftParameters())
52
53
54
        # model.analyze()
        # model.plot()
    elif model == 'benchmark':
55
        model = Benchmark(kernel, machine, KerncraftParameters())
56
    else:
57
        model = ECM(kernel, machine, KerncraftParameters())
58
59
60
61
    model.analyze()
    return model


62
@pytest.mark.kerncraft
63
64
def test_3d_7pt_osaca():

65
    size = [20, 200, 200]
66
67
    kernel_file_path = INPUT_FOLDER / "3d-7pt.c"
    machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
68
    machine_model = MachineModel(path_to_yaml=machine_file_path)
69
    with open(kernel_file_path) as kernel_file:
70
        reference_kernel = KernelCode(kernel_file.read(), machine=machine_model, filename=kernel_file_path)
71
72
73
74
75
76
77
78
79
80
81
82
83
    reference_kernel.set_constant('M', size[0])
    reference_kernel.set_constant('N', size[1])
    assert size[1] == size[2]
    analysis(reference_kernel, model='ecm')

    arr = np.zeros(size)
    a = Field.create_from_numpy_array('a', arr, index_dimensions=0)
    b = Field.create_from_numpy_array('b', arr, index_dimensions=0)
    s = sp.Symbol("s")
    rhs = a[0, -1, 0] + a[0, 1, 0] + a[-1, 0, 0] + a[1, 0, 0] + a[0, 0, -1] + a[0, 0, 1]

    update_rule = Assignment(b[0, 0, 0], s * rhs)
    ast = create_kernel([update_rule])
84
    k = PyStencilsKerncraftKernel(ast, machine=machine_model)
85
86
87
88
89
    analysis(k, model='ecm')
    assert reference_kernel._flops == k._flops
    # assert reference.results['cl throughput'] == analysis.results['cl throughput']


90
@pytest.mark.kerncraft
91
92
def test_2d_5pt():
    size = [30, 50, 3]
93
    kernel_file_path = INPUT_FOLDER / "2d-5pt.c"
94
    with open(kernel_file_path) as kernel_file:
95
        reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    reference = analysis(reference_kernel)

    arr = np.zeros(size)
    a = Field.create_from_numpy_array('a', arr, index_dimensions=1)
    b = Field.create_from_numpy_array('b', arr, index_dimensions=1)
    s = sp.Symbol("s")
    rhs = a[0, -1](0) + a[0, 1] + a[-1, 0] + a[1, 0]
    update_rule = Assignment(b[0, 0], s * rhs)
    ast = create_kernel([update_rule])
    k = PyStencilsKerncraftKernel(ast)
    result = analysis(k)

    for e1, e2 in zip(reference.results['cycles'], result.results['cycles']):
        assert e1 == e2


112
@pytest.mark.kerncraft
113
114
def test_3d_7pt():
    size = [30, 50, 50]
115
    kernel_file_path = INPUT_FOLDER / "3d-7pt.c"
116
    with open(kernel_file_path) as kernel_file:
117
        reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    reference_kernel.set_constant('M', size[0])
    reference_kernel.set_constant('N', size[1])
    assert size[1] == size[2]
    reference = analysis(reference_kernel)

    arr = np.zeros(size)
    a = Field.create_from_numpy_array('a', arr, index_dimensions=0)
    b = Field.create_from_numpy_array('b', arr, index_dimensions=0)
    s = sp.Symbol("s")
    rhs = a[0, -1, 0] + a[0, 1, 0] + a[-1, 0, 0] + a[1, 0, 0] + a[0, 0, -1] + a[0, 0, 1]

    update_rule = Assignment(b[0, 0, 0], s * rhs)
    ast = create_kernel([update_rule])
    k = PyStencilsKerncraftKernel(ast)
    result = analysis(k)

    for e1, e2 in zip(reference.results['cycles'], result.results['cycles']):
        assert e1 == e2
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160


@pytest.mark.kerncraft
def test_benchmark():
    size = [30, 50, 50]
    arr = np.zeros(size)
    a = Field.create_from_numpy_array('a', arr, index_dimensions=0)
    b = Field.create_from_numpy_array('b', arr, index_dimensions=0)
    s = sp.Symbol("s")
    rhs = a[0, -1, 0] + a[0, 1, 0] + a[-1, 0, 0] + a[1, 0, 0] + a[0, 0, -1] + a[0, 0, 1]

    update_rule = Assignment(b[0, 0, 0], s * rhs)
    ast = create_kernel([update_rule])

    c_benchmark_run = run_c_benchmark(ast, inner_iterations=1000, outer_iterations=1)

    kernel = ast.compile()
    a = np.full(size, fill_value=0.23)
    b = np.full(size, fill_value=0.23)

    timeloop = TimeLoop(steps=1)
    timeloop.add_call(kernel, {'a': a, 'b': b, 's': 0.23})

    timeloop_time = timeloop.benchmark(number_of_time_steps_for_estimation=1)

161
    np.testing.assert_almost_equal(c_benchmark_run, timeloop_time, decimal=4)
Markus Holzer's avatar
Markus Holzer committed
162
163
164
165
166
167
168
169
170
171
172
173


@pytest.mark.kerncraft
def test_kerncraft_generic_field():
    a = fields('a: double[3D]')
    b = fields('b: double[3D]')
    s = sp.Symbol("s")
    rhs = a[0, -1, 0] + a[0, 1, 0] + a[-1, 0, 0] + a[1, 0, 0] + a[0, 0, -1] + a[0, 0, 1]

    update_rule = Assignment(b[0, 0, 0], s * rhs)
    ast = create_kernel([update_rule])
    k = PyStencilsKerncraftKernel(ast, debug_print=True)