Skip to content
Snippets Groups Projects
Select Git revision
  • f16eadeaac18588bacb2af4f973a5cf94f141cb9
  • 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

stencils.py

Blame
  • test_sum_prod.py 2.68 KiB
    # -*- coding: utf-8 -*-
    #
    # Copyright © 2019 Stephan Seitz <stephan.seitz@fau.de>
    #
    # Distributed under terms of the GPLv3 license.
    
    """
    
    """
    import numpy as np
    import sympy
    from sympy.abc import k
    
    import pystencils
    from pystencils.data_types import create_type
    
    
    def test_sum():
    
        sum = sympy.Sum(k, (k, 1, 100))
        expanded_sum = sum.doit()
    
        print(sum)
        print(expanded_sum)
    
        x = pystencils.fields('x: float32[1d]')
    
        assignments = pystencils.AssignmentCollection({
            x.center(): sum
        })
    
        ast = pystencils.create_kernel(assignments)
        code = str(pystencils.get_code_obj(ast))
        kernel = ast.compile()
    
        print(code)
        assert 'double sum' in code
    
        array = np.zeros((10,), np.float32)
    
        kernel(x=array)
    
        assert np.allclose(array, int(expanded_sum) * np.ones_like(array))
    
    
    def test_sum_use_float():
    
        sum = sympy.Sum(k, (k, 1, 100))
        expanded_sum = sum.doit()
    
        print(sum)
        print(expanded_sum)
    
        x = pystencils.fields('x: float32[1d]')
    
        assignments = pystencils.AssignmentCollection({
            x.center(): sum
        })
    
        ast = pystencils.create_kernel(assignments, data_type=create_type('float32'))
        code = str(pystencils.get_code_obj(ast))
        kernel = ast.compile()
    
        print(code)
        print(pystencils.get_code_obj(ast))
        assert 'float sum' in code
    
        array = np.zeros((10,), np.float32)
    
        kernel(x=array)
    
        assert np.allclose(array, int(expanded_sum) * np.ones_like(array))
    
    
    def test_product():
    
        k = pystencils.TypedSymbol('k', create_type('int64'))
    
        sum = sympy.Product(k, (k, 1, 10))
        expanded_sum = sum.doit()
    
        print(sum)
        print(expanded_sum)
    
        x = pystencils.fields('x: int64[1d]')
    
        assignments = pystencils.AssignmentCollection({
            x.center(): sum
        })
    
        ast = pystencils.create_kernel(assignments)
        code = pystencils.get_code_str(ast)
        kernel = ast.compile()
    
        print(code)
        assert 'int64_t product' in code
    
        array = np.zeros((10,), np.int64)
    
        kernel(x=array)
    
        assert np.allclose(array, int(expanded_sum) * np.ones_like(array))
    
    
    def test_prod_var_limit():
    
        k = pystencils.TypedSymbol('k', create_type('int64'))
        limit = pystencils.TypedSymbol('limit', create_type('int64'))
    
        sum = sympy.Sum(k, (k, 1, limit))
        expanded_sum = sum.replace(limit, 100).doit()
    
        print(sum)
        print(expanded_sum)
    
        x = pystencils.fields('x: int64[1d]')
    
        assignments = pystencils.AssignmentCollection({
            x.center(): sum
        })
    
        ast = pystencils.create_kernel(assignments)
        pystencils.show_code(ast)
        kernel = ast.compile()
    
        array = np.zeros((10,), np.int64)
    
        kernel(x=array, limit=100)
    
        assert np.allclose(array, int(expanded_sum) * np.ones_like(array))