Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 1380 additions and 1603 deletions
from pystencils import Target, CreateKernelConfig, no_jit
from lbmpy import create_lb_update_rule, LBMOptimisation
from pystencilssfg import SourceFileGenerator, SfgConfig
sfg_config = SfgConfig()
sfg_config.extensions.impl = "cu"
sfg_config.output_directory = "out/test_cuda"
sfg_config.outer_namespace = "gen_code"
with SourceFileGenerator(sfg_config) as sfg:
gen_config = CreateKernelConfig(target=Target.CUDA, jit=no_jit)
opt = LBMOptimisation(field_layout="fzyx")
update = create_lb_update_rule()
kernel = sfg.kernels.create(update, "lbm_update", gen_config)
sfg.function("lb_update")(sfg.call(kernel))
from pystencils import Target, CreateKernelConfig, create_kernel, no_jit
from lbmpy import create_lb_update_rule, LBMOptimisation
from pystencilssfg import SourceFileGenerator, SfgConfiguration, SfgOutputMode
from pystencilssfg.lang.cpp import mdspan_ref
sfg_config = SfgConfiguration(
output_directory="out/test_sycl",
outer_namespace="gen_code",
impl_extension="ipp",
output_mode=SfgOutputMode.INLINE
)
with SourceFileGenerator(sfg_config) as sfg:
gen_config = CreateKernelConfig(target=Target.SYCL, jit=no_jit)
opt = LBMOptimisation(field_layout="fzyx")
update = create_lb_update_rule()
kernel = sfg.kernels.create(update, "lbm_update", gen_config)
sfg.function("lb_update")(
sfg.call(kernel)
)
from pystencils import Target, CreateKernelConfig, no_jit
from lbmpy import create_lb_update_rule, LBMOptimisation
from pystencilssfg import SourceFileGenerator, SfgConfig
from pystencilssfg.lang.cpp.sycl_accessor import SyclAccessor
import pystencilssfg.extensions.sycl as sycl
from itertools import chain
sfg_config = SfgConfig(
output_directory="out/test_sycl_buffer",
outer_namespace="gen_code",
header_only=True
)
with SourceFileGenerator(sfg_config) as sfg:
sfg = sycl.SyclComposer(sfg)
gen_config = CreateKernelConfig(target=Target.SYCL, jit=no_jit)
opt = LBMOptimisation(field_layout="fzyx")
update = create_lb_update_rule(lbm_optimisation=opt)
kernel = sfg.kernels.create(update, "lbm_update", gen_config)
cgh = sfg.sycl_handler("handler")
rang = sfg.sycl_range(update.method.dim, "range")
mappings = [
sfg.map_field(field, SyclAccessor.from_field(field))
for field in chain(update.free_fields, update.bound_fields)
]
sfg.function("lb_update")(
cgh.parallel_for(rang)(
*mappings,
sfg.call(kernel),
),
)
site_name: pystencils Source File Generator Documentation
site_author: Frederik Hennig
copyright: © 2023 Frederik Hennig
repo_name: GitLab
repo_url: https://i10git.cs.fau.de/pycodegen/pystencils-sfg
theme:
name: material
features:
- navigation.tabs
- content.code.copy
palette:
scheme: slate
primary: deep purple
extra_css:
- css/mkdocstrings.css
plugins:
- search
- autorefs
- mkdocstrings:
default_handler: python
handlers:
python:
paths: [src]
options:
heading_level: 2
members_order: source
group_by_category: False
show_root_heading: True
show_root_full_path: True
show_symbol_type_heading: True
show_symbol_type_toc: True
show_source: False
show_signature_annotations: True
signature_crossrefs: True
markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.superfences
nav:
- Home: index.md
- 'User Guides':
- 'Overview': usage/index.md
- 'Writing Generator Scripts': usage/generator_scripts.md
- 'In-Depth: Building Source Files': usage/building.md
- 'CLI and Build System Integration': usage/cli_and_build_system.md
- 'Tips and Tricks': usage/tips_n_tricks.md
- 'API Documentation':
- 'Overview': api/index.md
- 'Front End':
- 'Source File Generator': api/generator.md
- 'Code Generation Context': api/context.md
- 'Composer': api/composer.md
- 'Source File Modelling':
- 'Source File Components': api/source_components.md
- 'Kernel Call Tree': api/tree.md
- 'High-Level Language Concepts':
- 'Base Classes': 'api/source_objects.md'
- 'C++ Standard Library': 'api/cpp_std.md'
- 'Code Generation':
- 'Emission and Printing': api/emission.md
......@@ -3,3 +3,6 @@ python_version=3.10
[mypy-pystencils.*]
ignore_missing_imports=true
[mypy-sympy.*]
ignore_missing_imports=true
from __future__ import annotations
from typing import Sequence
import nox
nox.options.sessions = ["lint", "typecheck", "testsuite"]
def add_pystencils_git(session: nox.Session):
"""Clone the pystencils 2.0 development branch and install it in the current session"""
cache_dir = session.cache_dir
pystencils_dir = cache_dir / "pystencils"
if pystencils_dir.exists():
with session.chdir(pystencils_dir):
session.run_install("git", "pull", external=True)
else:
session.run_install(
"git",
"clone",
"--branch",
"v2.0-dev",
"--single-branch",
"https://i10git.cs.fau.de/pycodegen/pystencils.git",
pystencils_dir,
external=True,
)
session.install("-e", str(pystencils_dir))
def editable_install(session: nox.Session, opts: Sequence[str] = ()):
add_pystencils_git(session)
if opts:
opts_str = "[" + ",".join(opts) + "]"
else:
opts_str = ""
session.install("-e", f".{opts_str}")
@nox.session(python="3.10", tags=["qa", "code-quality"])
def lint(session: nox.Session):
"""Lint code using flake8"""
session.install("flake8")
session.run("flake8", "src/pystencilssfg")
@nox.session(python="3.10", tags=["qa", "code-quality"])
def typecheck(session: nox.Session):
"""Run MyPy for static type checking"""
editable_install(session)
session.install("mypy")
session.run("mypy", "src/pystencilssfg")
@nox.session(python=["3.10", "3.11", "3.12", "3.13"], tags=["tests"])
def testsuite(session: nox.Session):
"""Run the testsuite and measure coverage."""
editable_install(session, ["testsuite"])
session.run(
"pytest",
"-v",
"--cov=src/pystencilssfg",
"--cov-report=term",
"--cov-config=pyproject.toml",
)
session.run("coverage", "html")
session.run("coverage", "xml")
@nox.session(python=["3.10"], tags=["docs"])
def docs(session: nox.Session):
"""Build the documentation pages"""
editable_install(session, ["docs"])
env = {}
session_args = session.posargs
if "--fail-on-warnings" in session_args:
env["SPHINXOPTS"] = "-W --keep-going"
session.chdir("docs")
if "--clean" in session_args:
session.run("make", "clean", external=True)
session.run("make", "html", external=True)
@nox.session()
def dev_env(session: nox.Session):
"""Set up the development environment at .venv"""
session.install("virtualenv")
session.run("virtualenv", ".venv", "--prompt", "pystencils-sfg")
session.run(
".venv/bin/pip",
"install",
"git+https://i10git.cs.fau.de/pycodegen/pystencils.git@v2.0-dev",
external=True,
)
session.run(".venv/bin/pip", "install", "-e", ".[dev]", external=True)
This diff is collapsed.
......@@ -5,11 +5,11 @@ authors = [
{name = "Frederik Hennig", email = "frederik.hennig@fau.de"},
]
dependencies = [
"pystencils>=1.3.2",
"pystencils>=2.0.dev0",
]
requires-python = ">=3.10"
readme = "README.md"
license = {text = "noneyet"}
license = { file = "LICENSE" }
dynamic = ["version"]
[project.scripts]
......@@ -18,23 +18,34 @@ sfg-cli = "pystencilssfg.cli:cli_main"
[build-system]
requires = [
"setuptools>=69",
"versioneer>=0.29",
"tomli; python_version < '3.11'"
"versioneer[toml]>=0.29",
]
build-backend = "setuptools.build_meta"
[tool.pdm.dev-dependencies]
interactive = [
"ipython>=8.17.2",
[project.optional-dependencies]
dev = [
"flake8",
"mypy",
"black",
"clang-format",
]
code_quality = [
"flake8>=6.1.0",
"mypy>=1.7.0",
testsuite = [
"pytest",
"pytest-cov",
"pyyaml",
"requests",
"fasteners",
]
docs = [
"mkdocs>=1.5.3",
"mkdocs-material>=9.4.8",
"mkdocstrings[python]>=0.24.0",
"sphinx",
"pydata-sphinx-theme==0.15.4",
"sphinx-book-theme==1.1.3", # workaround for https://github.com/executablebooks/sphinx-book-theme/issues/865
"myst-nb",
"sphinx_design",
"sphinx_autodoc_typehints",
"sphinx-copybutton",
"packaging",
"clang-format"
]
[tool.versioneer]
......@@ -42,5 +53,21 @@ VCS = "git"
style = "pep440"
versionfile_source = "src/pystencilssfg/_version.py"
versionfile_build = "pystencilssfg/_version.py"
tag_prefix = ""
tag_prefix = "v"
parentdir_prefix = "pystencilssfg-"
[tool.coverage.run]
omit = [
"setup.py",
"noxfile.py",
"src/pystencilssfg/_version.py",
"integration/*"
]
[tool.coverage.report]
exclude_also = [
"\\.\\.\\.\n",
"if TYPE_CHECKING:",
"@(abc\\.)?abstractmethod",
"assert False"
]
[pytest]
testpaths = src/pystencilssfg tests/
python_files = "test_*.py"
# Need to ignore the generator scripts, otherwise they would be executed
# during test collection
addopts =
--doctest-modules
--ignore=tests/generator_scripts/source
--ignore=tests/generator_scripts/deps
--ignore=tests/generator_scripts/expected
--ignore=tests/data
--ignore=tests/integration/cmake_project
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
from .configuration import SfgConfiguration
from .config import SfgConfig, GLOBAL_NAMESPACE
from .generator import SourceFileGenerator
from .composer import SfgComposer
from .context import SfgContext
from .lang import SfgVar, AugExpr
from .exceptions import SfgException
__all__ = [
"SourceFileGenerator", "SfgComposer", "SfgConfiguration", "SfgContext"
"SfgConfig",
"GLOBAL_NAMESPACE",
"SourceFileGenerator",
"SfgComposer",
"SfgContext",
"SfgVar",
"AugExpr",
"SfgException",
]
from . import _version
__version__ = _version.get_versions()['version']
__version__ = _version.get_versions()["version"]
if __name__ == "__main__":
from .cli import cli_main
cli_main("python -m pystencilssfg")
This diff is collapsed.
import sys
import os
from os import path
from typing import NoReturn
from argparse import ArgumentParser, BooleanOptionalAction
from .configuration import (
SfgConfigException, SfgConfigSource,
add_config_args_to_parser, config_from_parser_args, merge_configurations
)
from .config import CommandLineParameters, SfgConfigException
def add_newline_arg(parser):
parser.add_argument("--newline", action=BooleanOptionalAction, default=True,
help="Whether to add a terminating newline to the output.")
parser.add_argument(
"--newline",
action=BooleanOptionalAction,
default=True,
help="Whether to add a terminating newline to the output.",
)
def cli_main(program='sfg-cli'):
parser = ArgumentParser(program,
description="pystencilssfg command-line utility for build system integration")
def cli_main(program="sfg-cli") -> NoReturn:
parser = ArgumentParser(
program,
description="pystencilssfg command-line utility for build system integration",
)
subparsers = parser.add_subparsers(required=True, title="Subcommands")
......@@ -26,25 +30,33 @@ def cli_main(program='sfg-cli'):
version_parser.set_defaults(func=version)
outfiles_parser = subparsers.add_parser(
"list-files", help="List files produced by given codegen script.")
"list-files", help="List files produced by given codegen script."
)
outfiles_parser.set_defaults(func=list_files)
add_config_args_to_parser(outfiles_parser)
CommandLineParameters.add_args_to_parser(outfiles_parser)
add_newline_arg(outfiles_parser)
outfiles_parser.add_argument("--sep", type=str, default=" ", dest="sep", help="Separator for list items")
outfiles_parser.add_argument(
"--sep", type=str, default=" ", dest="sep", help="Separator for list items"
)
outfiles_parser.add_argument("codegen_script", type=str)
cmake_parser = subparsers.add_parser("cmake", help="Operations for CMake integation")
cmake_parser = subparsers.add_parser(
"cmake", help="Operations for CMake integation"
)
cmake_subparsers = cmake_parser.add_subparsers(required=True)
modpath = cmake_subparsers.add_parser(
"modulepath", help="Print the include path for the pystencils-sfg cmake module")
"modulepath", help="Print the include path for the pystencils-sfg cmake module"
)
add_newline_arg(modpath)
modpath.set_defaults(func=print_cmake_modulepath)
findmod = cmake_subparsers.add_parser("make-find-module",
help="Creates the pystencils-sfg CMake find module as" +
"'FindPystencilsSfg.cmake' in the current directory.")
findmod = cmake_subparsers.add_parser(
"make-find-module",
help="Creates the pystencils-sfg CMake find module as"
+ "'FindPystencilsSfg.cmake' in the current directory.",
)
findmod.set_defaults(func=make_cmake_find_module)
args = parser.parse_args()
......@@ -53,57 +65,45 @@ def cli_main(program='sfg-cli'):
exit(-1) # should never happen
def version(args):
def version(args) -> NoReturn:
from . import __version__
print(__version__, end=os.linesep if args.newline else '')
print(__version__, end=os.linesep if args.newline else "")
exit(0)
def list_files(args):
try:
project_config, cmdline_config = config_from_parser_args(args)
except SfgConfigException as exc:
abort_with_config_exception(exc)
config = merge_configurations(project_config, cmdline_config, None)
def list_files(args) -> NoReturn:
cli_params = CommandLineParameters(args)
config = cli_params.get_config()
_, scriptname = path.split(args.codegen_script)
basename = path.splitext(scriptname)[0]
from .emission import HeaderImplPairEmitter
emitter = HeaderImplPairEmitter(config.get_output_spec(basename))
output_files = config._get_output_files(basename)
print(args.sep.join(emitter.output_files), end=os.linesep if args.newline else '')
print(
args.sep.join(str(of) for of in output_files),
end=os.linesep if args.newline else "",
)
exit(0)
def print_cmake_modulepath(args):
def print_cmake_modulepath(args) -> NoReturn:
from .cmake import get_sfg_cmake_modulepath
print(get_sfg_cmake_modulepath(), end=os.linesep if args.newline else '')
print(get_sfg_cmake_modulepath(), end=os.linesep if args.newline else "")
exit(0)
def make_cmake_find_module(args):
def make_cmake_find_module(args) -> NoReturn:
from .cmake import make_find_module
make_find_module()
exit(0)
def abort_with_config_exception(exception: SfgConfigException):
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
match exception.config_source:
case SfgConfigSource.PROJECT:
eprint(
f"Invalid project configuration: {exception.message}\nCheck your configurator script.")
case SfgConfigSource.COMMANDLINE:
eprint(
f"Invalid configuration on command line: {exception.message}")
case _: assert False, "(Theoretically) unreachable code. Contact the developers."
def abort_with_config_exception(exception: SfgConfigException, source: str) -> NoReturn:
print(f"Invalid {source} configuration: {exception.args[0]}.", file=sys.stderr)
exit(1)
set( PystencilsSfg_FOUND OFF CACHE BOOL "pystencils source file generator found" )
#[[
Find-Module for pystencils-sfg.
mark_as_advanced( PystencilsSfg_FOUND )
# Setting the Python interpreter
find_package( Python COMPONENTS Interpreter REQUIRED )
If the cache entry PystencilsSfg_PYTHON_INTERPRETER is set, e.g. via the commandline
(`-DPystencilsSfg_PYTHON_INTERPRETER=<...>`), its value be taken as the Python interpreter
used to find and run pystencils-sfg.
If the cache entry is unset, but the hint PystencilsSfg_PYTHON_PATH is set, its value will
be used as the Python interpreter.
If none of these is set, a Python interpreter will be selected using the `FindPython` module.
#]]
if(NOT DEFINED CACHE{PystencilsSfg_PYTHON_INTERPRETER})
# The Python interpreter cache variable is not set externally, so...
if(DEFINED PystencilsSfg_PYTHON_PATH)
# ... either initialize it from the hint variable ...
set( _sfg_cache_python_init ${PystencilsSfg_PYTHON_PATH} )
else()
# ... or, if that is also unset, use the system Python
find_package( Python COMPONENTS Interpreter REQUIRED )
set( _sfg_cache_python_init ${Python_EXECUTABLE} )
endif()
endif()
set(PystencilsSfg_PYTHON_INTERPRETER ${_sfg_cache_python_init} CACHE PATH "Path to the Python executable used to run pystencils-sfg")
# Try to find pystencils-sfg in the python environment
execute_process(COMMAND ${Python_EXECUTABLE} -m pystencilssfg version --no-newline
execute_process(COMMAND ${PystencilsSfg_PYTHON_INTERPRETER} -m pystencilssfg version --no-newline
RESULT_VARIABLE _PystencilsSfgFindResult OUTPUT_VARIABLE PystencilsSfg_VERSION )
if(${_PystencilsSfgFindResult} EQUAL 0)
set( PystencilsSfg_FOUND ON )
else()
set( PystencilsSfg_FOUND OFF )
endif()
if(DEFINED PystencilsSfg_FIND_REQUIRED)
......@@ -21,8 +47,9 @@ endif()
if(${PystencilsSfg_FOUND})
message( STATUS "Found pystencils Source File Generator (Version ${PystencilsSfg_VERSION})")
execute_process(COMMAND ${Python_EXECUTABLE} -m pystencilssfg cmake modulepath --no-newline
message( STATUS "Using Python interpreter ${PystencilsSfg_PYTHON_INTERPRETER} for SFG generator scripts.")
execute_process(COMMAND ${PystencilsSfg_PYTHON_INTERPRETER} -m pystencilssfg cmake modulepath --no-newline
OUTPUT_VARIABLE _PystencilsSfg_CMAKE_MODULE_PATH)
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${_PystencilsSfg_CMAKE_MODULE_PATH})
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.