generate_benchmark.py 2.55 KB
Newer Older
1
from jinja2 import Template
Martin Bauer's avatar
Martin Bauer committed
2
from pystencils.backends.cbackend import generate_c
3
from pystencils.sympyextensions import prod
Martin Bauer's avatar
Martin Bauer committed
4
from pystencils.data_types import get_base_type
5

Martin Bauer's avatar
Martin Bauer committed
6
benchmark_template = Template("""
7
8
9
#include "kerncraft.h"
#include <stdlib.h>
#include <stdint.h>
Martin Bauer's avatar
Martin Bauer committed
10
#include <stdbool.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <math.h>
{%- if likwid %}
#include <likwid.h>
{%- endif %}

#define RESTRICT __restrict__
#define FUNC_PREFIX
void dummy(double *);
extern int var_false;


{{kernelCode}}


int main(int argc, char **argv)
{
  {%- if likwid %}
  likwid_markerInit();
  likwid_markerThreadInit();
  {%- endif %}

Martin Bauer's avatar
Martin Bauer committed
32
  {%- for field_name, dataType, size in fields %}
Martin Bauer's avatar
Martin Bauer committed
33
34

  // Initialization {{field_name}}
Martin Bauer's avatar
Martin Bauer committed
35
  double * {{field_name}} = aligned_malloc(sizeof({{dataType}}) * {{size}}, 32);
36
  for (int i = 0; i < {{size}}; ++i)
Martin Bauer's avatar
Martin Bauer committed
37
    {{field_name}}[i] = 0.23;
Martin Bauer's avatar
Martin Bauer committed
38

39
  if(var_false)
Martin Bauer's avatar
Martin Bauer committed
40
41
    dummy({{field_name}});

42
  {%- endfor %}
Martin Bauer's avatar
Martin Bauer committed
43
44
45



46
  {%- for constantName, dataType in constants %}
Martin Bauer's avatar
Martin Bauer committed
47

48
49
50
51
52
  // Constant {{constantName}}
  {{dataType}} {{constantName}};
  {{constantName}} = 0.23;
  if(var_false)
      dummy(& {{constantName}});
Martin Bauer's avatar
Martin Bauer committed
53

54
  {%- endfor %}
Martin Bauer's avatar
Martin Bauer committed
55

56
  int repeat = atoi(argv[1]);
Martin Bauer's avatar
Martin Bauer committed
57
  {%- if likwid %}
58
59
  likwid_markerStartRegion("loop");
  {%- endif %}
Martin Bauer's avatar
Martin Bauer committed
60

61
62
  for (; repeat > 0; --repeat)
  {
Jan Hönig's avatar
Jan Hönig committed
63
    {{kernelName}}({{callArgumentList}});
Martin Bauer's avatar
Martin Bauer committed
64
65

    // Dummy calls
Martin Bauer's avatar
Martin Bauer committed
66
    {%- for field_name, dataType, size in fields %}
Martin Bauer's avatar
Martin Bauer committed
67
    if(var_false) dummy({{field_name}});
68
69
70
71
72
    {%- endfor %}
    {%- for constantName, dataType in constants %}
    if(var_false) dummy(&{{constantName}});
    {%- endfor %}
  }
Martin Bauer's avatar
Martin Bauer committed
73
74

  {%- if likwid %}
75
76
  likwid_markerStopRegion("loop");
  {%- endif %}
Martin Bauer's avatar
Martin Bauer committed
77
78

  {%- if likwid %}
79
80
81
82
83
84
  likwid_markerClose();
  {%- endif %}
}
""")


Martin Bauer's avatar
Martin Bauer committed
85
86
def generate_benchmark(ast, likwid=False):
    accessed_fields = {f.name: f for f in ast.fields_accessed}
87
88
    constants = []
    fields = []
Martin Bauer's avatar
Martin Bauer committed
89
    call_parameters = []
90
    for p in ast.parameters:
Martin Bauer's avatar
Martin Bauer committed
91
        if not p.is_field_argument:
92
            constants.append((p.name, str(p.dtype)))
Martin Bauer's avatar
Martin Bauer committed
93
            call_parameters.append(p.name)
94
        else:
Martin Bauer's avatar
Martin Bauer committed
95
            assert p.is_field_ptr_argument, "Benchmark implemented only for kernels with fixed loop size"
Martin Bauer's avatar
Martin Bauer committed
96
            field = accessed_fields[p.field_name]
Martin Bauer's avatar
Martin Bauer committed
97
            dtype = str(get_base_type(p.dtype))
Martin Bauer's avatar
Martin Bauer committed
98
99
            fields.append((p.field_name, dtype, prod(field.shape)))
            call_parameters.append(p.field_name)
100
101
102

    args = {
        'likwid': likwid,
Martin Bauer's avatar
Martin Bauer committed
103
104
        'kernelCode': generate_c(ast),
        'kernelName': ast.function_name,
105
106
        'fields': fields,
        'constants': constants,
Martin Bauer's avatar
Martin Bauer committed
107
        'callArgumentList': ",".join(call_parameters),
108
    }
Martin Bauer's avatar
Martin Bauer committed
109
    return benchmark_template.render(**args)