diff --git a/src/walberla/codegen/__init__.py b/src/walberla/codegen/__init__.py index bf850906eba200d7ce81976a9a3319acd8d45a3f..8f08ca8d480ccea626cf575109d0f81a35f65366 100644 --- a/src/walberla/codegen/__init__.py +++ b/src/walberla/codegen/__init__.py @@ -1,6 +1,6 @@ from .api import real_t, Vector3, GhostLayerFieldPtr, glfield, IBlockPtr from .sweep import Sweep -from .build_config import WalberlaBuildConfig +from .build_config import WalberlaBuildConfig, get_build_config __all__ = [ "real_t", @@ -10,6 +10,7 @@ __all__ = [ "IBlockPtr", "Sweep", "WalberlaBuildConfig", + "get_build_config", ] from . import _version diff --git a/src/walberla/codegen/boundaries/linkwise.py b/src/walberla/codegen/boundaries/linkwise.py index 777e6b0a883f7000e891285d88b2cb7cc9281b31..2f0b9593a4fa03747e0f89a166e6d59f8e8f7278 100644 --- a/src/walberla/codegen/boundaries/linkwise.py +++ b/src/walberla/codegen/boundaries/linkwise.py @@ -50,13 +50,11 @@ class NoSlip(GenericLinkwiseBoundary): lb_method: AbstractLbMethod, pdf_field: Field, wall_orientation: tuple[int, int, int] | _IrregularSentinel, - target: Target | None = None, ): self._name = name self._lb_method = lb_method self._pdf_field = pdf_field self._wall_normal = wall_orientation - self._target = target @property def target(self) -> Target | None: @@ -77,7 +75,7 @@ class NoSlip(GenericLinkwiseBoundary): def _generate_regular(self, sfg: SfgComposer): bc_asm = self._grid_aligned_assignments() - bc_cfg = CreateKernelConfig(target=self._target) + bc_cfg = CreateKernelConfig() bc_sweep = Sweep(self._name, bc_asm, bc_cfg) sfg.generate(bc_sweep) @@ -220,13 +218,11 @@ class FreeSlip(GenericLinkwiseBoundary): lb_method: AbstractLbMethod, pdf_field: Field, wall_orientation: tuple[int, int, int] | _IrregularSentinel, - target: Target | None = None, ): self._name = name self._lb_method = lb_method self._pdf_field = pdf_field self._wall_normal = wall_orientation - self._target = target @property def target(self) -> Target | None: @@ -250,7 +246,7 @@ class FreeSlip(GenericLinkwiseBoundary): bc_asm = bc_obj.get_assignments(self._lb_method, self._pdf_field) # Build generator config - bc_cfg = CreateKernelConfig(target=self._target) + bc_cfg = CreateKernelConfig() bc_cfg.index_dtype = BoundaryIndexType index_field = bc_obj.get_index_field() bc_cfg.index_field = index_field @@ -299,7 +295,7 @@ class FreeSlip(GenericLinkwiseBoundary): def _generate_regular(self, sfg: SfgComposer): bc_asm = self._grid_aligned_assignments() - bc_cfg = CreateKernelConfig(target=self._target) + bc_cfg = CreateKernelConfig() bc_sweep = Sweep(self._name, bc_asm, bc_cfg) sfg.generate(bc_sweep) diff --git a/src/walberla/codegen/build_config.py b/src/walberla/codegen/build_config.py index 25a7fab2226c82b9f9cabccdf132b5210489dbfe..e9048cdfb5a3f38344601126fdb64d61c591a313 100644 --- a/src/walberla/codegen/build_config.py +++ b/src/walberla/codegen/build_config.py @@ -1,6 +1,6 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field from pystencils import CreateKernelConfig, Target from pystencils.types.quick import Fp @@ -20,6 +20,12 @@ def cmake_parse_bool(var: str): raise ValueError(f"Could not parse cmake value `{var}` as boolean.") +@dataclass +class CodegenOverrides: + target: Target | None = None + """Override code generation target""" + + @dataclass class WalberlaBuildConfig: """Represents a waLBerla build system configuration""" @@ -51,6 +57,9 @@ class WalberlaBuildConfig: likwid_enabled: bool """Value of `WALBERLA_BUILD_WITH_LIKWID_MARKERS`""" + override: CodegenOverrides = field(default_factory=CodegenOverrides) + """Override code generator options that would otherwise be inferred from the build system.""" + @staticmethod def from_sfg(sfg: SfgContext | SfgIComposer) -> WalberlaBuildConfig: if isinstance(sfg, SfgIComposer): @@ -72,7 +81,9 @@ class WalberlaBuildConfig: cfg.default_dtype = Fp(64) if self.use_double_precision else Fp(32) cfg.jit = no_jit - if self.cuda_enabled: + if self.override.target is not None: + cfg.target = self.override.target + elif self.cuda_enabled: cfg.target = Target.CUDA elif self.hip_enabled: cfg.target = Target.HIP @@ -83,12 +94,17 @@ class WalberlaBuildConfig: else: cfg.target = Target.GenericCPU - if self.openmp_enabled: - cfg.cpu.openmp.enable = True + if self.openmp_enabled: + cfg.cpu.openmp.enable = True return cfg +def get_build_config(sfg: SfgContext | SfgIComposer): + """Get the waLBerla build config object for the current generator script.""" + return WalberlaBuildConfig.from_sfg(sfg) + + class DEBUG_MOCK_CMAKE: BUILD_CONFIG: WalberlaBuildConfig | None = None diff --git a/tests/BasicLbmScenarios/LbmAlgorithms.py b/tests/BasicLbmScenarios/LbmAlgorithms.py index 1ba0036ee6e5032d62a8141ec76f8e3c35c09b0c..d18a48beffe13d6f99305a74c790eeefd2864c53 100644 --- a/tests/BasicLbmScenarios/LbmAlgorithms.py +++ b/tests/BasicLbmScenarios/LbmAlgorithms.py @@ -14,7 +14,7 @@ from lbmpy import ( ) from lbmpy.macroscopic_value_kernels import macroscopic_values_setter -from walberla.codegen import Sweep +from walberla.codegen import Sweep, get_build_config from walberla.codegen.boundaries import NoSlip, FreeSlip from walberla.codegen.build_config import DEBUG_MOCK_CMAKE @@ -27,17 +27,17 @@ with SourceFileGenerator(keep_unknown_argv=True) as sfg: args = parser.parse_args(sfg.context.argv) - cfg = CreateKernelConfig() + build_config = get_build_config(sfg) match args.target: case "cpu": - cfg.target = Target.CurrentCPU + build_config.override.target = Target.CurrentCPU sfg.code("#define LBM_SCENARIOS_CPU_BUILD true") case "hip": - cfg.target = Target.HIP + build_config.override.target = Target.HIP sfg.code("#define LBM_SCENARIOS_GPU_BUILD true") case "cuda": - cfg.target = Target.CUDA + build_config.override.target = Target.CUDA sfg.code("#define LBM_SCENARIOS_GPU_BUILD true") case _: raise ValueError(f"Unexpected target id: {args.target}") @@ -72,7 +72,7 @@ with SourceFileGenerator(keep_unknown_argv=True) as sfg: assert lb_update is not None with sfg.namespace("bulk"): - lb_update_sweep = Sweep("LbStreamCollide", lb_update, cfg) + lb_update_sweep = Sweep("LbStreamCollide", lb_update) lb_update_sweep.swap_fields(f, f_tmp) sfg.generate(lb_update_sweep) @@ -83,7 +83,7 @@ with SourceFileGenerator(keep_unknown_argv=True) as sfg: pdfs=f, set_pre_collision_pdfs=True ) - lb_init_sweep = Sweep("LbInitFromFields", lb_init, cfg) + lb_init_sweep = Sweep("LbInitFromFields", lb_init) sfg.generate(lb_init_sweep) lb_init_constant = macroscopic_values_setter( @@ -93,20 +93,20 @@ with SourceFileGenerator(keep_unknown_argv=True) as sfg: pdfs=f, set_pre_collision_pdfs=True ) - lb_init_constant_sweep = Sweep("LbInitConstant", lb_init_constant, cfg) + lb_init_constant_sweep = Sweep("LbInitConstant", lb_init_constant) sfg.generate(lb_init_constant_sweep) with sfg.namespace("bc_grid_aligned"): - sfg.generate(NoSlip("NoSlipTop", lb_update.method, f, wall_orientation=(0, 0, 1), target=cfg.target)) + sfg.generate(NoSlip("NoSlipTop", lb_update.method, f, wall_orientation=(0, 0, 1))) - sfg.generate(NoSlip("NoSlipBottom", lb_update.method, f, wall_orientation=(0, 0, -1), target=cfg.target)) + sfg.generate(NoSlip("NoSlipBottom", lb_update.method, f, wall_orientation=(0, 0, -1))) sfg.generate( - FreeSlip("FreeSlipTop", lb_update.method, f, wall_orientation=(0, 0, 1), target=cfg.target) + FreeSlip("FreeSlipTop", lb_update.method, f, wall_orientation=(0, 0, 1)) ) sfg.generate( - FreeSlip("FreeSlipBottom", lb_update.method, f, wall_orientation=(0, 0, -1), target=cfg.target) + FreeSlip("FreeSlipBottom", lb_update.method, f, wall_orientation=(0, 0, -1)) ) with sfg.namespace("bc_sparse"): @@ -115,6 +115,5 @@ with SourceFileGenerator(keep_unknown_argv=True) as sfg: lb_update.method, f, wall_orientation=FreeSlip.IRREGULAR, - target=cfg.target, ) sfg.generate(irreg_freeslip)