Skip to content
Snippets Groups Projects
Commit 0391c91d authored by Markus Holzer's avatar Markus Holzer
Browse files

Replaced os.path with pathlib in kerncraft interface

parent 15dcdab3
No related branches found
No related tags found
No related merge requests found
import os
import subprocess import subprocess
import warnings import warnings
import tempfile import tempfile
from pathlib import Path
from jinja2 import Environment, PackageLoader, StrictUndefined from jinja2 import Environment, PackageLoader, StrictUndefined
...@@ -85,25 +85,28 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None): ...@@ -85,25 +85,28 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None):
if path is None: if path is None:
path = tempfile.mkdtemp() path = tempfile.mkdtemp()
with open(os.path.join(path, 'bench.c'), 'w') as f: if isinstance(path, str):
path = Path(path)
with open(path / 'bench.c', 'w') as f:
f.write(benchmark_code) f.write(benchmark_code)
kerncraft_path = os.path.dirname(kerncraft.__file__) kerncraft_path = Path(kerncraft.__file__).parent
extra_flags = ['-I' + get_pystencils_include_path(), extra_flags = ['-I' + get_pystencils_include_path(),
'-I' + os.path.join(kerncraft_path, 'headers')] '-I' + str(kerncraft_path / 'headers')]
compiler_config = get_compiler_config() compiler_config = get_compiler_config()
compile_cmd = [compiler_config['command']] + compiler_config['flags'].split() compile_cmd = [compiler_config['command']] + compiler_config['flags'].split()
compile_cmd += [*extra_flags, compile_cmd += [*extra_flags,
os.path.join(kerncraft_path, 'headers', 'timing.c'), kerncraft_path / 'headers' / 'timing.c',
os.path.join(kerncraft_path, 'headers', 'dummy.c'), kerncraft_path / 'headers' / 'dummy.c',
os.path.join(path, 'bench.c'), path / 'bench.c',
'-o', os.path.join(path, 'bench'), '-o', path / 'bench',
] ]
run_compile_step(compile_cmd) run_compile_step(compile_cmd)
time_pre_estimation_per_iteration = float(subprocess.check_output([os.path.join('./', path, 'bench'), str(10)])) time_pre_estimation_per_iteration = float(subprocess.check_output(['./' / path / 'bench', str(10)]))
benchmark_time_limit = 20 benchmark_time_limit = 20
if benchmark_time_limit / time_pre_estimation_per_iteration < inner_iterations: if benchmark_time_limit / time_pre_estimation_per_iteration < inner_iterations:
warn = (f"A benchmark run with {inner_iterations} inner_iterations will probably take longer than " warn = (f"A benchmark run with {inner_iterations} inner_iterations will probably take longer than "
...@@ -112,6 +115,6 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None): ...@@ -112,6 +115,6 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None):
results = [] results = []
for _ in range(outer_iterations): for _ in range(outer_iterations):
benchmark_time = float(subprocess.check_output([os.path.join('./', path, 'bench'), str(inner_iterations)])) benchmark_time = float(subprocess.check_output(['./' / path / 'bench', str(inner_iterations)]))
results.append(benchmark_time) results.append(benchmark_time)
return results return results
import os
import numpy as np import numpy as np
import pytest import pytest
import sympy as sp import sympy as sp
from pathlib import Path
from kerncraft.kernel import KernelCode from kerncraft.kernel import KernelCode
from kerncraft.machinemodel import MachineModel from kerncraft.machinemodel import MachineModel
...@@ -13,16 +13,16 @@ from pystencils.kerncraft_coupling import KerncraftParameters, PyStencilsKerncra ...@@ -13,16 +13,16 @@ from pystencils.kerncraft_coupling import KerncraftParameters, PyStencilsKerncra
from pystencils.kerncraft_coupling.generate_benchmark import generate_benchmark, run_c_benchmark from pystencils.kerncraft_coupling.generate_benchmark import generate_benchmark, run_c_benchmark
from pystencils.timeloop import TimeLoop from pystencils.timeloop import TimeLoop
SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__)) SCRIPT_FOLDER = Path(__file__).parent
INPUT_FOLDER = os.path.join(SCRIPT_FOLDER, "kerncraft_inputs") INPUT_FOLDER = SCRIPT_FOLDER / "kerncraft_inputs"
@pytest.mark.kerncraft @pytest.mark.kerncraft
def test_compilation(): def test_compilation():
machine_file_path = os.path.join(INPUT_FOLDER, "Example_SandyBridgeEP_E5-2680.yml") machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
machine = MachineModel(path_to_yaml=machine_file_path) machine = MachineModel(path_to_yaml=machine_file_path)
kernel_file_path = os.path.join(INPUT_FOLDER, "2d-5pt.c") kernel_file_path = INPUT_FOLDER / "2d-5pt.c"
with open(kernel_file_path) as kernel_file: with open(kernel_file_path) as kernel_file:
reference_kernel = KernelCode(kernel_file.read(), machine=machine, filename=kernel_file_path) reference_kernel = KernelCode(kernel_file.read(), machine=machine, filename=kernel_file_path)
reference_kernel.get_kernel_header(name='test_kernel') reference_kernel.get_kernel_header(name='test_kernel')
...@@ -43,7 +43,7 @@ def test_compilation(): ...@@ -43,7 +43,7 @@ def test_compilation():
@pytest.mark.kerncraft @pytest.mark.kerncraft
def analysis(kernel, model='ecmdata'): def analysis(kernel, model='ecmdata'):
machine_file_path = os.path.join(INPUT_FOLDER, "Example_SandyBridgeEP_E5-2680.yml") machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
machine = MachineModel(path_to_yaml=machine_file_path) machine = MachineModel(path_to_yaml=machine_file_path)
if model == 'ecmdata': if model == 'ecmdata':
model = ECMData(kernel, machine, KerncraftParameters()) model = ECMData(kernel, machine, KerncraftParameters())
...@@ -63,8 +63,8 @@ def analysis(kernel, model='ecmdata'): ...@@ -63,8 +63,8 @@ def analysis(kernel, model='ecmdata'):
def test_3d_7pt_osaca(): def test_3d_7pt_osaca():
size = [20, 200, 200] size = [20, 200, 200]
kernel_file_path = os.path.join(INPUT_FOLDER, "3d-7pt.c") kernel_file_path = INPUT_FOLDER / "3d-7pt.c"
machine_file_path = os.path.join(INPUT_FOLDER, "Example_SandyBridgeEP_E5-2680.yml") machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
machine_model = MachineModel(path_to_yaml=machine_file_path) machine_model = MachineModel(path_to_yaml=machine_file_path)
with open(kernel_file_path) as kernel_file: with open(kernel_file_path) as kernel_file:
reference_kernel = KernelCode(kernel_file.read(), machine=machine_model, filename=kernel_file_path) reference_kernel = KernelCode(kernel_file.read(), machine=machine_model, filename=kernel_file_path)
...@@ -90,7 +90,7 @@ def test_3d_7pt_osaca(): ...@@ -90,7 +90,7 @@ def test_3d_7pt_osaca():
@pytest.mark.kerncraft @pytest.mark.kerncraft
def test_2d_5pt(): def test_2d_5pt():
size = [30, 50, 3] size = [30, 50, 3]
kernel_file_path = os.path.join(INPUT_FOLDER, "2d-5pt.c") kernel_file_path = INPUT_FOLDER / "2d-5pt.c"
with open(kernel_file_path) as kernel_file: with open(kernel_file_path) as kernel_file:
reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path) reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
reference = analysis(reference_kernel) reference = analysis(reference_kernel)
...@@ -112,7 +112,7 @@ def test_2d_5pt(): ...@@ -112,7 +112,7 @@ def test_2d_5pt():
@pytest.mark.kerncraft @pytest.mark.kerncraft
def test_3d_7pt(): def test_3d_7pt():
size = [30, 50, 50] size = [30, 50, 50]
kernel_file_path = os.path.join(INPUT_FOLDER, "3d-7pt.c") kernel_file_path = INPUT_FOLDER / "3d-7pt.c"
with open(kernel_file_path) as kernel_file: with open(kernel_file_path) as kernel_file:
reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path) reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
reference_kernel.set_constant('M', size[0]) reference_kernel.set_constant('M', size[0])
...@@ -158,4 +158,4 @@ def test_benchmark(): ...@@ -158,4 +158,4 @@ def test_benchmark():
timeloop_time = timeloop.benchmark(number_of_time_steps_for_estimation=1) timeloop_time = timeloop.benchmark(number_of_time_steps_for_estimation=1)
np.testing.assert_almost_equal(c_benchmark_run, timeloop_time, decimal=5) np.testing.assert_almost_equal(c_benchmark_run, timeloop_time, decimal=4)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment