Skip to content
Snippets Groups Projects
Commit c8a7c75a authored by Frederik Hennig's avatar Frederik Hennig
Browse files

migrate test suite

parent 1edd2d5c
No related branches found
No related tags found
1 merge request!409Migrate Test Suite
Showing
with 10 additions and 213 deletions
import pytest
try:
import blitzdb
except ImportError:
pytest.skip("BlitzDB not available", allow_module_level=True)
import io
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
......
File moved
File moved
File moved
import pystencils
def test_assignment_collection_dict_conversion():
x, y = pystencils.fields('x,y: [2D]')
collection_normal = pystencils.AssignmentCollection(
[pystencils.Assignment(x.center(), y[1, 0] + y[0, 0])],
[]
)
collection_dict = pystencils.AssignmentCollection(
{x.center(): y[1, 0] + y[0, 0]},
{}
)
assert str(collection_normal) == str(collection_dict)
assert collection_dict.main_assignments_dict == {x.center(): y[1, 0] + y[0, 0]}
assert collection_dict.subexpressions_dict == {}
collection_normal = pystencils.AssignmentCollection(
[pystencils.Assignment(y[1, 0], x.center()),
pystencils.Assignment(y[0, 0], x.center())],
[]
)
collection_dict = pystencils.AssignmentCollection(
{y[1, 0]: x.center(),
y[0, 0]: x.center()},
{}
)
assert str(collection_normal) == str(collection_dict)
assert collection_dict.main_assignments_dict == {y[1, 0]: x.center(),
y[0, 0]: x.center()}
assert collection_dict.subexpressions_dict == {}
import numpy as np
import pystencils
from pystencils.sympyextensions.astnodes import assignment_from_stencil
def test_assignment_from_stencil():
stencil = [
[0, 0, 4, 1, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 0],
[0, 0, 0, 3, 0, 0, 0]
]
x, y = pystencils.fields('x, y: [2D]')
assignment = assignment_from_stencil(stencil, x, y)
assert isinstance(assignment, pystencils.Assignment)
assert assignment.rhs == x[0, 1] + 4 * x[-1, 1] + 2 * x[0, 0] + 3 * x[0, -1]
assignment = assignment_from_stencil(stencil, x, y, normalization_factor=1 / np.sum(stencil))
assert isinstance(assignment, pystencils.Assignment)
File deleted
import pytest
import sympy as sp
import numpy as np
import pystencils as ps
from pystencils.sympyextensions.fast_approximation import fast_division
@pytest.mark.parametrize('dtype', ["float64", "float32"])
@pytest.mark.parametrize('func', [sp.Pow, sp.atan2])
@pytest.mark.parametrize('target', [ps.Target.CPU, ps.Target.GPU])
def test_two_arguments(dtype, func, target):
if target == ps.Target.GPU:
pytest.importorskip("cupy")
dh = ps.create_data_handling(domain_size=(10, 10), periodicity=True, default_target=target)
x = dh.add_array('x', values_per_cell=1, dtype=dtype)
dh.fill("x", 0.0, ghost_layers=True)
y = dh.add_array('y', values_per_cell=1, dtype=dtype)
dh.fill("y", 1.0, ghost_layers=True)
z = dh.add_array('z', values_per_cell=1, dtype=dtype)
dh.fill("z", 2.0, ghost_layers=True)
config = ps.CreateKernelConfig(target=target)
# test sp.Max with one argument
up = ps.Assignment(x.center, func(y.center, z.center))
ast = ps.create_kernel(up, config=config)
code = ps.get_code_str(ast)
if dtype == 'float32':
assert func.__name__.lower() in code
kernel = ast.compile()
dh.all_to_gpu()
dh.run_kernel(kernel)
dh.all_to_cpu()
np.testing.assert_allclose(dh.gather_array("x")[0, 0], float(func(1.0, 2.0).evalf()),
13 if dtype == 'float64' else 5)
@pytest.mark.parametrize('dtype', ["float64", "float32"])
@pytest.mark.parametrize('func', [sp.sin, sp.cos, sp.sinh, sp.cosh, sp.atan])
@pytest.mark.parametrize('target', [ps.Target.CPU, ps.Target.GPU])
def test_single_arguments(dtype, func, target):
if target == ps.Target.GPU:
pytest.importorskip("cupy")
dh = ps.create_data_handling(domain_size=(10, 10), periodicity=True, default_target=target)
x = dh.add_array('x', values_per_cell=1, dtype=dtype)
dh.fill("x", 0.0, ghost_layers=True)
y = dh.add_array('y', values_per_cell=1, dtype=dtype)
dh.fill("y", 1.0, ghost_layers=True)
config = ps.CreateKernelConfig(target=target)
# test sp.Max with one argument
up = ps.Assignment(x.center, func(y.center))
ast = ps.create_kernel(up, config=config)
code = ps.get_code_str(ast)
if dtype == 'float32':
assert func.__name__.lower() in code
kernel = ast.compile()
dh.all_to_gpu()
dh.run_kernel(kernel)
dh.all_to_cpu()
np.testing.assert_allclose(dh.gather_array("x")[0, 0], float(func(1.0).evalf()),
rtol=10**-3 if dtype == 'float32' else 10**-5)
@pytest.mark.parametrize('a', [sp.Symbol('a'), ps.fields('a: float64[2d]').center])
def test_avoid_pow(a):
x = ps.fields('x: float64[2d]')
up = ps.Assignment(x.center_vector[0], 2 * a ** 2 / 3)
ast = ps.create_kernel(up)
code = ps.get_code_str(ast)
assert "pow" not in code
def test_avoid_pow_fast_div():
x = ps.fields('x: float64[2d]')
a = ps.fields('a: float64[2d]').center
up = ps.Assignment(x.center_vector[0], fast_division(1, (a**2)))
ast = ps.create_kernel(up, config=ps.CreateKernelConfig(target=ps.Target.GPU))
# ps.show_code(ast)
code = ps.get_code_str(ast)
assert "pow" not in code
def test_avoid_pow_move_constants():
# At the end of the kernel creation the function move_constants_before_loop will be called
# This function additionally contains substitutions for symbols with the same value
# Thus it simplifies the equations again
x = ps.fields('x: float64[2d]')
a, b, c = sp.symbols("a, b, c")
up = [ps.Assignment(a, 0.0),
ps.Assignment(b, 0.0),
ps.Assignment(c, 0.0),
ps.Assignment(x.center_vector[0], a**2/18 - a*b/6 - a/18 + b**2/18 + b/18 - c**2/36)]
ast = ps.create_kernel(up)
code = ps.get_code_str(ast)
ps.show_code(ast)
assert "pow" not in code
import numpy as np
import pytest
import pystencils as ps
from pystencils.cpu.vectorization import get_supported_instruction_sets
from pystencils.cpu.vectorization import replace_inner_stride_with_one, vectorize
def test_basic_kernel():
......@@ -33,6 +32,7 @@ def test_basic_kernel():
assert (dh.reduce_int_sequence(int_seq, op) == int_seq).all()
@pytest.mark.xfail(reason="Staggered kernels and blocking not implemented yet")
def test_basic_blocking_staggered():
f = ps.fields("f: double[2D]")
stag = ps.fields("stag(2): double[2D]", field_type=ps.FieldType.STAGGERED)
......@@ -52,6 +52,7 @@ def test_basic_blocking_staggered():
np.testing.assert_almost_equal(stag_arr, stag_ref)
@pytest.mark.xfail(reason="Vectorization not implemented yet")
def test_basic_vectorization():
supported_instruction_sets = get_supported_instruction_sets()
if supported_instruction_sets:
......
import numpy as np
from pystencils import Assignment, Field, create_kernel
def test_fixed_sized_field():
for order in ('f', 'c'):
for align in (True, False):
dt = np.dtype([('e1', np.float32), ('e2', np.double), ('e3', np.double)], align=align)
arr = np.zeros((3, 2), dtype=dt, order=order)
f = Field.create_from_numpy_array("f", arr)
d = Field.create_from_numpy_array("d", arr)
update_rules = [Assignment(d[0, 0]['e2'], f[0, 0]['e3'])]
result = arr.copy(order=order)
assert result.strides == arr.strides
arr['e2'] = 0
arr['e3'] = np.random.rand(3, 2)
kernel = create_kernel(update_rules).compile()
kernel(f=arr, d=result)
np.testing.assert_almost_equal(result['e2'], arr['e3'])
np.testing.assert_equal(arr['e2'], np.zeros((3, 2)))
def test_variable_sized_field():
for order in ('f', 'c'):
for align in (True, False):
dt = np.dtype([('e1', np.float32), ('e2', np.double), ('e3', np.double)], align=align)
f = Field.create_generic("f", 2, dt, layout=order)
d = Field.create_generic("d", 2, dt, layout=order)
update_rules = [Assignment(d[0, 0]['e2'], f[0, 0]['e3'])]
arr = np.zeros((3, 2), dtype=dt, order=order)
result = arr.copy(order=order)
arr['e2'] = 0
arr['e3'] = np.random.rand(3, 2)
kernel = create_kernel(update_rules).compile()
kernel(f=arr, d=result)
np.testing.assert_almost_equal(result['e2'], arr['e3'])
np.testing.assert_equal(arr['e2'], np.zeros((3, 2)))
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment