Skip to content
Snippets Groups Projects
Commit f81d83b7 authored by Michael Kuron's avatar Michael Kuron :mortar_board:
Browse files

Merge branch 'master' of i10git.cs.fau.de:pycodegen/pystencils into oldsympy

parents 16d731b5 46b7b88d
No related branches found
No related tags found
1 merge request!135CI: Replace minimal-ubuntu job with ubuntu
Pipeline #21338 failed
Showing
with 200 additions and 142 deletions
import os import os
import runpy import runpy
import sys import sys
import warnings
import tempfile import tempfile
import warnings
import nbformat import nbformat
import pytest import pytest
...@@ -34,7 +34,7 @@ def add_path_to_ignore(path): ...@@ -34,7 +34,7 @@ def add_path_to_ignore(path):
collect_ignore = [os.path.join(SCRIPT_FOLDER, "doc", "conf.py"), collect_ignore = [os.path.join(SCRIPT_FOLDER, "doc", "conf.py"),
os.path.join(SCRIPT_FOLDER, "pystencils", "opencl", "opencl.autoinit")] os.path.join(SCRIPT_FOLDER, "pystencils", "opencl", "opencl.autoinit")]
add_path_to_ignore('pystencils_tests/benchmark') add_path_to_ignore('pystencils_tests/benchmark')
add_path_to_ignore('_local_tmp') add_path_to_ignore('_local_tmp')
...@@ -98,8 +98,6 @@ for root, sub_dirs, files in os.walk('.'): ...@@ -98,8 +98,6 @@ for root, sub_dirs, files in os.walk('.'):
collect_ignore.append(f) collect_ignore.append(f)
class IPythonMockup: class IPythonMockup:
def run_line_magic(self, *args, **kwargs): def run_line_magic(self, *args, **kwargs):
pass pass
......
...@@ -5,7 +5,7 @@ from . import stencil as stencil ...@@ -5,7 +5,7 @@ from . import stencil as stencil
from .assignment import Assignment, assignment_from_stencil from .assignment import Assignment, assignment_from_stencil
from .data_types import TypedSymbol from .data_types import TypedSymbol
from .datahandling import create_data_handling from .datahandling import create_data_handling
from .display_utils import show_code, to_dot from .display_utils import show_code, get_code_obj, get_code_str, to_dot
from .field import Field, FieldType, fields from .field import Field, FieldType, fields
from .kernel_decorator import kernel from .kernel_decorator import kernel
from .kernelcreation import create_indexed_kernel, create_kernel, create_staggered_kernel from .kernelcreation import create_indexed_kernel, create_kernel, create_staggered_kernel
...@@ -25,7 +25,7 @@ __all__ = ['Field', 'FieldType', 'fields', ...@@ -25,7 +25,7 @@ __all__ = ['Field', 'FieldType', 'fields',
'TypedSymbol', 'TypedSymbol',
'make_slice', 'make_slice',
'create_kernel', 'create_indexed_kernel', 'create_staggered_kernel', 'create_kernel', 'create_indexed_kernel', 'create_staggered_kernel',
'show_code', 'to_dot', 'show_code', 'to_dot', 'get_code_obj', 'get_code_str',
'AssignmentCollection', 'AssignmentCollection',
'Assignment', 'Assignment',
'assignment_from_stencil', 'assignment_from_stencil',
......
# -*- coding: utf-8 -*-
import numpy as np import numpy as np
import sympy as sp import sympy as sp
from sympy.printing.latex import LatexPrinter from sympy.printing.latex import LatexPrinter
...@@ -24,9 +23,20 @@ def assignment_str(assignment): ...@@ -24,9 +23,20 @@ def assignment_str(assignment):
if Assignment: if Assignment:
_old_new = sp.codegen.ast.Assignment.__new__
def _Assignment__new__(cls, lhs, rhs, *args, **kwargs):
if isinstance(lhs, (list, tuple, sp.Matrix)) and isinstance(rhs, (list, tuple, sp.Matrix)):
assert len(lhs) == len(rhs), f'{lhs} and {rhs} must have same length when performing vector assignment!'
return tuple(_old_new(cls, a, b, *args, **kwargs) for a, b in zip(lhs, rhs))
return _old_new(cls, lhs, rhs, *args, **kwargs)
Assignment.__str__ = assignment_str Assignment.__str__ = assignment_str
Assignment.__new__ = _Assignment__new__
LatexPrinter._print_Assignment = print_assignment_latex LatexPrinter._print_Assignment = print_assignment_latex
sp.MutableDenseMatrix.__hash__ = lambda self: hash(tuple(self))
else: else:
# back port for older sympy versions that don't have Assignment yet # back port for older sympy versions that don't have Assignment yet
......
from typing import Any, Dict, Optional from typing import Any, Dict, Optional, Union
import sympy as sp import sympy as sp
...@@ -35,10 +35,10 @@ def highlight_cpp(code: str): ...@@ -35,10 +35,10 @@ def highlight_cpp(code: str):
return HTML(highlight(code, CppLexer(), HtmlFormatter())) return HTML(highlight(code, CppLexer(), HtmlFormatter()))
def show_code(ast: KernelFunction, custom_backend=None): def get_code_obj(ast: Union[KernelFunction, KernelWrapper], custom_backend=None):
"""Returns an object to display generated code (C/C++ or CUDA) """Returns an object to display generated code (C/C++ or CUDA)
Can either be displayed as HTML in Jupyter notebooks or printed as normal string. Can either be displayed as HTML in Jupyter notebooks or printed as normal string.
""" """
from pystencils.backends.cbackend import generate_c from pystencils.backends.cbackend import generate_c
...@@ -65,3 +65,17 @@ def show_code(ast: KernelFunction, custom_backend=None): ...@@ -65,3 +65,17 @@ def show_code(ast: KernelFunction, custom_backend=None):
def __repr__(self): def __repr__(self):
return generate_c(self.ast, dialect=dialect, custom_backend=custom_backend) return generate_c(self.ast, dialect=dialect, custom_backend=custom_backend)
return CodeDisplay(ast) return CodeDisplay(ast)
def get_code_str(ast, custom_backend=None):
return str(get_code_obj(ast, custom_backend))
def show_code(ast: Union[KernelFunction, KernelWrapper], custom_backend=None):
code = get_code_obj(ast, custom_backend)
try:
from IPython.display import display
display(code)
except Exception:
print(code)
"""
Light-weight wrapper around a compiled kernel
"""
import pystencils import pystencils
class KernelWrapper: class KernelWrapper:
def __init__(self, kernel, parameters, ast_node): """
Light-weight wrapper around a compiled kernel.
Can be called while still providing access to underlying AST.
"""
def __init__(self, kernel, parameters, ast_node: pystencils.astnodes.KernelFunction):
self.kernel = kernel self.kernel = kernel
self.parameters = parameters self.parameters = parameters
self.ast = ast_node self.ast = ast_node
...@@ -16,4 +19,4 @@ class KernelWrapper: ...@@ -16,4 +19,4 @@ class KernelWrapper:
@property @property
def code(self): def code(self):
return str(pystencils.show_code(self.ast)) return pystencils.get_code_str(self.ast)
import itertools
from copy import copy from copy import copy
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Union from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Union
...@@ -43,6 +44,11 @@ class AssignmentCollection: ...@@ -43,6 +44,11 @@ class AssignmentCollection:
subexpressions = [Assignment(k, v) subexpressions = [Assignment(k, v)
for k, v in subexpressions.items()] for k, v in subexpressions.items()]
main_assignments = list(itertools.chain.from_iterable(
[(a if isinstance(a, Iterable) else [a]) for a in main_assignments]))
subexpressions = list(itertools.chain.from_iterable(
[(a if isinstance(a, Iterable) else [a]) for a in subexpressions]))
self.main_assignments = main_assignments self.main_assignments = main_assignments
self.subexpressions = subexpressions self.subexpressions = subexpressions
......
...@@ -18,8 +18,7 @@ def test_type_interference(): ...@@ -18,8 +18,7 @@ def test_type_interference():
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = str(pystencils.show_code(ast)) code = str(pystencils.get_code_str(ast))
print(code)
assert 'double a' in code assert 'double a' in code
assert 'uint16_t b' in code assert 'uint16_t b' in code
assert 'uint16_t f' in code assert 'uint16_t f' in code
......
...@@ -17,16 +17,14 @@ def test_address_of(): ...@@ -17,16 +17,14 @@ def test_address_of():
}, {}) }, {})
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = pystencils.show_code(ast) pystencils.show_code(ast)
print(code)
assignments = pystencils.AssignmentCollection({ assignments = pystencils.AssignmentCollection({
y[0, 0]: cast_func(address_of(x[0, 0]), create_type('int64')) y[0, 0]: cast_func(address_of(x[0, 0]), create_type('int64'))
}, {}) }, {})
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = pystencils.show_code(ast) pystencils.show_code(ast)
print(code)
def test_address_of_with_cse(): def test_address_of_with_cse():
...@@ -39,9 +37,8 @@ def test_address_of_with_cse(): ...@@ -39,9 +37,8 @@ def test_address_of_with_cse():
}, {}) }, {})
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = pystencils.show_code(ast) pystencils.show_code(ast)
assignments_cse = sympy_cse(assignments) assignments_cse = sympy_cse(assignments)
ast = pystencils.create_kernel(assignments_cse) ast = pystencils.create_kernel(assignments_cse)
code = pystencils.show_code(ast) pystencils.show_code(ast)
print(code)
import pytest
import sympy as sp import sympy as sp
from pystencils import Assignment, AssignmentCollection from pystencils import Assignment, AssignmentCollection
...@@ -40,3 +41,39 @@ def test_free_and_defined_symbols(): ...@@ -40,3 +41,39 @@ def test_free_and_defined_symbols():
print(ac) print(ac)
print(ac.__repr__) print(ac.__repr__)
def test_vector_assignments():
"""From #17 (https://i10git.cs.fau.de/pycodegen/pystencils/issues/17)"""
import pystencils as ps
import sympy as sp
a, b, c = sp.symbols("a b c")
assignments = ps.Assignment(sp.Matrix([a,b,c]), sp.Matrix([1,2,3]))
print(assignments)
def test_wrong_vector_assignments():
"""From #17 (https://i10git.cs.fau.de/pycodegen/pystencils/issues/17)"""
import pystencils as ps
import sympy as sp
a, b = sp.symbols("a b")
with pytest.raises(AssertionError,
match=r'Matrix(.*) and Matrix(.*) must have same length when performing vector assignment!'):
ps.Assignment(sp.Matrix([a,b]), sp.Matrix([1,2,3]))
def test_vector_assignment_collection():
"""From #17 (https://i10git.cs.fau.de/pycodegen/pystencils/issues/17)"""
import pystencils as ps
import sympy as sp
a, b, c = sp.symbols("a b c")
y, x = sp.Matrix([a,b,c]), sp.Matrix([1,2,3])
assignments = ps.AssignmentCollection({y: x})
print(assignments)
assignments = ps.AssignmentCollection([ps.Assignment(y,x)])
print(assignments)
...@@ -52,7 +52,7 @@ def test_complex_numbers(assignment, scalar_dtypes, target): ...@@ -52,7 +52,7 @@ def test_complex_numbers(assignment, scalar_dtypes, target):
ast = pystencils.create_kernel(assignment, ast = pystencils.create_kernel(assignment,
target=target, target=target,
data_type=scalar_dtypes) data_type=scalar_dtypes)
code = str(pystencils.show_code(ast)) code = pystencils.get_code_str(ast)
print(code) print(code)
assert "Not supported" not in code assert "Not supported" not in code
...@@ -98,7 +98,7 @@ def test_complex_numbers_64(assignment, target): ...@@ -98,7 +98,7 @@ def test_complex_numbers_64(assignment, target):
ast = pystencils.create_kernel(assignment, ast = pystencils.create_kernel(assignment,
target=target, target=target,
data_type='double') data_type='double')
code = str(pystencils.show_code(ast)) code = pystencils.get_code_str(ast)
print(code) print(code)
assert "Not supported" not in code assert "Not supported" not in code
......
...@@ -63,7 +63,7 @@ def test_boundary_check(with_cse): ...@@ -63,7 +63,7 @@ def test_boundary_check(with_cse):
print(assignments) print(assignments)
kernel_checked = ps.create_kernel(assignments, ghost_layers=0).compile() kernel_checked = ps.create_kernel(assignments, ghost_layers=0).compile()
print(ps.show_code(kernel_checked)) ps.show_code(kernel_checked)
# No SEGFAULT, please!! # No SEGFAULT, please!!
kernel_checked(f=f_arr, g=g_arr) kernel_checked(f=f_arr, g=g_arr)
...@@ -20,8 +20,8 @@ def test_cuda_known_functions(): ...@@ -20,8 +20,8 @@ def test_cuda_known_functions():
}) })
ast = pystencils.create_kernel(assignments, 'gpu') ast = pystencils.create_kernel(assignments, 'gpu')
print(pystencils.show_code(ast))
pytest.importorskip('pycuda') pytest.importorskip('pycuda')
pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
assert(kernel is not None) assert(kernel is not None)
...@@ -35,7 +35,7 @@ def test_cuda_but_not_c(): ...@@ -35,7 +35,7 @@ def test_cuda_but_not_c():
}) })
ast = pystencils.create_kernel(assignments, 'cpu') ast = pystencils.create_kernel(assignments, 'cpu')
print(pystencils.show_code(ast)) pystencils.show_code(ast)
def test_cuda_unknown(): def test_cuda_unknown():
...@@ -46,5 +46,4 @@ def test_cuda_unknown(): ...@@ -46,5 +46,4 @@ def test_cuda_unknown():
}) })
ast = pystencils.create_kernel(assignments, 'gpu') ast = pystencils.create_kernel(assignments, 'gpu')
code = str(pystencils.show_code(ast)) pystencils.show_code(ast)
print(code)
...@@ -3,6 +3,7 @@ from subprocess import CalledProcessError ...@@ -3,6 +3,7 @@ from subprocess import CalledProcessError
import pytest import pytest
import sympy import sympy
import pycuda.driver
import pystencils import pystencils
import pystencils.cpu.cpujit import pystencils.cpu.cpujit
import pystencils.gpucuda.cudajit import pystencils.gpucuda.cudajit
...@@ -31,7 +32,7 @@ def test_custom_backends_cpu(): ...@@ -31,7 +32,7 @@ def test_custom_backends_cpu():
z[0, 0], x[0, 0] * sympy.log(x[0, 0] * y[0, 0]))], []) z[0, 0], x[0, 0] * sympy.log(x[0, 0] * y[0, 0]))], [])
ast = pystencils.create_kernel(normal_assignments, target='cpu') ast = pystencils.create_kernel(normal_assignments, target='cpu')
print(pystencils.show_code(ast, ScreamingBackend())) pystencils.show_code(ast, ScreamingBackend())
with pytest.raises(CalledProcessError): with pytest.raises(CalledProcessError):
pystencils.cpu.cpujit.make_python_function(ast, custom_backend=ScreamingBackend()) pystencils.cpu.cpujit.make_python_function(ast, custom_backend=ScreamingBackend())
...@@ -46,6 +47,6 @@ def test_custom_backends_gpu(): ...@@ -46,6 +47,6 @@ def test_custom_backends_gpu():
z[0, 0], x[0, 0] * sympy.log(x[0, 0] * y[0, 0]))], []) z[0, 0], x[0, 0] * sympy.log(x[0, 0] * y[0, 0]))], [])
ast = pystencils.create_kernel(normal_assignments, target='gpu') ast = pystencils.create_kernel(normal_assignments, target='gpu')
print(pystencils.show_code(ast, ScreamingGpuBackend())) pystencils.show_code(ast, ScreamingGpuBackend())
with pytest.raises(pycuda.driver.CompileError): with pytest.raises(pycuda.driver.CompileError):
pystencils.gpucuda.cudajit.make_python_function(ast, custom_backend=ScreamingGpuBackend()) pystencils.gpucuda.cudajit.make_python_function(ast, custom_backend=ScreamingGpuBackend())
...@@ -12,7 +12,7 @@ def test_fast_sqrt(): ...@@ -12,7 +12,7 @@ def test_fast_sqrt():
assert len(insert_fast_sqrts(expr).atoms(fast_sqrt)) == 1 assert len(insert_fast_sqrts(expr).atoms(fast_sqrt)) == 1
assert len(insert_fast_sqrts([expr])[0].atoms(fast_sqrt)) == 1 assert len(insert_fast_sqrts([expr])[0].atoms(fast_sqrt)) == 1
ast = ps.create_kernel(ps.Assignment(g[0, 0], insert_fast_sqrts(expr)), target='gpu') ast = ps.create_kernel(ps.Assignment(g[0, 0], insert_fast_sqrts(expr)), target='gpu')
code_str = str(ps.show_code(ast)) code_str = ps.get_code_str(ast)
assert '__fsqrt_rn' in code_str assert '__fsqrt_rn' in code_str
expr = ps.Assignment(sp.Symbol("tmp"), 3 / sp.sqrt(f[0, 0] + f[1, 0])) expr = ps.Assignment(sp.Symbol("tmp"), 3 / sp.sqrt(f[0, 0] + f[1, 0]))
...@@ -21,7 +21,7 @@ def test_fast_sqrt(): ...@@ -21,7 +21,7 @@ def test_fast_sqrt():
ac = ps.AssignmentCollection([expr], []) ac = ps.AssignmentCollection([expr], [])
assert len(insert_fast_sqrts(ac).main_assignments[0].atoms(fast_inv_sqrt)) == 1 assert len(insert_fast_sqrts(ac).main_assignments[0].atoms(fast_inv_sqrt)) == 1
ast = ps.create_kernel(insert_fast_sqrts(ac), target='gpu') ast = ps.create_kernel(insert_fast_sqrts(ac), target='gpu')
code_str = str(ps.show_code(ast)) code_str = ps.get_code_str(ast)
assert '__frsqrt_rn' in code_str assert '__frsqrt_rn' in code_str
...@@ -34,5 +34,5 @@ def test_fast_divisions(): ...@@ -34,5 +34,5 @@ def test_fast_divisions():
assert len(insert_fast_divisions(expr).atoms(fast_division)) == 1 assert len(insert_fast_divisions(expr).atoms(fast_division)) == 1
ast = ps.create_kernel(ps.Assignment(g[0, 0], insert_fast_divisions(expr)), target='gpu') ast = ps.create_kernel(ps.Assignment(g[0, 0], insert_fast_divisions(expr)), target='gpu')
code_str = str(ps.show_code(ast)) code_str = ps.get_code_str(ast)
assert '__fdividef' in code_str assert '__fdividef' in code_str
...@@ -49,7 +49,7 @@ def test_interpolation(): ...@@ -49,7 +49,7 @@ def test_interpolation():
print(assignments) print(assignments)
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
print(ast) print(ast)
print(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
pyconrad.imshow(lenna) pyconrad.imshow(lenna)
...@@ -69,7 +69,7 @@ def test_scale_interpolation(): ...@@ -69,7 +69,7 @@ def test_scale_interpolation():
print(assignments) print(assignments)
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
print(ast) print(ast)
print(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
out = np.zeros_like(lenna) out = np.zeros_like(lenna)
...@@ -81,9 +81,9 @@ def test_scale_interpolation(): ...@@ -81,9 +81,9 @@ def test_scale_interpolation():
['border', ['border',
'clamp', 'clamp',
pytest.param('warp', marks=pytest.mark.xfail( pytest.param('warp', marks=pytest.mark.xfail(
reason="requires interpolation-refactoring branch")), reason="Fails on newer SymPy version due to complex conjugate()")),
pytest.param('mirror', marks=pytest.mark.xfail( pytest.param('mirror', marks=pytest.mark.xfail(
reason="requires interpolation-refactoring branch")), reason="Fails on newer SymPy version due to complex conjugate()")),
]) ])
def test_rotate_interpolation(address_mode): def test_rotate_interpolation(address_mode):
""" """
...@@ -100,7 +100,7 @@ def test_rotate_interpolation(address_mode): ...@@ -100,7 +100,7 @@ def test_rotate_interpolation(address_mode):
print(assignments) print(assignments)
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
print(ast) print(ast)
print(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
out = np.zeros_like(lenna) out = np.zeros_like(lenna)
...@@ -108,90 +108,93 @@ def test_rotate_interpolation(address_mode): ...@@ -108,90 +108,93 @@ def test_rotate_interpolation(address_mode):
pyconrad.imshow(out, "out " + address_mode) pyconrad.imshow(out, "out " + address_mode)
@pytest.mark.parametrize('dtype', (np.int32, np.float32, np.float64)) @pytest.mark.parametrize('address_mode', ['border', 'wrap', 'clamp', 'mirror'])
@pytest.mark.parametrize('address_mode', ('border', 'wrap', 'clamp', 'mirror')) def test_rotate_interpolation_gpu(address_mode):
@pytest.mark.parametrize('use_textures', ('use_textures', False))
def test_rotate_interpolation_gpu(dtype, address_mode, use_textures):
pytest.importorskip('pycuda') pytest.importorskip('pycuda')
import pycuda.autoinit # NOQA
import pycuda.gpuarray as gpuarray import pycuda.gpuarray as gpuarray
import pycuda.autoinit # noqa
rotation_angle = sympy.pi / 5 rotation_angle = sympy.pi / 5
scale = 1 scale = 1
if dtype == np.int32: previous_result = None
lenna_gpu = gpuarray.to_gpu( for dtype in [np.int32, np.float32, np.float64]:
np.ascontiguousarray(lenna * 255, dtype)) if dtype == np.int32:
else: lenna_gpu = gpuarray.to_gpu(
lenna_gpu = gpuarray.to_gpu( np.ascontiguousarray(lenna * 255, dtype))
np.ascontiguousarray(lenna, dtype)) else:
x_f, y_f = pystencils.fields('x,y: %s [2d]' % type_map[dtype], ghost_layers=0) lenna_gpu = gpuarray.to_gpu(
np.ascontiguousarray(lenna, dtype))
transformed = scale * \ for use_textures in [True, False]:
sympy.rot_axis3(rotation_angle)[:2, :2] * sympy.Matrix((x_, y_)) - sympy.Matrix([2, 2]) x_f, y_f = pystencils.fields('x,y: %s [2d]' % type_map[dtype], ghost_layers=0)
assignments = pystencils.AssignmentCollection({
y_f.center(): LinearInterpolator(x_f, address_mode=address_mode).at(transformed) transformed = scale * sympy.rot_axis3(rotation_angle)[:2, :2] * \
}) sympy.Matrix((x_, y_)) - sympy.Matrix([2, 2])
print(assignments) assignments = pystencils.AssignmentCollection({
ast = pystencils.create_kernel(assignments, target='gpu', use_textures_for_interpolation=use_textures) y_f.center(): LinearInterpolator(x_f, address_mode=address_mode).at(transformed)
print(ast) })
print(pystencils.show_code(ast)) print(assignments)
kernel = ast.compile() ast = pystencils.create_kernel(assignments, target='gpu', use_textures_for_interpolation=use_textures)
print(ast)
out = gpuarray.zeros_like(lenna_gpu) pystencils.show_code(ast)
kernel(x=lenna_gpu, y=out) kernel = ast.compile()
pyconrad.imshow(out,
f"out {address_mode} texture:{use_textures} {type_map[dtype]}") out = gpuarray.zeros_like(lenna_gpu)
kernel(x=lenna_gpu, y=out)
pyconrad.imshow(out,
@pytest.mark.parametrize('address_mode', ['border', 'wrap', f"out {address_mode} texture:{use_textures} {type_map[dtype]}")
pytest.param('warp', marks=pytest.mark.xfail( skimage.io.imsave(f"/tmp/out {address_mode} texture:{use_textures} {type_map[dtype]}.tif",
reason="% printed as fmod on old sympy")), np.ascontiguousarray(out.get(), np.float32))
pytest.param('mirror', marks=pytest.mark.xfail( if previous_result is not None:
reason="% printed as fmod on old sympy")), try:
]) assert np.allclose(previous_result[4:-4, 4:-4], out.get()[4:-4, 4:-4], rtol=100, atol=1e-3)
@pytest.mark.parametrize('dtype', [np.float64, np.float32, np.int32]) except AssertionError: # NOQA
@pytest.mark.parametrize('use_textures', ('use_textures', False,)) print("Max error: %f" % np.max(previous_result - out.get()))
def test_shift_interpolation_gpu(address_mode, dtype, use_textures): # pyconrad.imshow(previous_result - out.get(), "Difference image")
sver = sympy.__version__.split(".") # raise e
if (int(sver[0]) == 1 and int(sver[1]) < 2) and address_mode in ['mirror', 'warp']: previous_result = out.get()
pytest.skip()
@pytest.mark.parametrize('address_mode', ['border', 'wrap', 'clamp', 'mirror'])
def test_shift_interpolation_gpu(address_mode):
pytest.importorskip('pycuda') pytest.importorskip('pycuda')
import pycuda.autoinit # NOQA
import pycuda.gpuarray as gpuarray import pycuda.gpuarray as gpuarray
import pycuda.autoinit # noqa
rotation_angle = 0 # sympy.pi / 5 rotation_angle = 0 # sympy.pi / 5
scale = 1 scale = 1
# shift = - sympy.Matrix([1.5, 1.5]) # shift = - sympy.Matrix([1.5, 1.5])
shift = sympy.Matrix((0.0, 0.0)) shift = sympy.Matrix((0.0, 0.0))
if dtype == np.int32: for dtype in [np.float64, np.float32, np.int32]:
lenna_gpu = gpuarray.to_gpu( if dtype == np.int32:
np.ascontiguousarray(lenna * 255, dtype)) lenna_gpu = gpuarray.to_gpu(
else: np.ascontiguousarray(lenna * 255, dtype))
lenna_gpu = gpuarray.to_gpu( else:
np.ascontiguousarray(lenna, dtype)) lenna_gpu = gpuarray.to_gpu(
np.ascontiguousarray(lenna, dtype))
x_f, y_f = pystencils.fields('x,y: %s [2d]' % type_map[dtype], ghost_layers=0) for use_textures in [True, False]:
x_f, y_f = pystencils.fields('x,y: %s [2d]' % type_map[dtype], ghost_layers=0)
if use_textures:
transformed = scale * sympy.rot_axis3(rotation_angle)[:2, :2] * sympy.Matrix((x_, y_)) + shift if use_textures:
else: transformed = scale * sympy.rot_axis3(rotation_angle)[:2, :2] * sympy.Matrix((x_, y_)) + shift
transformed = scale * sympy.rot_axis3(rotation_angle)[:2, :2] * sympy.Matrix((x_, y_)) + shift else:
assignments = pystencils.AssignmentCollection({ transformed = scale * sympy.rot_axis3(rotation_angle)[:2, :2] * sympy.Matrix((x_, y_)) + shift
y_f.center(): LinearInterpolator(x_f, address_mode=address_mode).at(transformed) assignments = pystencils.AssignmentCollection({
}) y_f.center(): LinearInterpolator(x_f, address_mode=address_mode).at(transformed)
# print(assignments) })
ast = pystencils.create_kernel(assignments, target='gpu', use_textures_for_interpolation=use_textures) # print(assignments)
# print(ast) ast = pystencils.create_kernel(assignments, target='gpu', use_textures_for_interpolation=use_textures)
print(pystencils.show_code(ast)) # print(ast)
kernel = ast.compile() pystencils.show_code(ast)
kernel = ast.compile()
out = gpuarray.zeros_like(lenna_gpu)
kernel(x=lenna_gpu, y=out) out = gpuarray.zeros_like(lenna_gpu)
pyconrad.imshow(out, kernel(x=lenna_gpu, y=out)
f"out {address_mode} texture:{use_textures} {type_map[dtype]}") pyconrad.imshow(out,
f"out {address_mode} texture:{use_textures} {type_map[dtype]}")
skimage.io.imsave(f"/tmp/out {address_mode} texture:{use_textures} {type_map[dtype]}.tif",
np.ascontiguousarray(out.get(), np.float32))
@pytest.mark.parametrize('address_mode', ['border', 'clamp']) @pytest.mark.parametrize('address_mode', ['border', 'clamp'])
...@@ -210,7 +213,7 @@ def test_rotate_interpolation_size_change(address_mode): ...@@ -210,7 +213,7 @@ def test_rotate_interpolation_size_change(address_mode):
print(assignments) print(assignments)
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
print(ast) print(ast)
print(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
out = np.zeros((100, 150), np.float64) out = np.zeros((100, 150), np.float64)
...@@ -219,7 +222,7 @@ def test_rotate_interpolation_size_change(address_mode): ...@@ -219,7 +222,7 @@ def test_rotate_interpolation_size_change(address_mode):
@pytest.mark.parametrize('address_mode, target', @pytest.mark.parametrize('address_mode, target',
itertools.product(['border', 'wrap', 'clamp', 'mirror'], ['cpu'])) itertools.product(['border', 'wrap', 'clamp', 'mirror'], ['cpu', 'gpu']))
def test_field_interpolated(address_mode, target): def test_field_interpolated(address_mode, target):
x_f, y_f = pystencils.fields('x,y: float64 [2d]') x_f, y_f = pystencils.fields('x,y: float64 [2d]')
...@@ -227,19 +230,13 @@ def test_field_interpolated(address_mode, target): ...@@ -227,19 +230,13 @@ def test_field_interpolated(address_mode, target):
y_f.center(): x_f.interpolated_access([0.5 * x_ + 2.7, 0.25 * y_ + 7.2], address_mode=address_mode) y_f.center(): x_f.interpolated_access([0.5 * x_ + 2.7, 0.25 * y_ + 7.2], address_mode=address_mode)
}) })
print(assignments) print(assignments)
ast = pystencils.create_kernel(assignments, target=target) ast = pystencils.create_kernel(assignments)
print(ast) print(ast)
print(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
out = np.zeros_like(lenna) out = np.zeros_like(lenna)
kernel(x=lenna, y=out) kernel(x=lenna, y=out)
pyconrad.imshow(out, "out " + address_mode) pyconrad.imshow(out, "out " + address_mode)
kernel(x=lenna, y=out)
pyconrad.imshow(out, "out " + address_mode)
def test_spatial_derivative():
x, y = pystencils.fields('x, y: float32[2d]')
tx, ty = pystencils.fields('t_x, t_y: float32[2d]')
diff = sympy.diff(x.interpolated_access((tx.center, ty.center)), tx.center)
print("diff: " + str(diff))
import numpy as np import numpy as np
from pystencils import show_code from pystencils import get_code_obj
from pystencils.astnodes import Block, KernelFunction, SympyAssignment from pystencils.astnodes import Block, KernelFunction, SympyAssignment
from pystencils.cpu import make_python_function from pystencils.cpu import make_python_function
from pystencils.field import Field from pystencils.field import Field
...@@ -36,7 +36,7 @@ def test_jacobi_fixed_field_size(): ...@@ -36,7 +36,7 @@ def test_jacobi_fixed_field_size():
error = np.sum(np.abs(dst_field_py - dst_field_c)) error = np.sum(np.abs(dst_field_py - dst_field_c))
np.testing.assert_allclose(error, 0.0, atol=1e-13) np.testing.assert_allclose(error, 0.0, atol=1e-13)
code_display = show_code(ast_node) code_display = get_code_obj(ast_node)
assert 'for' in str(code_display) assert 'for' in str(code_display)
assert 'for' in code_display._repr_html_() assert 'for' in code_display._repr_html_()
......
...@@ -52,7 +52,7 @@ def test_jacobi_fixed_field_size_gpu(): ...@@ -52,7 +52,7 @@ def test_jacobi_fixed_field_size_gpu():
jacobi = Assignment(d[0, 0], (f[1, 0] + f[-1, 0] + f[0, 1] + f[0, -1]) / 4) jacobi = Assignment(d[0, 0], (f[1, 0] + f[-1, 0] + f[0, 1] + f[0, -1]) / 4)
ast = create_kernel([jacobi], target='gpu') ast = create_kernel([jacobi], target='gpu')
print(show_code(ast)) show_code(ast)
for x in range(1, size[0] - 1): for x in range(1, size[0] - 1):
for y in range(1, size[1] - 1): for y in range(1, size[1] - 1):
......
...@@ -27,10 +27,9 @@ def test_print_opencl(): ...@@ -27,10 +27,9 @@ def test_print_opencl():
print(ast) print(ast)
code = pystencils.show_code(ast, custom_backend=CudaBackend()) pystencils.show_code(ast, custom_backend=CudaBackend())
print(code)
opencl_code = pystencils.show_code(ast, custom_backend=OpenClBackend()) opencl_code = pystencils.get_code_str(ast, custom_backend=OpenClBackend())
print(opencl_code) print(opencl_code)
assert "__global double * RESTRICT const _data_x" in str(opencl_code) assert "__global double * RESTRICT const _data_x" in str(opencl_code)
...@@ -108,10 +107,9 @@ def test_opencl_jit(): ...@@ -108,10 +107,9 @@ def test_opencl_jit():
print(ast) print(ast)
code = pystencils.show_code(ast, custom_backend=CudaBackend()) pystencils.show_code(ast, custom_backend=CudaBackend())
print(code)
opencl_code = pystencils.show_code(ast, custom_backend=OpenClBackend()) pystencils.show_code(ast, custom_backend=OpenClBackend())
print(opencl_code)
cuda_kernel = ast.compile() cuda_kernel = ast.compile()
assert cuda_kernel is not None assert cuda_kernel is not None
......
...@@ -27,4 +27,5 @@ def test_source_code_comment(): ...@@ -27,4 +27,5 @@ def test_source_code_comment():
print(ast) print(ast)
compiled = ast.compile() compiled = ast.compile()
assert compiled is not None assert compiled is not None
print(pystencils.show_code(ast))
pystencils.show_code(ast)
...@@ -30,7 +30,7 @@ def test_sum(): ...@@ -30,7 +30,7 @@ def test_sum():
}) })
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = str(pystencils.show_code(ast)) code = str(pystencils.get_code_obj(ast))
kernel = ast.compile() kernel = ast.compile()
print(code) print(code)
...@@ -58,11 +58,11 @@ def test_sum_use_float(): ...@@ -58,11 +58,11 @@ def test_sum_use_float():
}) })
ast = pystencils.create_kernel(assignments, data_type=create_type('float32')) ast = pystencils.create_kernel(assignments, data_type=create_type('float32'))
code = str(pystencils.show_code(ast)) code = str(pystencils.get_code_obj(ast))
kernel = ast.compile() kernel = ast.compile()
print(code) print(code)
print(pystencils.show_code(ast)) print(pystencils.get_code_obj(ast))
assert 'float sum' in code assert 'float sum' in code
array = np.zeros((10,), np.float32) array = np.zeros((10,), np.float32)
...@@ -89,7 +89,7 @@ def test_product(): ...@@ -89,7 +89,7 @@ def test_product():
}) })
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = str(pystencils.show_code(ast)) code = pystencils.get_code_str(ast)
kernel = ast.compile() kernel = ast.compile()
print(code) print(code)
...@@ -120,11 +120,9 @@ def test_prod_var_limit(): ...@@ -120,11 +120,9 @@ def test_prod_var_limit():
}) })
ast = pystencils.create_kernel(assignments) ast = pystencils.create_kernel(assignments)
code = str(pystencils.show_code(ast)) pystencils.show_code(ast)
kernel = ast.compile() kernel = ast.compile()
print(code)
array = np.zeros((10,), np.int64) array = np.zeros((10,), np.int64)
kernel(x=array, limit=100) kernel(x=array, limit=100)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment