diff --git a/pystencils_benchmark/benchmark.py b/pystencils_benchmark/benchmark.py index 34bdf6f8704cf4366f9a1e03ef5e4130ae17a302..28b52493a60e44d58e6548938b80d219485dbbfe 100644 --- a/pystencils_benchmark/benchmark.py +++ b/pystencils_benchmark/benchmark.py @@ -22,7 +22,8 @@ _env = Environment(loader=PackageLoader('pystencils_benchmark'), undefined=Stric def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]], path: Path = None, *, - compiler: Compiler = Compiler.GCC) -> None: + compiler: Compiler = Compiler.GCC, + likwid: bool = False) -> None: if path is None: path = Path('.') else: @@ -47,16 +48,17 @@ def generate_benchmark(kernel_asts: Union[KernelFunction, List[KernelFunction]], f.write(source) with open(src_path / 'main.c', 'w+') as f: - f.write(kernel_main(kernel_asts)) + f.write(kernel_main(kernel_asts, likwid=likwid)) copy_static_files(path) - compiler_toolchain(path, compiler) + compiler_toolchain(path, compiler, likwid) -def compiler_toolchain(path: Path, compiler: Compiler) -> None: +def compiler_toolchain(path: Path, compiler: Compiler, likwid: bool) -> None: name = compiler.name jinja_context = { 'compiler': name, + 'likwid': likwid, } files = ['Makefile', f'{name}.mk'] @@ -85,7 +87,8 @@ def copy_static_files(path: Path) -> None: f.write(template) -def kernel_main(kernels_ast: List[KernelFunction], timing: bool = True): +def kernel_main(kernels_ast: List[KernelFunction], *, + timing: bool = True, likwid: bool = False) -> str: """ Return C code of a benchmark program for the given kernel. @@ -145,6 +148,7 @@ def kernel_main(kernels_ast: List[KernelFunction], timing: bool = True): 'kernels': kernels, 'includes': includes, 'timing': timing, + 'likwid': likwid, } main = _env.get_template('main.c').render(**jinja_context) diff --git a/pystencils_benchmark/templates/Makefile b/pystencils_benchmark/templates/Makefile index 98fcaaa19d9a753fd346da9480fd4935d112f2e5..66b68b8d009137ffe0a3069d0f9e5e5a5f65d550 100644 --- a/pystencils_benchmark/templates/Makefile +++ b/pystencils_benchmark/templates/Makefile @@ -7,10 +7,25 @@ SRC_DIR = ./src MAKE_DIR = ./ Q ?= @ +{% if likwid %} +# LIKWID DEFINES +LIKWID_DEFINES := -DLIKWID_PERFMON +LIKWID_PATH = $(shell dirname $(shell which likwid-perfctr)) +LIKWID_LIB := -L$(LIKWID_PATH)/../lib/ +LIKWID_INC := -I$(LIKWID_PATH)/../include/ +{% endif %} + #DO NOT EDIT BELOW include $(MAKE_DIR)/$(TAG).mk INCLUDES += -I./include +{% if likwid %} +INCLUDES += $(LIKWID_INC) +DEFINES += $(LIKWID_DEFINES) +LFLAGS += $(LIKWID_LIB) +LIBS += -llikwid +{% endif %} + VPATH = $(SRC_DIR) ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s,$(wildcard $(SRC_DIR)/*.c)) OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o,$(wildcard $(SRC_DIR)/*.c)) diff --git a/pystencils_benchmark/templates/main.c b/pystencils_benchmark/templates/main.c index a4c462d598ba4c88ad0f63c0afb66d1962c07b7f..26fa4a623d0da65d87fd4c6c250261c37b69e400 100644 --- a/pystencils_benchmark/templates/main.c +++ b/pystencils_benchmark/templates/main.c @@ -6,6 +6,9 @@ #include <stdlib.h> #include "timing.h" +{% if likwid %} +#include <likwid-marker.h> +{% endif %} //kernels {% for include in includes %} @@ -22,6 +25,10 @@ int main(int argc, char **argv) return -1; } int n_repeat = atoi(argv[1]); + {% if likwid %} + LIKWID_MARKER_INIT; + {%- endif %} + {% for kernel in kernels %} { // Kernel: {{kernel.name}} @@ -42,10 +49,17 @@ int main(int argc, char **argv) {{constantName}} = 0.23; {% endfor %} + {% if likwid %} + LIKWID_MARKER_REGISTER("{{kernel.name}}"); + {% endif %} + for(int warmup = 1; warmup >= 0; --warmup) { int repeat = 2; if(warmup == 0) { repeat = n_repeat; + {% if likwid %} + LIKWID_MARKER_START("{{kernel.name}}"); + {% endif %} } {% if timing %} @@ -65,9 +79,18 @@ int main(int argc, char **argv) printf("%s\t%e\n", "{{kernel.name}}",(wcEndTime - wcStartTime) / n_repeat ); {% endif %} } + {% for field_name, dataType, elements, size, offset, alignment in kernel.fields %} free({{field_name}}); {% endfor %} + + {% if likwid %} + LIKWID_MARKER_STOP("{{kernel.name}}"); + {% endif %} } {% endfor %} + + {% if likwid %} + LIKWID_MARKER_CLOSE; + {% endif %} }