Skip to content
Snippets Groups Projects
Select Git revision
  • 5eedf7cc3d07a53d64c507dfaab7896326733469
  • 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_aligned_array.py

Blame
  • Martin Bauer's avatar
    Martin Bauer authored
    This restructuring allows for easier separation of modules into
    separate repositories later. Also, now pip install with repo url can be
    used.
    
    The setup.py files have also been updated to correctly reference each
    other. Module versions are not extracted from git state
    1e02cdc7
    History
    test_aligned_array.py 2.82 KiB
    from pystencils import create_data_handling
    from pystencils.alignedarray import *
    from pystencils.field import create_numpy_array_with_layout
    
    
    def is_aligned(arr, alignment, byte_offset=0):
        address = arr.__array_interface__['data'][0]
        rest = (address + byte_offset) % alignment
        if rest:
            print("Alignment rest", rest)
        return rest == 0
    
    
    def test_1d_arrays():
        for alignment in [8, 8*4]:
            for shape in [17, 16, (16, 16), (17, 17), (18, 18), (19, 19)]:
                arrays = [
                    aligned_zeros(shape, alignment),
                    aligned_ones(shape, alignment),
                    aligned_empty(shape, alignment),
                ]
                for arr in arrays:
                    assert is_aligned(arr, alignment)
    
    
    def test_3d_arrays():
        for order in ('C', 'F'):
            for alignment in [8, 8*4]:
                for shape in [(16, 16), (17, 17), (18, 18), (19, 19)]:
                    arrays = [
                        aligned_zeros(shape, alignment, order=order),
                        aligned_ones(shape, alignment, order=order),
                        aligned_empty(shape, alignment, order=order),
                    ]
                    for arr in arrays:
                        assert is_aligned(arr, alignment)
                        if order == 'C':
                            assert is_aligned(arr[1], alignment)
                            assert is_aligned(arr[5], alignment)
                        else:
                            assert is_aligned(arr[..., 1], alignment)
                            assert is_aligned(arr[..., 5], alignment)
    
    
    def test_data_handling():
        for parallel in (False, True):
            for tries in range(16):  # try a few times, since we might get lucky and get randomly a correct alignment
                dh = create_data_handling((6, 7), default_ghost_layers=1, parallel=parallel)
                dh.add_array('test', alignment=8 * 4)
                for b in dh.iterate(ghost_layers=True, inner_ghost_layers=True):
                    arr = b['test']
                    assert is_aligned(arr[1:, 3:], 8*4)
    
    
    def test_alignment_of_different_layouts():
        offset = 1
        byte_offset = 8
        for tries in range(16):  # try a few times, since we might get lucky and get randomly a correct alignment
            arr = create_numpy_array_with_layout((3, 4, 5), layout=(0, 1, 2),
                                                 alignment=True, byte_offset=byte_offset)
            assert is_aligned(arr[offset, ...], 8*4, byte_offset)
    
            arr = create_numpy_array_with_layout((3, 4, 5), layout=(2, 1, 0),
                                                 alignment=True, byte_offset=byte_offset)
            assert is_aligned(arr[..., offset], 8*4, byte_offset)
    
            arr = create_numpy_array_with_layout((3, 4, 5), layout=(2, 0, 1),
                                                 alignment=True, byte_offset=byte_offset)
            assert is_aligned(arr[:, 0, :], 8*4, byte_offset)