Skip to content
Snippets Groups Projects
Select Git revision
  • 0834b5c9021cdc9e1d39058a0b28bc5cc89db09b
  • master default protected
  • v2.0-dev protected
  • zikeliml/Task-96-dotExporterForAST
  • zikeliml/124-rework-tutorials
  • fma
  • fhennig/v2.0-deprecations
  • holzer-master-patch-46757
  • 66-absolute-access-is-probably-not-copied-correctly-after-_eval_subs
  • gpu_bufferfield_fix
  • hyteg
  • vectorization_sqrt_fix
  • target_dh_refactoring
  • const_fix
  • improved_comm
  • gpu_liveness_opts
  • release/1.3.7 protected
  • release/1.3.6 protected
  • release/2.0.dev0 protected
  • release/1.3.5 protected
  • release/1.3.4 protected
  • release/1.3.3 protected
  • release/1.3.2 protected
  • release/1.3.1 protected
  • release/1.3 protected
  • release/1.2 protected
  • release/1.1.1 protected
  • release/1.1 protected
  • release/1.0.1 protected
  • release/1.0 protected
  • release/0.4.4 protected
  • last/Kerncraft
  • last/OpenCL
  • last/LLVM
  • release/0.4.3 protected
  • release/0.4.2 protected
36 results

test_math_functions.py

Blame
  • markus holzer's avatar
    Markus Holzer authored
    f4218729
    History
    test_math_functions.py 2.42 KiB
    import pytest
    import sympy as sp
    import numpy as np
    import pystencils as ps
    
    
    @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("pycuda")
        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("pycuda")
        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)