From 0391c91d1b2d66689228a2a2c48ebc65a4d15d01 Mon Sep 17 00:00:00 2001
From: markus <markus.holzer@fau.de>
Date: Tue, 14 Jul 2020 16:26:11 +0200
Subject: [PATCH] Replaced os.path with pathlib in kerncraft interface

---
 .../kerncraft_coupling/generate_benchmark.py  | 23 +++++++++++--------
 pystencils_tests/test_kerncraft_coupling.py   | 22 +++++++++---------
 2 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/pystencils/kerncraft_coupling/generate_benchmark.py b/pystencils/kerncraft_coupling/generate_benchmark.py
index 2c618219e..9a012d6c2 100644
--- a/pystencils/kerncraft_coupling/generate_benchmark.py
+++ b/pystencils/kerncraft_coupling/generate_benchmark.py
@@ -1,7 +1,7 @@
-import os
 import subprocess
 import warnings
 import tempfile
+from pathlib import Path
 
 from jinja2 import Environment, PackageLoader, StrictUndefined
 
@@ -85,25 +85,28 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None):
     if path is None:
         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)
 
-    kerncraft_path = os.path.dirname(kerncraft.__file__)
+    kerncraft_path = Path(kerncraft.__file__).parent
 
     extra_flags = ['-I' + get_pystencils_include_path(),
-                   '-I' + os.path.join(kerncraft_path, 'headers')]
+                   '-I' + str(kerncraft_path / 'headers')]
 
     compiler_config = get_compiler_config()
     compile_cmd = [compiler_config['command']] + compiler_config['flags'].split()
     compile_cmd += [*extra_flags,
-                    os.path.join(kerncraft_path, 'headers', 'timing.c'),
-                    os.path.join(kerncraft_path, 'headers', 'dummy.c'),
-                    os.path.join(path, 'bench.c'),
-                    '-o', os.path.join(path, 'bench'),
+                    kerncraft_path / 'headers' / 'timing.c',
+                    kerncraft_path / 'headers' / 'dummy.c',
+                    path / 'bench.c',
+                    '-o', path / 'bench',
                     ]
     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
     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 "
@@ -112,6 +115,6 @@ def run_c_benchmark(ast, inner_iterations, outer_iterations=3, path=None):
 
     results = []
     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)
     return results
diff --git a/pystencils_tests/test_kerncraft_coupling.py b/pystencils_tests/test_kerncraft_coupling.py
index 25efc4210..653ed34d9 100644
--- a/pystencils_tests/test_kerncraft_coupling.py
+++ b/pystencils_tests/test_kerncraft_coupling.py
@@ -1,7 +1,7 @@
-import os
 import numpy as np
 import pytest
 import sympy as sp
+from pathlib import Path
 
 from kerncraft.kernel import KernelCode
 from kerncraft.machinemodel import MachineModel
@@ -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.timeloop import TimeLoop
 
-SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__))
-INPUT_FOLDER = os.path.join(SCRIPT_FOLDER, "kerncraft_inputs")
+SCRIPT_FOLDER = Path(__file__).parent
+INPUT_FOLDER = SCRIPT_FOLDER / "kerncraft_inputs"
 
 
 @pytest.mark.kerncraft
 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)
 
-    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:
         reference_kernel = KernelCode(kernel_file.read(), machine=machine, filename=kernel_file_path)
         reference_kernel.get_kernel_header(name='test_kernel')
@@ -43,7 +43,7 @@ def test_compilation():
 
 @pytest.mark.kerncraft
 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)
     if model == 'ecmdata':
         model = ECMData(kernel, machine, KerncraftParameters())
@@ -63,8 +63,8 @@ def analysis(kernel, model='ecmdata'):
 def test_3d_7pt_osaca():
 
     size = [20, 200, 200]
-    kernel_file_path = os.path.join(INPUT_FOLDER, "3d-7pt.c")
-    machine_file_path = os.path.join(INPUT_FOLDER, "Example_SandyBridgeEP_E5-2680.yml")
+    kernel_file_path = INPUT_FOLDER / "3d-7pt.c"
+    machine_file_path = INPUT_FOLDER / "Example_SandyBridgeEP_E5-2680.yml"
     machine_model = MachineModel(path_to_yaml=machine_file_path)
     with open(kernel_file_path) as kernel_file:
         reference_kernel = KernelCode(kernel_file.read(), machine=machine_model, filename=kernel_file_path)
@@ -90,7 +90,7 @@ def test_3d_7pt_osaca():
 @pytest.mark.kerncraft
 def test_2d_5pt():
     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:
         reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
     reference = analysis(reference_kernel)
@@ -112,7 +112,7 @@ def test_2d_5pt():
 @pytest.mark.kerncraft
 def test_3d_7pt():
     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:
         reference_kernel = KernelCode(kernel_file.read(), machine=None, filename=kernel_file_path)
     reference_kernel.set_constant('M', size[0])
@@ -158,4 +158,4 @@ def test_benchmark():
 
     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)
-- 
GitLab