From 7d9f3dfaa57b949d1276b4b3cf3e6a34cd5b36c5 Mon Sep 17 00:00:00 2001
From: Markus Holzer <markus.holzer@fau.de>
Date: Sun, 23 Oct 2022 10:07:20 +0200
Subject: [PATCH] Clean up

---
 pystencils/cpu/vectorization.py               |  4 +-
 .../test_vectorization_specific.py            | 48 ++++++++++++++-----
 2 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/pystencils/cpu/vectorization.py b/pystencils/cpu/vectorization.py
index 84ce6f24..420e57c7 100644
--- a/pystencils/cpu/vectorization.py
+++ b/pystencils/cpu/vectorization.py
@@ -134,7 +134,6 @@ def vectorize(kernel_ast: ast.KernelFunction, instruction_set: str = 'best',
     vectorize_inner_loops_and_adapt_load_stores(kernel_ast, assume_aligned, nontemporal,
                                                 strided, keep_loop_stop, assume_sufficient_line_padding,
                                                 default_float_type)
-    a = 1
 
 
 def vectorize_inner_loops_and_adapt_load_stores(ast_node, assume_aligned, nontemporal_fields,
@@ -299,6 +298,9 @@ def insert_vector_casts(ast_node, instruction_set, default_float_type='double'):
                     for a, t in zip(new_args, arg_types)]
                 return expr.func(*casted_args)
         elif expr.func is sp.UnevaluatedExpr:
+            assert expr.args[0].is_Pow or expr.args[0].is_Mul, "UnevaluatedExpr only implemented holding Mul or Pow"
+            # TODO this is only because cut_loop evaluates the multiplications again due to deepcopy. All this should
+            # TODO be fixed for real at some point.
             if expr.args[0].is_Pow:
                 base = expr.args[0].base
                 exp = expr.args[0].exp
diff --git a/pystencils_tests/test_vectorization_specific.py b/pystencils_tests/test_vectorization_specific.py
index 72366e59..c6a3bf22 100644
--- a/pystencils_tests/test_vectorization_specific.py
+++ b/pystencils_tests/test_vectorization_specific.py
@@ -170,12 +170,10 @@ def test_square_root(dtype, instruction_set, field_layout):
     print(code)
 
 
-# @pytest.mark.parametrize('dtype', ('float32', 'float64'))
+@pytest.mark.parametrize('dtype', ('float32', 'float64'))
 @pytest.mark.parametrize('instruction_set', supported_instruction_sets)
-# @pytest.mark.parametrize('padding', (True, False))
-def test_square_root_2(instruction_set):
-    dtype = 'float32'
-    padding = False
+@pytest.mark.parametrize('padding', (True, False))
+def test_square_root_2(dtype, instruction_set, padding):
     x, y = sp.symbols("x y")
     src = ps.fields(f"src: {dtype}[2D]", layout='fzyx')
 
@@ -247,15 +245,12 @@ def test_issue62(dtype, instruction_set, padding):
 
 @pytest.mark.parametrize('dtype', ('float32', 'float64'))
 @pytest.mark.parametrize('instruction_set', supported_instruction_sets)
-def test_div_and_unev(dtype, instruction_set):
+def test_div_and_unevaluated_expr(dtype, instruction_set):
     opt = {'instruction_set': instruction_set, 'assume_aligned': True, 'assume_inner_stride_one': True,
            'assume_sufficient_line_padding': False}
 
     x, y, z = sp.symbols("x y z")
-    # rhs = (-4 * x ** 4 * y ** 2 * z ** 2 + (x ** 4 * y ** 2 / 3) + (x ** 4 * z ** 2 / 3)) / x ** 3
-    # rhs = 1 / x ** 2 * (x + y)
-
-    rhs = 1 / x**2 * (x**2 + y**2)
+    rhs = (-4 * x ** 4 * y ** 2 * z ** 2 + (x ** 4 * y ** 2 / 3) + (x ** 4 * z ** 2 / 3)) / x ** 3
 
     src = ps.fields(f"src: {dtype}[2D]", layout='fzyx')
 
@@ -266,9 +261,38 @@ def test_div_and_unev(dtype, instruction_set):
                                    cpu_vectorize_info=opt)
 
     ast = ps.create_kernel(up, config=config)
-    ast.compile()
     code = ps.get_code_str(ast)
+    # print(code)
 
-    print(code)
+    ast.compile()
 
     assert 'pow' not in code
+
+
+# TODO this test case needs a complete rework of the vectoriser. The reason is that the vectoriser does not
+# TODO vectorise symbols at the moment because they could be strides or field sizes, thus involved in pointer arithmetic
+# TODO This means that the vectoriser only works if fields are involved on the rhs.
+# @pytest.mark.parametrize('dtype', ('float32', 'float64'))
+# @pytest.mark.parametrize('instruction_set', supported_instruction_sets)
+# def test_vectorised_symbols(dtype, instruction_set):
+#     opt = {'instruction_set': instruction_set, 'assume_aligned': True, 'assume_inner_stride_one': True,
+#            'assume_sufficient_line_padding': False}
+#
+#     x, y, z = sp.symbols("x y z")
+#     rhs = 1 / x ** 2 * (x + y)
+#
+#     src = ps.fields(f"src: {dtype}[2D]", layout='fzyx')
+#
+#     up = ps.Assignment(src[0, 0], rhs)
+#
+#     config = ps.CreateKernelConfig(data_type=dtype,
+#                                    default_number_float=dtype,
+#                                    cpu_vectorize_info=opt)
+#
+#     ast = ps.create_kernel(up, config=config)
+#     code = ps.get_code_str(ast)
+#     print(code)
+#
+#     ast.compile()
+#
+#     assert 'pow' not in code
-- 
GitLab