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

fixtures.py

Blame
  • fixtures.py 2.19 KiB
    """Fixtures for the pystencils test suite
    
    This module provides a number of fixtures used by the pystencils test suite.
    Use these fixtures wherever applicable to extend the code surface area covered
    by your tests:
    
    - All tests that should work for every target should use the `target` fixture
    - All tests that should work with the highest optimization level for every target
      should use the `gen_config` fixture
    - Use the `xp` fixture to access the correct array module (numpy or cupy) depending
      on the target
    
    """
    
    import pytest
    
    from types import ModuleType
    
    import pystencils as ps
    
    AVAILABLE_TARGETS = [ps.Target.GenericCPU]
    
    try:
        import cupy
    
        AVAILABLE_TARGETS += [ps.Target.CUDA]
    except ImportError:
        pass
    
    AVAILABLE_TARGETS += ps.Target.available_vector_cpu_targets()
    TARGET_IDS = [t.name for t in AVAILABLE_TARGETS]
    
    
    def pytest_addoption(parser: pytest.Parser):
        parser.addoption(
            "--experimental-cpu-jit",
            dest="experimental_cpu_jit",
            action="store_true"
        )
    
    
    @pytest.fixture(params=AVAILABLE_TARGETS, ids=TARGET_IDS)
    def target(request) -> ps.Target:
        """Provides all code generation targets available on the current hardware"""
        return request.param
    
    
    @pytest.fixture
    def gen_config(request: pytest.FixtureRequest, target: ps.Target):
        """Default codegen configuration for the current target.
    
        For GPU targets, set default indexing options.
        For vector-CPU targets, set default vectorization config.
        """
    
        gen_config = ps.CreateKernelConfig(target=target)
    
        if target.is_vector_cpu():
            gen_config.cpu.vectorize.enable = True
            gen_config.cpu.vectorize.assume_inner_stride_one = True
    
        if target.is_cpu() and request.config.getoption("experimental_cpu_jit"):
            from pystencils.jit.cpu import CpuJit, GccInfo
    
            gen_config.jit = CpuJit.create(compiler_info=GccInfo(target=target))
    
        return gen_config
    
    
    @pytest.fixture()
    def xp(target: ps.Target) -> ModuleType:
        """Primary array module for the current target.
    
        Returns:
            `cupy` if `target == Target.CUDA`, and `numpy` otherwise
        """
        if target == ps.Target.CUDA:
            import cupy as xp
    
            return xp
        else:
            import numpy as np
    
            return np